MATLAB Answers

How to obtain Std of Coefficients from Curve Fitting

178 views (last 30 days)
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
  1 Comment
George on 2 Apr 2012
if the general model is nonlinear, for example:
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)
in this case, how can I obtain standard deviation or standard error, and convergence history? thank you!

Sign in to comment.

Accepted Answer

Richard Willey
Richard Willey on 2 Apr 2012
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 =,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.
Richard Willey
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.

More Answers (3)

Tom Lane
Tom Lane on 2 Apr 2012
Edited: 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
Tom Lane
Tom Lane on 6 May 2018
The 1 comes from wanting 1 standard error. The negative sign is to get the level associated with 1 standard error below zero. The multiplication by 2 is to include the values beyond 2 standard error above the mean, by symmetry. You are right, to get a confidence level you should subtract from 1. I will try to correct that.

Sign in to comment.

Richard Willey
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 =,Y)
The object that is generated by LinearModel includes the Standard Error as part of the default display.
myFit =,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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by