```
# NOT RUN {
data(Verizon)
ILEC <- with(Verizon, Time[Group == "ILEC"])
CLEC <- with(Verizon, Time[Group == "CLEC"])
#### Sections in this set of examples
### Different ways to specify the data and statistic
### Example with plots and confidence intervals.
### Different ways to specify the data and statistic
# This code is flexible; there are different ways to call it,
# depending on how the data are stored and on the statistic.
## One-sample Bootstrap
# }
# NOT RUN {
# Ordinary vector, give statistic as a function
bootstrap(CLEC, mean)
# Vector by name, give statistic as an expression
bootstrap(CLEC, mean(CLEC))
# Vector created by an expression, use the name 'data'
bootstrap(with(Verizon, Time[Group == "CLEC"]), mean(data))
# A column in a data frame; use the name of the column
temp <- data.frame(foo = CLEC)
bootstrap(temp, mean(foo))
# Put function arguments into an expression
bootstrap(CLEC, mean(CLEC, trim = .25))
# Put function arguments into a separate list
bootstrap(CLEC, mean, args.stat = list(trim = .25))
# }
# NOT RUN {
# }
# NOT RUN {
## One-sample jackknife
# Like bootstrap. E.g.
jackknife(CLEC, mean)
## One-sample permutation test
# To test H0: two variables are independent, exactly
# one of them just be permuted. For the CLEC data,
# we'll create an artificial variable.
CLEC2 <- data.frame(Time = CLEC, index = 1:length(CLEC))
# }
# NOT RUN {
permutationTest(CLEC2, cor(Time, index),
resampleColumns = "index")
# Could permute "Time" instead.
# resampleColumns not needed for variables outside 'data'
permutationTest(CLEC, cor(CLEC, 1:length(CLEC)))
# }
# NOT RUN {
# }
# NOT RUN {
### Two-sample problems
## Different ways to specify data and statistic
## Two-sample bootstrap
# Two data objects (one for each group)
# }
# NOT RUN {
bootstrap2(CLEC, data2 = ILEC, mean)
# }
# NOT RUN {
# data frame containing y variable(s) and a treatment variable
# }
# NOT RUN {
bootstrap2(Verizon, mean(Time), treatment = Group)
# }
# NOT RUN {
# treatment variable as a separate object
temp <- Verizon$Group
# }
# NOT RUN {
bootstrap2(Verizon$Time, mean, treatment = temp)
# }
# NOT RUN {
## Two-sample permutation test
# Like bootstrap2. E.g.
# }
# NOT RUN {
permutationTest2(CLEC, data2 = ILEC, mean
# }
# NOT RUN {
### Example with plots and confidence intervals.
# }
# NOT RUN {
boot <- bootstrap2(CLEC, data2 = ILEC, mean)
perm <- permutationTest2(CLEC, data2 = ILEC, mean,
alternative = "greater")
# }
# NOT RUN {
par(mfrow = c(2,2))
hist(boot)
qqnorm(boot)
qqline(boot$replicates)
hist(perm)
# }
# NOT RUN {
# P-value
perm
# Standard error, and bias estimate
boot
# Confidence intervals
CI.percentile(boot) # Percentile interval
CI.t(boot) # t interval using bootstrap SE
# CI.bootstrapT and CI.bca do't currently support two-sample problems.
# Statistic can be multivariate.
# For the bootstrap2, it must have the estimate first, and a standard
# error second (don't need to divide by sqrt(n), that cancels out).
bootC <- bootstrap(CLEC, mean, seed = 0)
bootC2 <- bootstrap(CLEC, c(mean = mean(CLEC), sd = sd(CLEC)), seed = 0)
identical(bootC$replicates[, 1], bootC2$replicates[, 1])
CI.percentile(bootC)
CI.t(bootC)
CI.bca(bootC)
CI.bootstrapT(bootC2)
# The bootstrapT is the most accurate for skewed data, especially
# for small samples.
# By default the percentile interval is "expanded", for better coverage
# in small samples. To turn this off:
CI.percentile(bootC, expand = FALSE)
# }
```

Run the code above in your browser using DataCamp Workspace