Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am struggling with understanding the code given in this original post

I understand how the tests are conducted, both of them. However, I do not understand this part:

FitModel[data_List, coords_List] :=
  With[{lm = LinearModelFit[data, coords, coords]},
      "BreuschPagan" -> BreuschPagan[data, lm, coords],
      "WhitesHeteroskedasticity" -> WhitesHeteroskedasticity[lm, coords]}]];
dispatch[list_][field_] := field /. list
dispatch[list_]["Properties"] := list /. Rule[field_, _] :> field
dispatch /: ReplaceAll[fields_, dispatch[list_]] := fields /. list
Format[dispatch[list_], StandardForm] := 
  HoldForm[dispatch]["<" <> ToString@Length@list <> ">"]

Could someone explain what this is doing? If you recommend that I do it in a chat, I have created a chat room for such discussion.

I find it particularly difficult to understand the code for dispatch and how it works.

share|improve this question
add comment

1 Answer 1

up vote 5 down vote accepted

I think the first few lines are self explanatory, they just build up a list of rules and wrap them in head dispatch. In this particular example the rules pertain to fitted models and particular test results but the dispatch mechanism is quite general.

Basically dispatch here is a way of creating a property extraction mechanism similar in spirit to that of FittedModel with the key-value behavior of Dispatch.

Lets start in a fresh kernel...

dispatch[list_][field_] := field /. list
dispatch[list_]["Properties"] := list /. Rule[field_, _] :> field
dispatch /: ReplaceAll[fields_, dispatch[list_]] := fields /. list

Here is a list of dummy properties to work with.

propList = {"P1" -> 1, "P2" -> 2, "P3" -> 3};

Notice that dispatch[list] will remain unevaluated unless a sub value is requested.


(* dispatch[{"P1" -> 1, "P2" -> 2, "P3" -> 3}] *)

The Format statement is just a way of hiding this output so that if we had a really long list of properties we wouldn't need to see them all.

Format[dispatch[list_], StandardForm] := 
   HoldForm[dispatch]["<" <> ToString@Length@list <> ">"]

out = dispatch[propList]

(* dispatch[<3>]*)

Now lets look at what each of the previous definitions are doing. The first replaces the property 'field' with its value given in the list of rules. (NOTE: As was pointed out by @Martin John Hadley this sort of assignment (via SubValues) is not often documented but you will find it frequently used for property extraction. Look here for a detailed discussion about this sort of thing.)


(* 2 *)

The second gives a list of field/property names contained in the dispatch object.


(* {"P1", "P2", "P3"} *)

The third definition allows dispatch to work like Dispatch by overloading ReplaceAll using TagSetDelayed creating an up value. Thus, if a list of properties is given we can replace them with their values contained in the dispatch object.

{"P2","P1","P3"} /. out

(* {2, 1, 3} *)
share|improve this answer
Andy, as they're not documented frequently is it worth making clear SubValues are being set in the first code block? –  Martin John Hadley Mar 30 at 23:28
Indeed. I use these so frequently that I often take them for granted. I'll add a comment to this effect. –  Andy Ross Mar 31 at 3:05
Would you be able to explain how Field is defined to have both "BreuschPagan" and "WhitesHeteroskedasticity". Cause I cant see where it is defined –  ALEXANDER Mar 31 at 10:18
Notice that the dispatch created in With contains a list of rules. The left-hand side of the rules are property names (in this case BreuschPagan and WhitesHeteroskedasticity) the right hand side are the values (here injected using With). –  Andy Ross Mar 31 at 13:16
thats okey, but if I type Field, i do not get anything, but I was expecting to get ("BreuschPagan","WhitesHeteroskedasticity") since it is defined? –  ALEXANDER Mar 31 at 15:36
show 3 more comments

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.