R-squared value for fitted line

9 views (last 30 days)
Nabeel
Nabeel on 13 Mar 2015
Edited: Star Strider on 14 Mar 2015
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)

Accepted Answer

Star Strider
Star Strider on 13 Mar 2015
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 Comments
Nabeel
Nabeel on 14 Mar 2015
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 on 14 Mar 2015
Edited: Star Strider on 14 Mar 2015
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!