R2 with a loglog plot

18 visualizzazioni (ultimi 30 giorni)
Flo
Flo il 8 Ago 2016
Modificato: dpb il 12 Ago 2016
Hi everyone.
I am quite new to matlab and I'd like to add a R2 to my loglog plot. I've seen some solution from few other posts, but none really does the job. My code is really simple:
bx= figure;
set(bx,'visible', 'on');
f = fit(x,y,'power1');
loglog(x,y,'O');
hold on;
plot(f);
[...]
the result look like that:
So far I haven't find any way of determining the r2: That post (https://uk.mathworks.com/matlabcentral/answers/182998-r-squared-value-for-fitted-line) overestimates my r2, and this bit of code found somewhere on the ML forum as well:
ba = [x,ones(size(x))]\y;
ypred = ba(1)*x + ba(2);
SSE=sum((y-ypred).^2);
SST=sum((y-mean(y)).^2);
Rsq = 1 - (SSE/SST);
I also tried that way, but I think it works fine only for linear distributions:
X = [ones(length(x),1) x];
b = X\y;
yCalc = X*b;
r = 1 - sum((y - yCalc).^2)/sum((y - mean(y)).^2);
Thank you very much for your help :)
Flo
PS: the the r2 on excel is equal to 0.9597

Risposte (1)

dpb
dpb il 8 Ago 2016
I see nothing wrong with Star's Answer nor follow-up comment. To compute SSE from such a model requires evaluating the residual of the fit in original metric, not in log space.
But, fit returns a cfit for curves (your case) and some additional optional outputs, the second of which is a goodness-of-fit structure, gof in the documentation. Fields in gof include
sse - Error SSquares
R2 - Coefficient of determination ("raw" R-square)
adjustedR2 - DOF-adjusted R-square
stdError - RMS (or "standard") error
so to save yourself some effort, use it...
[f,gof] = fit(x,y,'power1');
  7 Commenti
Flo
Flo il 12 Ago 2016
Maybe my previous graph was not the good version. I apologize for that, but I run the code again, and my graph look like yours dpb.
what did you mean by "OP should compare the results of the fitting from each." ?
Cheers everyone! flo
dpb
dpb il 12 Ago 2016
Modificato: dpb il 12 Ago 2016
Well, you need to look at the results obtained from the models (for the same, not disparate datasets) from Excel and Matlab to uncover where there's a difference. Clearly the results for the data you posted appear correct; if you got wildly different results from Excel, the most likely cause given the plot you posted is that it isn't the same dataset you're actually comparing to.
ADDENDUM
I attempted to read values off the above plot to see what kind of fit it actually provided; the Rsq was lower some but about 0.998 as 0.95 altho I could see guesstimating didn't work to get terribly close to the plot.
Agan, I can only suggest if you can reproduce the results you first quote in Excel, attach that set of data and model coefficients and results.
>> [f,gof]=fit(x,y,'power1')
f =
General model Power1:
f(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 1.026 (1.011, 1.04)
b = 1.013 (0.9852, 1.042)
gof =
sse: 1.6864e-04
rsquare: 0.9997
dfe: 13
adjrsquare: 0.9996
rmse: 0.0036
>> B % results from S-S:
B =
0.0254
1.0134
>> Rsq
Rsq =
0.9997
>>
BTW, I used fit in comparison to the fminsearch solution--results are essentially identical--
ADDENDUM 2
"*fit* in comparison to the fminsearch"
Actually, I just noticed there are two different solutions reached; the power term with fminsearch is about the magnitude of that of the fit solution less 1.0 -- 0.0254 vis a vis 1.026. How can that be???
Oh,
yfit = @(b,x) exp(b(1)) .* x.^b(2); % Power Function
has a definition problem; it's estimating log(B(1)) instead of B(1) directly...let's see what happens if redefine in same model space as fit uses--
>> yfit = @(b,x) b(1).*x.^b(2); % model A*x^B; B(1),B(2)-->A,B
>> SSECF = @(b) sum((yfit(b,x) - y).^2);
>> B = fminsearch(SSECF, [1; 1]);
>> B
B =
1.0256
1.0134
>>
Ah! As expected, now we agree...whew! :) Was worried there for a minute...

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by