仅打印带显著性标记的线性回归(lm)摘要的系数表格。

huangapple go评论97阅读模式
英文:

Printing only the coefficient table of the summary of an lm with significance marks

问题

考虑一个R中的线性模型:

  1. frame <- data.frame(y=rnorm(100), a=rnorm(100), b=rnorm(100))
  2. frame["c"] <- frame["y"] + 2*rnorm(100)
  3. model <- lm(y ~ a + b + c, data=frame) #lm

调用 summary(model) 会在控制台产生以下输出:

  1. Call:
  2. lm(formula = y ~ a + b + c, data = frame)
  3. Residuals:
  4. Min 1Q Median 3Q Max
  5. -2.13983 -0.54933 0.01975 0.44218 2.38721
  6. Coefficients:
  7. Estimate Std. Error t value Pr(>|t|)
  8. (Intercept) -0.060624 0.088597 -0.684 0.4955
  9. a 0.009773 0.088171 0.111 0.9120
  10. b 0.157979 0.080924 1.952 0.0538 .
  11. c 0.188913 0.039788 4.748 7.17e-06 ***
  12. ---
  13. Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 ‘.’ 0.1 1
  14. Residual standard error: 0.8464 on 96 degrees of freedom
  15. Multiple R-squared: 0.2146, Adjusted R-squared: 0.1901
  16. F-statistic: 8.745 on 3 and 96 DF, p-value: 3.478e-05

我想要只打印系数部分,与这个帖子的要求相反,如下所示:

  1. Coefficients:
  2. Estimate Std. Error t value Pr(>|t|)
  3. (Intercept) -0.060624 0.088597 -0.684 0.4955
  4. a 0.009773 0.088171 0.111 0.9120
  5. b 0.157979 0.080924 1.952 0.0538 .
  6. c 0.188913 0.039788 4.748 7.17e-06 ***
  7. ---
  8. Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 ‘.’ 0.1 1

不建议使用 coef(summary(model)),因为它会产生以下结果,缺少右侧的显著性标记,如这里所建议:

  1. Estimate Std. Error t value Pr(>|t|)
  2. (Intercept) -0.060624 0.088597 -0.684 0.4955
  3. a 0.009773 0.088171 0.111 0.9120
  4. b 0.157979 0.080924 1.952 0.0538
  5. c 0.188913 0.039788 4.748 7.17e-06

最好的解决方案不涉及重新编写整个 summary() 主体,如上面的帖子中所建议。

英文:

Consider a linear model in R:

  1. frame <- data.frame(y=rnorm(100), a=rnorm(100), b=rnorm(100))
  2. frame["c"] <- frame["y"] + 2*rnorm(100)
  3. model <- lm(y ~ a + b + c, data=frame) #lm

Calling summary(model) produces the following output in the console:

  1. Call:
  2. lm(formula = y ~ a + b + c, data = frame)
  3. Residuals:
  4. Min 1Q Median 3Q Max
  5. -2.13983 -0.54933 0.01975 0.44218 2.38721
  6. Coefficients:
  7. Estimate Std. Error t value Pr(>|t|)
  8. (Intercept) -0.060624 0.088597 -0.684 0.4955
  9. a 0.009773 0.088171 0.111 0.9120
  10. b 0.157979 0.080924 1.952 0.0538 .
  11. c 0.188913 0.039788 4.748 7.17e-06 ***
  12. ---
  13. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 1
  14. Residual standard error: 0.8464 on 96 degrees of freedom
  15. Multiple R-squared: 0.2146, Adjusted R-squared: 0.1901
  16. F-statistic: 8.745 on 3 and 96 DF, p-value: 3.478e-05

I would like to only print the coefficients part, the exact complement of this thread, like so:

  1. Coefficients:
  2. Estimate Std. Error t value Pr(>|t|)
  3. (Intercept) -0.060624 0.088597 -0.684 0.4955
  4. a 0.009773 0.088171 0.111 0.9120
  5. b 0.157979 0.080924 1.952 0.0538 .
  6. c 0.188913 0.039788 4.748 7.17e-06 ***
  7. ---
  8. Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 1

This is not possible by calling coef(summary(model)) as suggested here, since that produces

  1. Estimate Std. Error t value Pr(>|t|)
  2. (Intercept) -0.060624 0.088597 -0.684 0.4955
  3. a 0.009773 0.088171 0.111 0.9120
  4. b 0.157979 0.080924 1.952 0.0538
  5. c 0.188913 0.039788 4.748 7.17e-06

which lacks the significance marks on the right. Preferably, the solution doesn't involve re-programming the entire summary() body as suggested in the thread above.

答案1

得分: 3

你正在寻找的是 printCoefmat

  1. printCoefmat(coef(summary(model)))
  2. 估计值 标准误差 t Pr(>|t|)
  3. (截距) 0.019348 0.089500 0.2162 0.8293
  4. a 0.023219 0.101134 0.2296 0.8189
  5. b 0.085962 0.085934 1.0003 0.3197
  6. c 0.190828 0.042007 4.5427 1.615e-05 ***
  7. ---
  8. 显著性标志: 0 *** 0.001 ** 0.01 * 0.05 ‘.’ 0.1 1
英文:

You're looking for printCoefmat.

  1. printCoefmat(coef(summary(model)))
  2. Estimate Std. Error t value Pr(>|t|)
  3. (Intercept) 0.019348 0.089500 0.2162 0.8293
  4. a 0.023219 0.101134 0.2296 0.8189
  5. b 0.085962 0.085934 1.0003 0.3197
  6. c 0.190828 0.042007 4.5427 1.615e-05 ***
  7. ---
  8. Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 ‘.’ 0.1 1

huangapple
  • 本文由 发表于 2023年6月2日 04:59:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定