诀窍是将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])
返回:
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