ftlim multiple regression with interaction term

Hi, I am trying to use ftlim to carry out multiple regression with an interaction term. I have looked at the example in mathworks and copied the example into my comand window. However, I keep getting an error "Predictor and response variables must have the same length". I have had the same problem with my own data. I am usin gMatlab R2013b. This is the example: http://www.mathworks.co.uk/help/stats/group-comparisons-using-categorical-arrays.html?refresh=true#zmw57dd0e3149 Can anyone help?
Thanks in advance.

댓글 수: 4

the cyclist
the cyclist 2014년 10월 2일
편집: the cyclist 2014년 10월 2일
Can you please confirm that if you type just these lines into the command window, that you get the error?
clear
load('carsmall')
cars = table(MPG,Weight,Model_Year);
cars.Model_Year = nominal(cars.Model_Year);
fit = fitlm(cars,'MPG~Weight*Model_Year')
and then what about this?
clear
rng(1)
X = randn(5,1);
Y = randn(5,1);
xy_table = table(X,Y);
fit = fitlm(xy_table,'Y~X')
Aoife
Aoife 2014년 10월 3일
Hi, thanks for your response. Yes, I get the same error using moth of those command lines. This is the error: Error using classreg.regr.FitObject/assignData (line 257) Predictor and response variables must have the same length.
Error in classreg.regr.TermsRegression/assignData (line 349) model = assignData@classreg.regr.ParametricRegression(model,X,y,w,asCat,varNames,excl);
Error in LinearModel.fit (line 852) model = assignData(model,X,y,weights,asCatVar,dummyCoding,model.Formula.VariableNames,exclude);
Error in fitlm (line 111) model = Linear
Aoife
Aoife 2014년 10월 3일
I have just used the latest version of Matlab and no longer receive this error.Thanks for your help.
Glad it worked out.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sven
Sven 2018년 4월 11일
편집: Sven 2018년 4월 11일

0 개 추천

It's a few years late but I think I've discovered the bug that may have been your problem (or at least generates a similar error that others might find via a search):
Linear regression (specifically the fitlm() method) fails when using a table as first input AND when that table contains a variable with 3 or more dimensions. This failure occurs even when that variable is not included as a term in the fit.
Bug is reproducible via the following, showing a fit working with minimal variables, still working with an unused 2d variable, and then failing with an unused 3d variable:
% Make a table
X = load('carsmall')
cars = table(X.MPG,X.Weight,nominal(X.Model_Year),'Var',{'MPG','Weight','Model_Year'})
% Show that fitlm works
fitRunsOk = fitlm(cars,'MPG~Weight*Model_Year')
cars.unused2dVar = rand(size(cars,1),5)
fitStillOk = fitlm(cars,'MPG~Weight*Model_Year')
% But it fails when an irrelevant variable is added that happens to be 3d
cars.unused3dVar = rand(size(cars,1),5,5)
fitFAILS = fitlm(cars,'MPG~Weight*Model_Year')
>> Error using classreg.regr.FitObject/assignData (line 134)
Predictor and response variables must have the same length.
The workaround until fixed (I've submitted a bug report) will be to make a temporary copy of your table that omits any variables with 3 or more dimensions.

질문:

2014년 10월 2일

편집:

2018년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by