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

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

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

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:

确定