I'm trying to do some bootstrapping in a more tidy way (I know ho to do it in base R and get the results, but am wondering how to put all of it into a more tidy pipeline).
First I define two functions. One for the statistic to bootstrap and one for the boostrap itself:
library(boot)
library(tidyverse)
share <- function(data, i)
{
share_boot <- data[i, ] %>%
summarize(across(everything(), mean)) %>%
pivot_longer(everything()) %>%
summarize(value/sum(value)) %>%
pull()
return(share_boot)
}
boot_results <- function(data, statistic, R)
{
boot_results_function <- boot(data = data,
statistic = statistic,
R = R)
return(boot_results_function)
}
I then want to bootstrap my statistic on some nested data where I basically want to do the bootstrap for each row:
# Creating toy data
set.seed(1)
df <- tibble(country = rep(1:3, each = 20),
group = rep(rep(1:2, each = 10), 3),
value1 = runif(60),
value2 = runif(60),
value3 = runif(60))
# Doing the boostrap and retreiving results
df2 <- df %>%
group_by(country, group) %>%
nest(data = -c(country, group)) %>%
rowwise() %>%
mutate(results = list(boot_results(data, share, 5))) %>%
ungroup() %>%
hoist(., results, "t0", "t") %>%
select(-results)
This gives me nested list columns for t
, t0
and data
.
# A tibble: 6 x 5
country group data t0 t
<int> <int> <list> <list> <list>
1 1 1 <tibble [10 x 3]> <dbl [3]> <dbl [5 x 3]>
2 1 2 <tibble [10 x 3]> <dbl [3]> <dbl [5 x 3]>
3 2 1 <tibble [10 x 3]> <dbl [3]> <dbl [5 x 3]>
4 2 2 <tibble [10 x 3]> <dbl [3]> <dbl [5 x 3]>
5 3 1 <tibble [10 x 3]> <dbl [3]> <dbl [5 x 3]>
6 3 2 <tibble [10 x 3]> <dbl [3]> <dbl [5 x 3]>
I now want to do several things:
- kind of unnest_longer these three columns, so that I end up with three rows per country/group.
- retrieve the column names from the
data
column.
I'm probably heavily overcomplicating things, but I'm stuck at this last step, where I don't know how to pivot/unnest the list columns.
Expected result:
# A tibble: 18 x 5
country group data_names t0 t
<int> <int> <chr> <dbl> <list>
1 1 1 value1 0.355 <dbl [5 x 1]>
2 1 1 value2 0.329 <dbl [5 x 1]>
3 1 1 value3 0.315 <dbl [5 x 1]>
4 1 2 value1 0.324 <dbl [5 x 1]>
5 1 2 value2 0.361 <dbl [5 x 1]>
6 1 2 value3 0.315 <dbl [5 x 1]>
7 2 1 value1 0.320 <dbl [5 x 1]>
8 2 1 value2 0.310 <dbl [5 x 1]>
9 2 1 value3 0.371 <dbl [5 x 1]>
10 2 2 value1 0.360 <dbl [5 x 1]>
11 2 2 value2 0.386 <dbl [5 x 1]>
12 2 2 value3 0.254 <dbl [5 x 1]>
13 3 1 value1 0.368 <dbl [5 x 1]>
14 3 1 value2 0.319 <dbl [5 x 1]>
15 3 1 value3 0.314 <dbl [5 x 1]>
16 3 2 value1 0.263 <dbl [5 x 1]>
17 3 2 value2 0.293 <dbl [5 x 1]>
18 3 2 value3 0.443 <dbl [5 x 1]>
CodePudding user response:
Extract the column names using map
, split the matrix column wise to create 1-column matrix and unnest
them together.
library(tidyverse)
df2 %>%
mutate(data = map(data, names),
t = map(t, ~map(asplit(.x, 2), matrix, ncol = 1))) %>%
unnest(c(data, t0, t))
# country group data t0 t
# <int> <int> <chr> <dbl> <list>
# 1 1 1 value1 0.355 <dbl [5 × 1]>
# 2 1 1 value2 0.329 <dbl [5 × 1]>
# 3 1 1 value3 0.315 <dbl [5 × 1]>
# 4 1 2 value1 0.324 <dbl [5 × 1]>
# 5 1 2 value2 0.361 <dbl [5 × 1]>
# 6 1 2 value3 0.315 <dbl [5 × 1]>
# 7 2 1 value1 0.320 <dbl [5 × 1]>
# 8 2 1 value2 0.310 <dbl [5 × 1]>
# 9 2 1 value3 0.371 <dbl [5 × 1]>
#10 2 2 value1 0.360 <dbl [5 × 1]>
#11 2 2 value2 0.386 <dbl [5 × 1]>
#12 2 2 value3 0.254 <dbl [5 × 1]>
#13 3 1 value1 0.368 <dbl [5 × 1]>
#14 3 1 value2 0.319 <dbl [5 × 1]>
#15 3 1 value3 0.314 <dbl [5 × 1]>
#16 3 2 value1 0.263 <dbl [5 × 1]>
#17 3 2 value2 0.293 <dbl [5 × 1]>
#18 3 2 value3 0.443 <dbl [5 × 1]>