bootstrap weighted mean in R

我与影子孤独终老i 提交于 2019-12-08 04:08:59

问题


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?


回答1:


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



回答2:


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)


来源:https://stackoverflow.com/questions/46231261/bootstrap-weighted-mean-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!