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

标签 r statistics rss 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

最佳答案

以下是一些使用内置 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

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

关于r - 如何从线性模型输出中获取 RSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40456314/

相关文章:

R函数递归合并未命名参数,输入一个列表

r - 如何在绘图中将子图与具有两个 y 轴的图结合起来?

r - 向量:如何过滤掉值递减的段

postgresql - 基准 Amazon Redshift JSON_EXTRACT_PATH_TEXT

java - 使用 SAXParser 从 XML 中检索 HTML 编码的文本

sql - 如何在 R 中对 data.table 进行基本的左外连接?

python-3.x - 从平均值和标准差计算 Z 分数

statistics - 预测泊松过程

rss - 获取要在 RSS 描述字段中显示的 Youtube 视频 URL?