英文:
Printing only the coefficient table of the summary of an lm with significance marks
问题
考虑一个R中的线性模型:
frame <- data.frame(y=rnorm(100), a=rnorm(100), b=rnorm(100))
frame["c"] <- frame["y"] + 2*rnorm(100)
model <- lm(y ~ a + b + c, data=frame) #lm
调用 summary(model)
会在控制台产生以下输出:
Call:
lm(formula = y ~ a + b + c, data = frame)
Residuals:
Min 1Q Median 3Q Max
-2.13983 -0.54933 0.01975 0.44218 2.38721
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.060624 0.088597 -0.684 0.4955
a 0.009773 0.088171 0.111 0.9120
b 0.157979 0.080924 1.952 0.0538 .
c 0.188913 0.039788 4.748 7.17e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.8464 on 96 degrees of freedom
Multiple R-squared: 0.2146, Adjusted R-squared: 0.1901
F-statistic: 8.745 on 3 and 96 DF, p-value: 3.478e-05
我想要只打印系数部分,与这个帖子的要求相反,如下所示:
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.060624 0.088597 -0.684 0.4955
a 0.009773 0.088171 0.111 0.9120
b 0.157979 0.080924 1.952 0.0538 .
c 0.188913 0.039788 4.748 7.17e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
不建议使用 coef(summary(model))
,因为它会产生以下结果,缺少右侧的显著性标记,如这里所建议:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.060624 0.088597 -0.684 0.4955
a 0.009773 0.088171 0.111 0.9120
b 0.157979 0.080924 1.952 0.0538
c 0.188913 0.039788 4.748 7.17e-06
最好的解决方案不涉及重新编写整个 summary()
主体,如上面的帖子中所建议。
英文:
Consider a linear model in R:
frame <- data.frame(y=rnorm(100), a=rnorm(100), b=rnorm(100))
frame["c"] <- frame["y"] + 2*rnorm(100)
model <- lm(y ~ a + b + c, data=frame) #lm
Calling summary(model)
produces the following output in the console:
Call:
lm(formula = y ~ a + b + c, data = frame)
Residuals:
Min 1Q Median 3Q Max
-2.13983 -0.54933 0.01975 0.44218 2.38721
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.060624 0.088597 -0.684 0.4955
a 0.009773 0.088171 0.111 0.9120
b 0.157979 0.080924 1.952 0.0538 .
c 0.188913 0.039788 4.748 7.17e-06 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.8464 on 96 degrees of freedom
Multiple R-squared: 0.2146, Adjusted R-squared: 0.1901
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:
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.060624 0.088597 -0.684 0.4955
a 0.009773 0.088171 0.111 0.9120
b 0.157979 0.080924 1.952 0.0538 .
c 0.188913 0.039788 4.748 7.17e-06 ***
---
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
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.060624 0.088597 -0.684 0.4955
a 0.009773 0.088171 0.111 0.9120
b 0.157979 0.080924 1.952 0.0538
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
。
printCoefmat(coef(summary(model)))
估计值 标准误差 t 值 Pr(>|t|)
(截距) 0.019348 0.089500 0.2162 0.8293
a 0.023219 0.101134 0.2296 0.8189
b 0.085962 0.085934 1.0003 0.3197
c 0.190828 0.042007 4.5427 1.615e-05 ***
---
显著性标志: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
英文:
You're looking for printCoefmat
.
printCoefmat(coef(summary(model)))
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.019348 0.089500 0.2162 0.8293
a 0.023219 0.101134 0.2296 0.8189
b 0.085962 0.085934 1.0003 0.3197
c 0.190828 0.042007 4.5427 1.615e-05 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论