如何从线性模型输出中获取RSS

wsz*_*mjj 8 statistics rss r linear-regression

下面是数据集的线性模型输出,该数据集由响应变量和三个解释变量组成.我如何获得原始回归的RSS?

  Call:
  lm(formula = y ~ x1 + x2 + x3)
Residuals:
      Min      1Q  Median      3Q     Max
  -4.9282 -1.3174  0.0059  1.3238  4.4560
  Coefficients:
               Estimate Std. Error t value Pr(>|t|)
  (Intercept) -7.056057   1.963805  -3.593 0.000481 ***
  x1           3.058592   0.089442  34.196  < 2e-16 ***
  x2          -5.763410   0.168072 -34.291  < 2e-16 ***
  x3           0.000571   0.165153   0.003 0.997247
  ---
  Signif. codes:  0 *** 0.001 ** 0.01 * 0.05 . 0.1   1
  Residual standard error: 1.928 on 116 degrees of freedom

Multiple R-squared:  0.9546,Adjusted R-squared:  0.9535
F-statistic:   814 on 3 and 116 DF,  p-value: < 2.2e-16
Run Code Online (Sandbox Code Playgroud)

G. *_*eck 20

以下是使用内置anscombe数据集计算残差平方和(RSS)的一些方法:

fm <- lm(y1 ~ x1+x2+x3, anscombe)

deviance(fm)
## [1] 13.76269

sum(resid(fm)^2)
## [1] 13.76269

anova(fm) # see the Residuals row of the Sum Sq column
## Analysis of Variance Table
##
## Response: y1
##           Df Sum Sq Mean Sq F value  Pr(>F)   
## x1         1 27.510 27.5100   17.99 0.00217 **
## Residuals  9 13.763  1.5292                   
## ---
## Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

anova(fm)["Residuals", "Sum Sq"]
## [1] 13.76269

with(summary(fm), df[2] * sigma^2)
## [1] 13.76269
Run Code Online (Sandbox Code Playgroud)

关于最后一个,请注意,如果您只想使用打印输出来计算RSS summary(fm)$df[2],summary(fm)$sigma则会在summary(fm)输出中显示summary.特别地,对于问题df [2] = 116和sigma = 1.928中所示的输出,所以RSS = df [2]*sigma ^ 2 = 116*1.928 ^ 2 = 431.1933.