-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultiway_boot_ex.R
59 lines (47 loc) · 1.41 KB
/
multiway_boot_ex.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
source("multiway_boot.R")
# In this example, we have two grouping factors, subjects and stimuli
# Generate simulated data
N <- 1e4
N.subjects <- 100
N.stimuli <- 20
subjects.re <- rnorm(100)
subject <- rep(1:100, each = 100)
stimuli.re <- rlnorm(N.stimuli)
stimulus <- sample.int(N.stimuli, N, replace = TRUE)
# x has subject, stimulus, and subject-stimulus (error) components
x <- subjects.re[subject] + stimuli.re[stimulus] + rnorm(N)
# point estimate of mean
mean(x)
library(Hmisc) # for wtd.mean
alpha <- .05
q <- c(alpha / 2, 1 - alpha / 2)
# two-way bootstrap
mb.2 <- multiway.boot(
statistic = wtd.mean,
R = 500,
groups = cbind(subject, stimulus),
.progress = 'text', # can use plyr progress indicators
x = x
)
mb.2 <- unlist(mb.2)
sd(mb.2) # bootstrap estimate of standard error of the mean
qnorm(q, mean(x), sd(mb.2)) # normal approximation
quantile(mb.2, q) # percentile bootstrap CI
# setup multicore for demonstration of parallel execution
library(foreach)
library(doMC)
registerDoMC()
# compare with (anti-conservative) one-way version
mb.1 <- multiway.boot(
statistic = wtd.mean,
R = 500,
groups = cbind(subject), # only cluster/block on subject
.parallel = TRUE, # can use plyr parallel tools
x = x
)
mb.1 <- unlist(mb.1)
sd(mb.1)
qnorm(q, mean(x), sd(mb.1))
quantile(mb.1, q)
# compare with CI based on the normal, ignoring both grouping factors
t.test(x, conf.level = 1 - alpha)$conf.int