Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: FittedModel Objects... How does Mathematica do it?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115472] Re: FittedModel Objects... How does Mathematica do it?
  • From: "Sjoerd C. de Vries" <sjoerd.c.devries at gmail.com>
  • Date: Tue, 11 Jan 2011 19:24:06 -0500 (EST)
  • References: <iggq2q$4be$1@smc.vnet.net>

Hi RG,

Application of FullForm shows that lm is actually:

FittedModel[List["Linear",List[0.18644067796610198`,
0.6949152542372878`],List[List[x],List[1,x]],List[0,0]],List[List[1.`,
1.`,1.`,
1.`]],List[List[0,1],List[1,0],List[3,2],List[5,4]],List[List[1.`,
0.`],List[1.`,1.`],List[1.`,3.`],List[1.`,
5.`]],Function[Null,Internal`LocalizedBlock[List[x],Slot[1]],List[HoldAll]]]


Similar output is obtained for the other fit functions. You can see
that the data, the model, the fit and the type of fit are stored as
FittedModel parameters and that's about all there is.

The special formatting of the FittedModel output is probably done by
means of a Format function like in:

myModal/: Format[myModel[a_, b_]] := myModel[Panel[a[b]]]

myModel[tata, titi]

Properties can be easily defined in the following way:

FittedModel[a_, b_, c_, d_]["FitResiduals"] := FittedModelResiduals[a,
b, c, d]
FittedModel[a_, b_, c_, d_]["BestFit"] := FittedModelBestFit[a, b, c,
d]
FittedModel[a_, b_, c_, d_]["ParameterErrors","LongDescription"] :=
"standard errors for parameter estimates"

etc. etc. Of course, you then have to define FittedModelResiduals,
FittedModelBestFit to do something useful with the input.

Alternatively, the functions could also be defined within FittedModel
as
FittedModel[a_, b_, c_, d_]["FitResiduals"] :=
FittedModel["FitResiduals",a, b, c, d],
FittedModel[a_, b_, c_, d_]["BestFit"] := FittedModel["BestFit",a, b,
c, d]
or so

If you change the y data in the FullForm version of lm into variables
and ask for fit residuals you'll see it's just calculating the
residuals with the data present in the FittedModel parameters:

In[90]:= FittedModel[List["Linear",List[0.18644067796610198`,
0.6949152542372878`],List[List[x],List[1,x]],List[0,0]],List[List[1.`,
1.`,1.`,
1.`]],List[List[0,1],List[1,0],List[3,2],List[5,4]],List[List[1.`,a],List[1.`,b],List[1.`,c],List[1.`,d]],Function[Null,Internal`LocalizedBlock[List[x],Slot[1]],List[HoldAll]]]
["FitResiduals"]

Out[90]= {0.813559- 0.694915 a, -0.186441 - 0.694915 b, 1.81356-
0.694915 c, 3.81356- 0.694915 d}

You see? It's actually quite simple. FittedModel[a_, b_, c_, d_]
doesn't do anything; it just sits there holding its parameters. There
are only definitions for FittedModel[a_, b_, c_, d_][...] type of
inputs.

Cheers -- Sjoerd

On Jan 11, 6:33 am, telefunkenvf14 <rgo... at gmail.com> wrote:
> Group:
>
> LinearModelFit (and similar statistical functions) are documented with
> examples such as:
>
> data = {{0, 1}, {1, 0}, {3, 2}, {5, 4}};
> lm = LinearModelFit[data, x, x]
>
> which returns a FittedModel 'object'. One can then execute code such
> as lm["FitResiduals"] or lm["RSquared"] to retrieve the properties one
> desires.
>
> 1. How exactly does Mathematica generate/store the objects associated
> with the head 'lm'?!? I'd *really* appreciate a simple example of how
> to do this and/or some tips on when/how to use more complicated head
> structures.
>
> 2. And taking that one step further, how does Mathematica generate/
> store additional info, such as 'Descriptions' on specific properties?
> For example, lm["ParameterErrors", "LongDescription"], returns
> "standard errors for parameter estimates".
>
> 3. Does Mathematica immediately calculate and store all these
> lm{"objects"], or are they generated (where appropriate) only when
> requested? (And is this really OOP? <-- not intended to start an OOP
> flame war!)
>
> 4. (Possibly related, so I figured I'd include it...) How does
> Mathematica store all the metadata available on example data sets?---
> Is there documentation on how to conform to this standard/approach?
> For instance: ExampleData[{"Matrix","FIDAP007"}, "Properties"]. (on
> second thought, I guess this is probably stored in some standard file
> format, hidden from view...)
>
> Thanks for any info and guidance you can provide,
>
> -RG



  • Prev by Date: Re: FindInstance does not abort with TimeConstrained
  • Next by Date: Re: FindInstance does not abort with TimeConstrained
  • Previous by thread: Re: FittedModel Objects... How does Mathematica do it?
  • Next by thread: FindInstance does not abort with TimeConstrained