Skip to main content
Found error in code and R-output, small expansions, fixed typos
Source Link
COOLSerdash
  • 31.2k
  • 10
  • 104
  • 157

There are several methods for calculating confidence intervals based on the bootstrap samples (this paper provides explanation and guidance). One very simple method for calculating a 95%-confidence interval is just calculating the empirical 2.5th and 97.5th percentiles of the bootstrap samples (seethis interval is called the bootstrap percentile interval; see code below). The simple percentile interval method is rarely used in practice as there are better methods, such as the bias-corrected and accelerated bootstrap (BCa). BCa intervals adjust for both bias and skewness in the bootstrap distribution.

The biasbias is simply estimated as the difference between the mean of the $n$ stored bootstrap samples and the original estimatesestimate(s).

Let's replicate the example from the website but using our own loop incorporating the ideas I've outlined above (drawing repeadetlyrepeatedly with replacement):

#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------

set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# QuantileBasic confidencebootstrap intervalsCIs based on the empirical quantiles
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T), 
ncol=2, byrow=TRUE)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")
#-----------------------------------------------------------------------------
# Set up summary data frame
#-----------------------------------------------------------------------------

summary.frame <- data.frame(mean=boot.means, median=boot.medians,
sd=boot.sds, bias=boot.bias, "CI_lower"=conf.mat[,1], "CI_upper"=conf.mat[,2])

summary.frame

         mean     median          sd          bias    CI_lower   CI_upper
1   1.29979032998  1.30130793013  0.3967380439674 -0.07129122500712912  0.5196003151960 -1  2.30663510605
2   0.25266882527  0.24857212486  0.0320779003208 -0.00344605090034461  20.0604901519898 -  0.43797613229
3  -1.56616865662 -1.55719895572  0.2622001326220 -0.0509238534 0509239 0-2.1989775912900  0-1.14493540920
4   0.20049572005  0.19864541986  0.0194943701949  0.00490189550049019  0.3229413016744   0.21401182418
5   0.95438349544  0.92520359252  0.4891452348915  0.07534051520753405 -2 0.1289978303493  0 1.44149579025
6   0.27023612702  0.26880952688  0.0204250402043  0.00095825920009583 -1 0.0919599823272  8 0.04711433137
7  -0.89968368997 -0.90823799082  0.2217392122174  0.0856792712 0856793 0-1.1674419830664  -0.58113944380
8   0.17889341789  0.17811611781  0.0166725001667  0.00295125650029513  0.2417977814494 57  0.64166802140
9   2.06826180683  1.77189037719  1.5910232259102  0.46548978654654898  0.0349261244150 -  8.43440380471
10  4.02087570209  0.82697758270 13.2343353923434  3.18457096801845710  10.9024510858114 -1 57.11561276417
11 -2.09694050969 -1.67171026717  1.5631076256311 -0.4306843771 4306844 0-8.2327156643440  0-1.33631011156
12  3.86603458660  0.64348596435 13.2752503327525  3.18706419411870642  0.3137059333631  57.60619936062
#-----------------------------------------------------------------------------
# Compare with boot output and confidence intervals
#-----------------------------------------------------------------------------

set.seed(10)
res <- boot(zinb, f, R = 2000, parallel = "snow", ncpus = 4)

res

Bootstrap Statistics :
       original       bias    std. error
t1*   1.3710504 -0.076735010  0.39842905
t2*   0.2561136 -0.003127401  0.03172301
t3*  -1.5152609 -0.064110745  0.26554358
t4*   0.1955916  0.005819378  0.01933571
t5*   0.8790522  0.083866901  0.49476780
t6*   0.2692734  0.001475496  0.01957823
t7*  -0.9853566  0.083186595  0.22384444
t8*   0.1759504  0.002507872  0.01648298
t9*   1.6031354  0.482973831  1.58603356
t10*  0.8365225  3.240981223 13.86307093
t11* -1.6665917 -0.453059768  1.55143344
t12*  0.6793077  3.247826469 13.90167954

perc.cis <- matrix(NA, nrow=dim(res$t)[2], ncol=2)
for( i in 1:dim(res$t)[2] ) {
  perc.cis[i,] <- boot.ci(res, conf=0.95, type="perc", index=i)$percent[4:5] 
}
colnames(perc.cis) <- c("95%-CI Lower", "95%-CI Upper")

