Search code examples
rstatistics-bootstrap

Bootstraoping using the boot package


Trying around with the boot package in R and it does not accomplish what I want, it just returns the same value. I have read the documentation and checked the first example which seems to be the same, but I don't get any bootstrapped results, just the original value is returned. My example:

dat      <- data.frame(A = rnorm(100), B = runif(100))

booty    <- function(x, ind) sum(x$A)/sum(x$B)

boot_out <- boot(dat, booty, R  = 50, stype = "w")

Solution

  • Like I've said in the comment, you must use the index, in this case ind, to subset the dataset passed to the bootstrap statistic function, sum(x$A[ind])/sum(x$B[ind]).
    The full function would then become

    booty    <- function(x, ind) sum(x$A[ind])/sum(x$B[ind])
    

    Two notes.
    One, the stype = "i" argument states that the second argument to booty is a vector of indices, which, I believe, is what you want, not a vector of weights, stype = "w".
    Also, in order to have reproducible results, the RNG seed should be set before calling boot or RNG functions. Something like the following.

    set.seed(4237)
    
    dat      <- data.frame(A = rnorm(100), B = runif(100))
    
    boot_out <- boot(dat, booty, R  = 50, stype = "i")