Currently I am obtaining 95% CI on the median by using this
x<-rnorm(100)
bootmed = apply(matrix(sample(x, rep=TRUE, 10^4*length(x)), nrow=10^4), 1, median)
quantile(bootmed, c(.025, 0.975))[1]->a1
quantile(bootmed, c(.025, 0.975))[2]->a2
the problem now is that i need to do this for a weighted Median. I use the weightedMedian
function in the matrixStats
package: matrixStats::weightedMedian
so I do not only have x with the numbers but also y with the weights (runif(100))
- so now I calculate
weightedMedian(x,runif(100))
but how does the equivalent for the above bootstrap go?
The ...
argument of the apply()
function allows you to pass optional arguments to your function. Alternatively, you can also define a custom function. Assuming that your median weights are identical for all rows, the answer to your question looks like this:
x <- rnorm(100)
y <- rnorm(100)
M <- matrix(sample(x, rep=TRUE, 10^4*length(x)), nrow=10^4)
bootmed = apply(M, 1, weightedMedian, w=y)
# Or, alternatively..
bootmed = apply(M, 1, function(x) weightedMedian(x,y))