perc.cis 

      95%-CI Lower 95%-CI Upper
 [1,]      0.52240       2.1035
 [2,]      0.19984       0.3220
 [3,]     -2.12820      -1.1012
 [4,]      0.16754       0.2430
 [5,]      0.04817       1.9084
 [6,]      0.23401       0.3124
 [7,]     -1.29964      -0.4314
 [8,]      0.14517       0.2149
 [9,]      0.29993       8.0463
[10,]      0.57248      56.6710
[11,]     -8.64798      -1.1088
[12,]      0.33048      56.6702

#-----------------------------------------------------------------------------
# Our summary table
#-----------------------------------------------------------------------------

summary.frame
  
         mean     median          sd          bias    CI_lower   CI_upper
1   1.29979032998  1.30130793013  0.3967380439674 -0.07129122500712912  0.5196003151960 -1  2.30663510605
2   0.25266882527  0.24857212486  0.0320779003208 -0.00344605090034461  20.0604901519898 -  0.43797613229
3  -1.56616865662 -1.55719895572  0.2622001326220 -0.0509238534 0509239 0-2.1989775912900  0-1.14493540920
4   0.20049572005  0.19864541986  0.0194943701949  0.00490189550049019  0.3229413016744   0.21401182418
5   0.95438349544  0.92520359252  0.4891452348915  0.07534051520753405 -2 0.1289978303493  0 1.44149579025
6   0.27023612702  0.26880952688  0.0204250402043  0.00095825920009583 -1 0.0919599823272  8 0.04711433137
7  -0.89968368997 -0.90823799082  0.2217392122174  0.0856792712 0856793 0-1.1674419830664  -0.58113944380
8   0.17889341789  0.17811611781  0.0166725001667  0.00295125650029513  0.2417977814494 57  0.64166802140
9   2.06826180683  1.77189037719  1.5910232259102  0.46548978654654898  0.0349261244150 -  8.43440380471
10  4.02087570209  0.82697758270 13.2343353923434  3.18457096801845710  10.9024510858114 -1 57.11561276417
11 -2.09694050969 -1.67171026717  1.5631076256311 -0.4306843771 4306844 0-8.2327156643440  0-1.33631011156
12  3.86603458660  0.64348596435 13.2752503327525  3.18706419411870642  0.3137059333631  57.60619936062

Compare the "bias" columns and the "std. error" with the "sd" column of our own summary table. Our 95%-confidence intervals are very similar to the confidence intervals calculated by boot.ci using the percentile method (not all though: look at the lower limit of parameter with index 9).

There are several methods for calculating confidence intervals based on the bootstrap samples (this paper provides explanation and guidance). One very simple method is just calculating the 2.5th and 97.5th percentiles of the bootstrap samples (see code below).

The bias is simply estimated as the difference between the bootstrap samples and the original estimates.

Let's replicate the example from the website but using our own loop incorporating the ideas I've outlined above (drawing repeadetly with replacement):

#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------

set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# Quantile confidence intervals
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T),ncol=2)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")
#-----------------------------------------------------------------------------
# Set up summary data frame
#-----------------------------------------------------------------------------

summary.frame <- data.frame(mean=boot.means, median=boot.medians,
sd=boot.sds, bias=boot.bias, "CI_lower"=conf.mat[,1], "CI_upper"=conf.mat[,2])

summary.frame

         mean     median          sd          bias    CI_lower   CI_upper
1   1.2997903  1.3013079  0.39673804 -0.0712912250  0.51960031 -1.3066351
2   0.2526688  0.2485721  0.03207790 -0.0034460509  2.06049015 -0.4379761
3  -1.5661686 -1.5571989  0.26220013 -0.0509238534  0.19897759  0.1449354
4   0.2004957  0.1986454  0.01949437  0.0049018955  0.32294130  0.2140118
5   0.9543834  0.9252035  0.48914523  0.0753405152 -2.12899783  0.4414957
6   0.2702361  0.2688095  0.02042504  0.0009582592 -1.09195998  8.0471143
7  -0.8996836 -0.9082379  0.22173921  0.0856792712  0.16744198  0.5811394
8   0.1788934  0.1781161  0.01667250  0.0029512565  0.24179778 57.6416680
9   2.0682618  1.7718903  1.59102322  0.4654897865  0.03492612 -8.4344038
10  4.0208757  0.8269775 13.23433539  3.1845709680  1.90245108 -1.1156127
11 -2.0969405 -1.6717102  1.56310762 -0.4306843771  0.23271566  0.3363101
12  3.8660345  0.6434859 13.27525033  3.1870641941  0.31370593 57.6061993
#-----------------------------------------------------------------------------
# Compare with boot output
#-----------------------------------------------------------------------------

