fitlm() with 0 intercept function returns different values for R2 betwen MATLAB 2020a and 2020b?

12 次查看(过去 30 天)
I am trying to find the coefficient of determination. I ran the same fitlm function in version 2020a and 2020b, but calling the 'Rsquared.Ordinary' variable gives me drastically different results.
2020a, R2 = 0.5432
2020b, R2 = -8.0637
Why are they different?
Here is the code I am using:
data =[1.8286, 2.0125; 2.5571, 2.3886; 2.0747, 2.3476; 1.6216, 2.0296;
4.6010, 2.4861; 2.6089, 2.4834; 2.0093, 2.5387; 2.4757, 2.5660;
2.5475, 2.6642; 2.4854, 2.6650];
mdl = fitlm(data(:,1), data(:,2),'Intercept',false);
R2 = mdl.Rsquared.Ordinary;

采纳的回答

the cyclist
the cyclist 2021-2-24
Interesting. The 2020b result seems to be correct, based on the formula using sums of squares. (See, e.g. the definition on wikipedia page.)
% Original data
data =[1.8286, 2.0125; 2.5571, 2.3886; 2.0747, 2.3476; 1.6216, 2.0296;
4.6010, 2.4861; 2.6089, 2.4834; 2.0093, 2.5387; 2.4757, 2.5660;
2.5475, 2.6642; 2.4854, 2.6650];
% Define x,y for convenience
x = data(:,1);
y = data(:,2);
mdl = fitlm(x,y,'Intercept',false);
R2 = mdl.Rsquared.Ordinary
R2 = -8.0618
% Predicted y value, at x data
y_pred = predict(mdl,data(:,1));
% Calculate R^2 via sums of squares of residuals and total
R_squared_via_sum_of_squares = 1 - sum((y - y_pred).^2)/sum((y - mean(y)).^2)
R_squared_via_sum_of_squares = -8.0618
Based on some playing around, but not quite well formulated enough to post here, I think 2020a might have been using a formula based on the correlation of y data vs. y predicted that is only valid when an intercept term is estimated, and therefore not accurate in your case.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Analysis of Variance and Covariance 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by