Skip to main content
added 110 characters in body
Source Link
knrumsey
  • 8.8k
  • 27
  • 52

I generally agree with you that it's more of an annoyance than anything, but here's a simple example:

Think about how you would do bootstrap for something like correlation between two vectors. All of these calls will fail to do what we want:

x <- rnorm(30)
y <- x + rnorm(30, 0, 0.1)

boot(x, cor, R=1000)
boot(x, y, cor, R=1000)
boot(cbind(x, y), cor, R=1000)

Instead, we need something like:

my_cor <- function(x, i){
   x <- x[i,]
   cor(x[,1], x[,2])
}
boot(cbind(x, y), my_cor, R=1000)

While there are other solutions, whatlike coercing x to be a matrix and internally setting x_boot = x[i,], this may not be ideal in all cases. What if my codedata is structured so that I want to take samples across columns rather than rows? Or what if my data is organized as a list? This extra requirement gives us flexibility in the end (but is just annoying 99% of the time).

I generally agree with you that it's more of an annoyance than anything, but here's a simple example:

Think about how you would do bootstrap for something like correlation between two vectors. All of these calls will fail to do what we want:

x <- rnorm(30)
y <- x + rnorm(30, 0, 0.1)

boot(x, cor, R=1000)
boot(x, y, cor, R=1000)
boot(cbind(x, y), cor, R=1000)

Instead, we need something like:

my_cor <- function(x, i){
   x <- x[i,]
   cor(x[,1], x[,2])
}
boot(cbind(x, y), my_cor, R=1000)

While there are other solutions, what if my code is structured so that I want to take samples across columns rather than rows? Or what if my data is organized as a list? This extra requirement gives us flexibility in the end (but is just annoying 99% of the time).

I generally agree with you that it's more of an annoyance than anything, but here's a simple example:

Think about how you would do bootstrap for something like correlation between two vectors. All of these calls will fail to do what we want:

x <- rnorm(30)
y <- x + rnorm(30, 0, 0.1)

boot(x, cor, R=1000)
boot(x, y, cor, R=1000)
boot(cbind(x, y), cor, R=1000)

Instead, we need something like:

my_cor <- function(x, i){
   x <- x[i,]
   cor(x[,1], x[,2])
}
boot(cbind(x, y), my_cor, R=1000)

While there are other solutions, like coercing x to be a matrix and internally setting x_boot = x[i,], this may not be ideal in all cases. What if my data is structured so that I want to take samples across columns rather than rows? Or what if my data is organized as a list? This extra requirement gives us flexibility in the end (but is just annoying 99% of the time).

Source Link
knrumsey
  • 8.8k
  • 27
  • 52

I generally agree with you that it's more of an annoyance than anything, but here's a simple example:

Think about how you would do bootstrap for something like correlation between two vectors. All of these calls will fail to do what we want:

x <- rnorm(30)
y <- x + rnorm(30, 0, 0.1)

boot(x, cor, R=1000)
boot(x, y, cor, R=1000)
boot(cbind(x, y), cor, R=1000)

Instead, we need something like:

my_cor <- function(x, i){
   x <- x[i,]
   cor(x[,1], x[,2])
}
boot(cbind(x, y), my_cor, R=1000)

While there are other solutions, what if my code is structured so that I want to take samples across columns rather than rows? Or what if my data is organized as a list? This extra requirement gives us flexibility in the end (but is just annoying 99% of the time).