set.seed(10)
res <- boot(zinb, f, R = 2000, parallel = "snow", ncpus = 4)

res

Bootstrap Statistics :
       original       bias    std. error
t1*   1.3710504 -0.076735010  0.39842905
t2*   0.2561136 -0.003127401  0.03172301
t3*  -1.5152609 -0.064110745  0.26554358
t4*   0.1955916  0.005819378  0.01933571
t5*   0.8790522  0.083866901  0.49476780
t6*   0.2692734  0.001475496  0.01957823
t7*  -0.9853566  0.083186595  0.22384444
t8*   0.1759504  0.002507872  0.01648298
t9*   1.6031354  0.482973831  1.58603356
t10*  0.8365225  3.240981223 13.86307093
t11* -1.6665917 -0.453059768  1.55143344
t12*  0.6793077  3.247826469 13.90167954

#-----------------------------------------------------------------------------
# Our summary table
#-----------------------------------------------------------------------------

summary.frame
  
         mean     median          sd          bias    CI_lower   CI_upper
1   1.2997903  1.3013079  0.39673804 -0.0712912250  0.51960031 -1.3066351
2   0.2526688  0.2485721  0.03207790 -0.0034460509  2.06049015 -0.4379761
3  -1.5661686 -1.5571989  0.26220013 -0.0509238534  0.19897759  0.1449354
4   0.2004957  0.1986454  0.01949437  0.0049018955  0.32294130  0.2140118
5   0.9543834  0.9252035  0.48914523  0.0753405152 -2.12899783  0.4414957
6   0.2702361  0.2688095  0.02042504  0.0009582592 -1.09195998  8.0471143
7  -0.8996836 -0.9082379  0.22173921  0.0856792712  0.16744198  0.5811394
8   0.1788934  0.1781161  0.01667250  0.0029512565  0.24179778 57.6416680
9   2.0682618  1.7718903  1.59102322  0.4654897865  0.03492612 -8.4344038
10  4.0208757  0.8269775 13.23433539  3.1845709680  1.90245108 -1.1156127
11 -2.0969405 -1.6717102  1.56310762 -0.4306843771  0.23271566  0.3363101
12  3.8660345  0.6434859 13.27525033  3.1870641941  0.31370593 57.6061993

Compare the "bias" columns and the "std. error" with the "sd" column of our own summary table.

There are several methods for calculating confidence intervals based on the bootstrap samples (this paper provides explanation and guidance). One very simple method for calculating a 95%-confidence interval is just calculating the empirical 2.5th and 97.5th percentiles of the bootstrap samples (this interval is called the bootstrap percentile interval; see code below). The simple percentile interval method is rarely used in practice as there are better methods, such as the bias-corrected and accelerated bootstrap (BCa). BCa intervals adjust for both bias and skewness in the bootstrap distribution.

The bias is simply estimated as the difference between the mean of the $n$ stored bootstrap samples and the original estimate(s).

Let's replicate the example from the website but using our own loop incorporating the ideas I've outlined above (drawing repeatedly with replacement):

#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------

set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# Basic bootstrap CIs based on the empirical quantiles
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T), 
ncol=2, byrow=TRUE)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")
#-----------------------------------------------------------------------------
# Set up summary data frame
#-----------------------------------------------------------------------------

summary.frame <- data.frame(mean=boot.means, median=boot.medians,
sd=boot.sds, bias=boot.bias, "CI_lower"=conf.mat[,1], "CI_upper"=conf.mat[,2])

summary.frame

      mean  median       sd       bias CI_lower CI_upper
