【问题标题】:help me improve my bootstrap帮助我改进我的引导程序
【发布时间】:2010-07-28 17:47:28
【问题描述】:

考虑以下代码:

require(Hmisc)
num.boots <- 10
data <- rchisq(500, df = 5) #generate fake data

#create bins
binx <- cut(data, breaks = 10)
binx <- levels(binx)
binx <- sub("^.*\\,", "", binx)
binx <- as.numeric(substr(binx, 1, nchar(binx) - 1))

#pre-allocate a matrix to be filled with samples
output <- matrix(NA, nrow = num.boots, ncol = length(binx)) 

#do random sampling from the vector and calculate percent
# of values equal or smaller to the bin number (i)
for (i in 1:num.boots) {
    walk.pair.sample <- sample(data, size = length(data), replace = TRUE)
    data.cut <- cut2(x = walk.pair.sample, cuts = binx)
    data.cut <- table(data.cut)/sum(table(data.cut))
    output[i, ] <- data.cut
}

#do some plotting
plot(1:10, seq(0, max(output), length.out = nrow(output)), type = "n", xlab = "", ylab = "")

for (i in 1:nrow(output)) {
    lines(1:10, output[i, 1:nrow(output)])
}

#mean values by columns
output.mean <- apply(output, 2, mean)
lines(output.mean, col="red", lwd = 3)
legend(x = 8, y = 0.25, legend = "mean", col = "red", lty = "solid", lwd = 3)

我想知道是否可以为 boot:boot() 函数提供一个输出长度为 n > 1 的向量的函数?有可能吗?

这是我微弱的尝试,但我一定做错了什么。

require(boot)
bootstrapDistances <- function(data, binx) {
    data.cut <- cut2(x = data, cuts = binx)
    data.cut <- table(data.cut)/sum(table(data.cut))
    return(data.cut)
}

> x <- boot(data = data, statistic = bootstrapDistances, R = 100)
Error in cut.default(x, k2) : 'breaks' are not unique

我真的不明白为什么Hmisc::cut2()boot() 调用中不能正常工作,但是当我在for() 循环中调用它时工作正常(参见上面的代码)。我的bootstrapDistances() 函数的逻辑与boot() 是否可行?任何指针都非常感谢。

.:编辑:.

Aniko 建议我以这种方式修改我的函数,以包含一个索引。在阅读 boot() 的文档时,我并不清楚它是如何工作的,这解释了为什么该函数可能无法正常工作。这是 Aniko 建议的新功能:

bootstrapDistances2 <- function(data, idx, binx) { 
  data.cut <- cut2(x = data[idx], cuts = binx) 
  data.cut <- table(data.cut)/sum(table(data.cut)) 
  return(data.cut) 
} 

但是,我设法产生了一个错误,我仍在研究如何删除它。

> x <- boot(data = data, statistic = bootstrapDistances2, R = 100, binx = binx)
Error in t.star[r, ] <- statistic(data, i[r, ], ...) : 
  number of items to replace is not a multiple of replacement length

重新启动我的 R 会话后(也尝试了另一个版本,2.10.1),它似乎工作正常。

【问题讨论】:

    标签: r statistics-bootstrap


    【解决方案1】:

    来自boot 函数的帮助文件:

    在所有其他情况下,statistic 必须至少有两个参数。传递的第一个参数将始终是原始数据。第二个将是定义引导样本的索引、频率或权重的向量。

    所以你需要在bootstrapDistances 函数中添加第二个参数,告诉它选择了哪些数据元素:

    bootstrapDistances2 <- function(data, idx, binx) { 
      data.cut <- cut2(x = data[idx], cuts = binx) 
      data.cut <- table(data.cut)/sum(table(data.cut)) 
      return(data.cut) 
    } 
    

    结果:

    x <- boot(data = data, statistic = bootstrapDistances2, R = 100, binx=binx)
    x
    
    ORDINARY NONPARAMETRIC BOOTSTRAP
    
    
    Call:
    boot(data = data, statistic = bootstrapDistances2, R = 100, binx = binx)
    
    
    Bootstrap Statistics :
         original   bias    std. error
    t1*     0.208  0.00134 0.017342783
    t2*     0.322  0.00062 0.021700803
    t3*     0.190 -0.00034 0.018873433
    t4*     0.136 -0.00116 0.016206197
    t5*     0.078 -0.00120 0.011413265
    t6*     0.036  0.00070 0.008510837
    t7*     0.016  0.00074 0.005816417
    t8*     0.006  0.00024 0.003654581
    t9*     0.000  0.00000 0.000000000
    t10*    0.008 -0.00094 0.003368961
    

    【讨论】:

    • 感谢您的尝试,但我收到此错误:x
    • 重启我的 R 会话后,一切正常。啊?感谢您的合作。
    【解决方案2】:

    很好的答案,阿尼科。

    此外,“引导”的帮助页面指出引导统计函数可能返回一个向量,而不仅仅是一个标量。

    【讨论】:

    • 现在我看到了——上面写着“统计数据”!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多