Can't get fitnlm to work?

4 views (last 30 days)
Devon Fisher-Chavez
Devon Fisher-Chavez on 28 Sep 2018
Answered: Steven Lord on 28 Sep 2018
I want to run a nonlinear regression on two variables, x_1 and y_1. Both are the same length (n=1461). My code looks like this:
tbl_1 = table(x_1,y_1);
modelfun_1 = 'y_1~b1*x_1^2 + b2*x_1 + b3';
beta0 = randn(3,1);
mdl_1 = fitnlm(tbl_1,modelfun_1,beta0);
I keep getting this error: "Error in fitnlm (line 99) model = NonLinearModel.fit(X,varargin{:});"
I have no idea what I'm doing wrong, can anyone help me? I am using matlab 2018b. Also, I don't know how to insert code into these questions.
  2 Comments
Steven Lord
Steven Lord on 28 Sep 2018
That doesn't look like it's the full text of the error message. There should have been a piece before the "Error in fitnlm" part. Can you post everything printed in red text when you receive that error?
Devon Fisher-Chavez
Devon Fisher-Chavez on 28 Sep 2018
Sorry about that!
"Error using classreg.regr.NonLinearFormula/setAllNames (line 419) The specified variable names do not include the response variable.
Error in classreg.regr.NonLinearFormula (line 265) f = setAllNames(f,coefNames,varNames,predictorNames,responseName,inferNames);
Error in NonLinearModel/createFormula (line 1636) formula = classreg.regr.NonLinearFormula(modelDef,coefNames,[],[],varNames,ncoefs);
Error in NonLinearModel.fit (line 1401) model.Formula = NonLinearModel.createFormula(supplied,modelDef,X,ncoefs,coefNames, ...
Error in fitnlm (line 99) model = NonLinearModel.fit(X,varargin{:});"

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 28 Sep 2018
Looking at the documentation for fitnlm, specifically the section describing the tbl input:
"Input data, specified as a table or dataset array. If you do not specify the predictor and response variables, the last variable is the response variable and the others are the predictor variables by default."
Try adding the 'PredictorVars' and 'ResponseVar' parameters to your fitnlm call to explicitly state which variables you want to use as the predictors and which the response.
If that doesn't work, can you show a summary of your table tbl_1? That will clearly show the sizes and types of the variables in the table.

Tags

Community Treasure Hunt

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

Start Hunting!