1   1.2998  1.3013  0.39674 -0.0712912  0.51960   2.0605
2   0.2527  0.2486  0.03208 -0.0034461  0.19898   0.3229
3  -1.5662 -1.5572  0.26220 -0.0509239 -2.12900  -1.0920
4   0.2005  0.1986  0.01949  0.0049019  0.16744   0.2418
5   0.9544  0.9252  0.48915  0.0753405  0.03493   1.9025
6   0.2702  0.2688  0.02043  0.0009583  0.23272   0.3137
7  -0.8997 -0.9082  0.22174  0.0856793 -1.30664  -0.4380
8   0.1789  0.1781  0.01667  0.0029513  0.14494   0.2140
9   2.0683  1.7719  1.59102  0.4654898  0.44150   8.0471
10  4.0209  0.8270 13.23434  3.1845710  0.58114  57.6417
11 -2.0969 -1.6717  1.56311 -0.4306844 -8.43440  -1.1156
12  3.8660  0.6435 13.27525  3.1870642  0.33631  57.6062
#-----------------------------------------------------------------------------
# Compare with boot output and confidence intervals
#-----------------------------------------------------------------------------

set.seed(10)
res <- boot(zinb, f, R = 2000, parallel = "snow", ncpus = 4)

res

Bootstrap Statistics :
       original       bias    std. error
t1*   1.3710504 -0.076735010  0.39842905
t2*   0.2561136 -0.003127401  0.03172301
t3*  -1.5152609 -0.064110745  0.26554358
t4*   0.1955916  0.005819378  0.01933571
t5*   0.8790522  0.083866901  0.49476780
t6*   0.2692734  0.001475496  0.01957823
t7*  -0.9853566  0.083186595  0.22384444
t8*   0.1759504  0.002507872  0.01648298
t9*   1.6031354  0.482973831  1.58603356
t10*  0.8365225  3.240981223 13.86307093
t11* -1.6665917 -0.453059768  1.55143344
t12*  0.6793077  3.247826469 13.90167954

perc.cis <- matrix(NA, nrow=dim(res$t)[2], ncol=2)
for( i in 1:dim(res$t)[2] ) {
  perc.cis[i,] <- boot.ci(res, conf=0.95, type="perc", index=i)$percent[4:5] 
}
colnames(perc.cis) <- c("95%-CI Lower", "95%-CI Upper")

perc.cis 

      95%-CI Lower 95%-CI Upper
 [1,]      0.52240       2.1035
 [2,]      0.19984       0.3220
 [3,]     -2.12820      -1.1012
 [4,]      0.16754       0.2430
 [5,]      0.04817       1.9084
 [6,]      0.23401       0.3124
 [7,]     -1.29964      -0.4314
 [8,]      0.14517       0.2149
 [9,]      0.29993       8.0463
[10,]      0.57248      56.6710
[11,]     -8.64798      -1.1088
[12,]      0.33048      56.6702

#-----------------------------------------------------------------------------
# Our summary table
#-----------------------------------------------------------------------------

summary.frame
  
      mean  median       sd       bias CI_lower CI_upper
1   1.2998  1.3013  0.39674 -0.0712912  0.51960   2.0605
2   0.2527  0.2486  0.03208 -0.0034461  0.19898   0.3229
3  -1.5662 -1.5572  0.26220 -0.0509239 -2.12900  -1.0920
4   0.2005  0.1986  0.01949  0.0049019  0.16744   0.2418
5   0.9544  0.9252  0.48915  0.0753405  0.03493   1.9025
6   0.2702  0.2688  0.02043  0.0009583  0.23272   0.3137
7  -0.8997 -0.9082  0.22174  0.0856793 -1.30664  -0.4380
8   0.1789  0.1781  0.01667  0.0029513  0.14494   0.2140
9   2.0683  1.7719  1.59102  0.4654898  0.44150   8.0471
10  4.0209  0.8270 13.23434  3.1845710  0.58114  57.6417
11 -2.0969 -1.6717  1.56311 -0.4306844 -8.43440  -1.1156
12  3.8660  0.6435 13.27525  3.1870642  0.33631  57.6062

Compare the "bias" columns and the "std. error" with the "sd" column of our own summary table. Our 95%-confidence intervals are very similar to the confidence intervals calculated by boot.ci using the percentile method (not all though: look at the lower limit of parameter with index 9).

