So I've fitted a linear trend to my data and calculated R^2 in two different ways (in Matlab), one is using corrcoef
and the other is "by hand". These return different results and both seem to make sense, so I'm not sure why that is. My methods are as follows, with x
being the number of years and y
my values:
(1)
rsq1 = corrcoef(x, y);
(2)
%// fitting the model
p = polyfit(x,y,1);
yfit = polyval(p,x);
%// calculating R^2
yresid = y - yfit;
SSresid = sum(yresid.^2);
SStotal = (length(y)-1) * var(y);
rsq2 = 1 - SSresid/SStotal;
Since I'm very new to this I can't seem to figure out why rsq1
and rsq2
are different. I have a feeling I'm missing something obvious... does anyone have an idea?
Thanks for any help!