# Bootstrapping in R using the boot {boot} and Boot {car}

I'm trying my hand at resampling techniques with a dataset I have, and I think either I'm missing a conceptual point with bootstrapping, or I'm doing something incorrectly in R. Basically, I'm trying to use it in a correlation/regression framework, and I'm able to get the original coefficients, the bootstrap bias, and the bootstrap coefficients but I can't find a way to have R easily display the bootstrap model $R^2$ (when I'm working with several predictors), the Pearson $r$, or the $p$-values for individual regression coefficients. (I'm using the Boot function in the car package).

A secondary question...the more general function boot in the boot package requires defining a function to use as an argument. The function must include an argument for the original data set, and a second argument which is a set of indices, frequencies, or weights for the bootstrap sample. I'm a little confused by this. What conceptually are these indices I am specifying, and how do I specify them syntactically within my function?

The exact issue with your first question isn't really clear to me (perhaps a small reproducible example would help?), but the second question I can explain:

The set of indices is what the bootstrap function passes to your function to say 'use these observations'.

e.g. let's take a super-simple example. Say I was just calculating a mean.

Here's my sample:

Index    value
1      13.98
2      14.29
3      16.91
4      11.23
5      16.64
6      15.96


So the first time through, the bootstrap routine samples the numbers 1 to 6 with replacement, as if it had done this:

> sample(6,replace=TRUE)
 1 6 3 2 3 6


So it tells me those numbers, so that I know to use this as my first bootstrap pseudo-sample:

Index    value
1      13.98
6      15.96
3      16.91
2      14.29
3      16.91
6      15.96


and my first bootstrap statistic would just be the mean of that pseudo-sample.

Then it passes me another set of indices as if it had done sample again:

> sample(6,replace=TRUE)
 5 3 2 1 6 5


So that I know to use this as my sample:

Index    value
5      16.64
3      16.91
2      14.29
1      13.98
6      15.96
5      16.64


and my second bootstrap statistic would just be the mean of that pseudo-sample, and so forth.

All you should need to do in practice is in your function use the index to select the appropriate rows:

mydataframe[index,]


or if there's only a single column, as here, you may want to use drop=FALSE

> mydataframe
x
1 13.98
2 14.29
3 16.91
4 11.23
5 16.64
6 15.96
> index <- sample(6,replace=TRUE)
> mydataframe[index,,drop=FALSE]
x
6   15.96
4   11.23
2   14.29
1   13.98
1.1 13.98
3   16.91

• Okay, this makes sense. So when I am selecting from my empirical sample, the size of the bootstrap sample is the same as the original sample? That is, n(sample) == n(pseudo-sample), as opposed to sampling smaller subsamples from my original sample? – Adam Daily Dec 10 '13 at 20:17
• Correct, to understand the behavior at a given sample size, you take the same sample size, sampling with replacement. Exactly what is resampled varies from situation to situation and even model to model. – Glen_b -Reinstate Monica Dec 10 '13 at 20:35
• If you can get hold of the book that goes with package boot, I urge you to do so. But at the least follow through the examples in the help for the functions if you can. – Glen_b -Reinstate Monica Dec 10 '13 at 20:41
• Specifically, the book is one by Davison and Hinkley. I like it a lot, but I have noticed that some people don't get as much out of it. That may reflect a difference in background. – Glen_b -Reinstate Monica Dec 10 '13 at 22:05
• Great, thank you for your help. I'll look into that book, and I think I'll just need to play around with boot() a little more to see what I can and can't coax out of it. – Adam Daily Dec 11 '13 at 0:45