added 314 characters in body
Source Link
COOLSerdash
  • 31.2k
  • 10
  • 104
  • 157

There are several "flavours" or forms of the bootstrap (e.g. non-parametric, parametric, residual resampling and many more). The bootstrap in the example is called a non-parametric bootstrap, or case resampling (see here, here, here and here for applications in regression). The basic idea is that you treat your sample as population and repeatedly draw new samples from it with replacement. All original observations have equal probability of being drawn into the new sample. After drawing the new samples from your sample,Then you calculate and store the statistic(s) of interest, this may be athe mean, athe median or regression coefficients using the newly drawn sample. This is repeated $n$ times. In each iteration, some observations from your original sample are drawn multiple times while some observations may not be sampleddrawn at all. After the$n$ iterations, you have $n$ stored bootstrap estimates of the statistic(s) of interest (e.g. if $n=1000$ and the statistic of interest is the mean, you have 1000 bootstrapped estimates of the mean). Lastly, summary statistics such as the mean, median and the standard deviationsdeviation of the $n$ bootstrap-estimates are calculated.

There are several methods for calculating confidence intervals based on the bootstrap samples (this paper provides explanation and guidance). One very simple method is just calculating the 2.55th and 97.5%5th percentiles of the bootstrap samples (see code below).

Let's replicate the example from the website but using our own loop incorporating the ideas I've outlined above (drawing repeadetly with replacement):

#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------
 
set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# Quantile confidence intervals
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T),ncol=2)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")

There are several "flavours" or forms of the bootstrap (e.g. non-parametric, parametric, residual resampling and many more). The bootstrap in the example is called a non-parametric bootstrap, or case resampling (see here, here, here and here for applications in regression). The basic idea is that you treat your sample as population and repeatedly draw new samples from it with replacement. All original observations have equal probability of being drawn into the new sample. After drawing the new samples from your sample, you calculate the statistic of interest, this may be a mean, a median or regression coefficients. This is repeated $n$ times. In each iteration, some observations from your original sample are drawn multiple times while some observations may not be sampled at all. After the iterations, the mean, median and standard deviations of the $n$ bootstrap-estimates are calculated.

There are several methods for calculating confidence intervals based on the bootstrap samples (this paper provides explanation and guidance). One very simple method is just calculating the 2.5 and 97.5% percentiles of the bootstrap samples (see code below).

Let's replicate the example from the website but using our own loop:

#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------
set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# Quantile confidence intervals
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T),ncol=2)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")

There are several "flavours" or forms of the bootstrap (e.g. non-parametric, parametric, residual resampling and many more). The bootstrap in the example is called a non-parametric bootstrap, or case resampling (see here, here, here and here for applications in regression). The basic idea is that you treat your sample as population and repeatedly draw new samples from it with replacement. All original observations have equal probability of being drawn into the new sample. Then you calculate and store the statistic(s) of interest, this may be the mean, the median or regression coefficients using the newly drawn sample. This is repeated $n$ times. In each iteration, some observations from your original sample are drawn multiple times while some observations may not be drawn at all. After $n$ iterations, you have $n$ stored bootstrap estimates of the statistic(s) of interest (e.g. if $n=1000$ and the statistic of interest is the mean, you have 1000 bootstrapped estimates of the mean). Lastly, summary statistics such as the mean, median and the standard deviation of the $n$ bootstrap-estimates are calculated.

There are several methods for calculating confidence intervals based on the bootstrap samples (this paper provides explanation and guidance). One very simple method is just calculating the 2.5th and 97.5th percentiles of the bootstrap samples (see code below).

Let's replicate the example from the website but using our own loop incorporating the ideas I've outlined above (drawing repeadetly with replacement):

#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------
 
set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# Quantile confidence intervals
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T),ncol=2)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")
added 39 characters in body
Source Link
COOLSerdash
  • 31.2k
  • 10
  • 104
  • 157
#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------
set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# Quantile confidence intervals
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T),ncol=2)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")

 

And here is our summary table:

#-----------------------------------------------------------------------------
# Set up summary data frame
#-----------------------------------------------------------------------------

summary.frame <- data.frame(mean=boot.means, median=boot.medians,  
sd=boot.sds, bias=boot.bias, "CI_lower"=conf.mat[,1], "CI_upper"=conf.mat[,2])

