-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statistical-Inference-Quiz.Rmd
100 lines (86 loc) · 2.32 KB
/
Statistical-Inference-Quiz.Rmd
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
## Quiz 4.
### Question 1.
alpha <- 0.05
mu <- 12
sd <- 4
n <- 100
z <- qnorm(alpha)
m0 <- mu + z * sd / sqrt(n)
m0 # 11.34206
---------------------------------------
### Question 2. T-test
pharm <- data.frame(baseline = c(140, 138, 150, 148, 135),
week2 = c(132, 135, 151, 146, 130))
t.test(pharm$baseline, pharm$week2, alternative = "two.sided", paired = TRUE)
# p-value = 0.08652
### Question 3.CI
n <- 9
mu <- 1100
sd <- 30
alpha <- .05
tstat <- qt(1 - alpha/2, n - 1)
mu + c(-1, 1)*tstat*sd / sqrt(n) # 1076.94 1123.06
---------------------------------------
### Question 4. P-value
library(stats)
binom.test(x = 3, n = 4, p = o.5, alt = "greater")
# p-value = 0.3125
---------------------------------------
### Question 5.
p <- (1 / 100)
pr <- (10 / 1787)
n <- 1787
std.err <- sqrt(p * (1-p) / n)
z <- (p-pr) / std.err
pnorm(z, lower.tail = F)
# 0.03066625
---------------------------------------
### Question 6.Two-sided T-test
n1 <- 9
n2 <- 9
df <- n1 + n2 - 2
meanTreat <- -3
meanPlacebo <- 1
sdTreat <- 1.5
sdPlacebo <- 1.8
pooledVar <- (sdTreat^2 * n1 + sdPlacebo^2 * n2)/df
se.diff <- sqrt(pooledVar/n1 + pooledVar/n2)
tstat <- (meanTreat - meanPlacebo) / se.diff
tstat
pValue <- 2 * pt(tstat, df = df)
pValue # 0.0001852248
---------------------------------------
### Question 7.
# No calculation is needed. We wouldn't reject because the 95% CI contains
# the 90% CI. µ = 1078 falls within the interval, so we don't reject H0.
---------------------------------------
### Question 8. Power
n <- 100
mu <- .01
sd <- .04
power.t.test(n, delta = mu, sd = sd, type = "one.sample", alt = "one.sided")$power # 0.7989855
---------------------------------------
### Question 9. Power
power <- .9
power.t.test(power = power, delta = mu, sd = sd, type = "one.sample", alt = "one.sided")$n # 138.3856
---------------------------------------
### Question 10.
# As you increase the type one error rate, α, or use one-sided test instead of two-sided test,
# or increase n, power will get larger.
---------------------------------------
### Question 11. P-value of two-sided Z-test
n <- 288
mu1 <- 44
mu2 <- 42.04
sd <- 12
se <- sd * sqrt(1/n + 1/n)
z <- (mu1 - mu2)/se
z
pValue <- 2 * pnorm(-abs(z))
pValue # 0.04999579
---------------------------------------
### Question 12. FWER: family wise error rate
m <- 10
alpha <- .05
alphaFwer <- alpha / m
alphaFwer