-
Notifications
You must be signed in to change notification settings - Fork 7
/
rsquare.m
50 lines (43 loc) · 1.6 KB
/
rsquare.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function [R2, AbsRes, NormRes, AvgAbsRes, MaxAbsRes] = rsquare(y,yhat)
% PURPOSE: calculate r square using data y and estimates yhat
% -------------------------------------------------------------------
% USAGE: R2 = rsquare(y,yhat)
% where:
% y are the original values as vector or 2D matrix and
% yhat are the estimates calculated from y using a regression, given in
% the same form (vector or raster) as y
% -------------------------------------------------------------------------
% OUTPUTS:
% R2 is the r square value calculated using 1-SS_E/SS_T
% -------------------------------------------------------------------
% Note: NaNs in either y or yhat are deleted from both sets.
%
% Felix Hebeler, Geography Dept., University Zurich, Feb 2007
if nargin ~= 2
error('This function needs some exactly 2 input arguments!');
end
% reshape if 2d matrix
yhat=reshape(yhat,1,size(yhat,1)*size(yhat,2));
y=reshape(y,1,size(y,1)*size(y,2));
% delete NaNs
while sum(isnan(y))~=0 || sum(isnan(yhat))~=0
if sum(isnan(y)) >= sum(isnan(yhat))
yhat(isnan(y))=[];
y(isnan(y))=[];
else
y(isnan(yhat))=[];
yhat(isnan(yhat))=[];
end
end
% 1 - SSe/SSt
SSe = sum( (y-yhat).^2 );
SSt = sum( (y-mean(y)).^2 );
R2 = 1 - ( SSe / SSt );
AbsRes = sum(abs(y-yhat));
NormRes = sqrt(SSe);
AvgAbsRes = AbsRes/length(y);
MaxAbsRes = max(abs(y-yhat));
% R2 = sum((yhat-mean(y)).^2) / sum( (y-mean(y)).^2 ) ;
%if R2<0 || R2>1
% error(['R^2 of ',num2str(R2),' : yhat does not appear to be the estimate of y from a regression.'])
%end