2

2 레벨의 계층 적 데이터가 있으며 최상위 레벨에서 비 파라 메트릭 부트 스트랩 샘플링을 수행하려고합니다. 즉 원래의 클러스터 내 데이터를 유지하면서 대체하여 가장 높은 수준의 클러스터를 임의로 샘플링합니다.{boot}의 boot() 함수를 사용하여 최상위 수준의 클러스터 된 데이터에 대한 비 파라 메트릭 부트 스트랩 R

부트 오브젝트가 필요한 boot.ci()를 사용하여 BCa 신뢰 구간을 빌드하려는 이유로 {boot} 패키지에서 boot() 함수를 사용하여이 작업을 수행하고 싶습니다.

다음은 내 불운 한 시도입니다. 부트 호출에서 디버그를 실행하면 클러스터 레벨 (= 주제)에서 무작위 샘플링이 발생하지 않는 것으로 나타났습니다.

### create a very simple two-level dataset with 'subject' as clustering variable 

rho <- 0.4 
dat <- expand.grid(
    trial=factor(1:5), 
    subject=factor(1:3) 
    ) 
sig <- rho * tcrossprod(model.matrix(~ 0 + subject, dat)) 
diag(sig) <- 1 
set.seed(17); dat$value <- chol(sig) %*% rnorm(15, 0, 1) 


### my statistic function (adapted from here: http://biostat.mc.vanderbilt.edu/wiki/Main/HowToBootstrapCorrelatedData) 

resamp.mean <- function(data, i){ 
    cluster <- c('subject', 'trial') 

    # sample the clustering factor 
    cls <- unique(data[[cluster[1]]])[i] 

    # subset on the sampled clustering factors 
    sub <- lapply(cls, function(b) subset(data, data[[cluster[1]]]==b)) 

    sub.2 <- do.call(rbind, sub)  # join and return samples 
    mean((sub.2$value))    # calculate the statistic 
} 


debugonce(boot) 
set.seed(17); dat.boot <- boot(data = dat, statistic = resamp.mean, 4) 


### stepping trough the debugger until object 'i' was assigned 
### investigating 'i' 
# Browse[2]> head(i) 

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] 
[1,] 3 7 12 13 10 14 14 15 12 12 12  4  5  9 10 
[2,] 15 9 3 13 4 10 2 4 6 11 10  4  9  4  3 
[3,] 8 4 7 15 10 12 9 8 9 12  4 15 14 10  4 
[4,] 12 3 1 15 8 13 9 1 4 13  9 13  2 11  2 

### which is not what I was hoping for. 


### I would like something that looks like this, supposing indices = c(2, 2, 1) for the first resample: 

    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] 
[1,] 6 7 8 9 10 6 7 8 9 10  1  2  3  4  5 

어떤 도움을 주시면 감사하겠습니다.

답변

2

이 문제는 수정 된 통계 함수 (구체적으로 함수 내의 cls 개체)에서 기인한다고 생각합니다. 이거해볼 수 있니? print 문을 주석 처리하여 샘플링 된 주제를 확인하십시오. boot이 예상하는 index 인수를 사용하지 않고 원래 기능처럼 sample을 사용합니다.

value의 평균 전에 resamp.mean 기능에서
resamp.mean <- function(dat, 
         indices, 
         cluster = c('subject', 'trial'), 
         replace = TRUE){ 
     # boot expects an indices argument but the sampling happens 
     # via sample() as in the original source of the function 

     # sample the clustering factor 
     cls <- sample(unique(dat[[cluster[1]]]), replace=replace) 

     # subset on the sampled clustering factors 
     sub <- lapply(cls, function(b) subset(dat, dat[[cluster[1]]]==b)) 

     # join and return samples 
     sub <- do.call(rbind, sub) 

     # UNCOMMENT HERE TO SEE SAMPLED SUBJECTS 
     # print(sub) 

     mean(sub$value) 
} 

한 재 샘플 계산 다음과 같습니다

trial subject  value 
1  1  1 -1.1581291 
2  2  1 -0.1458287 
3  3  1 -0.2134525 
4  4  1 -0.5796521 
5  5  1 0.6501587 
11  1  3 2.6678441 
12  2  3 1.3945740 
13  3  3 1.4849435 
14  4  3 0.4086737 
15  5  3 1.3399146 
111  1  1 -1.1581291 
121  2  1 -0.1458287 
131  3  1 -0.2134525 
141  4  1 -0.5796521 
151  5  1 0.6501587 
+0

정말 감사합니다, Khl4v! 제안한 코드가 제대로 작동하는 것 같습니다. 또한'print (cls)'를 사용하여 클러스터링 요소의 다양한 재 샘플링을 확인합니다. 나는 당신이 indicies 인수를 주면 부팅 기능을 "속일 수있다"는 것을 알지 못했지만 실제 재 샘플링에 사용하지는 않습니다. 다시 한번 감사드립니다. – exfalso

+0

예, 실제로 이것은 "boot"기능을 속이는 것입니다. 어떤 경우에는'strata' 논쟁이 필요합니다. 그러나이 특정 애플리케이션에서는 작동하지 않습니다. 그래서 다른 사람이 처음에 그 기능을 속 였지만 솔루션을'지층 (strata) '을 사용하도록 변경 한 곳에서 또 다른 [질문] (http://stackoverflow.com/questions/3615718/bootstrapping-to-compare-two-groups)이 있습니다. – thie1e