2
$\begingroup$

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?

$\endgroup$

1 Answer 1

5
$\begingroup$

i is what allows the bootstrap to perform resamples. Resampling is sampling from the data with replacement.

In every iteration of the bootstrap, it takes the data and filters it by the row index i. i is basically generated from 1:nrow(d), sampled with replacement, of sample size nrow(d).

Here's an example of calculating the standard error on the mean via a bootstrap

library(boot)

set.seed(123)
df <- data.frame(
  id = 1:20
  , value = rpois(n = 20, lambda = 5)
)

boot::boot(
  data = df
  , statistic = function(d, i) {
    print(i)
    df_tmp <- d[i,]
    mean(df_tmp$value)
  }
  , R = 2
)

I have set R = 2, which means two bootstrap estimates. print(i) is called three times.

Function output:

 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
 [1]  7  2  8 13  1 15 15 20 11  7 17 17  4  5 20  3 16 14 14  3
 [1]  6  5 12 18  6  9 16  6  8 16 18  2 13 19 14  8 12  3  7 15

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot::boot(data = df, statistic = function(d, i) {
    print(i)
    df_tmp <- d[i, ]
    mean(df_tmp$value)
}, R = 2)


Bootstrap Statistics :
    original  bias    std. error
t1*      5.5   -0.15   0.5656854

The first time the print is called, is calculating the "original" statistic. This is the data from the full, un-resampled estimate, which can be calculated via d[1:nrow(df),].

The second print is from the first bootstrap estimate. As you can see, i is a sample of the row indices with replacement (e.g. index 7 occurs twice times). And so on.

$\endgroup$
1
  • 1
    $\begingroup$ +1 though I've used boot to do what I've thought was fairly sophisticated work, and I have yet to see why that i argument should exist, why the package is written to require that instead of just passing resampled data to a given function. I have posted here asking for clarification about why boot::boot should operate this way. $\endgroup$
    – Dave
    Commented Jul 25 at 10:48

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