DesignModelFit linear model fit by design matrix​
​LinearModelFit for multivariate data by DesignMatrix
​by Robert Nowak
Suppose you want to fit a multivariate model via the DesignMatrix by LinearModelFit[] you will notice some information loss regarding the multivariate variables of the model.
Suppose we have sampled data points of a arc/circle/ellipse etc.
Lets generate some noisy elliptical bivariate test data t
In[]:=
t=With{n=32},​​Table{2Cos@p,Sin@p}+RandomReal[.1{-1,1},2],p,0,2π-
2π
n
,
2π
n
;​​ListPlot[t,AspectRatioAutomatic,Frame->True]
Out[]=
Lets perform a plain LinearModelFit lmf to get an algebraic fit of the data.
The fit model we choose is the general conic model mdl
​This works as expected ;-).
In[]:=
mdl={x,y,
2
x
,
2
y
,xy};​​lmf=LinearModelFit[t//Map[Append[1]],mdl,{x,y},IncludeConstantBasisFalse];​​lmf["BestFit"]//Labeled[#,"BestFit"]&//Framed​​lmf["BestFitParameters"]//Labeled[#,"BestFitParameters"]&//Framed​​ContourPlot[lmf[x,y]1,{x,-3,3},{y,-2,2},AspectRatioAutomatic,EpilogPoint[t]]
Out[]=
0.0050368x+0.240401
2
x
+0.0182574y+0.0147671xy+1.02258
2
y
BestFit
Out[]=
{0.0050368,0.0182574,0.240401,1.02258,0.0147671}
BestFitParameters
Out[]=
Now we try to repeat the fit df via the DesignMatrix dm and the ResponseVector r.
In[]:=
dm=DesignMatrix[t//Map[Append[1]],mdl,{x,y},IncludeConstantBasisFalse];​​r=Table[1,Length@t];​​df=LinearModelFit[{dm,r},IncludeConstantBasisFalse];​​df["BestFit"]//Labeled[#,"BestFit"]&//Framed​​df["BestFitParameters"]//Labeled[#,"BestFitParameters"]&//Framed
Out[]=
0.0050368#1+0.0182574#2+0.240401#3+1.02258#4+0.0147671#5
BestFit
Out[]=
{0.0050368,0.0182574,0.240401,1.02258,0.0147671}
BestFitParameters
In[]:=
df["BestFitParameters"]lmf["BestFitParameters"]
Out[]=
True
As we can see, we get the same result in terms of “BestFitParameters”.
Unfortunately we have lost the information about the original two variables.
Now we are faced with five Slot parameters instead of two variables ;-(.
To overcome this inconvenience we define DesignModelFit[] a wrapper for LinearModelFit as well as some auxiliary functions.

DesignModelFit

implementation


Out[]=
Symbol
DesignModel[data, exp, var]construct the DesignModel List suitable for DesignModelFit[] containing DesignMatrix, Responsevector and expressions, variables.
Definitions
DesignModel[data_,exp_,var_]:={DesignMatrix[data,exp,var,IncludeConstantBasisFalse],data〚All,-1〛,exp,var}
Full Name
Global`DesignModel
​
Symbol
DesignModelFit[{DesignMatrix, Response, exp, var}]wrapper for LinearModelFit based on DesignMatrix, ResponseVector along with bookkeeping of expressions and variables. Returns a FittedDesignModel object in contrast to FittedModel as obtained by LinearModelFit.
Definitions
DesignModelFit[m:{DesignMatrix_,Response_,exp_,var_}]:=FittedDesignModel[LinearModelFit[{DesignMatrix,Response},IncludeConstantBasisFalse],exp,var]
Full Name
Global`DesignModelFit
​
Symbol
FittedDesignModel[...]represents the symbolic fitted model obtained from DesignModelFit[]
SubValue Definitions
FittedDesignModel[lmf_,exp_,var_][DesignModel]:={lmf[DesignMatrix],lmf[Response],exp,var}
FittedDesignModel[lmf_,exp_,var_][Function]:=Evaluate[FittedDesignModel[fm,exp,var][BestFit]/.MapThread[Rule,{var,Table[Slot[i],{i,Length[var]}]}]]&
FittedDesignModel[lmf_,exp_,var_][s_String]:=lmf[s]/.MapThread[Rule,{Table[Slot[i],{i,Length[exp]}],exp}]
FittedDesignModel[lmf_,exp_,var_][p__]:=FittedDesignModel[lmf,exp,var][Function][p]
Full Name
Global`FittedDesignModel
​
Now we ready to use DesignModelFit[]
First we create the DesignModel List dm containing the four items:
{DesignMatrix, ResponseVector, expressions, variables}.
In[]:=
dm=DesignModel[t//Map[Append[1]],mdl,{x,y}];​​Length@dm
Out[]=
4
Now lets do the DesignMatrix fit dmf:
In[]:=
dmf=DesignModelFit[dm]
Out[]=
FittedDesignModelFittedModel
0.0050368#1+3+0.0147671#5
,{x,y,
2
x
,
2
y
,xy},{x,y}
The original variables are preserved.
The fit is callable with two parameters as expected
In[]:=
dmf["BestFit"]​​dmf["Function"]​​dmf[a1,a2]​​dmf[42,Pi]​​
Out[]=
0.0050368x+0.240401
2
x
+0.0182574y+0.0147671xy+1.02258
2
y
Out[]=
0.0050368#1+0.240401
2
#1
+0.0182574#2+0.0147671#1#2+1.02258
2
#2
&
Out[]=
0.0050368a1+0.240401
2
a1
+0.0182574a2+0.0147671a1a2+1.02258
2
a2
Out[]=
436.377
All “Properties” of LinearModelFit are preserved/adapted, the five Slot parameters are converted back to the two variables x, y.
In[]:=
dmf["BestFit"]​​dmf["Function"]​​dmf["ANOVATable"]​​
Out[]=
0.0050368x+0.240401
2
x
+0.0182574y+0.0147671xy+1.02258
2
y
Out[]=
0.0050368#1+0.240401
2
#1
+0.0182574#2+0.0147671#1#2+1.02258
2
#2
&
Out[]=
DF
SS
MS
F-Statistic
P-Value
x
1
0.00176808
0.00176808
0.232767
0.633368
y
1
0.00420514
0.00420514
0.553603
0.463276
2
x
1
21.1316
21.1316
2781.95
8.95874×
-29
10
2
y
1
10.6538
10.6538
1402.56
8.20849×
-25
10
xy
1
0.00357288
0.00357288
0.470367
0.498666
Error
27
0.205091
0.00759595
​
​
Total
32
32.
​
​
​