-
Notifications
You must be signed in to change notification settings - Fork 1
/
complaint_traps_hierarchical.Rmd
677 lines (522 loc) · 16.3 KB
/
complaint_traps_hierarchical.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# 物业经理的困惑 {#complaints}
```{r}
library(tidyverse)
library(tidybayes)
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
```
[蟑螂投诉的案例](https://github.com/jgabry/bayes-workflow-book),
物业经理一方面担心投诉太多降低威望,一方面担心部署太多蟑螂诱捕器需要花费太多费用
## 数据
```{r}
pest <- readRDS("./rawdata/pest_data.RDS")
pest
```
```{r}
pest %>% count(building_id, live_in_super)
```
## Hierarchical modeling
modeling varying intercepts for each building
```{r, warning=FALSE, message=FALSE}
stan_program <- "
functions {
/*
* Alternative to neg_binomial_2_log_rng() that
* avoids potential numerical problems during warmup
*/
int neg_binomial_2_log_safe_rng(real eta, real phi) {
real gamma_rate = gamma_rng(phi, phi / exp(eta));
if (gamma_rate >= exp(20.79))
return -9;
return poisson_rng(gamma_rate);
}
}
data {
int<lower=1> N;
int complaints[N];
int traps[N];
int n_building;
int building[N];
vector<lower=0,upper=1>[N] live_in_super;
vector[N] log_sq_foot;
}
parameters {
vector[n_building] alpha;
real beta;
real beta_super;
real<lower=0> inv_phi;
}
transformed parameters {
real phi = inv(inv_phi);
}
model {
vector[N] lambda;
for (i in 1:N) {
lambda[i] = alpha[building[i]] + beta * traps[i] + beta_super * live_in_super[i] + log_sq_foot[i];
}
for (i in 1:N) {
target += neg_binomial_2_log_lpmf(complaints[i] | lambda[i], phi);
}
alpha ~ normal(log(4), 1);
beta ~ normal(-0.25, 1);
beta_super ~ normal(-0.5, 1);
inv_phi ~ normal(0, 1);
}
generated quantities {
int y_rep[N];
for (n in 1:N) {
y_rep[n] = neg_binomial_2_log_safe_rng(alpha[building[n]] + beta * traps[n] + beta_super * live_in_super[n] + log_sq_foot[n], phi);
}
}
"
stan_data <- pest %>%
select(complaints, traps, building_id, live_in_super, total_sq_foot) %>%
mutate(log_sq_foot = log(total_sq_foot/1e4)) %>%
mutate(building_id = as_factor(building_id)) %>% #compose_data() convert factors into `int` for grouping
tidybayes::compose_data(
N = nrow(.),
complaints = complaints,
traps = traps,
live_in_super = live_in_super,
log_sq_foot = log_sq_foot,
n_building = n_distinct(building_id),
building = building_id
)
fit_pest_Hierarchical <- stan(model_code = stan_program, data = stan_data)
```
$$
\text{complaints}_{b,t} \sim \text{Neg-Binomial}(\lambda_{b,t}, \phi) \\
\lambda_{b,t} = \exp{(\eta_{b,t})} \\
\eta_{b,t} = \mu_b + \beta \, {\rm traps} + \text{log}\_\textrm{sq}\_\textrm{foot}\\
\mu_b \sim \text{normal}(\alpha + \texttt{building}\_\textrm{data} \, \zeta, \,\sigma_{\mu})
$$
```{r, warning=FALSE, message=FALSE}
stan_program <- "
functions {
/*
* Alternative to neg_binomial_2_log_rng() that
* avoids potential numerical problems during warmup
*/
int neg_binomial_2_log_safe_rng(real eta, real phi) {
real gamma_rate = gamma_rng(phi, phi / exp(eta));
if (gamma_rate >= exp(20.79))
return -9;
return poisson_rng(gamma_rate);
}
}
data {
int<lower=1> N;
int<lower=0> complaints[N];
int traps[N];
int<lower=1> n_building;
int<lower=1, upper=n_building> building[N];
vector[N] log_sq_foot;
matrix[n_building,4] building_data;
}
parameters {
real<lower=0> inv_phi; // 1/phi (easier to think about prior for 1/phi instead of phi)
real beta; // coefficient on traps
vector[n_building] mu; // buildings-specific intercepts
real<lower=0> sigma_mu; // sd of building-specific intercepts
real alpha; // intercept of model for mu
vector[4] zeta; // coefficients on building-level predictors in model for mu
}
transformed parameters {
real phi = inv(inv_phi);
}
model {
sigma_mu ~ normal(0, 1);
alpha ~ normal(log(4), 1);
zeta ~ normal(0, 1); // could also use informative priors on the different elements
beta ~ normal(-0.25, 1);
inv_phi ~ normal(0, 1);
//for (j in 1:n_building) {
// mu[j] ~ normal(alpha + building_data[j, ] * zeta, sigma_mu);
//}
mu ~ normal(alpha + building_data * zeta, sigma_mu);
for (i in 1:N) {
target += neg_binomial_2_log_lpmf(complaints[i] | mu[building[i]] + beta * traps[i] + log_sq_foot[i], phi);
}
}
generated quantities {
int y_rep[N];
for (n in 1:N) {
y_rep[n] = neg_binomial_2_log_safe_rng(mu[building[n]] + beta * traps[n] + log_sq_foot[n], phi);
}
}
"
```
```{r prep-data}
N_buildings <- length(unique(pest$building_id))
N_months <- length(unique(pest$date))
#
building_data <- pest %>%
mutate(
building_fac = factor(building_id, levels = unique(building_id)),
building_idx = as.integer(building_fac),
ids = rep(1:N_months, N_buildings),
month_idx = lubridate::month(date)
) %>%
select(building_idx,
live_in_super,
age_of_building,
total_sq_foot,
average_tenant_age,
monthly_average_rent) %>%
distinct() %>%
arrange(building_idx) %>%
select(-building_idx) %>%
mutate(
across(everything(), scale, scale = FALSE)
) %>%
mutate( # scale by constants
age_of_building = age_of_building / 10,
total_sq_foot = total_sq_foot / 10000, # not used
average_tenant_age = average_tenant_age / 10,
monthly_average_rent = monthly_average_rent / 1000
) %>%
as.matrix()
building_data
building_data[, -3]
```
```{r}
stan_data <- pest %>%
select(complaints, traps, building_id, total_sq_foot) %>%
mutate(log_sq_foot = log(total_sq_foot/1e4)) %>%
mutate(building_id = as_factor(building_id)) %>%
tidybayes::compose_data(
N = nrow(.),
complaints = complaints,
traps = traps,
log_sq_foot = log_sq_foot,
n_building = n_distinct(building_id),
building = building_id,
building_data = building_data[,-3],
)
fitted_model_NB_hier <- stan(model_code = stan_program, data = stan_data)
```
有效样本都很低,
```{r}
fitted_model_NB_hier %>%
print(pars = c('sigma_mu','beta','alpha','phi','mu'))
```
我们先看trace plots
```{r}
# use as.array to keep the markov chains separate for trace plots
bayesplot::mcmc_trace(as.array(fitted_model_NB_hier, pars = 'sigma_mu'),
np = nuts_params(fitted_model_NB_hier), window = c(500,1000)
)
```
Looks as if the divergent parameters, the little red bars underneath
the traceplots correspond to samples where the sampler gets stuck at
one parameter value for $\sigma_\mu$.
### want this to look more like a funnel than a cloud
What we have here is a cloud-like shape, with most of the divergences
clustering towards the bottom. We'll see a bit later that we actually
want this to look more like a funnel than a cloud.
```{r}
# assign to object so we can compare to another plot later
scatter_with_divs <- bayesplot::mcmc_scatter(as.array(fitted_model_NB_hier),
pars = c("mu[4]", 'sigma_mu'),
transform = list('sigma_mu' = "log"), np = nuts_params(fitted_model_NB_hier)
)
scatter_with_divs
```
这是我们期待的漏斗图(因为是我们模拟的)
```{r}
d_sim <- tibble(
log_sigma = rnorm(1000, mean = 0, sd = 1)
) %>%
mutate(
theta = map_dbl(log_sigma, ~ rnorm(1, mean = 0, sd = exp(.x)))
)
d_sim
d_sim %>%
ggplot(aes(x = log_sigma, y = theta)) +
geom_point()
```
```{r}
N_sims <- 1000
log_sigma <- rep(NA, N_sims)
theta <- rep(NA, N_sims)
for (j in 1:N_sims) {
log_sigma[j] <- rnorm(1, mean = 0, sd = 1)
theta[j] <- rnorm(1, mean = 0, sd = exp(log_sigma[j]))
}
draws <- cbind("mu" = theta, "log(sigma_mu)" = log_sigma)
bayesplot::mcmc_scatter(draws)
```
```{r}
parcoord_with_divs <-
bayesplot::mcmc_parcoord(as.array(fitted_model_NB_hier, pars = c("sigma_mu", "mu")),
np = nuts_params(fitted_model_NB_hier))
parcoord_with_divs
```
## hier_NB_regression_ncp
上面的分析,发现有效样本量比较低,原因是sigma_mu 太小,所以作者这里Reparameterizing,
$$
\text{complaints}_{b,t} \sim \text{Neg-Binomial}(\lambda_{b,t}, \phi) \\
\lambda_{b,t} = \exp{(\eta_{b,t})} \\
\eta_{b,t} = \mu_b + \beta \, {\rm traps} + \text{log}\_\textrm{sq}\_\textrm{foot}\\
\mu_b \sim \text{normal}(\alpha + \texttt{building}\_\textrm{data} \, \zeta, \,\sigma_{\mu})
$$
```{r, warning=FALSE, message=FALSE}
stan_program <- "
functions {
/*
* Alternative to neg_binomial_2_log_rng() that
* avoids potential numerical problems during warmup
*/
int neg_binomial_2_log_safe_rng(real eta, real phi) {
real gamma_rate = gamma_rng(phi, phi / exp(eta));
if (gamma_rate >= exp(20.79))
return -9;
return poisson_rng(gamma_rate);
}
}
data {
int<lower=1> N;
int<lower=0> complaints[N];
int traps[N];
int<lower=1> n_building;
int<lower=1, upper=n_building> building[N];
vector[N] log_sq_foot;
matrix[n_building,4] building_data;
}
parameters {
real<lower=0> inv_phi; // 1/phi (easier to think about prior for 1/phi instead of phi)
real beta; // coefficient on traps
real<lower=0> sigma_mu; // sd of building-specific intercepts
real alpha; // intercept of model for mu
vector[4] zeta; // coefficients on building-level predictors in model for mu
vector[n_building] mu_raw;
}
transformed parameters {
real phi = inv(inv_phi);
vector[n_building] mu;
mu = alpha + building_data * zeta + sigma_mu * mu_raw;
}
model {
sigma_mu ~ normal(0, 1);
alpha ~ normal(log(4), 1);
zeta ~ normal(0, 1); // could also use informative priors on the different elements
beta ~ normal(-0.25, 1);
inv_phi ~ normal(0, 1);
mu_raw ~ normal(0, 1); // implies mu ~ normal(alpha + building_data * zeta, sigma_mu)
for (i in 1:N) {
target += neg_binomial_2_log_lpmf(complaints[i] | mu[building[i]] + beta * traps[i] + log_sq_foot[i], phi);
}
}
generated quantities {
int y_rep[N];
for (n in 1:N) {
y_rep[n] = neg_binomial_2_log_safe_rng(mu[building[n]] + beta * traps[n] + log_sq_foot[n], phi);
}
}
"
```
```{r}
N_buildings <- length(unique(pest$building_id))
N_months <- length(unique(pest$date))
#
building_data <- pest %>%
mutate(
building_fac = factor(building_id, levels = unique(building_id)),
building_idx = as.integer(building_fac),
ids = rep(1:N_months, N_buildings),
month_idx = lubridate::month(date)
) %>%
select(building_idx,
live_in_super,
age_of_building,
total_sq_foot,
average_tenant_age,
monthly_average_rent) %>%
distinct() %>%
arrange(building_idx) %>%
select(-building_idx) %>%
mutate(
across(everything(), scale, scale = FALSE)
) %>%
mutate( # scale by constants
age_of_building = age_of_building / 10,
total_sq_foot = total_sq_foot / 10000, # not used
average_tenant_age = average_tenant_age / 10,
monthly_average_rent = monthly_average_rent / 1000
) %>%
as.matrix()
stan_data <- pest %>%
select(complaints, traps, building_id, total_sq_foot) %>%
mutate(log_sq_foot = log(total_sq_foot/1e4)) %>%
mutate(building_id = as_factor(building_id)) %>%
tidybayes::compose_data(
N = nrow(.),
complaints = complaints,
traps = traps,
log_sq_foot = log_sq_foot,
n_building = n_distinct(building_id),
building = building_id,
building_data = building_data[,-3],
)
fitted_model_NB_hier_ncp <- stan(model_code = stan_program, data = stan_data)
```
再看看有效样本
```{r}
fitted_model_NB_hier_ncp %>%
print(pars = c('sigma_mu','beta','alpha','phi','mu'))
```
haha 有效样本改进很大耶
```{r}
scatter_no_divs <- bayesplot::mcmc_scatter(as.array(fitted_model_NB_hier_ncp),
pars = c("mu[4]", 'sigma_mu'), transform = list('sigma_mu' = "log"),
np = nuts_params(fitted_model_NB_hier_ncp))
bayesplot::bayesplot_grid(scatter_with_divs, scatter_no_divs,
grid_args = list(ncol = 2), ylim = c(-11, 1))
```
```{r}
parcoord_no_divs <- bayesplot::mcmc_parcoord(
as.array(fitted_model_NB_hier_ncp, pars = c("sigma_mu", "mu")),
np = nuts_params(fitted_model_NB_hier_ncp)
)
bayesplot::bayesplot_grid(parcoord_with_divs, parcoord_no_divs, ylim = c(-3, 3))
```
```{r sims-full-hier}
sims_NB_hier_ncp <-
rstan::extract(fitted_model_NB_hier_ncp, pars = c('y_rep','inv_phi'))
```
The marginal plot, again:
```{r ppc-full-hier}
y_rep <- as.matrix(fitted_model_NB_hier_ncp, pars = "y_rep")
bayesplot::ppc_dens_overlay(stan_data$complaints, y_rep[1:200,])
```
Predictions by number of bait stations:
```{r}
y_rep <- as.matrix(fitted_model_NB_hier_ncp, pars = "y_rep")
bayesplot::ppc_intervals(y = stan_data$complaints, yrep = y_rep,
x = stan_data$traps) +
labs(x = "Number of bait stations", y = "Number of complaints")
```
# Varying intercepts and varying slopes {-}
```{r}
stan_dat_hier <- readRDS('data/pest_data_longer_stan_dat.RDS')
stan_dat_hier %>% str()
```
这个数据比之前的要丰富些,因此,我们需要重新组建一下,弄成
```{r}
stan_data <- list(
N = 360,
complaints = stan_dat_hier$complaints,
traps = stan_dat_hier$traps,
log_sq_foot = stan_dat_hier$log_sq_foot,
building_id = stan_dat_hier$building_idx,
n_building = unique(stan_dat_hier$building_idx),
building_data = stan_dat_hier$building_data
)
```
模型需要扩展下
$$
\text{complaints}_{b,t} \sim \text{Neg-Binomial}(\lambda_{b,t}, \phi)
\\
\lambda_{b,t} = \exp{(\eta_{b,t})}
\\
\eta_{b,t} = \mu_b + \kappa_b \, \texttt{traps}_{b,t}
+ \text{log}\_\textrm{sq}\_\textrm{foot}_b
\\
\mu_b \sim \text{normal}(\alpha + \texttt{building}\_\textrm{data} \, \zeta,
\sigma_{\mu}) \\
\kappa_b \sim \text{normal}(\beta + \texttt{building}\_\textrm{data} \, \gamma,
\sigma_{\kappa})
$$
```{r, warning=FALSE, message=FALSE}
stan_program <- "
functions {
/*
* Alternative to neg_binomial_2_log_rng() that
* avoids potential numerical problems during warmup
*/
int neg_binomial_2_log_safe_rng(real eta, real phi) {
real gamma_rate = gamma_rng(phi, phi / exp(eta));
if (gamma_rate >= exp(20.79))
return -9;
return poisson_rng(gamma_rate);
}
}
data {
int<lower=1> N;
int<lower=0> complaints[N];
int traps[N];
int<lower=1> n_building;
int<lower=1, upper=n_building> building[N];
vector[N] log_sq_foot;
matrix[n_building,4] building_data;
}
parameters {
real alpha;
real beta;
real<lower=0> sigma_mu;
real<lower=0> sigma_kappa;
vector[n_building] mu_raw;
vector[n_building] kappa_raw;
vector[4] zeta;
vector[4] gamma;
real<lower=0> inv_phi;
}
transformed parameters {
real phi = inv(inv_phi);
vector[n_building] mu = alpha + building_data * zeta + sigma_mu * mu_raw;
vector[n_building] kappa = beta + building_data * gamma + sigma_kappa * kappa_raw;
}
model {
alpha ~ normal(log(4), 1);
beta ~ normal(-0.25, 1);
sigma_mu ~ normal(0, 1);
sigma_kappa ~ normal(0, 1);
mu_raw ~ normal(0,1);
kappa_raw ~ normal(0, 1);
zeta ~ normal(0, 1);
gamma ~ normal(0, 1);
inv_phi ~ normal(0, 1);
for (i in 1:N) {
target += neg_binomial_2_log_lpmf(complaints[i] | mu[building[i]] + kappa[building[i]] * traps[i] + log_sq_foot[i], phi);
}
}
generated quantities {
int y_rep[N];
for (n in 1:N) {
y_rep[n] = neg_binomial_2_log_safe_rng(mu[building[n]] + kappa[building[n]] * traps[n] + log_sq_foot[n], phi);
}
}
"
```
```{r}
stan_data <- list(
N = 360,
complaints = stan_dat_hier$complaints,
traps = stan_dat_hier$traps,
log_sq_foot = stan_dat_hier$log_sq_foot,
building = stan_dat_hier$building_idx,
n_building = length(unique(stan_dat_hier$building_idx)),
building_data = stan_dat_hier$building_data
)
fitted_model_NB_hier_slopes <- stan(model_code = stan_program, data = stan_data)
```
```{r}
bayesplot::mcmc_hist(as.matrix(fitted_model_NB_hier_slopes, pars = "sigma_kappa"),
binwidth = 0.005
)
```
```{r}
print(fitted_model_NB_hier_slopes,
pars = c('kappa','beta','alpha','phi','sigma_mu','sigma_kappa','mu'))
```
```{r}
bayesplot::mcmc_hist(as.matrix(fitted_model_NB_hier_slopes, pars = "beta"),
binwidth = 0.005
)
```
```{r ppc-full-hier-slopes}
y_rep <- as.matrix(fitted_model_NB_hier_slopes, pars = "y_rep")
bayesplot::ppc_dens_overlay(y = stan_dat_hier$complaints, yrep = y_rep[1:200,]
)
```