-
Notifications
You must be signed in to change notification settings - Fork 3
/
lecture_week_12.R
256 lines (191 loc) · 5.92 KB
/
lecture_week_12.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
library(rethinking)
library(dplyr)
#==========================================================
# Import and prep the country data
data(rugged)
d <- rugged
# Make log version of outcome (GDP in year 2000)
d$log_gdp <- log(d$rgdppc_2000)
# Extract countries with GDP data
dd <- d[complete.cases(d$rgdppc_2000), ]
#==========================================================
# Fit a multiple regression with an interaction term
m8.1 <- map(
data = dd,
alist(
log_gdp ~ dnorm(mu, sigma),
mu <- a + bA*cont_africa + bR*rugged + bAR*rugged*cont_africa,
a ~ dnorm(0, 100),
bA ~ dnorm(0, 10),
bR ~ dnorm(0, 10),
bAR ~ dnorm(0, 10),
sigma ~ dunif(0, 10)
)
)
# Fit a multiple regression with an interaction term
# using map2stan()
# Trim data to ensure we don't run into any Stan issues
dd.trim <- dd %>%
select(log_gdp, rugged, cont_africa)
# Fit the model using map2stan()
m8.1stan <- map2stan(
data = dd.trim,
alist(
log_gdp ~ dnorm(mu, sigma),
mu <- a + bA*cont_africa + bR*rugged + bAR*rugged*cont_africa,
a ~ dnorm(0, 100),
bA ~ dnorm(0, 10),
bR ~ dnorm(0, 10),
bAR ~ dnorm(0, 10),
sigma ~ dcauchy(0, 2)
)
)
# Compare model fits using precis()
precis(m8.1, prob = 0.97)
precis(m8.1stan, prob = 0.97)
# Other Stan fit model summary functions
show(m8.1stan)
summary(m8.1stan)
# To extract the actual samples embedded in the fit model
# object...
e8.1 <- extract.samples(m8.1stan)
summary(e8.1)
#==========================================================
# Plot parameter trace plots
# Using the rethinking plotting method
plot(m8.1stan)
plot(m8.1stan, window = c(1000, 2000)) # modify x-axis
# Using the rstan plotting method
rstan::traceplot(m8.1stan@stanfit,
pars = c("a", "bR", "bA", "bAR", "sigma"))
#==========================================================
# Reminders about the Poisson distribution
# To generate random samples from the Poisson:
rpois(n = 10, lambda = 5)
# Lambda can be any positive number:
rpois(n = 10, lambda = 0.536787)
# Or 0:
rpois(n = 10, lambda = 0)
# But it can't be negative:
rpois(n = 10, lambda = -1)
# The Poisson probability mass function:
dpois(0, lambda = 5)
dpois(3, lambda = 5)
dpois(5, lambda = 5)
dpois(20, lambda = 5)
dpois(100, lambda = 5)
#==========================================================
# Fitting a Poisson GLM
# Import the Kline dataset
data(Kline)
d <- Kline
d
# Generate modified predictor variables
d$log_pop <- log(d$population)
d$contact_high <- ifelse(d$contact == "high", 1, 0)
d
# Fit a Poisson GLM with the effects of log population
# size, contact rate, and their interaction
m10.10 <- map(
data = d,
alist(
total_tools ~ dpois(lambda),
log(lambda) <- a + bp*log_pop + bc*contact_high + bpc*log_pop*contact_high,
a ~ dnorm(0, 100),
bp ~ dnorm(0, 1),
bc ~ dnorm(0, 1),
bpc ~ dnorm(0, 1)
)
)
precis(m10.10, prob = 0.97)
#==========================================================
# Generate model-based predictions (the manual way)
# Get 5,000 posterior samples from the fit model
post <- extract.samples(m10.10, n = 5000)
# Generate expected lambda values for an island with a
# log population size of 8 and high contact rate by
# applying the inverse link function to the linear model
# for lambda
lambda.high <-
exp(post$a + post$bp*8 + post$bc*1 + post$bpc*8*1)
# Generate expected lambda values for an island with a
# log population size of 8 and low contact rate by
# applying the inverse link function to the linear model
# for lambda
lambda.low <-
exp(post$a + post$bp*8 + post$bc*0 + post$bpc*8*0)
# Remember, these represent the expected values
# for lambda, NOT the actual counts
head(lambda.high)
head(lambda.low)
# To get count predictions, we need to feed these
# lambda values through rpois()
preds.high <- rpois(n = 5000, lambda.high)
preds.low <- rpois(n = 5000, lambda.low)
head(preds.high)
head(preds.low)
# Or we could generate the same predictions by using the
# convenience function sim(), feeding it counterfactual
# data
# Generate counterfactual data for high and low contact
# islands
counterfactual.high <-
data.frame(log_pop = 8, contact_high = 1)
counterfactual.low <-
data.frame(log_pop = 8, contact_high = 0)
# Generate predictions using sim()
preds.high.sim <-
sim(m10.10, n = 5000, data = counterfactual.high)
preds.low.sim <-
sim(m10.10, n = 5000, data = counterfactual.low)
head(preds.high.sim)
head(preds.low.sim)
# Visualize the predictions
par(mfrow = c(2, 2))
simplehist(preds.high,
xlab = "", ylab = "",
xlim = c(0, 60), ylim = c(0, 500),
col = "red",
main = "High contact preds, manual")
simplehist(preds.high.sim,
xlab = "", ylab = "",
xlim = c(0, 60), ylim = c(0, 500),
col = alpha("red", 0.5),
main = "High contact preds, sim")
simplehist(preds.low,
xlab = "", ylab = "",
xlim = c(0, 60), ylim = c(0, 500),
col = "blue",
main = "Low contact preds, manual")
simplehist(preds.low.sim,
xlab = "", ylab = "",
xlim = c(0, 60), ylim = c(0, 500),
col = alpha("blue", 0.5),
main = "Low contact preds, sim")
#==========================================================
# Fit the same Poisson GLM using map2stan() with
# 4 Markov chains
m10.10stan <- map2stan(
data = d,
alist(
total_tools ~ dpois(lambda),
log(lambda) <- a + bp*log_pop + bc*contact_high + bpc*log_pop*contact_high,
a ~ dnorm(0, 100),
bp ~ dnorm(0, 1),
bc ~ dnorm(0, 1),
bpc ~ dnorm(0, 1)
),
chains = 4
)
# Compare model fits using precis()
precis(m10.10, prob = 0.97)
precis(m10.10stan, prob = 0.97)
# Extract samples
e <- extract.samples(m10.10stan)
summary(e)
# Plot parameter trace plots
plot(m10.10stan)
plot(m10.10stan, window = c(500, 2000))
plot(m10.10stan, window = c(1000, 2000))
rstan::traceplot(m10.10stan@stanfit,
pars = c("a", "bp", "bc", "bpc"))