-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
8.1_FakeDataSimulation.R
66 lines (57 loc) · 2.04 KB
/
8.1_FakeDataSimulation.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
library(rstan)
## Fake-data simulation
a <- 1.4
b <- 2.3
sigma <- 0.9
x <- 1:5
n <- length(x)
# Simulate data, fit the model, and check the coverage of the conf intervals
y <- a + b*x + rnorm (n, 0, sigma)
# (y_x.stan)
# lm(y ~ x)
dataList.1 <- list(N=length(y), y=y, x=x)
y_x.sf1 <- stan(file='y_x.stan', data=dataList.1, iter=1000, chains=4)
print(y_x.sf1)
post <- extract(y_x.sf1)
b.hat <- colMeans(post$beta)[2] # "b" is the 2nd coef in the model
b.se <- sd(post$beta[,2]) / sqrt(4000) # "b" is the 2nd coef in the model
cover.68 <- abs (b - b.hat) < b.se # this will be TRUE or FALSE
cover.95 <- abs (b - b.hat) < 2*b.se # this will be TRUE or FALSE
cat (paste ("68% coverage: ", cover.68, "\n"))
cat (paste ("95% coverage: ", cover.95, "\n"))
# Put it in a loop
n.fake <- 1000
cover.68 <- rep (NA, n.fake)
cover.95 <- rep (NA, n.fake)
for (s in 1:n.fake){
y <- a + b*x + rnorm (n, 0, sigma)
dataList.1 <- list(N=length(y), y=y, x=x)
y_x.sf1 <- sampling(y_x.sm, dataList.1)
print(y_x.sf1)
post <- extract(y_x.sf1)
b.hat <- colMeans(post$beta)[2] # "b" is the 2nd coef in the model
b.se <- sd(post$beta[,2]) / sqrt(4000) # "b" is the 2nd coef in the model
cover.68[s] <- abs (b - b.hat) < b.se
cover.95[s] <- abs (b - b.hat) < 2*b.se
}
cat (paste ("68% coverage: ", mean(cover.68), "\n"))
cat (paste ("95% coverage: ", mean(cover.95), "\n"))
# Do it again, this time using t intervals
n.fake <- 1000
cover.68 <- rep (NA, n.fake)
cover.95 <- rep (NA, n.fake)
t.68 <- qt (.84, n-2)
t.95 <- qt (.975, n-2)
for (s in 1:n.fake){
y <- a + b*x + rnorm (n, 0, sigma)
dataList.1 <- list(N=length(y), y=y, x=x)
y_x.sf1 <- sampling(y_x.sm, dataList.1)
print(y_x.sf1)
post <- extract(y_x.sf1)
b.hat <- colMeans(post$beta)[2] # "b" is the 2nd coef in the model
b.se <- sd(post$beta[,2]) / sqrt(4000) # "b" is the 2nd coef in the model
cover.68[s] <- abs (b - b.hat) < t.68*b.se
cover.95[s] <- abs (b - b.hat) < t.95*b.se
}
cat (paste ("68% coverage: ", mean(cover.68), "\n"))
cat (paste ("95% coverage: ", mean(cover.95), "\n"))