tidyverse / modelr

Helper functions for modelling
https://modelr.tidyverse.org
GNU General Public License v3.0
401 stars 66 forks source link

unnesting resample class looes drops strap object? #63

Closed MatthieuStigler closed 6 years ago

MatthieuStigler commented 6 years ago

I have a resample object, with an additional column containing nested data-frames. Trying to unnest() will do the job, yet drop the relevant strap column?

Interestingly, doing the opposite -standard data-frame with resample object nested, unnest over the resample- works.

Where is the issue coming from? Thanks!

library(tidyverse)
#> ── Attaching packages ────────────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 2.2.1     ✔ purrr   0.2.4
#> ✔ tibble  1.4.2     ✔ dplyr   0.7.4
#> ✔ tidyr   0.8.0     ✔ stringr 1.3.0
#> ✔ readr   1.1.1     ✔ forcats 0.3.0
#> ── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
library(modelr)

some_param <- data_frame(param=c(1,2))

boot_para <- bootstrap(mtcars, 2) %>%
  mutate(par=list(some_param))

boot_para
#> # A tibble: 2 x 3
#>   strap          .id   par             
#>   <list>         <chr> <list>          
#> 1 <S3: resample> 1     <tibble [2 × 1]>
#> 2 <S3: resample> 2     <tibble [2 × 1]>

boot_para %>%
  unnest(par)
#> # A tibble: 4 x 2
#>   .id   param
#>   <chr> <dbl>
#> 1 1        1.
#> 2 1        2.
#> 3 2        1.
#> 4 2        2.

## unnest the other way: works
para_boot <- some_param %>%
  mutate(boot=list(boot_para))

para_boot %>%
  unnest(boot)
#> # A tibble: 4 x 4
#>   param strap          .id   par             
#>   <dbl> <list>         <chr> <list>          
#> 1    1. <S3: resample> 1     <tibble [2 × 1]>
#> 2    1. <S3: resample> 2     <tibble [2 × 1]>
#> 3    2. <S3: resample> 1     <tibble [2 × 1]>
#> 4    2. <S3: resample> 2     <tibble [2 × 1]>
hadley commented 6 years ago

This is a general problem which is hard to fix. We are slowly making progress and will hopefully have better tooling rolling out across the tidyverse later in the year.