我正在使用以下代码来引导中位数的置信区间,这很好。
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"))
但是,我想为此计算引导加权中位数,并且只发现如何计算加权中位数或引导它。
答案 0 :(得分:1)
您只需将data
乘以weight
并在产品上进行自举。
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"))
让我们分解引导程序。基本上,R=10000
生产了10000个样品并替换了产品。
然后计算每个引导程序的中位数。您现在有加权中位数的抽样分布。
df <- data * weight
weighted_medians <- replicate(10000, median(sample(df, length(df), replace = T)))
自举加权中位数为:
median(weighted_medians)
使用分位数,95%置信区间
quantile(boot_medians, c(.025, .975))