【问题标题】:How to order the coefficients in LM summary?如何对LM摘要中的系数进行排序?
【发布时间】:2019-10-05 18:34:18
【问题描述】:

假设我有这样的模型:

x1 <- rnorm(100)
x2 <- rnorm(100)
y <- x1 + 5 * x2 + rnorm(100)
fit <- lm(y ~ x1 + x2)

我怎样才能输出summary(fit),但要按照估计系数的大小排序?

【问题讨论】:

  • 你可以用coef(fit)试试fit$coefficients &lt;- coef(fit)[order(coef(fit))]
  • 你可以这样做来得到你想要的:coef(summary(fit))[order(coef(summary(fit))[,1], decreasing = T),]。它使错误等按应有的顺序排列,并显示实际的 p 值而不是(显示为的)截断值。但是,将它们输入回summary.lm 对象看起来更棘手。根据?summary.lm,似乎每次调用summary 时都会重新计算标准误差、t 值和p 值,这似乎就是所有这些值都发生变化的原因。执行您所要求的任务似乎比最初看起来要复杂得多。

标签: r


【解决方案1】:

如果您不介意加载外部包的开销,broom 让这变得微不足道:

x1 <- rnorm(100)
x2 <- rnorm(100)
y <- x1 + 5 * x2 + rnorm(100)
fit <- lm(y ~ x1 + x2)

library(broom)
coefs <- tidy(fit)
coefs[order(coefs$estimate, decreasing = TRUE),]
#> # A tibble: 3 x 5
#>   term        estimate std.error statistic  p.value
#>   <chr>          <dbl>     <dbl>     <dbl>    <dbl>
#> 1 x2            4.95      0.0883    56.1   1.04e-75
#> 2 x1            1.17      0.109     10.7   3.27e-18
#> 3 (Intercept)   0.0131    0.103      0.128 8.99e- 1

reprex package (v0.2.1) 于 2019 年 5 月 18 日创建

编辑 - 添加统计意义注释

你可以在事后添加这个:

x1 <- rnorm(100)
x2 <- rnorm(100)
y <- x1 + 5 * x2 + rnorm(100)
fit <- lm(y ~ x1 + x2)

library(broom)
coefs <- tidy(fit)
coefs$p.value <- with(coefs, 
                      ifelse(abs(p.value) > .1, paste0(formatC(p.value, format = "e", digits = 2),""),
                             ifelse(abs(p.value) > .05, paste0(formatC(p.value, format = "e", digits = 2),"."),
                                    ifelse(abs(p.value) > .01, paste0(formatC(p.value, format = "e", digits = 2),"*"),
                                           ifelse(abs(p.value) > .001, paste0(formatC(p.value, format = "e", digits = 2),"**"),
                                           paste0(formatC(p.value, format = "e", digits = 2),"***"))))))
coefs[order(coefs$estimate, decreasing = TRUE),]
#> # A tibble: 3 x 5
#>   term        estimate std.error statistic p.value    
#>   <chr>          <dbl>     <dbl>     <dbl> <chr>      
#> 1 x2            4.91      0.0923    53.2   1.51e-73***
#> 2 x1            0.768     0.0890     8.64  1.17e-13***
#> 3 (Intercept)  -0.0327    0.0990    -0.330 7.42e-01

reprex package (v0.2.1) 于 2019-05-18 创建

【讨论】:

  • 这非常接近,但我希望保留摘要中的星等,以便快速了解重要内容。
  • @badmax 查看 p 值列。例如,x1 和 x2 系数在统计上非常显着。如有疑问,请使用以下计算器将 e 表示法转换为更易于理解的格式free-online-calculator-use.com/…
  • @badmax - 见编辑。这种方法的缺点是您将 p.value 列转换为字符,因此不能再像操作数字一样操作此列。
  • 我最终添加了一个带有注释的新列。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-31
  • 2015-08-02
  • 2016-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多