MATLAB: R-squared value for fitted line

r-squared

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)

Best Answer

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.