I am currently learning to code in R and exploring different statistical packages. I came across the boot()
function and quickly became confused by the statistic
argument. As I understand it, boot()
performs R
resamples, each time computing the statistic
for the given sample. According to the documentation, statistic
must be a function with two arguments. The first argument is the data, and the second is an index vector. The latter argument does not make sense to me. What is an index vector in this context? I hoped that seeing bootstrapping in practice would clarify the argument, but, after stumbling upon the second post in this thread, my confusion grew:
diff2 = function(d1,i){
d = d1;
d$group <- d$group[i]; # randomly re-assign groups
Mean= tapply(X=d$time, INDEX=d$group, mean)
Diff = Mean[1]-Mean[2]
Diff
}
> set.seed(1234)
> b4 = boot(data = sleep, statistic = diff2, R = 5000)
> mean(abs(b4$t) > abs(b4$t0))
[1] 0.046
In particular, I cannot seem to understand the second line of the function. How is that line randomly re-assigning groups? To me, it looks like the command is taking the group
column of the entire dataset d
, and replacing it with a vector that is a subset of the same column. How is this randomly reassigning groups?
If I were to simplify my question, it would be the following: what do the arguments of statistic
represent? More generally, what does boot()
do under the hood? That is, how does boot()
apply the function statistic
to each resample?