-2

I'm using the below code to bootstrap a confidence interval for a median, which works just fine.

library(boot)
data <- c(6, 11, 7, 8, 3, 9, 4, 1, 1, 8, 2, 2, 5, 3, 1)
weight <- c(0.839432605459112, 0.774215027235327, 0.709256693551626, 0.809376516981207, 0.809698716683444, 0.880849581474519, 0.829263837448813, 1.80390621483409, 1.12749447791778, 0.93389158146594, 1.07832286911631, 0.79541512406283, 1.06708509325217, 0.946752658104578, 0.968003233015867)
Mboot = boot(data, function(x,i) median(x[i]), R=10000)
boot.ci(Mboot, conf = 0.95,  type = c("norm", "basic" ,"perc"))

However, I would like to calculate the bootstrapped weighed median for this and only found how I can either calculate the weighed median or bootstrap it.

Ivo
  • 3,890
  • 5
  • 22
  • 53
  • Can you put `dput(data)` in your question? Also, please indicate the important packages that you are using. – hpesoj626 Mar 19 '18 at 12:39
  • See updated question – Ivo Mar 19 '18 at 12:50
  • 2
    It will be difficult to reproduce your problem without a minimal reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. – hpesoj626 Mar 19 '18 at 12:52
  • The main problem, as pointed out in the question, is not the error message but the question how to bootstrap a weighed median with confidence interval, so the latter part can be ignored. – Ivo Mar 19 '18 at 14:28
  • Well, we can't help you in bootstrapping a weighted median if we don't know the weights, in the first place. Your data is just a vector, and we don't know the weights of each element of the vector. – hpesoj626 Mar 19 '18 at 14:33
  • see edited question. – Ivo Mar 19 '18 at 14:42

1 Answers1

2

You just have to multiply data by weight and do bootstrap on the product.

library(boot)
Mboot_w = boot(data * weight, function(x,i) median(x[i]), R=10000)
Mboot_w
boot.ci(Mboot_w, conf = 0.95,  type = c("norm", "basic" ,"perc"))

Let's break down the bootstrapping procedure. Essentially, the R=10000 produced 10000 samples with replacement of the product. Then the median of each bootstrap is computed. You now have a sampling distribution of weighted medians.

df <- data * weight
weighted_medians <- replicate(10000, median(sample(df, length(df), replace = T)))

The bootstrap weighted median is:

median(weighted_medians)

Using quantiles, a 95% confidence interval is

quantile(boot_medians, c(.025, .975))
hpesoj626
  • 3,529
  • 1
  • 17
  • 25