Take the 2-minute tour ×

I have data and I need to do a linear regression on the data to obtain


Alpha and Beta are estimators given by the regression, polyfit can give me those with no problem but this is a physical science report and I need to give error estimators on those values

I know from statistics that standard deviation exists for simple linear regression coefficients.

How can I calculate then in Matlab

Thank you

share|improve this question
The least squares linear fit is a statistic, it does not have error bars per say. For instance, if you calculate the mean value of a bunch of data points does it have error bars? –  justaname Nov 24 '11 at 16:22
look at cftool, it might be what you need –  Rasman Nov 24 '11 at 16:24
Do you need the standard errors of the regression coefficients Alpha, or are you looking to calculate confidence bounds on y? Do you have access to Statistics Toolbox, or just MATLAB? –  Sam Roberts Nov 24 '11 at 16:37
I am not talking about error bars at all, I am talking about the SD of the estimators –  David MZ Nov 24 '11 at 16:39
@SamRoberts I am looking for the SD of Alpha and Beta and I do have access to the Statistics Toolbox –  David MZ Nov 24 '11 at 16:40

3 Answers 3

up vote 0 down vote accepted

The second output of the regress command will give you 95% confidence intervals for the regression coefficients. Here's an example:

>> x = [ones(100,1), (1:100)'];
>> alpha = 3; beta = 2;
>> y = x*[alpha; beta]+randn(100,1);
>> [coeffs, coeffints] = regress(y,x);
>> coeffs
coeffs =
>> coeffints
coeffints =
       2.5851       3.3573
       1.9932       2.0064

Here alpha has been estimated 2.9712, with 95% confidence interval of between 2.5851 and 3.3573, and beta has been estimated as 1.9998, with 95% confidence interval between 1.9932 and 2.0064.

share|improve this answer
The standard deviation of the coefficients is in the fifth output, stats. –  Jonas Nov 24 '11 at 17:09

Or just use the relationship that the length of the 95% Confidence Interval is 2*1.9654 standard errors so the standard errors in the example with regress above are given by:

st errors = (coeffints(:,2)-coeffints(:,1))/(2*1.9654).

The number 1.9654 appears because of the normality assumption in the regress function

share|improve this answer

The easiest solution is to use LSCOV:

%# create some data
x = 1:10;
y = 3*x+randn(size(x))*0.1;

%# create the design matrix
A = [x(:),ones(length(x),1)];

[u,std_u] = lscov(A,y(:));

u =
std_u =
share|improve this answer

Your Answer


