/
Ch5.R
285 lines (215 loc) · 9.15 KB
/
Ch5.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
##############################################################################
######################## Translating Statistics R Code #######################
##############################################################################
##### Note - to run code in R either copy and paste into the R Console window
##### or place cursor anywhere on the line, hold Ctrl and press r
##### Code may require datasets loaded from previous Chapter scripts
##############################################################################
###################### Chapter 5 - Measuring Uncertainty #####################
##############################################################################
##### Confidence Intervals
j = rnorm(100000, mean = 0, sd = 1)
dj = density(j)
q2l = quantile(j, 0.05); q1l = quantile(j, 0.1); q2u = quantile(j, 0.95)
q2l; q1l; q2u
jdata = with(dj, data.frame(x,y))
k = qplot(x, y, data = jdata, geom = "line") + theme_bw() +
xlab("") + ylab("Density\n") +
ggtitle("Division of Risk for Two-Sided
Interval\n") +
scale_x_continuous(limits = c(-4,4), breaks = seq(-4,4, by = 1)) +
scale_y_continuous(limits = c(-0.025,0.41)) +
geom_ribbon(data = subset(data2, x < q2l), aes(ymax = y),
ymin = 0, fill = "firebrick3", colour = NA, alpha = 0.5) +
geom_ribbon(data = subset(data2, x > q2u), aes(ymax = y),
ymin = 0, fill = "firebrick3", colour = NA, alpha = 0.5) +
annotate("text", x = -2.8, y = -0.018, label = "5%") +
# Replace xend = -1.639917 to the value of q2l
geom_segment(aes(x = -4, y = -0.008, xend = -1.639917,
colour = "firebrick3", yend = -0.008)) +
annotate("text", x = 2.8, y = -0.018, label = "5%") +
# Replace x = 1.644374 to the value of q2u
geom_segment(aes(x = 1.644374, y = -0.008, xend = 4,
yend = -0.008), colour = "firebrick3") +
theme(legend.position = "none")
l = qplot(x, y, data = jdata, geom = "line") + theme_bw() +
xlab("") + ylab("Density\n") +
ggtitle("Division of Risk for One-Sided
Interval\n") +
scale_x_continuous(limits = c(-4,4), breaks = seq(-4,4, by = 1)) +
scale_y_continuous(limits = c(-0.025,0.41)) +
geom_ribbon(data = subset(data2, x < q1l), aes(ymax = y),
ymin = 0, fill = "forestgreen", colour = NA, alpha = 0.5) +
annotate("text", x = -2.5, y = -0.018, label = "10%") +
# Replace x = -1.275591 to the value of q1l
geom_segment(aes(x = -1.275591, y = -0.008, xend = -4,
yend = -0.008), colour = "forestgreen") +
theme(legend.position = "none")
windows(8,6)
grid.arrange(k, l, nrow = 1, ncol = 2)
##### Example 5.1 - Continuous CIs
data7 = c(26.33, 27.31, 27.38, 26.63, 26.87, 26.67, 28.36, 28.52,
26.91, 28.90, 27.99, 27.17, 28.32, 26.93, 26.93, 26.65,
27.73, 26.93)
x = mean(data7); s = sd(data7); n = length(data7); c = 0.90
x; s; n; c
se = s/sqrt(n)
t2 = qt(c + (1 - c)/2, df = n - 1)
error2 = se*t2; error2
t1 = qt(c, df = n - 1)
error1 = se*t1; error1
lower.2s.CI = x - error2; upper.2s.CI = x + error2
lower.1s.CI = x - error1
lower.2s.CI; upper.2s.CI
lower.1s.CI
library(Rmisc)
CI(data7, ci = 0.90)
CI(data7, ci = 0.80)
##### Example 5.2 - Continuous CIs
CI(data7, ci = 0.70)
data8 = c(data7, 26.01, 28.33, 26.62, 26.99, 27.48, 27.74, 27.89)
CI(data8, ci = 0.90)
##### Example 5.3 - Transformed CIs
data9 = c(9.2, 7.4, 10.7, 3.6, 4.3, 3.2, 14.2, 30.1, 15.7, 6.8, 8.9,
9.1, 8.2, 7.5, 7.4, 14.9, 19.7, 26.3, 6.4, 14.2, 8.3, 6.9,
8.5, 11.5, 22.7, 16.9, 31.4, 10.7, 17.9, 10.0)
data10 = log10(data9)
qqnorm(data10); qqline(data10)
ci = CI(data10, ci = 0.95); ci
10^ci
CI(data9, ci = 0.95)
##### Example 5.4 - Binary CIs
x = 20; n = 25; alpha = 0.10
x/n
df1l = 2*(n - x + 1); df2l = 2*x
df1u = df2l + 2; df2u = df1l - 2
lci = ifelse(x > 0, x / (x + qf(1 - alpha/2, df1l, df2l) *
(n - x + 1)), 0)
uci = ifelse(x < n, ((x + 1) * qf(1 - alpha/2, df1u, df2u)) /
(n - x + (x + 1) * qf(1 - alpha/2, df1u, df2u)), 1)
lci; uci
uci1 = ifelse(x < n, ((x + 1) * qf(1 - alpha, df1u, df2u)) /
(n - x + (x + 1) * qf(1 - alpha, df1u, df2u)), 1)
uci1
library(Hmisc)
binconf(x = 20, n = 25, alpha = 0.1, method = "exact")
binconf(x = 20, n = 25, alpha = 0.2, method = "exact")
##### Example 5.5 - Binary CIs
binconf(x = 20, n = 25, alpha = 0.3, method = "exact")
binconf(x = 4, n = 5, alpha = 0.1, method = "exact")
##### Example 5.6 - Continuous TIs
x = mean(data7); s = sd(data7); n = length(data7); P = 0.75
conf = 0.9; x; s; n; P; conf
n2 = (n - 1)*(1 + 1/n)
ncrit = (qnorm((1 - P)/2))^2
ccrit = qchisq(1 - conf, n - 1)
k2 = sqrt((n2*ncrit)/ccrit); k2
lower.2s.TI = x - k2*s
upper.2s.TI = x + k2*s
lower.2s.TI; upper.2s.TI
ncritcov = qnorm(P)
ncp = sqrt(n) * ncritcov
tcrit = qt(conf, df = n - 1, ncp = ncp)
k1 = tcrit/sqrt(n); k1
lower.1s.TI = x - k1*s; lower.1s.TI
library(tolerance)
normtol.int(data7, alpha = 0.1, P = 0.75, side = 2, method = "HE2")
normtol.int(data7, alpha = 0.1, P = 0.75, side = 1)
##### Example 5.7 - Continuous TIs
normtol.int(data7, alpha = 0.3, P = 0.75, side = 2, method = "HE2")
normtol.int(data7, alpha = 0.1, P = 0.95, side = 2, method = "HE2")
##### Example 5.8 - Binary TIs
x = 20; n = 25; P = 0.75; alpha = 0.10
alpha = alpha/2; P = (P + 1)/2
lower.p = (1 + ((n - x + 1) * qf(1 - alpha, df1 = 2 * (n - x + 1),
df2 = (2 * x)))/x)^(-1)
upper.p = (1 + (n - x)/((x + 1) * qf(1 - alpha, df1 = 2 * (x + 1),
df2 = 2 * (n - x))))^(-1)
lower.p = max(0, lower.p); upper.p = min(upper.p, 1)
lower = qbinom(1 - P, size = n, prob = lower.p)
upper = qbinom(P, size = n, prob = upper.p)
lower; upper
x = 20; n = 25; P = 0.75; alpha = 0.10
lower.p = (1 + ((n - x + 1) * qf(1 - alpha, df1 = 2 * (n - x + 1),
df2 = (2 * x)))/x)^(-1)
lower.p = max(0, lower.p)
lower = qbinom(1 - P, size = n, prob = lower.p)
lower
bintol.int(x = 20, n = 25, P = 0.75, alpha = 0.1, side = 2,
method = "CP")
bintol.int(x = 20, n = 25, P = 0.75, alpha = 0.1, side = 1,
method = "CP")
##### Example 5.9 - Binary TIs
bintol.int(x = 20, n = 25, P = 0.75, alpha = 0.3, side = 2,
method = "CP")
bintol.int(x = 20, n = 25, P = 0.95, alpha = 0.1, side = 2,
method = "CP")
##### CI, TI, PI
Conc = c(3.9, 3.8, 3.6, 4.2, 5.7, 5, 5.5, 3.7, 4.9, 4, 6, 5)
Yield = c(498, 480.3, 476.4, 546, 715.4, 666, 741.2, 522, 683.6, 574,
804, 637)
m = data.frame(Conc, Yield)
mod = lm(Yield ~ Conc, data = m)
newdata = data.frame(Conc = c(3, 3.6, 3.7, 3.8, 3.9, 4, 4.2, 4.5, 4.9,
5, 5, 5.5, 5.7, 6, 8))
pi = data.frame(predict(mod, newdata, interval = "prediction"))
ci = data.frame(predict(mod, newdata, interval = "confidence"))
ti = data.frame(regtol.int(mod, alpha = 0.05, P = 0.95, side = 2,
new.x = cbind(c(3, 4.5, 8))))
x = newdata
127.8*3; 127.8*4.5; 127.8*8
# Yield ordered plus the three new values
y = c(383.4, 476.4, 522, 480.3, 498, 574, 546, 575.1, 683.6, 666,
637, 741.2, 715.4, 804, 1022.4)
lci = ci$lwr; uci = ci$upr; lpi = pi$lwr; upi = pi$upr
lti = ti$X2.sided.lower; uti = ti$X2.sided.upper
n = data.frame(x, y, lci, uci, lti, uti, lpi, upi); n
ggplot(n, aes(x = x, y = y)) + theme_bw() +
geom_abline(intercept = 23.2, slope = 127.8,
colour = "dodgerblue3", size = 1.5) +
geom_line(aes(x = x, y = lci), n, colour = "firebrick3",
size = 1) +
geom_line(aes(x = x, y = uci), n, colour = "firebrick3",
size = 1) +
geom_line(aes(x = x, y = lti), n, colour = "forestgreen",
size = 1) +
geom_line(aes(x = x, y = uti), n, colour = "forestgreen",
size = 1) +
geom_line(aes(x = x, y = lpi), n, colour = "orange", size = 1) +
geom_line(aes(x = x, y = upi), n, colour = "orange", size = 1) +
scale_x_continuous(limits = c(3,8.1), breaks = seq(3,8, by = 1)) +
scale_y_continuous(limits = c(275,1200), breaks = seq(300,1200,
by = 100)) + xlab("Concentration") + ylab("Yield") +
ggtitle("Yield by Concentration with Intervals") +
theme(plot.title = element_text(size = 16, face = "bold"),
strip.text = element_text(size = 12, face = "bold"),
axis.text.x = element_text(size = 10, face = "bold.italic"),
axis.text.y = element_text(size = 9, face = "bold"),
axis.title = element_text(size = 12, face = "bold")) +
geom_point(size = 2) +
geom_segment(aes(x = 5.5, y = 525, xend = 5.75,
yend = 525), col = "dodgerblue3", size = 2) +
annotate("text", x = 7, y = 525, cex = 4.25,
label = "Linear Model") +
geom_segment(aes(x = 5.5, y = 475, xend = 5.75,
yend = 475), col = "firebrick3", size = 2) +
annotate("text", x = 7, y = 475, cex = 4.25,
label = "95% Confidence Intervals") +
geom_segment(aes(x = 5.5, y = 400, xend = 5.75,
yend = 400), col = "orange", size = 2) +
annotate("text", x = 7, y = 400, cex = 4.25,
label = "95% Confidence
Prediction Intervals") +
geom_segment(aes(x = 5.5, y = 315, xend = 5.75,
yend = 315), col = "forestgreen", size = 2) +
annotate("text", x = 7, y = 315, cex = 4.25,
label = "95% Confidence, 95% Coverage
Tolerance Intervals") +
geom_segment(aes(x = 5.4, y = 275, xend = 5.4,
yend = 540), size = 1, lty = 2) +
geom_segment(aes(x = 5.4, y = 275, xend = 8.09,
yend = 275), size = 1, lty = 2) +
geom_segment(aes(x = 8.09, y = 275, xend = 8.09,
yend = 540), size = 1, lty = 2) +
geom_segment(aes(x = 5.4, y = 540, xend = 8.09,
yend = 540), size = 1, lty = 2)