R:重新排列列表,purrr

R:重新排列列表,purrr,r,apply,purrr,R,Apply,Purrr,这是关于重新排列列表的:我正在做一个类似于以下代码的交叉验证,其中cv_chunk的数量是任意的: library(purrr) # randomly assign to a cross validation chunk set.seed(11) mtcars$cv_chunk <- sample(rep(1:3), nrow(mtcars), 1) model_confint <- mtcars %>% split(.$cv_chunk) %>% map(

这是关于重新排列列表的:我正在做一个类似于以下代码的交叉验证,其中
cv_chunk
的数量是任意的:

library(purrr)
# randomly assign to a cross validation chunk
set.seed(11)
mtcars$cv_chunk <- sample(rep(1:3), nrow(mtcars), 1)

model_confint <- mtcars %>% 
  split(.$cv_chunk) %>% 
  map(~lm(mpg ~ cyl*qsec + gear - cv_chunk, data = .)) %>% 
  map(confint, levels = 0.95) %>%
  map(t)

  names(model_confint) <- paste0("CV_", names(model_confint))

# first element of the list
$CV_1
       (Intercept)        cyl      qsec      gear   cyl:qsec
2.5 %     -54.8983 -25.691233 -8.958490 -5.175215 -0.7161008
97.5 %    215.2629   9.901694  4.322784  5.185608  1.2804372
我确信使用
apply
函数或
purr
包是一种很好的方法。但是我被卡住了


谢谢你的帮助。

谢谢你的好例子!我的两个直觉让这个问题变得更容易:可以使用
purrr::set_names
在循环中更早地命名列表,也可以通过更早地映射到数据帧来避免转置


库(purrr)
#随机分配给交叉验证块
种子(11)
mtcars$cv_%
分割(.$cv_块)%>%
#在此处命名列表,以便稍后将其用作参数
设置名称(粘贴0(“CV”),名称(.))%>%
#映射实际列表和列表名称以创建一个数据帧
地图(~lm(mpg~cyl*qsec+gear-cv_区块,数据=))%>%
map2_dfr(.x=,.y=名称(.),函数(x,y){
df$`(截获)`
#>2.5%97.5%cv\U块
#>1-54.8983215.26286 CV_1
#>6-193.2069 84.48072 CV_2
#>11-361.1489 545.04010 CV_3
#> 
#>$cyl
#>2.5%97.5%cv\U块
#>2-25.69123 9.901694 CV_1
#>7-11.44012 24.073391 CV_2
#>12-26.36288 16.589356 CV_3
#> 
#>$`cyl:qsec`
#>2.5%97.5%cv\U块
#>5-0.7161008 1.2804372 CV_1
#>10-1.4870954 0.5295074 CV_2
#>15纳纳库夫3
#> 
#>$gear
#>2.5%97.5%cv\U块
#>4-5.175215 5.185608 CV_1
#>9-3.975621 8.092960 CV_2
#>14-30.785104 33.533967 CV_3
#> 
#>$qsec
#>2.5%97.5%cv\U块
#>3-8.958490 4.322784 CV_1
#>8-2.257827 11.261528 CV_2
#>13-17.741244 12.929339 CV_3

这可以使用
扫帚来完成:

library(purrr)
library(dplyr)
library(tidyr)
# randomly assign to a cross validation chunk
set.seed(11)
mtcars$cv_chunk <- sample(seq(3), nrow(mtcars), replace = TRUE)

mtcars %>% 
  split(.$cv_chunk) %>% 
  map(~lm(mpg ~ cyl*qsec + gear - cv_chunk, data = .)) %>% 
  # the following will work uder different seed. I will report a bug to `broom``
  #map_dfr(~broom::tidy(.x, conf.int=TRUE), .id="cv_chunk")
  map_dfr(~bind_cols(broom::tidy(.x), drop_na(broom::confint_tidy(.x))), .id="cv_chunk") %>% 
  select(cv_chunk, term, conf.low, conf.high) %>% 
  split(.$term) 
library(purrr)
library(dplyr)
library(tidyr)
# randomly assign to a cross validation chunk
set.seed(11)
mtcars$cv_chunk <- sample(seq(3), nrow(mtcars), replace = TRUE)

mtcars %>% 
  split(.$cv_chunk) %>% 
  map(~lm(mpg ~ cyl*qsec + gear - cv_chunk, data = .)) %>% 
  # the following will work uder different seed. I will report a bug to `broom``
  #map_dfr(~broom::tidy(.x, conf.int=TRUE), .id="cv_chunk")
  map_dfr(~bind_cols(broom::tidy(.x), drop_na(broom::confint_tidy(.x))), .id="cv_chunk") %>% 
  select(cv_chunk, term, conf.low, conf.high) %>% 
  split(.$term) 
$`(Intercept)`
   cv_chunk        term  conf.low conf.high
1         1 (Intercept)  -54.8983 215.26286
6         2 (Intercept) -193.2069  84.48072
11        3 (Intercept) -361.1489 545.04010

$cyl
   cv_chunk term  conf.low conf.high
2         1  cyl -25.69123  9.901694
7         2  cyl -11.44012 24.073391
12        3  cyl -26.36288 16.589356

$`cyl:qsec`
   cv_chunk     term   conf.low conf.high
5         1 cyl:qsec -0.7161008 1.2804372
10        2 cyl:qsec -1.4870954 0.5295074

$gear
   cv_chunk term   conf.low conf.high
4         1 gear  -5.175215  5.185608
9         2 gear  -3.975621  8.092960
14        3 gear -30.785104 33.533967

$qsec
   cv_chunk term   conf.low conf.high
3         1 qsec  -8.958490  4.322784
8         2 qsec  -2.257827 11.261528
13        3 qsec -17.741244 12.929339