Predictor and Response Variables must have same length error

7 views (last 30 days)
Hello,
I am attempting to use fitnlm, and come across an error I don't understand.
x =[p;l];
model=@(b,x)((x(1,:)+x(2,:)+b(2))-sqrt((x(1,:)+x(2,:)+b(2)).^2)-(4*x(1,:)*x(2,:)))*(b(1)/(2*x(1,:)));
beta0=[0.1,1];
for i=1:rows
y=csp(i,:)
x
size(x(1,:))
size(x(2,:))
size(y)
fitnlm(x,y,model,beta0)
end
Issue is, the x and y are the same length so I don't quite know why I'm getting this error
y =
0 0 0 0 0 0 0 0 0
x =
0.1500 0.1800 0.1900 0.2100 0.2300 0.2500 0.2600 0.2800 0.3000
1.1000 0.8800 0.7700 0.6300 0.4700 0.3700 0.2600 0.1400 0
ans =
1 9
ans =
1 9
ans =
1 9

Accepted Answer

the cyclist
the cyclist on 1 Jun 2023
Edited: the cyclist on 1 Jun 2023
fitnlm expects column vectors as input, not row vectors. Rows are the observations, and columns are variables.
y = [0 0 0 0 0 0 0 0 0]';
x = [0.1500 0.1800 0.1900 0.2100 0.2300 0.2500 0.2600 0.2800 0.3000;
1.1000 0.8800 0.7700 0.6300 0.4700 0.3700 0.2600 0.1400 0]';
model=@(b,x)((x(:,1)+x(:,2)+b(2))-sqrt((x(:,1)+x(:,2)+b(2)).^2)-(4*x(:,1).*x(:,2))).*(b(1)./(2.*x(:,1)));
beta0=[0.1,1];
fitnlm(x,y,model,beta0)
Warning: Rank deficient, rank = 1, tol = 9.077909e-15.
Warning: Rank deficient, rank = 1, tol = 9.037373e-15.
Warning: Rank deficient, rank = 1, tol = 9.033309e-15.
Warning: Rank deficient, rank = 1, tol = 9.032903e-15.
Warning: Some columns of the Jacobian are effectively zero at the solution, indicating that the model is insensitive to some of its parameters. That may be because those parameters are not present in the model, or otherwise do not affect the predicted values. It may also be due to numerical underflow in the model function, which can sometimes be avoided by choosing better initial parameter values, or by rescaling or recentering. Parameter estimates may be unreliable.
ans =
Nonlinear regression model: y ~ ((x1 + x2 + b2) - sqrt((x1 + x2 + b2)^2) - (4*x1*x2))*(b1/(2*x1)) Estimated Coefficients: Estimate SE tStat pValue ________ __________ ______ ________ b1 9.89e-16 3.4966e-16 2.8284 0.022204 b2 1 0 Inf 0 Number of observations: 9, Error degrees of freedom: 8 Root Mean Squared Error: 1.29e-15 R-Squared: -Inf, Adjusted R-Squared -Inf F-statistic vs. zero model: 0, p-value = 1
Note that I also did two fixes to your model definition:
  • converted it to accept the columns instead of the rows
  • used element-wise multiplication and division operators, instead of matrix operations

More Answers (0)

Community Treasure Hunt

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

Start Hunting!