问题
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