Asked by George
on 2 Apr 2012

Dear folkers, I want to obtain standard deviation of coefficients after using curve fitting. but I couldn't find information from help documents. how can I get it? thanks!!

ex.: the general model is: f(x) = a*x +b Coefficients: a = 1.5 (-1 3) b = 2 (0.5 4.5) now, how do i get the "std" of "a" and "std" of "b"

thank you

Answer by Richard Willey
on 2 Apr 2012

Accepted Answer

Hi George

Conveniently, 12a also has a function call NonLinearModel

%%Generate some data

X = 2* pi*rand(100,1);

X = sortrows(X);

Y = 9 + 7*sin(1*X + 3) + randn(100,1);

Generate a fit

myFit = NonLinearModel.fit(X,Y, 'y ~ b0 + b1*sin(b2*x1 + b3)', [9, 7, 1, 3])

Here's the output

myFit =

Nonlinear regression model:

y ~ b0 + b1*sin(b2*x1 + b3)

Estimated Coefficients:

Estimate SE tStat pValue

b0 8.9014 0.094189 94.506 1.5635e-96

b1 6.8951 0.13773 50.06 1.3538e-70

b2 1.0018 0.011212 89.356 3.1924e-94

b3 3.0188 0.038947 77.511 2.2541e-88

The one thing that you won't get is convergence history. If you need a complete description of the path that the solvers are following you're probably better off using Optimization Toolbox rather than Stats.

George
on 2 Apr 2012

Hi Richard, thank you again.

I was trying to use NonLinearModel.fit, but it gives me error:

%%

load carbig

X = [Horsepower,Weight];

y = MPG;

modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...

b(4)*x(:,2).^b(5);

beta0 = [-50 500 -1 500 -1];

mdl = NonLinearModel.fit(X,y,modelfun,beta0)

??? Undefined variable "NonLinearModel" or class "NonLinearModel.fit".

I installed Stats tool box (11a)？ do you know this is the reason (giving error) or not?

thank you!

Richard Willey
on 2 Apr 2012

LinearModel and NonLinearModel are new in 12a.

Prior to 12a, you can use nlinfit to perform the same analysis.

Sign in to comment.

Answer by Tom Lane
on 2 Apr 2012

Edited by Tom Lane
on 6 May 2018

You can get more information when you invoke the fit command:

[obj,gof,opt] = fit(...)

This gives the fitted obj, goodness-of-fit statistics, and optimization info.

The Curve Fitting output is aimed at confidence intervals rather than standard errors. The confidence intervals are roughly the estimated coefficient plus or minus two standard errors. If you have the Statistics Toolbox then you can find the confidence level you'd need to get intervals that are plus or minus one standard error, then pass that level into the confint method. Something like this:

level = 2*tcdf(-1,gof.dfe)

% confint(obj,level) <- this original is incorrect

confint(obj,1-level) %<- corrected

Carlos Claiton Noschang Kuhn
on 27 Mar 2018

Pavel Kolesnichenko
on 30 Mar 2018

Hi Tom. I am also curious, why did you put -1 in tcdf function. Also, I reckon it should be

level = 1 - 2*tcdf(-1,gof.dfe)

Tom Lane
on 6 May 2018

Sign in to comment.

Answer by Richard Willey
on 2 Apr 2012

The 12a release of Statistics Toolbox has some very nice new capabilities for regression analysis.

%%Generate some data

X = linspace(1,100, 50);

X = X';

Y = 5*X + 50;

Y = Y + 20*randn(50,1);

%%Generate a fit

myFit = LinearModel.fit(X,Y)

The object that is generated by LinearModel includes the Standard Error as part of the default display.

myFit = LinearModel.fit(X,Y)

myFit =

Linear regression model:

y ~ 1 + x1

Estimated Coefficients:

Estimate SE tStat pValue

(Intercept) 63.499 7.0973 8.9469 8.4899e-12

x1 4.8452 0.12171 39.809 2.0192e-38

Number of observations: 50, Error degrees of freedom: 48

Root Mean Squared Error: 25.1

R-squared: 0.971, Adjusted R-Squared 0.97

F-statistic vs. constant model: 1.58e+03, p-value = 2.02e-38

Please note:

This same information is available in earlier versions of the product. For example, the second output from regress is "bint" which are the confidence intervals for the regression coefficients.

However, I think that the display capabilities for the LinearModel objects are a big improvement over what came before.

George
on 2 Apr 2012

Hi Richard, thank you.

this seems for linear model. if my model is nonlinear, then how do I obtain find this information again?

again, thank you very much!

ex.:

General model:

f(x) = (b-a)./(1+((x/x0).^k)) +a

Coefficients (with 95% confidence bounds):

a = 3.281 (2.625, 3.938)

b = 0.2708 (-0.1386, 0.6803)

k = 20.24 (-6.81, 47.3)

x0 = 13.51 (12.48, 14.54)

Goodness of fit:

SSE: 6.448

R-square: 0.8444

Adjusted R-square: 0.8152

RMSE: 0.6348

George
on 3 Apr 2012

Thank you for your help, Richard.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## George (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/34234-how-to-obtain-std-of-coefficients-from-curve-fitting#comment_71463

Sign in to comment.