summary.frame

         mean     median          sd          bias    CI_lower   CI_upper
1   1.2997903  1.3013079  0.39673804 -0.0712912250  0.51960031 -1.3066351
2   0.2526688  0.2485721  0.03207790 -0.0034460509  2.06049015 -0.4379761
3  -1.5661686 -1.5571989  0.26220013 -0.0509238534  0.19897759  0.1449354
4   0.2004957  0.1986454  0.01949437  0.0049018955  0.32294130  0.2140118
5   0.9543834  0.9252035  0.48914523  0.0753405152 -2.12899783  0.4414957
6   0.2702361  0.2688095  0.02042504  0.0009582592 -1.09195998  8.0471143
7  -0.8996836 -0.9082379  0.22173921  0.0856792712  0.16744198  0.5811394
8   0.1788934  0.1781161  0.01667250  0.0029512565  0.24179778 57.6416680
9   2.0682618  1.7718903  1.59102322  0.4654897865  0.03492612 -8.4344038
10  4.0208757  0.8269775 13.23433539  3.1845709680  1.90245108 -1.1156127
11 -2.0969405 -1.6717102  1.56310762 -0.4306843771  0.23271566  0.3363101
12  3.8660345  0.6434859 13.27525033  3.1870641941  0.31370593 57.6061993
#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------
set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# Quantile confidence intervals
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T),ncol=2)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")

 #-----------------------------------------------------------------------------
# Set up summary data frame
#-----------------------------------------------------------------------------

summary.frame <- data.frame(mean=boot.means, median=boot.medians, sd=boot.sds, bias=boot.bias, "CI_lower"=conf.mat[,1], "CI_upper"=conf.mat[,2])

summary.frame

         mean     median          sd          bias    CI_lower   CI_upper
1   1.2997903  1.3013079  0.39673804 -0.0712912250  0.51960031 -1.3066351
2   0.2526688  0.2485721  0.03207790 -0.0034460509  2.06049015 -0.4379761
3  -1.5661686 -1.5571989  0.26220013 -0.0509238534  0.19897759  0.1449354
4   0.2004957  0.1986454  0.01949437  0.0049018955  0.32294130  0.2140118
5   0.9543834  0.9252035  0.48914523  0.0753405152 -2.12899783  0.4414957
6   0.2702361  0.2688095  0.02042504  0.0009582592 -1.09195998  8.0471143
7  -0.8996836 -0.9082379  0.22173921  0.0856792712  0.16744198  0.5811394
8   0.1788934  0.1781161  0.01667250  0.0029512565  0.24179778 57.6416680
9   2.0682618  1.7718903  1.59102322  0.4654897865  0.03492612 -8.4344038
10  4.0208757  0.8269775 13.23433539  3.1845709680  1.90245108 -1.1156127
11 -2.0969405 -1.6717102  1.56310762 -0.4306843771  0.23271566  0.3363101
12  3.8660345  0.6434859 13.27525033  3.1870641941  0.31370593 57.6061993
#-----------------------------------------------------------------------------
# Load packages
#-----------------------------------------------------------------------------

require(ggplot2)
require(pscl)
require(MASS)
require(boot)

#-----------------------------------------------------------------------------
# Load data
#-----------------------------------------------------------------------------

zinb <- read.csv("http://www.ats.ucla.edu/stat/data/fish.csv")
zinb <- within(zinb, {
  nofish <- factor(nofish)
  livebait <- factor(livebait)
  camper <- factor(camper)
})

#-----------------------------------------------------------------------------
# Calculate zero-inflated regression
#-----------------------------------------------------------------------------

m1 <- zeroinfl(count ~ child + camper | persons, data = zinb,
               dist = "negbin", EM = TRUE)

#-----------------------------------------------------------------------------
# Store the original regression coefficients
#-----------------------------------------------------------------------------

original.estimates <- as.vector(t(do.call(rbind, coef(summary(m1)))[, 1:2]))
    
#-----------------------------------------------------------------------------
# Set the number of replications
#-----------------------------------------------------------------------------

n.sim <- 2000

#-----------------------------------------------------------------------------
# Set up a matrix to store the results
#-----------------------------------------------------------------------------

