Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

can polyfit give R^2 value for linear fit?

4,308 views
Skip to first unread message

noob

unread,
Jan 18, 2006, 2:04:41 PM1/18/06
to
Hi all,
Sorry I'm asking this question as its own thread:

Can polyfit give R^2 value for linear fit? How??

Thanks a lot.
-Noob

Tom Lane

unread,
Jan 23, 2006, 3:31:46 PM1/23/06
to
> Can polyfit give R^2 value for linear fit? How??

Noob, it does not compute this directly but you can compute it using the
"normr" field in the output structure. This shows that you get the same
results as from the regstats function in the Statistics Toolbox.

>> load census
>> [p,s] = polyfit(cdate,pop,2);
>> 1 - s.normr^2 / norm(pop-mean(pop))^2
ans =
0.99871296577201
>> regstats(pop, cdate,'quadratic', 'rsquare')
ans =
source: 'regstats'
rsquare: 0.99871296577201

-- Tom


Ben

unread,
Jul 1, 2014, 10:11:09 AM7/1/14
to
noob <no...@noob.noob> wrote in message <ef25e...@webx.raydaftYaTP>...
From http://www.mathworks.com/help/matlab/ref/polyfit.html:

[p,S] = polyfit(x,y,n) returns the polynomial coefficients p and a structure S for use with polyval to obtain error estimates or predictions. Structure S contains fields R, df, and normr, for the triangular factor from a QR decomposition of the Vandermonde matrix of x, the degrees of freedom, and the norm of the residuals, respectively. If the data y are random, an estimate of the covariance matrix of p is (Rinv*Rinv')*normr^2/df, where Rinv is the inverse of R.

I had this question too, I'm not sure if this is the right way to go about it.

John D'Errico

unread,
Jul 1, 2014, 10:20:11 AM7/1/14
to
"Ben" wrote in message <loufht$2ol$1...@newscl01ah.mathworks.com>...
You could just use polyfitn, from the file exchange.

It returns R^2, as well as some other parameters.

John

Ben

unread,
Jul 1, 2014, 10:47:12 AM7/1/14
to
"Ben" wrote in message <loufht$2ol$1...@newscl01ah.mathworks.com>...
Follow-up to earlier answer

If x and y are your data vectors

p = polyfit(x,y,degree);
polydata = polyval(p,x);
sstot = sum((y - mean(y)).^2);
ssres = sum((y - polydata).^2);
rsquared = 1 - (ssres / sstot);
0 new messages