@@ -178,17 +178,21 @@ Sub-sample the data for speed.
178
178
Use ADVI for speed.
179
179
180
180
``` {r}
181
+ set.seed(1)
182
+
181
183
N <- nrow(obs_cens)
182
184
obs_cens <- obs_cens[complete.cases(obs_cens)]
183
185
N_complete <- nrow(obs_cens)
184
- obs_cens <- obs_cens[sample(seq_len(.N), N_complete / 10, replace = FALSE)]
186
+ N_complete / N
187
+ obs_cens <- obs_cens[sample(seq_len(.N), N_complete / 5, replace = FALSE)]
185
188
obs_prep <- as_latent_individual(obs_cens)
186
189
187
190
fit <- epidist::epidist(
188
191
data = obs_prep,
189
192
formula = brms::bf(mu ~ 1, sigma ~ 1),
190
193
family = brms::lognormal(),
191
- algorithm = "laplace"
194
+ algorithm = "laplace",
195
+ refresh = 0
192
196
)
193
197
194
198
summary(fit)
@@ -201,7 +205,8 @@ fit_sex <- epidist::epidist(
201
205
data = obs_prep,
202
206
formula = brms::bf(mu ~ 1 + sex, sigma ~ 1 + sex),
203
207
family = brms::lognormal(),
204
- algorithm = "laplace"
208
+ algorithm = "laplace",
209
+ refresh = 0
205
210
)
206
211
207
212
summary(fit_sex)
@@ -214,7 +219,8 @@ fit_sex_district <- epidist::epidist(
214
219
data = obs_prep,
215
220
formula = brms::bf(mu ~ 1 + sex + district, sigma ~ 1 + sex + district),
216
221
family = brms::lognormal(),
217
- algorithm = "laplace"
222
+ algorithm = "laplace",
223
+ refresh = 0
218
224
)
219
225
220
226
summary(fit_sex_district)
@@ -227,8 +233,16 @@ Posterior expectation of the delay distribution by covariate under no censoring
227
233
``` {r}
228
234
epred_draws <- obs_prep |>
229
235
as.data.frame() |>
236
+ modelr::data_grid(NA) |>
237
+ dplyr::mutate(obs_t = NA, pwindow_upr = NA, swindow_upr = NA) |>
230
238
add_epred_draws(fit, dpar = TRUE)
231
239
240
+ epred_draws |>
241
+ ggplot(aes(x = .epred)) +
242
+ tidybayes::stat_slab() +
243
+ labs(x = "Posterior expectation of the delay", y = "") +
244
+ theme_minimal()
245
+
232
246
# We will probably want to add functionality for users to mutate these things
233
247
# to the newdata themselves
234
248
epred_draws_sex <- obs_prep |>
@@ -245,7 +259,7 @@ epred_draws_sex |>
245
259
)
246
260
) |>
247
261
ggplot(aes(x = .epred, y = sex)) +
248
- stat_slab() +
262
+ tidybayes:: stat_slab() +
249
263
labs(x = "Posterior expectation of the delay", y = "") +
250
264
theme_minimal()
251
265
@@ -255,9 +269,47 @@ epred_draws_sex_district <- obs_prep |>
255
269
dplyr::mutate(obs_t = NA, pwindow_upr = NA, swindow_upr = NA) |>
256
270
add_epred_draws(fit_sex_district, dpar = TRUE)
257
271
258
- # Some strata that don't have much data?
259
- # Some very bad priors?
272
+ epred_draws_sex_district |>
273
+ dplyr::mutate(
274
+ sex = dplyr::case_when(
275
+ sex == "F" ~ "Female",
276
+ sex == "M" ~ "Male"
277
+ )
278
+ ) |>
279
+ ggplot(aes(x = .epred, y = district)) +
280
+ tidybayes::stat_slab() +
281
+ facet_grid(. ~ sex) +
282
+ labs(x = "Posterior expectation of the delay", y = "") +
283
+ theme_minimal()
284
+
285
+ # Some strata that don't have much data? Some very bad priors?
260
286
summary(epred_draws_sex_district$.epred)
287
+
288
+ obs_prep$district |>
289
+ table()
290
+
291
+ epred_draws_sex_district |>
292
+ dplyr::filter(district %in% c("Port Loko", "Western Urban")) |>
293
+ dplyr::mutate(
294
+ sex = dplyr::case_when(
295
+ sex == "F" ~ "Female",
296
+ sex == "M" ~ "Male"
297
+ )
298
+ ) |>
299
+ ggplot(aes(x = .epred, y = district)) +
300
+ tidybayes::stat_slab() +
301
+ facet_grid(. ~ sex) +
302
+ labs(x = "Posterior expectation of the delay", y = "") +
303
+ theme_minimal()
304
+
305
+ epred_draws_sex_district |>
306
+ dplyr::group_by(district) |>
307
+ dplyr::summarise(q90 = quantile(.epred, 0.9)) |>
308
+ ggplot(aes(x = district, y = q90)) +
309
+ geom_point()
310
+
311
+ obs_prep$district |>
312
+ table()
261
313
```
262
314
263
315
## Posterior predictions
0 commit comments