> # A random sample of size n=20 from exponential distribution with mean 1
> # is generated. Confidence intervals are then calculated.
> n <- 20 # sample size
> mean(eg12.80 <- rexp(n)) # Generate a random sample and calculate its mean
[1] 1.108687
> # Usual Confidence Interval
> 2*n*mean(eg12.80)/qchisq(c(.025,.975),2*n,lower=F)
[1] 0.7473239 1.8150618
> # Bootstrap Confidence Intervals
> eg12.80.boot <- bootInterval(eg12.80)
> # Check normality of bootstrap sample.
> # Normal quantile plot is generated. The argument print=T
> # asks normality test outcomes be printed too.
> check(eg12.80.boot, print=T)
Shapiro-Wilk normality test data: Bootstrap Sample W = 0.9971, p-value = 0.07182
The plot is shown below (also available PDF & PS versions):
> # Now produce summary of the result. The second argument, by default,
> # asks bootstrap percentile confidence interval be printed. Here
> # "both" is used to indicate that bootstrap confidence t interval
> # be printed too.
> summary(eg12.80.boot, "both")
One-Sample Bootstrap Confidence Interval ======================================== Data: eg12.80 Parameter of interest: mu Bootstrap estimate: 1.107818 Bootstrap standard error: 0.2555253 2.5 % 97.5 % Bootstrap percentile c.i.: 0.63702 1.607642 2.5 % 97.5 % Bootstrap t c.i.: 0.5729974 1.642639 Note: Bootstrap size = 1000
> # Note that the bootstrap may not be normally distributed
> # as seen in the example below.
> check(bootInterval(eg12.80), print=TRUE)
Shapiro-Wilk normality test data: Bootstrap Sample W = 0.9941, p-value = 0.0005372
> # To request for different bootstrap size, use B=2000 for example
> check(x <- bootInterval(eg12.80, B=2000), plot=F)
Shapiro-Wilk normality test data: Bootstrap Sample W = 0.9935, p-value = 1.071e-07
> summary(x)
One-Sample Bootstrap Confidence Interval ======================================== Data: eg12.80 Parameter of interest: mu Bootstrap estimate: 1.107353 Bootstrap standard error: 0.2704598 2.5 % 97.5 % Bootstrap percentile c.i.: 0.6232117 1.693989 Note: Bootstrap size = 2000
> # To request for other confidence level, use conf.level=0.90, for example.
> check(x <- bootInterval(eg12.80, conf.level=0.90), plot=F)
Shapiro-Wilk normality test data: Bootstrap Sample W = 0.996, p-value = 0.01145
> summary(x)
One-Sample Bootstrap Confidence Interval ======================================== Data: eg12.80 Parameter of interest: mu Bootstrap estimate: 1.080947 Bootstrap standard error: 0.2622555 5 % 95 % Bootstrap percentile c.i.: 0.6644416 1.520192 Note: Bootstrap size = 1000