How can I create an array of LinearModel objects

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

Risposte (1)

Steven Lord
Steven Lord il 22 Ott 2015
Store them in a cell array.

2 Commenti

Thank you for your answer, I have tested
prove=cell(mdl)
and Matlab returned: Error using cell Conversion to cell from LinearModel is not possible.
prove=cell{mdl}
The result of calling the static method or constructor "cell" cannot be indexed with cell array indexing.
finally it works in a simple way:
prove ={mdl;mdl2}
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)

Accedi per commentare.

Richiesto:

il 22 Ott 2015

Commentato:

il 23 Ott 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by