I have a question regarding bootstrapping of a weighted mean.
Depending on how my data is structured, I sometimes want to bootstrap across columns and sometimes across rows.
In another post (bootstrap weighted mean in R), the following code was provided to bootstrap the weighted mean across columns:
library(boot)
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])
This works perfectly (check: weighted.mean(mtcars[,7], mtcars[,6]).
However, I now also want to bootstrap across rows, which I thought the following code would do:
samplewmean2 <- function(d, i, j) {
d <- d[, i]
w <- j[, i]
return(weighted.mean(d, w))
}
results_qsec2 <- boot(data= mtcars[7, , drop = FALSE],
statistic = samplewmean2,
R=10000,
j = mtcars[6, , drop = FALSE])
Unfortunately this is not working, and I don't know what I should change?
Many thanks in advance.