bootstrap 使用 R 的问题

bootstrap issue using R

有什么方法可以在bootstrap完成后打印所有selected数据吗?

一旦我使用下面的代码

library(boot)
set.seed(1234)

rsq = function(data,indices) {
  d = data[indices,]
  fit = lm(formula=mpg~wt+disp,data=d)
  return(summary(fit)$r.square)
}
results = boot(data = mtcars, statistic = rsq, R=1000)
print(results)
plot(results)
boot.ci(results,conf=0.95,type=c('perc','bca'))


BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
Based on 1000 bootstrap replicates

CALL : 
boot.ci(boot.out = results, conf = 0.95, type = c("perc", "bca"))

Intervals : 
Level     Percentile            BCa          
95%   ( 0.6838,  0.8833 )   ( 0.6344,  0.8549 )  

为了获得信心 interval.I 想要打印所有 select 由 bootstrap 方法编辑的 select obs。

谢谢。

你可以这样做:

ind <- list()
rsq <- function(data,indices) {
  d <- data[indices,]
  ind[[length(ind)+1]] <<- indices
  fit <- lm(formula=mpg~wt+disp,data=d)
  return(summary(fit)$r.square)
}

那么你所有的 1000 组索引都会在列表中 ind

然后也许使用 unique 来查看采样了哪些独特的观察结果:

lapply(ind, unique)[1:2]

[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

[[2]]
 [1] 29  4  3 15 16  7 32  5 31 17 28 20 26 19 10 18  1  6 24  8