95% confidence interval on a linear regression with polyfit?

Greetings once again all,
Pardon the title line, but if it shows confusion on my end, it's because I am!
I am trying to find a 95% confidence interval on my data set of 15 values. I did use polyfit to receive the slope and intercept, so I take it the next steps are the standard error and the t-test/Student's T-distribution (???).
But, there are so many functions and methods out there (I've done a search) it's confusing to me as I am doing this for the first time.
I do have some code already, and I'd like to get this nailed down and understand the concepts behind it.
[p2,S2] = polyfit(Data1,Data2(:,3),1);
slope_p2 = p2(1);
intercept_p2= p2(2);
Just stuck on what to do after this. I see suggestions for lscov, polyconf, fitlm, and the list goes on.
Any suggestions and teaching points would be very helpful. And I do have the statistics toolbox if that helps.
Thanks!

 Accepted Answer

If you want to do a linear regression and you have the Statistics Toolbox, my choice would be the regress function. If you ask it, you can get the regression coefficients and their confidence intervals, and the confidence intervals on the fit, as well as other statistics. All that information is in the documentation, so I won’t repeat it here.
If you want to stay with polyfit and polyval, asking polyval for ‘delta’ produces what appear from the documentation as standard errors of the estimate for various estimated values of y. You can calculate the 95% confidence intervals using the inverse t-statistic with n-2 degrees of freedom, n being the number of data pairs. Multiply each ‘delta’ by the same t-score.
If you want the 95% confidence limits on the parameter estimates calculated by polyfit, the File Exchange function polyparci can provide them.

8 Comments

Star Strider,
How did I know you were going to respond? ;)
Ok, I will look into those other functions you suggest and post back. Just wanted to acknowledge your answer and thank you.
Probably because I have some experience with linear and nonlinear regression (in biostatistical contexts mostly), and I never pass up the opportunity to mention polyparci. :)
My pleasure! I check my Profile Page during the day (it’s a Poisson process), so I’ll find your follow-up there if I don’t see it on the main Answer page.
Quick question on the polyparci function you wrote, more so the arguments of the function.
Obviously alpha is going to be 95% since I'm interested in the 95% confidence interval.
PolyPrms - the estimate, so I take it it's the intercept of your data? (I know I'm off, but just want to be sure).
Also, PolyS is the p structure returned by polyfit? So we have to use polyfit before we invoke your function?
Also, do you have any references that you used for the formulas for this code? And, should I decide (no offense) to use your code, how can I cite you?
Still looking at all the options.
Thanks!
You’re essentially correct — ‘PolyPrms’ are the parameters estimated by polyfit, and ‘PolyS’ is the ‘S’ structure returned.
So the call to polyfit would be (for a linear fit):
[p,S] = polyfit(X,Y,1);
and the call to polyparci:
CI = polyparci(p,S);
The references (other than the MATLAB documentation) was for the t-distribution calculation and is:
  • Abramowitz, M. and I.A. Stegun, Handbook of Mathematical Functions, Dover Publications, New York, 1970, P. 948, 26.7.1
I used the incomplete beta function betainc that is a core MATLAB function. The inverse t-distribution calculation uses fzero.
No offense taken! I’m honored! I’ll send you those details through your profile e-mail.
Star Strider,
Apologies in my delayed response but I did send a detailed email to your user profile to expand this discussion. Please let me know if you've received it.
Thanks!
Ok Star Strider - response sent.

Sign in to comment.

More Answers (0)

Asked:

on 8 Sep 2014

Edited:

on 6 Aug 2018

Community Treasure Hunt

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

Start Hunting!