Skip to content
Prev 286644 / 398502 Next

indexing??

On Tue, Feb 28, 2012 at 05:59:24AM -0800, helin_susam wrote:
Hi.

First of all, there are some unclear points in your code.
In particular, i would expect "}" between the line

  out[[i]] <- list(...

and

  kk <- sapply(...

Moreover, i do not see, why the loop over j contains the
loop over i. I would expect these loops be disjoint,
since the loop over i collects all the samples to a list.

The following code is a modification, which i suggest
as an alternative.

  y <- c(1:5, 1:5)
  x <- c(1,0,0,1,1,0,0,1,1,0)
 
  n <- length(x)
 
  t <- matrix(cbind(y,x), ncol=2)
 
  z = x+y
 
  # generate 10 bootstrap samples and keep z.s, y.s
  out <- vector("list", 10)
  for(i in 1:10) {
    t.s <- t[sample(n,n,replace=T),]
    y.s <- t.s[,1]
    x.s <- t.s[,2]
    z.s <- y.s+x.s
    out[[i]] <- list(zz = z.s, yy =y.s)
  }

  # check, which replications do not contain y[j] in their y.s,
  # and take the OR of these conditions over j
  ff <- rep(FALSE, times=length(out))
  for(j in 1:length(y)) {
     kk <- sapply(out, function(x) {any(x$yy == y[j])})
     ff <- ff | (! kk)
  }
  out[ff]

With the original y <- c(1,1,1,0,0,1,0,1,0,0), the probability
that a bootstrap sample contains only 1's or only 0's is
2 * (1/2)^10, so i replaced the vector y with another, where
a missing value is more frequent. I obtained, for example

  [[1]]
  [[1]]$zz
   [1] 2 2 5 2 3 2 3 2 2 6
  
  [[1]]$yy
   [1] 1 1 5 1 3 2 3 2 1 5   # 4 is missing
  
  
  [[2]]
  [[2]]$zz
   [1] 5 5 5 5 3 5 2 5 6 4
  
  [[2]]$yy
   [1] 4 4 5 4 3 5 2 5 5 3  # 1 is missing
  
  
  [[3]]
  [[3]]$zz
   [1] 5 2 5 1 5 1 2 5 5 5
  
  [[3]]$yy
   [1] 4 2 5 1 5 1 1 4 5 4  # 3 is missing
 
Hope this helps.

Petr Savicky.