bootstrap weighted mean in R

2019-07-26 20:54发布

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?

2条回答
Emotional °昔
2楼-- · 2019-07-26 21:14

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
查看更多
Melony?
3楼-- · 2019-07-26 21:16

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)
查看更多
登录 后发表回答