store.matrix <- matrix(NA, nrow=n.sim, ncol=12)

#-----------------------------------------------------------------------------
# The loop
#-----------------------------------------------------------------------------
set.seed(123)

for(i in 1:n.sim) {
  
  #-----------------------------------------------------------------------------
  # Draw the observations WITH replacement
  #-----------------------------------------------------------------------------
  
  data.new <- zinb[sample(1:dim(zinb)[1], dim(zinb)[1], replace=TRUE),]
  
  #-----------------------------------------------------------------------------
  # Calculate the model with this "new" data
  #-----------------------------------------------------------------------------
  
  m <- zeroinfl(count ~ child + camper | persons,
                data = data.new, dist = "negbin",
                start = list(count = c(1.3711, -1.5152, 0.879),
                             zero = c(1.6028, -1.6663)))
  
  #-----------------------------------------------------------------------------
  # Store the results
  #-----------------------------------------------------------------------------
  
  store.matrix[i, ] <- as.vector(t(do.call(rbind, coef(summary(m)))[, 1:2]))
  
}


#-----------------------------------------------------------------------------
# Save the means, medians and SDs of the bootstrapped statistics
#-----------------------------------------------------------------------------

boot.means <- colMeans(store.matrix, na.rm=T)

boot.medians <- apply(store.matrix,2,median, na.rm=T)

boot.sds <- apply(store.matrix,2,sd, na.rm=T)

#-----------------------------------------------------------------------------
# The bootstrap bias is the difference between the mean bootstrap estimates
# and the original estimates
#-----------------------------------------------------------------------------

boot.bias <- colMeans(store.matrix, na.rm=T) - original.estimates

#-----------------------------------------------------------------------------
# Quantile confidence intervals
#-----------------------------------------------------------------------------

conf.mat <- matrix(apply(store.matrix, 2 ,quantile, c(0.025, 0.975), na.rm=T),ncol=2)
colnames(conf.mat) <- c("95%-CI Lower", "95%-CI Upper")

And here is our summary table:

#-----------------------------------------------------------------------------
# Set up summary data frame
#-----------------------------------------------------------------------------

summary.frame <- data.frame(mean=boot.means, median=boot.medians, 
sd=boot.sds, bias=boot.bias, "CI_lower"=conf.mat[,1], "CI_upper"=conf.mat[,2])

summary.frame

         mean     median          sd          bias    CI_lower   CI_upper
1   1.2997903  1.3013079  0.39673804 -0.0712912250  0.51960031 -1.3066351
2   0.2526688  0.2485721  0.03207790 -0.0034460509  2.06049015 -0.4379761
3  -1.5661686 -1.5571989  0.26220013 -0.0509238534  0.19897759  0.1449354
4   0.2004957  0.1986454  0.01949437  0.0049018955  0.32294130  0.2140118
5   0.9543834  0.9252035  0.48914523  0.0753405152 -2.12899783  0.4414957
6   0.2702361  0.2688095  0.02042504  0.0009582592 -1.09195998  8.0471143
7  -0.8996836 -0.9082379  0.22173921  0.0856792712  0.16744198  0.5811394
8   0.1788934  0.1781161  0.01667250  0.0029512565  0.24179778 57.6416680
9   2.0682618  1.7718903  1.59102322  0.4654897865  0.03492612 -8.4344038
10  4.0208757  0.8269775 13.23433539  3.1845709680  1.90245108 -1.1156127
11 -2.0969405 -1.6717102  1.56310762 -0.4306843771  0.23271566  0.3363101
12  3.8660345  0.6434859 13.27525033  3.1870641941  0.31370593 57.6061993
added 238 characters in body
Source Link
COOLSerdash
  • 31.2k
  • 10
  • 104
  • 157
Loading
added 122 characters in body
Source Link
COOLSerdash
  • 31.2k
  • 10
  • 104
  • 157
Loading
added 449 characters in body
Source Link
COOLSerdash
  • 31.2k
  • 10
  • 104
  • 157
Loading
added 449 characters in body
Source Link
COOLSerdash
  • 31.2k
  • 10
  • 104
  • 157
Loading
Source Link
COOLSerdash
  • 31.2k
  • 10
  • 104
  • 157
Loading