-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForLoopApplyBootstrap_example.R
132 lines (83 loc) · 3.5 KB
/
ForLoopApplyBootstrap_example.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# Bootstrap mean of petalL
# get a sample with replacement
petalL <- iris[iris$Species=="setosa","Petal.Length"]
sample( petalL, replace=T )
mean( sample( petalL, replace=T ) )
allmeans <- vector("list", 100)
for ( i in 1:100) {
allmeans[i] <- mean( sample( petalL, replace=T ) )
}
head(allmeans)
allmeans <- vector(length=100)
for ( i in 1:100) {
allmeans[i] <- mean( sample( petalL, replace=T ) )
}
## baby steps: take a sample, get the mean
x <- sample(petalL, replace=T)
mean(x)
## use lapply to get 100 samples
lapply( 1:100, sample, x=petalL, replace=T)
samples <- lapply (1:100, sample, x=petalL, replace=T)
## Does this work? What is the problem?
# mean(samples)
## samples is a list, so use lapply to get means of each sample. This works:
lapply (samples, mean)
## So save it:
allmeans <- lapply(samples, mean)
## We need two sequential lapply statements, not too bad:
samples <- lapply (1:100, sample, x=petalL, replace=T)
allmeans <- lapply(samples, mean)
head(allmeans)
## Another way is to define the function within lapply:
## Note here that x is just a dummy variable. It takes on a single value of the input list, but we donʻt actually use it
lapply ( 1:100, function(x) { mean(sample( petalL, replace=T))})
## Now try sapply on any/all of the above:
sapply ( 1:100, function(x) { mean(sample( petalL, replace=T))})
## see what they mean about "pretty" output? sapply returns a vector or a matrix if it can
## Back to our confidence interval example:
allmeans <- sapply ( 1:100, function(x) { mean(sample( petalL, replace=T))})
hist(allmeans)
## This is a bit blocky, so letʻs plot more bins:
hist(allmeans, breaks=20)
## The 95% confidence interval is where 95% of the sampled means falls. So if we sort them and draw a line at the 2.5 percentile, and the 97.5 percentile, weʻll see the 95%CI
ss <- sort(allmeans)
ss[3] ## lower 2.5% only 100, so round to 3
ss[97] ## upper 97.5%, round to 97
## you could do more samples to cleanly separate 2.5 and 97.5%
## Plot with bells and whistles
## Plot the true mean of petalL in red, CI in blue
## Add labels to plot using text
## Lines with abline
## Arrows to indicate 95% CI
hist(allmeans, breaks=20)
abline( v=mean(petalL), col="red", lwd=3)
abline( v=ss[3], col="blue", lwd=3)
abline( v=ss[97], col="blue", lwd=3)
text(mean(petalL), 10, "True Mean", pos=4, col="red", cex=1.5)
text(ss[3], 10, "Lower 2.5%", pos=4, col="blue")
text(ss[97], 10, "Upper 97.5%", pos=4, col="blue")
arrows(ss[3], 9.75, ss[97], 9.75, lty=2, code=3, col="blue")
text(mean(petalL), 9.5, "95% CI", col="blue")
## The mean is mean(petalL), and the 95% CI is (ss[3], ss[97])
## We can also plot the "density" of the means using a
## kernal density method assuming Gaussian kernal densities
## (Normal)
density(allmeans)
plot(density(allmeans))
d <- density(allmeans)
## what is the object returned? Check using structure function:
str(d)
## it is a list with 7 elements, y is the probability density. To get the maximum height to put our labels on the graph:
d <- density(allmeans)
maxd <- max(d[["y"]])
plot(d, ylim=c(0, maxd+1))
## Bells and whistles:
plot(d, ylim=c(0, maxd+1))
abline( v=mean(petalL), col="red", lwd=3)
abline( v=ss[3], col="blue", lwd=3)
abline( v=ss[97], col="blue", lwd=3)
text(mean(petalL), maxd+1.25, " True Mean", col="red", cex=1.5)
text(ss[3], maxd+1, "Lower 2.5%", pos=2, col="blue")
text(ss[97], maxd+1, "Upper 97.5%", pos=4, col="blue")
arrows(ss[3], maxd+.75, ss[97], maxd+.75, lty=2, code=3, col="blue")
text(mean(petalL), maxd+.25, "95% CI", col="blue")