I need to bootstrap my "automated' lapply
t.test
function to calculate Bootstrap statistics (original, bias, and standard error). Here's the basic t.test
code I've gotten so far (no bootstrapping):
# create data
val<-runif(60, min = 0, max = 100)
distance<-floor(runif(60, min=1, max=3))
phase<-rep(c("a", "b", "c"), 20)
color<-rep(c("red", "blue","green","yellow","purple"), 12)
df<-data.frame(val, distance, phase, color)
# run function to obtain t.tests
lapply(split(df, list(df$color, df$phase)), function(d) {
tryCatch({ t.test(val ~ distance, var.equal=FALSE, data=d) },
error = function(e) NA)
})
Which works great. However, I'm unsure how I could incorporate a bootstrap method into this apply function.
Maybe something like the following does what you want. Note that the return value is a list of lists of objects of class "htest"
(which are lists) or NA
.
boot_fun <- function(DF){
n <- nrow(DF)
i <- sample(n, n, TRUE)
df <- DF[i, ]
lapply(split(df, list(df$color, df$phase)), function(d) {
tryCatch({ t.test(val ~ distance, var.equal=FALSE, data=d) },
error = function(e) NA)
})
}
set.seed(1234)
R <- 10
result <- lapply(seq_len(R), function(i) boot_fun(df))