Straight line fitting and error calculation

Hello everyone,
I have done straight line fitting by using polyfit command where i gave polynomial = 1 and found the values of m and c of the equation mx + c = y
Now i want to find errors associated with m and c i.e I want finally
m +/- error c +/- error
It would be nice if anyone could help me with it. I am badly stuck :(
Thanks

 Accepted Answer

I am glad you asked! See polyparci in the File Exchange.

10 Comments

Thanks strider for your reply.
Could you please help on how to use it.
My present program giving m and c values uses polyfit and polyval. How should i include this in my program.
Thanks
My pleasure!
First, download it and put it in with your other user files.
It’s internally documented, so quoting from that:
% CALLING POLYPARCI:
% [p,S] = polyfit(x,y,n); % Fit the data
% ci = polyparci(p,S); % NOTE: specifying a value for ‘alpha’ is
% optional
Call polyfit as you would normally, but include ‘S’ in the desired outputs. (Here ‘p’ is the vector of polynomial coefficients.) Then pass ‘p’ and ‘S’ to ‘polyparci’ as illustrated. It will return a matrix of confidence intervals as ‘ci’, whose columns correspond to the columns in the ‘p’ vector, so ‘ci(:,1)’ are the confidence intervals for ‘p(1)’, and so for all the others.
Example:
x = rand(10,1);
y = rand(10,1);
[p,S] = polyfit(x,y,1)
ci = polyparci(p,S)
Note that ‘polyparci’ has no relevance to polyval or anything it does.
hey.. thanks a lot for the detailed explanation. I am highly obliged.
One more small confusion.. this will give confidence intervals for m and c but i want errors associated with these two parameters. So how will i find that from confidence intervals?
My pleasure!
You can get the standard errors from the ‘S’ structure returned by polyfit (I call it ‘PolyS’ in this code snippet):
COVB = (PolyS.R'*PolyS.R)\eye(size(PolyS.R)) * PolyS.normr^2/PolyS.df;
SE = sqrt(diag(COVB)); % Standard Errors
okay.. So COVB gives matrix or errors with slope and intercept? and what does SE gives? also what is R in COVB formula.. could you please explain jst a little bit so that i can understand what these commands are acually doing??
Thanks a lott once again..!! :)
My pleasure!
The ‘COVB’ matrix is the covariance matrix for the parameters. Its diagonal are the variances of the individual parameters. The ‘SE’ variable is the standard error of the estimate for each parameter. As for its calculation from the ‘S’ structure, the documentation for polyfit explains it better than I can. See the documentation for S — Error estimation structure for details. I used those values to calculate the data necessary to generate the t-scores for the parameter confidence intervals.
ohh... nice.. so in m +/- error and C +/- error errors are those which we are getting from SE?
Yes.
I use the SE to calculate the confidence intervals.
hey.. thanks a lot..!! :-)
I will bother you again if required.. bt thanks for everything.. you were of great help.. :-)

Sign in to comment.

More Answers (0)

Asked:

on 30 Jan 2015

Commented:

on 10 Feb 2015

Community Treasure Hunt

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

Start Hunting!