# polyfit and R^2 value

966 views (last 30 days)
Rick on 20 Oct 2014
Commented: John D'Errico on 4 Dec 2018
Hello,
Is there a way to show the R^2 value when fitting a curve to data with polyfit? I know if I have two outputs, I get a structure
P =
R: [2x2 double]
df: 3
normr: 0.1782
EDU>> P.R
ans =
-4.3248 -2.1613
0 0.5734
but I don't know how to interpret this. I just want to know what the R^2 value is from a least squares fit. What are the numbers in the array of P.R? Thanks!

Orion on 20 Oct 2014
Hi,
Did you read the help of polyfit ?
[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. If the errors in the data y are independent normal with constant variance, polyval produces error bounds that contain at least 50% of the predictions.
in your case, P must be the 2nd output argument.

Star Strider on 20 Oct 2014
The ‘R’ returned in the ‘S’ structure is the covariance matrix of the parameters. If you want the correlation coefficients and the related statistics on your data, use the coorrcoef function.

#### 1 Comment

hoppingbuffalo on 7 Nov 2018
Good answer except it's the corrcoef function. I just want to add if your data is two column vectors then the off-diagonal elements of the 2x2 matrix corrcoef returns is what we conventionally think of as the correlation coefficient. That off-diagonal element squared is R^2.
Both polyfit and corrcoef are order N algorithms so both run very fast. Legendre and Gauss performed fitting by hand circa 1800. You can run polyfit and corrcoef one right after the other. If you don't want to use corrcoef you have to do a little extra work to get the output structure of polyfit to a correlation coefficient. The corrcoef documentation shows how to connect the covariance matrix to the correlation coefficients.
I'm a bit surprised that Mathworks doesn't have polyfit output the correlation coefficient matrix. Most people want the correlation coefficient and not the QR decomposition of the Vandermonde matrix of x.

Sarah on 29 Nov 2018
Hello ,
can someone confirm, is R here (if squared) the regression coefficient of the fit polynomial?

John D'Errico on 29 Nov 2018
NO. NO. NO. R can be used to obtain an approximate (estimated) covariance matrix of the parameters, althought it is NOT the covariace matrix itself. And R is definitely NOT the regression coefficients.
x = rand(10,1);y = rand(10,1);
[P,S] = polyfit(x,y,1)
P =
0.119244718933779 0.392023572442104
S =
struct with fields:
R: [2×2 double]
df: 8
normr: 0.808027325351178
The regression coefficients are contained in the first returned argument, here P.
S.R is a 2x2 upper triangular matrix, that contains information about the uncertainty in the model parameters as estimated.
Squaring the matrix S.R will not give you the frequently bandied about parameter R^2 either! This will work, in case you want that number:
1 - (S.normr/norm(y - mean(y)))^2
ans =
0.022268658204696
which is the correct value of R^2 for this problem.
Sarah on 4 Dec 2018
thank you, for the clarification, i am aiming for a value of R^2 as is usually obtained when fitting is performed on excel. For that as i understand from your comment i can apply this code 1 - (S.normr/norm(y - mean(y)))^2 correct?
John D'Errico on 4 Dec 2018
Yes. That expression will work to produce R^2. As long as your model has a constant term in it, and all models that polyfit would produce have a constant term, so that point is a given.
As a check, I'll compare that expression to what I get from my own code, polyfitn.
x = rand(10,1);y = rand(10,1);
[P,S] = polyfit(x,y,1)
P =
0.269646796107357 0.322887986613605
S =
struct with fields:
R: [2×2 double]
df: 8
normr: 0.920139196035294
1 - (S.normr/norm(y - mean(y)))^2
ans =
0.0877303425800532
mdl = polyfitn(x,y,1)
mdl =
struct with fields:
ModelTerms: [2×1 double]
Coefficients: [0.269646796107357 0.322887986613605]
ParameterVar: [0.0945091695169763 0.0448722642822286]
ParameterStd: [0.307423436837493 0.211830744421646]
DoF: 8
p: [0.405990496340959 0.165946560438871]
R2: 0.0877303425800532