【发布时间】:2014-08-20 16:11:16
【问题描述】:
我已经为此苦苦挣扎了一段时间; 简而言之,我找不到 Excel 用于 R2 的方程式。
-
这是我的数据:
x:1 2 3 4 5 6 7 8 9 10
y: 4 9 1 2 1 1 8 5 5 1 -
我绘制数据,拟合幂律函数(“添加趋势线”)并使用“添加趋势线 > 选项 > 在图表上显示 R 平方值”
显示的值:
R2 = 0.03008。
问题 1
如果我使用 'RSQ()' 函数在 Excel 中计算它(获取 Excel 为拟合函数找到的参数值),或者使用定义(维基百科)手动计算...
R2 = 0.0272
问题 2
在Matlab中,使用'fit'函数,拟合函数的参数(当然还有R2)不是EXCEL找到的。
问题:
所以这是我的主要问题:
Excel 如何计算“添加趋势线”函数中的 R2,因为它显然不是定义中的那个(维基百科)?
还有额外的问题:
为什么 Excel 和 Matlab 的拟合参数不一样?
非常感谢!
%%%%%% 在下面编辑! %%%%
作为对评论的回答;
这是我使用的 Matlab 代码:
%% R-squared with the fit function
% use the fit function in Matlab, yobs being the data
[param, results] = fit(x,yobs,'power1');
% R-squared from the fit function :
r_sq_from_fit = results.rsquare;
%% here I calculate "by hand" the R-squared, from the general definition (wikipedia!)
% calculates the fitting data yfit
yfit = (p_powerlaw.a).*x.^p_powerlaw.b;
% mean of the yobs, total sum of squares, and residual sum of squares
yobs_mean = mean(yobs);
SStot = sum((yobs-yobs_mean).^2);
SSres = sum((yobs-yfit).^2);
r_sq_hand = 1-SSres/SStot;
无论我从 Matlab 中的 fit 函数得到 R 平方还是“手动”计算它,我都能找到相同的值。 Matlab 似乎是一致的,显然在其函数中使用了 R-squared 的严格定义......
但是;当我比较时:
- Excel 由
RSQ()函数给出的 R 平方值 - 以及我通过从定义中手动计算 R 平方获得的值(当然是 Excel 返回给我的 yfit 值,而不是 Matlab 返回的值,因为 Excel 和 Matlab 不同意关于拟合参数!)
...我得到不同的值! Excel:0.027,正如我之前所说,手工计算:-0.1109(!)
【问题讨论】:
-
你是如何精确到 0.0272 的?
-
我采用 Excel 为拟合找到的参数(3.6153 和 -0.217),计算拟合值(我们称它们为 yfit)并使用数据参数计算 RSQ 函数(y) 和拟合值 (yfit)。
标签: excel matlab regression curve-fitting trendline