0
$\begingroup$

I am having a bit of trouble understanding Bootstrapping and what/how I can manipulate the bootstrap created dataset.

This is all in R

My original dataset is structured like this:

Time Animal_ID Treatment Volume
0     1          Control   1
2     1          Control   2
4     1          Control   6
0     2          T1        6
2     2          T1        6
4     2          T1        8
0     3          T2        21
2     3          T2        7
4     3          T2        8
0     4          T3        4
2     4          T3        3
4     4          T3        2

I am going to compute a Z-score,mean, and median statistic on the bootstrap generated data sets which is something I can do in the function. However, What I really need as a proof of concepts is a way to extract my bootstrap generated data sets into individual data frames or write them as excel files.

To clarify, I want to showcase a simple example of the statistic I am performing so if I create 5 bootstrap data sets I would need a way to explore them like they were unique individual data sets. I would need to show that this works in concept when looking at R=5 and then I would be able to confidently expand this to R = 100 no extraction required.

Does anyone know how to accomplish this? Or if there are other alternative?

$\endgroup$

1 Answer 1

0
$\begingroup$

One way is to use the infer package.

library(tidyverse)
library(infer)

df <- read_table(pipe("pbpaste"))

five_bootstraps <- df %>%
  generate(reps = 5, type = "bootstrap")

five_bootstraps

# # A tibble: 60 x 5
# # Groups:   replicate [5]
#     replicate  Time Animal_ID Treatment Volume
#         <int> <int>     <int> <chr>      <int>
#   1         1     4         3 T2             8
#   2         1     2         1 Control        2
#   3         1     0         3 T2            21
#   4         1     0         3 T2            21
#   5         1     2         2 T1             6
#   6         1     0         3 T2            21
#   7         1     0         4 T3             4
#   8         1     2         3 T2             7
#   9         1     0         2 T1             6
#  10         1     2         4 T3             3
# # ... with 50 more rows

So each "bootstrap" replicate is labeled in the replicate column. If you'd like to write each replicate to a separate .csv file, you could do:

five_bootstraps %>%
  split(.$replicate) %>%
  walk2(.x = names(.), .y = ., ~ write_csv(.y, paste0(.x, ".csv")))
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.