Im using nlinfit function and I need to know the R square for the fitted solution

13 views (last 30 days)
Im using nlinfit function and I need to know the R square for the fitted solution

Answers (1)

the cyclist
the cyclist on 15 Dec 2022
rng default
N = 100;
x = randn(N,1);
y = 2 + 3*x + 5*randn(N,1);
modelfun = @(F,x) F(1) + F(2)*x;
beta0 = [0 0];
[beta,R] = nlinfit(x,y,modelfun,beta0);
Rsquared = 1 - sum(R.^2)/sum(((y-mean(y)).^2))
Rsquared = 0.3732
but I would recommend using fitnlm instead:
mdl = fitnlm(x,y,modelfun,beta0)
mdl =
Nonlinear regression model: y ~ F1 + F2*x Estimated Coefficients: Estimate SE tStat pValue ________ _______ _____ __________ F1 1.5965 0.50649 3.152 0.0021516 F2 3.3261 0.43546 7.638 1.4894e-11 Number of observations: 100, Error degrees of freedom: 98 Root Mean Squared Error: 5.04 R-Squared: 0.373, Adjusted R-Squared 0.367 F-statistic vs. constant model: 58.3, p-value = 1.49e-11
mdl.Rsquared
ans = struct with fields:
Ordinary: 0.3732 Adjusted: 0.3668

Community Treasure Hunt

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

Start Hunting!