如何使用 lapply 函数将回归输出列表转换为具有 broom::tidy 的数据框?

How to convert list of regression outputs into data frames with broom::tidy by using lapply function?

我有一个包含多个数据框的列表。每个数据框包含三列(ColumnOne、ColumnTwo 和 ColumnThree)。

list <- list(df1, df2, df3)

我正在使用 lapply 到 运行 对每个数据框进行回归。

regression <- lapply(list, function (x) 
  lm(x$ColumnOne ~ x$ColumnTwo + x$ColumnThree))

当我显示回归的输出时,一切似乎都是正确的。

现在,我想使用 broom::tidy 收集 table 中每个数据框的回归输出。

library(broom)
df <- lapply(regression, function(x)
  tidy(regression$x))
df

但是,当我显示 df 时,它只显示空 (0x0) 数据帧。

非常感谢任何帮助!

这与purrr非常紧凑。

首先,模拟一些数据:

library(tidyverse)
library(broom)

df_list = map(1:3, ~ data.frame(matrix(sample.int(10, 30, replace = TRUE), ncol = 3)))

然后简单地拟合你的模型并扫出结果:

> df_list %>% map( ~ tidy(lm(.)))
[[1]]
# A tibble: 3 x 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept)   7.40       3.08     2.40    0.0474
2 X2            0.0309     0.341    0.0905  0.930
3 X3           -0.0387     0.358   -0.108   0.917

[[2]]
# A tibble: 3 x 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept)   4.63       1.51      3.07   0.0181
2 X2            0.252      0.272     0.923  0.387
3 X3            0.0340     0.261     0.130  0.900

[[3]]
# A tibble: 3 x 5
  term        estimate std.error statistic p.value
  <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 (Intercept)   6.62       5.68      1.17    0.282
2 X2            0.0946     0.630     0.150   0.885
3 X3           -0.405      0.419    -0.967   0.366

对于这样的应用程序,我建议以稍微不同的方式使用 broom 包。方法如下:

require(broom)

# simulate data
make_df <- function(){data.frame(ColumnOne = rnorm(5), 
                                 ColumnTwo=rnorm(5),
                                 ColumnThree=rnorm(5)
                                 )
                     }

my_list <- list(df1 = make_df(), 
                df2 = make_df(),
                df3=make_df()
                )

# bind the rows of the dataframe together and group by origin
my_list %>% 
     bind_rows(.id='df') %>% 
     group_by(df) %>% 
     do(tidy(lm(data=., 
                formula=ColumnOne ~ ColumnTwo + ColumnThree
                )
             )
        )

我制作的随机玩具数据的结果是一个如下所示的数据框:

 A tibble: 9 x 6
# Groups:   df [3]
  df    term        estimate std.error statistic p.value
  <chr> <chr>          <dbl>     <dbl>     <dbl>   <dbl>
1 df1   (Intercept)  -1.23      0.840     -1.47  0.280  
2 df1   ColumnTwo     0.944     0.573      1.65  0.241  
3 df1   ColumnThree  -0.532     0.486     -1.09  0.388  
4 df2   (Intercept)   0.942     0.718      1.31  0.320  
5 df2   ColumnTwo     0.900     1.02       0.885 0.470  
6 df2   ColumnThree  -0.0596    0.443     -0.135 0.905  
7 df3   (Intercept)   0.0453    0.0742     0.610 0.604  
8 df3   ColumnTwo     0.554     0.0509    10.9   0.00833
9 df3   ColumnThree  -0.229     0.114     -2.00  0.183  

Broom 的设计策略是尽可能多地使用数据框。如果您从具有相同列的数据框列表开始,将它们组合成一个数据框会更容易,然后扫帚让您可以直接处理它,而不必对列表进行函数式编程。