필터 지우기
필터 지우기

polyfit and R^2 value

조회 수: 585 (최근 30일)
Rick
Rick 2014년 10월 20일
편집: Bruno Luong 2023년 7월 27일
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!

답변 (3개)

Sarah
Sarah 2018년 11월 29일
Hello ,
can someone confirm, is R here (if squared) the regression coefficient of the fit polynomial?
  댓글 수: 5
Edmund Wascher
Edmund Wascher 2023년 7월 27일
Does this approach also holds for higher order fits or only for linear ones?
Bruno Luong
Bruno Luong 2023년 7월 27일
편집: Bruno Luong 2023년 7월 27일
@Edmund Wascher "Does this approach also holds for higher order fits or only for linear ones?"
Yes the formula
[P, S] = polyfit(x, y, n)
R_squared = 1 - (S.normr/norm(y - mean(y)))^2
returns R^2 for any order polyfit n.

댓글을 달려면 로그인하십시오.


Orion
Orion 2014년 10월 20일
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.
  댓글 수: 1
Igor
Igor 2022년 10월 25일
편집: Igor 2022년 10월 25일
The help is written is an overcomplicated way and the parameters are not explained at all for somebody starting with matlab trying to do some simple linear fit. Why does the polyfit do not calculate the regular R correlation coeficient right away it would be much more simple than some S matrix of values

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2014년 10월 20일
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
hoppingbuffalo
hoppingbuffalo 2018년 11월 7일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by