Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 845
asher Avatar asked Nov 20 '25 07:11

asher


1 Answers

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.

like image 170
asher Avatar answered Nov 21 '25 22:11

asher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!