Question

I have a data frame and would like to apply a function each column using the boot function. I also would like to get the confidence intervals on the applied function using boot.ci. I have been able to get perform the boot-strap as shown in the following simple example, but don't understand why it works:

library(boot)
resample.number<-1000

df <- data.frame(A = 0:10,B = 10:20,C = 20:30)

sample.mean<-function(data,x){
    apply(data[x,],2,mean)
}

b<-boot(df,sample.mean,resample.number)

Firstly, why do I need the x and [x,] accompanying the data input to my function? ( I thought that it would work just fine by defining my function as:

sample.mean<-function(data){
apply(data,2,mean)
}

,but this throws the error: Error in statistic(data, original, ...) : unused argument(s) (original). Since this I don't understand why the x's are needed. I'm having some trouble defining my function to also return the confidence intervals associated with each boot-strapped statistic (i.e., the confidence intervals associated with each column mean). Perhaps my trouble with the confidence intervals is rooted in my misunderstanding of the purpose of the x's? For example,

boot.ci(b)

Only returns one set of confidence intervals.

Any direction to other questions or comments on why the x reference is needed would be greatly appreciated; any guidance on how return the confidence intervals would also be appreciated.

Was it helpful?

Solution

Maybe something more like this:

sample.mean<-function(data,x){
    mean(data[x])
}

boot_obj <- lapply(df,boot,statistic = sample.mean,R = 200)
lapply(boot_obj,boot.ci)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top