-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.R
40 lines (27 loc) · 859 Bytes
/
bootstrap.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
library('boot')
set.seed(21)
mean_index <- function(x, index){
mean(x[index])
}
inside <- function(sampsize=100){
# Returns true if population mean is inside two sd of bootstrap
# confidence interval
normdata <- rnorm(sampsize)
b <- boot(normdata, mean_index, R=100)
two_sd <- 2 * sd(b$t)
# Simple 2 standard deviation confidence interval
low <- b$t0 - two_sd
high <- b$t0 + two_sd
return(low < 0 & 0 < high)
}
# Experiment to see how many times this confidence interval contains
# the true value of t0 = 0.
actual_inside <- replicate(1000, inside())
print(sum(actual_inside))
# Prints 951 - Very close to 95% confidence interval. Cool.
superboot = function(data, statistic, R){
# A simpler way to make this work
boot(data, function(x, ind) statistic(x[ind]), R)
}
x = rnorm(100)
superboot(x, mean, 1000)