简体   繁体   中英

Weighted Median bootstrap

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM