R-squared value for fitted line

조회 수: 10 (최근 30일)
Nabeel
Nabeel 2015년 3월 13일
편집: Star Strider 2015년 3월 14일
I have plotted log-log graph for data series. than fit a line by ployfit i want to find R-squared for line and data how it can be done (R-squared is explained variance)

채택된 답변

Star Strider
Star Strider 2015년 3월 13일
Using the Wikipedia article on Coefficient of Determination, it’s easiest (and likely correct) to compute the R-squared value on your data using the nonlinear regression and not the log-log linear fit:
dry=[49 12 5 1 1 1 0 0 0 ];
x1=[1 2 3 4 5 6 7 8 9];
x1dry = linspace(min(x1), max(x1));
pwrfit = @(b,x) b(2) .* x.^b(1);
OLSCF = @(b) sum((dry-pwrfit(b,x1)).^2);
B = fminsearch(OLSCF, [-2; 50]);
SStot = sum((dry - mean(dry)).^2); % Compute R-squared
SSres = OLSCF(B);
Rsq = 1 - (SSres/SStot);
When I did this calculation, I got R-squared to be 0.998. Do the same with your ‘wet’ value, with the appropriate changes in the code.
  댓글 수: 2
Nabeel
Nabeel 2015년 3월 14일
Thanks this this graph was a part of research paper which i have studied recently. this code work well for it. but when i apply this on my original data it has r-squared of 0 for both wet and dry......
Star Strider
Star Strider 2015년 3월 14일
편집: Star Strider 2015년 3월 14일
My pleasure.
When I run this code:
wet=[120 49 30 21 12 10 9 7 4];
dry=[49 12 5 1 1 1 0 0 0 ];
x1=[1 2 3 4 5 6 7 8 9];
pwrfit = @(b,x) b(2) .* x.^b(1);
% ‘Dry’ Calculations
OLSCF = @(b) sum((dry-pwrfit(b,x1)).^2);
Bdry = fminsearch(OLSCF, [-2; 50]);
SStot = sum((dry - mean(dry)).^2); % Compute R-squared
SSres = OLSCF(Bdry);
RsqDry = 1 - (SSres/SStot);
% ‘Wet’ Calcualtions
OLSCF = @(b) sum((wet-pwrfit(b,x1)).^2);
Bwet = fminsearch(OLSCF, [-2; 50]);
SStot = sum((wet - mean(wet)).^2); % Compute R-squared
SSres = OLSCF(Bwet);
RsqWet = 1 - (SSres/SStot);
fprintf(1, '\n\t Wet = %8.3f * x^%.3f,\t\tRsq = %.4f\n', Bwet(2), Bwet(1), RsqWet)
fprintf(1, '\n\t Dry = %8.3f * x^%.3f,\t\tRsq = %.4f\n', Bdry(2), Bdry(1), RsqDry)
I get:
Wet = 120.447 * x^-1.323, Rsq = 0.9980
Dry = 49.123 * x^-2.148, Rsq = 0.9977
I have no idea why you’re getting 0 for those. At worst, if your default format is set to something that rounds to integers, you should get 1 instead.
Copy and paste my code and run it. You should get the same results.

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

추가 답변 (0개)

태그

Community Treasure Hunt

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

Start Hunting!

Translated by