显示决定系数
此示例说明如何显示 R 方(决定系数)和调整 R 方。加载样本数据并定义响应和自变量。
load hospital
y = hospital.BloodPressure(:,1);
X = double(hospital(:,2:5));
拟合线性回归模型。
mdl = fitlm(X,y)
mdl =
Linear regression model:
y ~ 1 + x1 + x2 + x3 + x4
Estimated Coefficients:
Estimate SE tStat pValue
_________ ________ ________ __________
(Intercept) 117.4 5.2451 22.383 1.1667e-39
x1 0.88162 2.9473 0.29913 0.76549
x2 0.08602 0.06731 1.278 0.20438
x3 -0.016685 0.055714 -0.29947 0.76524
x4 9.884 1.0406 9.498 1.9546e-15
Number of observations: 100, Error degrees of freedom: 95
Root Mean Squared Error: 4.81
R-squared: 0.508, Adjusted R-Squared: 0.487
F-statistic vs. constant model: 24.5, p-value = 5.99e-14
R 方值和调整 R 方值分别为 0.508 和 0.487。模型解释了响应变量中大约 50% 的变异。
使用拟合的 LinearModel 对象的属性访问 R 方值和调整 R 方值。
mdl.Rsquared.Ordinary
ans = 0.5078
mdl.Rsquared.Adjusted
ans = 0.4871
调整 R 方值小于普通的 R 方值。