How can I create an array of LinearModel objects
Show older comments
I am modeling a system with multiple inputs (X) and multiple outputs (Y). I´m using stepwiselm to model each output Y(i) separately:
mdl = stepwiselm (X,Y(:,i),'Upper','linear','PEnter',0.03,'PRemove',0.05)
it works fine, but when I try to create a loop and store all models:
mdl(i) = stepwiselm (X,Y(:,i),'Upper','linear','PEnter',0.03,'PRemove',0.05)
there is an error:
Error using classreg.regr.FitObject/subsasgn (line 738) Assignment using () is not allowed for a FitObject.
I have tried to concatenate models in several ways:
prove=[mdl1;mdl2]
Error using classreg.learning.internal.DisallowVectorOps/throwNoCatError (line57) Concatenation of LinearModel objects is not allowed. Use a cell array to contain multiple objects.
Error in classreg.learning.internal.DisallowVectorOps/vertcat (line 49) function a = vertcat(this,varargin), throwNoCatError(this); end
prove=cell(mdl1,mdl2)
Error using cell Conversion to double from LinearModel is not possible.
prove=cell{mdl1,mdl2}
The result of calling the static method or constructor "cell" cannot be indexed with cell array indexing.
but with no result.
I can store one model in a different variable:
mdl1=mdl
but I need to store a different number of models to be used later, and it's mandatory that they can be indexed.
I have tried to overcome this by storing some parts of the LinearModel objects in separate arrays
coeficients(i,1)=mdl.Coefficients.Estimate(1);
for j=2:1:mdl.NumCoefficients
for k=1:1:M
if strcmp(cell2mat(mdl.CoefficientNames(j)), cell2mat(NomVarDat(k)))
coeficients (i,k+1)= mdl.Coefficients.Estimate(j);
end
end
end
fitted(i,:)=mdl.Fitted;
residus(i,:)=mdl.Residuals.Raw;
but then I want to use some LinearModel methods to evaluate new variables, ....
So, the question is: How can I create a indexable group of models (LinearModel objects)?
Thank You
Answers (1)
Steven Lord
on 22 Oct 2015
1 vote
Store them in a cell array.
2 Comments
Manuel Lamich
on 23 Oct 2015
Steven Lord
on 23 Oct 2015
That's one way; another is to preallocate the cell array and then use indexed assignment to store the LinearModels in each cell.
C = cell(1, 10);
for k = 1:10
C{k} = magic(k+2);
end
X = C{5} % X will be magic(7)
Categories
Find more on Multiple Linear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!