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
?
The trick is to specify the weights for
weighted.mean
as part of the...
argument toboot
. Here I usej
for the weights, and pass it through as a data frame, to match thedata =
argument.Here you go:
returns:
Compare with:
here's how: