简体   繁体   中英

bootstrap weighted mean in R

I know how to bootstrap the mean of a vector:

library(boot)
samplemean <- function(x, d) {
  return(mean(x[d]))
}
results_qsec <- boot(data=mtcars$qsec, statistic = samplemean, R=1000)

but how do I bootstrap the weighted mean, considering for instance values are in mtcars$qsec and weights on these values are in mtcars$wt ?

The trick is to specify the weights for weighted.mean as part of the ... argument to boot . Here I use j for the weights, and pass it through as a data frame, to match the data = argument.

Here you go:

samplewmean <- function(d, i, j) {
    d <- d[i, ]
    w <- j[i, ]
    return(weighted.mean(d, w))   
  }

results_qsec <- boot(data= mtcars[, 7, drop = FALSE], 
                     statistic = samplewmean, 
                     R=10000, 
                     j = mtcars[, 6 , drop = FALSE])

returns:

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = mtcars[, 7, drop = FALSE], statistic = samplewmean, 
    R = 10000, j = mtcars[, 6, drop = FALSE])


Bootstrap Statistics :
    original       bias    std. error
t1* 17.75677 0.0006948823   0.3046888

Compare with:

weighted.mean(mtcars[,7], mtcars[,6])
[1] 17.75677

here's how:

samplewmean <- function(data, d) {
  return(weighted.mean(x=data[d,1], w=data[d,2]))
}

results_qsec <- boot(data=mtcars[,c(7,6)], statistic = samplewmean, R=1000)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM