在 bootstrap 中获取 R 中的 NaN

Getting NaN in R in bootstrap

我将在 R 中使用以下函数 bootstrap:

robustri <- function(xv,gam=0.95,indices) {
            d <- xv[indices] # allows boot to select sample
            pc <- (1+gam)/2
            cc <- 1/(0.581734 - 0.607227*gam)
            x <- sort(d)
            nx <- length(x)
            tpctpt <- qt(pc,(nx-1))
            ...
 }

当我 运行 bootstrap:

 boot(data=dataex , statistic=robustri , R=5) 

我收到这些警告:

In qt(pc, (nx - 1)) : NaNs produced

但是,当我绕过所需的 bootstrap 语法(indices 参数)时 运行 我的数据一切正常。

尝试 "rolling your own" bootstrap 像这样:

#specify number of bootstrap samples
B <- 1000

#create storage vector to hold output from each bootstrap sample
resultvec <- numeric(B)

#get length of data so we don't have to recompute this in each iteration
N <- nrow(mydata)

#do bootstrap
for(b in seq(B)) {
    #take a sample of your data
    this_boot_sample <- mydata[sample(1:N, size=N, replace=T), ]
    #calculate your statistic of interest and save it
    resultvec[b] <- myfun(this_boot_sample)
}

然后当您遇到错误时,您应该能够深入研究并诊断问题。