(This isn't really a question thread - I'm just providing some possibly useful code, and asking for suggestions on how it can be improved).
The boot.ci function in the boot package allows users to get confidence intervals for particular statistics, with the limits obtained via bootstrapping instead of traditional parametric methods. However, boot.ci doesn't readily allow one to bootstrap confidence intervals for multiple statistics at once (e.g. all the factor loadings from an exploratory factor analysis).
So I've written a function that uses the boot.ci function to produce confidence intervals for multiple statistics, with output given in a convenient matrix. Jacob Wegelin has previously suggested an alternative way to do this on the R help list. But I'm a bit of an R newbie, didn't understand his code, and thought it might be interesting to see if I could figure out how to do this from scratch.
Code for the function is below; I've called it bcafunction.
Usage (once above code is pasted into the R window):
bcafunction(boot.out, conf = 0.95)
Arguments
boot.out An object of class "boot" containing the output of a bootstrap calculation for the statistics of interest.
conf The required confidence level. The default is 0.95 (a 95% confidence interval).
I should probably comment the code above, but I'm having a bit of trouble doing so in a senseful way (the code makes sense in my head, but explaining it is harder). Let me know if it would be valuable to anyone for me to have another go at doing so.
The function is only set up to work for bias corrected accelerated (bca) confidence intervals, which the boot package documentation refers to as "adjusted bootstrap percentile" intervals. Applying the function to work with other methods (e.g. first order normal approximation, studentized), is presumably feasible, but this paper suggests bca intervals are one of the best methods for bootstrapping confidence intervals.
When used in its unadulterated form to produce confidence intervals one at a time, the boot.ci function sometimes produces warnings that a confidence interval may be unstable, sometimes accompanied by a statement that extreme quantiles were used in the calculation for a particular limit. The bcafunction I've suggested here doesn't produce these warnings, and I'm not sure how these could be implemented. Using a large number of replicates (e.g. R > 2000) when using boot object (boot.out) may help reduce the risk of these problems.
The function can be quite time-consuming. If anyone has any suggestions for how the code could be improved or sped up, let me know!
The boot.ci function in the boot package allows users to get confidence intervals for particular statistics, with the limits obtained via bootstrapping instead of traditional parametric methods. However, boot.ci doesn't readily allow one to bootstrap confidence intervals for multiple statistics at once (e.g. all the factor loadings from an exploratory factor analysis).
So I've written a function that uses the boot.ci function to produce confidence intervals for multiple statistics, with output given in a convenient matrix. Jacob Wegelin has previously suggested an alternative way to do this on the R help list. But I'm a bit of an R newbie, didn't understand his code, and thought it might be interesting to see if I could figure out how to do this from scratch.
Code for the function is below; I've called it bcafunction.
Code:
bcafunction <- function(boot.out, conf = 0.95){
matrixcis <- cbind(seq(1:ncol(boot.out$t)), sapply(seq(1:ncol(boot.out$t)), function(index){
bootci <- boot.ci(boot.out, type = "bca", index = index, conf = conf)
bootci$bca[1, 4]
}),
sapply(seq(1:ncol(boot.out$t)), function(index){
bootci <- boot.ci(boot.out, type = "bca", index = index, conf = conf)
bootci$bca[1, 5]
}))
colnames(matrixcis) <- c("index", "LL", "UL")
matrixcis
}
bcafunction(boot.out, conf = 0.95)
Arguments
boot.out An object of class "boot" containing the output of a bootstrap calculation for the statistics of interest.
conf The required confidence level. The default is 0.95 (a 95% confidence interval).
I should probably comment the code above, but I'm having a bit of trouble doing so in a senseful way (the code makes sense in my head, but explaining it is harder). Let me know if it would be valuable to anyone for me to have another go at doing so.
The function is only set up to work for bias corrected accelerated (bca) confidence intervals, which the boot package documentation refers to as "adjusted bootstrap percentile" intervals. Applying the function to work with other methods (e.g. first order normal approximation, studentized), is presumably feasible, but this paper suggests bca intervals are one of the best methods for bootstrapping confidence intervals.
When used in its unadulterated form to produce confidence intervals one at a time, the boot.ci function sometimes produces warnings that a confidence interval may be unstable, sometimes accompanied by a statement that extreme quantiles were used in the calculation for a particular limit. The bcafunction I've suggested here doesn't produce these warnings, and I'm not sure how these could be implemented. Using a large number of replicates (e.g. R > 2000) when using boot object (boot.out) may help reduce the risk of these problems.
The function can be quite time-consuming. If anyone has any suggestions for how the code could be improved or sped up, let me know!