0
我在R中做了一個非常簡單的線性迴歸,但計算的R^2似乎並不正確。我也做了迴歸分析如下:在R中使用線性迴歸計算不正確的R平方
data(cats)
fit = lm(Hwts ~ Bwts+0, data = cats)
summary(fit)
我從這個迴歸得到的結果是:在
Call:
lm(formula = cats$Hwt ~ cats$Bwt + 0)
Residuals:
Min 1Q Median 3Q Max
-3.4563 -0.9980 -0.1003 1.0044 5.2623
Coefficients:
Estimate Std. Error t value Pr(>|t|)
cats$Bwt 3.90711 0.04364 89.53 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.449 on 143 degrees of freedom
Multiple R-squared: 0.9825, Adjusted R-squared: 0.9823
F-statistic: 8015 on 1 and 143 DF, p-value: < 2.2e-16
計算的R-sqaured更是高達0.9825,但一個簡單的情節數據會給你一個R平方不能那麼高的直覺。如果我計算使用維基百科給出的公式R平方,我得到了以下結果:
1-sum(residuals(fit1)^2)/sum((cats$Hwt - mean(cats$Hwt))^2)
[1] 0.6459604
此R平方的0.6459定義看起來更合理,而且我發現,如果我能夠在迴歸截距我會
fit = lm(Hwt ~ Bwt, data = cats)
summary(fit)
Call:
lm(formula = Hwt ~ Bwt, data = cats)
Residuals:
Min 1Q Median 3Q Max
-3.5694 -0.9634 -0.0921 1.0426 5.1238
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.3567 0.6923 -0.515 0.607
Bwt 4.0341 0.2503 16.119 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.452 on 142 degrees of freedom
Multiple R-squared: 0.6466, Adjusted R-squared: 0.6441
F-statistic: 259.8 on 1 and 142 DF, p-value: < 2.2e-16
怎麼來的R平方變得如此不同,當我允許或不允許攔截:解決此號碼獲得一個R平方?請給出意見。