bootstrap R 中的加权平均值

bootstrap weighted mean in R

我知道如何bootstrap向量的平均值:

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

但是我如何 bootstrap 加权平均值,例如考虑值在 mtcars$qsec 中并且这些值的权重在 mtcars$wt 中?

诀窍是将 weighted.mean 的权重指定为 boot... 参数的一部分。这里我使用 j 作为权重,并将其作为数据框传递,以匹配 data = 参数。

给你:

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

比较:

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

方法如下:

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)