Map a function with few pipes to bootstrapped splits

Hi there,

I have a tibble called mep looks like the following

# A tibble: 5,623 x 4
# Groups:   id, time, interv [80]
   id    interv        mep time 
   <chr> <chr>       <dbl> <chr>
 1 Sub01 excitation 0.377  pre  
 2 Sub01 excitation 0.494  pre  
 3 Sub01 excitation 1.55   pre  
 4 Sub01 excitation 0.965  pre  
 5 Sub01 excitation 1.04   pre  
 6 Sub01 excitation 0.748  pre  
 7 Sub01 excitation 1.14   pre  
 8 Sub01 excitation 0.416  pre  
 9 Sub01 excitation 0.0532 pre  
10 Sub01 excitation 1.11   pre  
# ... with 5,613 more rows

I bootstrapped this tibble using

boots_mep <- bootstraps(mep, times = 2000, apparent = TRUE)

and got

# Bootstrap sampling with apparent sample 
# A tibble: 2,001 x 2
   splits              id           
   <list>              <chr>        
 1 <split [5623/2021]> Bootstrap0001
 2 <split [5623/2060]> Bootstrap0002
 3 <split [5623/2042]> Bootstrap0003
 4 <split [5623/2045]> Bootstrap0004
 5 <split [5623/2060]> Bootstrap0005
 6 <split [5623/2075]> Bootstrap0006
 7 <split [5623/2027]> Bootstrap0007
 8 <split [5623/2065]> Bootstrap0008
 9 <split [5623/2055]> Bootstrap0009
10 <split [5623/2107]> Bootstrap0010
# ... with 1,991 more rows

I would like to do a simple averaging on mep within each split, such averaging should be group by id, time and interv, I did as follows but it could not work with errors

boots_mep %>% 
    mutate(mep_avg = map2(splits, analysis(splits) 
                          ~{group_by(id, time, interv) %>% 
                              summarize(avg_mep = mean(mep, na.rm=TRUE))}),
           tidied = map(mep_avg, tidy)) %>% 
    unnest(cols = tidied) %>% 

Could you please help? I am not sure if I should have used map instead of map2, and the syntax seems also wrong. Thanks a lot.

Not really enough to test this:

suppressPackageStartupMessages({
  library(purrr)
  library(rsample)
})

mep <- dplyr::tibble(
  id =
    c("Sub01", "Sub01", "Sub01", "Sub01", "Sub01", "Sub01", "Sub01", "Sub01", "Sub01", "Sub01"),
  interv =
    c("excitation", "excitation", "excitation", "excitation", "excitation", "excitation", "excitation", "excitation", "excitation", "excitation"),
  mep =
    c(0.377, 0.494, 1.55, 0.965, 1.04, 0.748, 1.14, 0.416, 0.0532, 1.11),
  time =
    c("pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre", "pre")
)

boots_mep <- bootstraps(mep, times = 2000, apparent = TRUE)

get_mean <- function(x) mean(x[[1]][3][[1]], na.rm = TRUE)

lapply(boots_mep$splits, get_mean) %>% head()
#> [[1]]
#> [1] 0.78932
#> 
#> [[2]]
#> [1] 0.78932
#> 
#> [[3]]
#> [1] 0.78932
#> 
#> [[4]]
#> [1] 0.78932
#> 
#> [[5]]
#> [1] 0.78932
#> 
#> [[6]]
#> [1] 0.78932

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.