MathGroup Archive 2010

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

Search the Archive

Re: FittedModel object type

Vincent wrote:
> Is there an easy way to get Mathematica to interpret custom code when
> a FittedModel is called with a custom argument, for example if I have
> modelfit = NonlinearModelFit[...]
> modelfit ["EstimatedPower"]
> which would then call my own code using the modelfit object to compute
> something?
> I know that it's trivial to achieve the same functionality just
> calling the function on the variable, but I rather like the object
> orientet like syntax of adding my own figures to the FittedModel
> object.

There currently isn't a mechanism in place for this. A possibility if 
you really want an object oriented approach could be to define a new 
operator that will get properties via FittedModel if possible and from 
other definitions when added.

In[1]:= nlm = NonlinearModelFit[Range[10]^2, b*Exp[a*x], {a, b}, x];

(* get property from built-in code for defined properties *)
In[2]:= myModelProperties[model : FittedModel[{"Nonlinear", __}, 
__]][prop_] :=
          model[prop] /; MemberQ[model["Properties"], prop]

(* give the definition for a new property *)
In[3]:= myModelProperties[model : FittedModel[{"Nonlinear", __}, __]][
          "SinVariance"] := Sin[model["EstimatedVariance"]]

(* define a construct for lists of properties *)
In[4]:= myModelProperties[model : FittedModel[{"Nonlinear", __}, __]][
          vals : {_String ...}] := Map[myModelProperties[model], vals]

(* this is our new object *)
In[5]:= op = myModelProperties[nlm];

(* get single newly defined and pre-existing properties and get both in 
a list at once *)
In[6]:= op["SinVariance"]

Out[6]= 0.618074

In[7]:= op["BestFit"]

                 0.288603 x
Out[7]= 5.86625 E

In[8]:= op[{"SinVariance", "BestFit"}]

                            0.288603 x
Out[8]= {0.618074, 5.86625 E          }

For lists of properties, the code above may be slower than the built-in 
FittedModel property code because the FittedModel code re-uses shared 
intermediate results while the code above does not.

Darren Glosemeyer
Wolfram Research

  • Prev by Date: Re: First nonzero in list
  • Next by Date: Re: FindMinimum numerical constraint functions
  • Previous by thread: FittedModel object type
  • Next by thread: Re: FittedModel object type