asher
asher

Reputation: 304

Return two values from boot R

I'm trying to bootstrap a function and I want the boot function to return more than 1 value:

library(boot)
r=50;c=50
m1 <- (sample(1000,r*c,T))
nboot = 100
boot_fun <- function(m,b){
  m <- m[b]
  mn <- mean(m)
  vr <- var(m)
  tmp <- list(mn,vr)
  return(tmp)
}
bmat <- boot(data=m1,statistic=boot_fun,R=nboot)

Here, I want to return both vr and mn values, but this of course doesn't work because I get this error:

Error in boot(data = m1, statistic = boot_fun, R = nboot) incorrect number of subscripts on matrix

I can bootstrap twice but that takes much more time.

Is there any way to return more than one objects from a boot function?

Upvotes: 3

Views: 1315

Answers (1)

asher
asher

Reputation: 304

Well, I fill kind of silly. The answer to my problem is simple:

I just attach the additional information to the returned vector in the boot function and later on I just subset the bmat$t matrix.

So the answer can look like this:

library(boot)
r=50;c=50
m1 <- (sample(1000,r*c,T))
nboot = 100
boot_fun <- function(m,b){
  m <- m[b]
  mn <- mean(m)
  vr <- var(m)
  return(c(mn,vr))
}
bmat <- boot(data=m1,statistic=boot_fun,R=nboot)
mn <- bmat$t[,1]
vr <- bmat$t[,2]

I hope this can be of some help to someone.

Upvotes: 3

Related Questions