繁体   English   中英

置信区间的引导程序

[英]Bootstrap for Confidence Intervals

我的问题如下:首先,我必须创建 1000 个大小为 100 的“theta hat”引导样本。 我有一个随机变量 X,它遵循缩放的 t_5 分布。 以下代码创建了 1000 个 theta hat 的 bootstrap 样本:

library("metRology", lib.loc="~/R/win-library/3.4")
 # Draw some data
data <- rt.scaled(100, df=5, mean=0, sd=2)

thetahatsq <- function(x){(3/500)*sum(x^2)}
sqrt(thetahatsq(data))

n <- 100

thetahat <- function(x){sqrt(thetahatsq(x))}
thetahat(data)

# Draw 1000 samples of size 100 from the fitted distribution, and compute the thetahat
tstar<-replicate(1000,thetahat(rt.scaled(n, df=5, mean=0, sd=thetahat(data)))) 
mean(tstar)

hist(tstar, breaks=20, col="lightgreen")

现在我想比较覆盖概率的准确性和使用百分位数方法构建的 95% 自举置信区间的宽度。 我想把上面的代码重复1000次,在每种情况下,检查参数的真值是否属于相应的bootstrap置信区间,并计算每个区间的长度。 然后平均结果值。

也许引导程序的最佳方法是使用基本包boot 函数bootboot.ci用于满足您的需求,函数boot.ci为您提供有关要计算的置信区间类型的选项,包括type = "perc"

看看以下是否回答了您的问题。

set.seed(402)    # make the results reproducible
data <- rt.scaled(100, df=5, mean=0, sd=2)

stat <- function(data, index) thetahat(data[index])

hans <- function(data, statistic, R){
    b <- boot::boot(data, statistic, R = R)
    ci <- boot::boot.ci(b, type = "perc")
    lower <- ci$percent[4]
    upper <- ci$percent[5]
    belongs <- lower <= true_val && true_val <= upper
    data.frame(lower, upper, belongs)
}

true_val <- sqrt(thetahatsq(data))

df <- do.call(rbind, lapply(seq_len(1000), function(i) hans(data, statistic = stat, R = n)))
head(df)
#     lower    upper belongs
#1 1.614047 2.257732    TRUE
#2 1.592893 2.144660    TRUE
#3 1.669754 2.187214    TRUE
#4 1.625061 2.210883    TRUE
#5 1.628343 2.220374    TRUE
#6 1.633949 2.341693    TRUE

colMeans(df)
#   lower    upper  belongs 
#1.615311 2.227224 1.000000

说明:

  • 函数stat是您感兴趣的统计信息的包装器,由boot
  • 函数hans自动调用boot::bootboot::boot.ci
  • hans的调用是由lapply ,这是一个伪装的循环。
  • 结果作为 data.frames 列表返回,因此我们需要调用do.call以便将它们rbinddf
  • 其余的是标准的R代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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