Skip to content

Commit 0e9cd1f

Browse files
committed
Updated generated quantities block
1 parent 26cb940 commit 0e9cd1f

File tree

4 files changed

+70
-54
lines changed

4 files changed

+70
-54
lines changed

education/causal_iv_one-sided/case_study_02_IV_one-sided.html

Lines changed: 31 additions & 31 deletions
Large diffs are not rendered by default.

education/causal_iv_one-sided/case_study_02_IV_one-sided.qmd

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,17 +296,17 @@ Note that the likelihood function is expressed as an increment to the log probab
296296
For the set $\mathcal{S}(0, 0)$, the built-in function in Stan, `log_mix()`, is employed to define mixtures on the log scale. The `log_mix(p, a, b)` function internally calculates the log-weighted mixture of `a` and `b` using the weight `p`, which provides a more numerically stable solution for mixtures (see [Chapter 5 of the Stan User's Guide](https://mc-stan.org/docs/stan-users-guide/vectorizing-mixtures.html)). By adopting this function, we directly determine the log-likelihood contributions for units in the subset $\mathcal{S}(0, 0)$, which originate from a mixture of outcome distributions for two compliance types: compliers and never-takers.
297297

298298

299-
#### Transformed Parameters Block
299+
#### Generated quantities Block
300300

301301
``` stan
302-
transformed parameters {
302+
generated quantities {
303303
// Superpopulation complier average causal effect (CACE)
304304
// in per-1000 units
305305
real CACE = (eta_c1 - eta_c0) * 10^3;
306306
}
307307
```
308308

309-
The estimand of primary interest is the super-population complier average causal effect (CACE), denoted as $\eta_{c1}-\eta_{c0}$. We can include an optional transformed parameters block to generate the posterior distribution for the CACE, which is defined as a function of the declared parameters. We rescaled the CACE estimates by simply multiplying the original estimates by $10^{3}$. The resulting CACE estimate represents the causal effect of vitamin A supplements on infant mortality per 1,000 individuals, specifically for compliers in the population.
309+
The estimand of primary interest is the super-population complier average causal effect (CACE), denoted as $\eta_{c1}-\eta_{c0}$. We can include an optional `generated quantities` block to generate the posterior distribution for the CACE, which is defined as a function of the declared parameters. We rescaled the CACE estimates by simply multiplying the original estimates by $10^{3}$. The resulting CACE estimate represents the causal effect of vitamin A supplements on infant mortality per 1,000 individuals, specifically for compliers in the population.
310310

311311

312312
#### Model estimation
@@ -354,7 +354,7 @@ ggplot(data = df_CACE, aes(x = CACE)) +
354354
) +
355355
geom_text(
356356
x = 3.8, y = 25,
357-
label = "Median = 3.19",
357+
label = paste0("Median = ", round(median(df_CACE$CACE), 2)),
358358
color = "blue", size = 5
359359
) +
360360
scale_x_continuous(
@@ -369,7 +369,15 @@ ggplot(data = df_CACE, aes(x = CACE)) +
369369
theme(panel.grid = element_blank())
370370
```
371371

372-
This histogram replicates Figure 3 from @imbens1997bayesian. Under the exclusion restriction for never-takers, the posterior mean of the CACE is 3.23. The 90% credible interval spans from 1.40 to 5.13 per 1,000 individuals, while the 99% credible interval ranges from 0.38 to 6.38. These findings suggest that vitamin A treatment is likely to confer a positive effect on infant survival among compliers in the studied population.
372+
```{r}
373+
#| echo: false
374+
CACE_mean <- df_CACE$CACE %>% mean() %>% round(2)
375+
CACE_CI_90 <- df_CACE$CACE %>% quantile(probs = c(0.05, 0.95)) %>% round(2)
376+
CACE_CI_99 <- df_CACE$CACE %>% quantile(probs = c(0.005, 0.995)) %>% round(2)
377+
```
378+
379+
380+
This histogram replicates Figure 3 from @imbens1997bayesian. Under the exclusion restriction for never-takers, the posterior mean of the CACE is `r CACE_mean`. The 90% credible interval spans from `r CACE_CI_90[1]` to `r CACE_CI_90[1]` per 1,000 individuals, while the 99% credible interval ranges from `r CACE_CI_99[1]` to `r CACE_CI_90[2]`. These findings suggest that vitamin A treatment is likely to confer a positive effect on infant survival among compliers in the studied population.
373381

374382
In practical terms, given a CACE of 3.23, it means that for every 1,000 complier infants, about an additional 3.23 infants are expected to survive due to the vitamin A treatment, compared to their counterparts who did not receive the intervention. To fully grasp the practical significance of the CACE, one must consider other relevant data, the overarching landscape of infant health, and any potential disadvantages or expenses related to the treatment. For instance, in regions grappling with high infant mortality rates, even marginal improvements in survival rates might carry substantial importance. The cost-effectiveness and safety profile of the vitamin A treatment further accentuate its potential benefits, especially if it is both affordable and exhibits minimal side effects.
375383

@@ -435,10 +443,10 @@ model {
435443
}
436444
```
437445

438-
In addition to the main causal estimand, the super-population complier average causal effect (CACE, $\eta_{c1} - \eta_{c0}$), we can now define the super-population average causal effect of treatment assignment on outcomes for never-takers ($\eta_{n1} - \eta_{n0}$) in the `transformed parameters` block. We will refer to this as the "NACE" (Never-taker Average Causal Effect):
446+
In addition to the main causal estimand, the super-population complier average causal effect (CACE, $\eta_{c1} - \eta_{c0}$), we can now define the super-population average causal effect of treatment assignment on outcomes for never-takers ($\eta_{n1} - \eta_{n0}$) in the `generated quantities` block. We will refer to this as the "NACE" (Never-taker Average Causal Effect):
439447

440448
``` stan
441-
transformed parameters {
449+
generated quantities {
442450
// Super-population average causal effects
443451
real CACE = (eta_c1 - eta_c0) * 10^3;
444452
real NACE = (eta_n1 - eta_n0) * 10^3;
@@ -487,7 +495,7 @@ ggplot(data = df_CACE, aes(x = CACE)) +
487495
) +
488496
geom_text(
489497
x = 3.8, y = 25,
490-
label = "Median = 2.71",
498+
label = paste0("Median = ", round(median(df_CACE$CACE), 2)),
491499
color = "blue", size = 5
492500
) +
493501
scale_x_continuous(
@@ -502,8 +510,14 @@ ggplot(data = df_CACE, aes(x = CACE)) +
502510
theme(panel.grid = element_blank())
503511
```
504512

513+
```{r}
514+
#| echo: false
515+
CACE_mean <- df_CACE$CACE %>% mean() %>% round(2)
516+
CACE_CI_90 <- df_CACE$CACE %>% quantile(probs = c(0.05, 0.95)) %>% round(2)
517+
CACE_CI_99 <- df_CACE$CACE %>% quantile(probs = c(0.005, 0.995)) %>% round(2)
518+
```
505519

506-
The histogram above replicates Figure 1 from @imbens1997bayesian. The 90% credible interval of the posterior distribution indicates that the true CACE, without exclusion restriction, likely falls within the range of -0.37 to 6.11 per 1,000 individuals.
520+
The histogram above replicates Figure 1 from @imbens1997bayesian. The 90% credible interval of the posterior distribution indicates that the true CACE, without exclusion restriction, likely falls within the range of `r CACE_CI_90[1]` to `r CACE_CI_90[2]` per 1,000 individuals.
507521

508522

509523
```{r, fig.width=7, fig.height=5}
@@ -527,7 +541,7 @@ ggplot(data = df_NACE, aes(x = NACE)) +
527541
) +
528542
geom_text(
529543
x = 3.8, y = 25,
530-
label = "Median = 1.77",
544+
label = paste0("Median = ", round(median(df_NACE$NACE), 2)),
531545
color = "blue", size = 5
532546
) +
533547
scale_x_continuous(
@@ -542,7 +556,6 @@ ggplot(data = df_NACE, aes(x = NACE)) +
542556
theme(panel.grid = element_blank())
543557
```
544558

545-
546559
We also replicate Figure 2 from @imbens1997bayesian, which represents the posterior distribution for NACE. Under the exclusion restriction, the NACE is constrained to be 0 because $\eta_{n0} = \eta_{n1}$. Without the exclusion restriction, however, the NACE has a posterior distribution that is centered around 0, lending credibility to the exclusion restriction [@imbens1997bayesian].
547560

548561

education/causal_iv_one-sided/stan/cace_with_exclusion.stan

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ parameters {
1515
real<lower=0, upper=1> eta_n;
1616
}
1717

18-
transformed parameters {
19-
// Superpopulation complier average causal effect (CACE)
20-
// in per-1000 units
21-
real CACE = (eta_c1 - eta_c0) * 10^3;
22-
}
23-
2418
model {
2519
// Define local variables for efficiency
2620
real log_pi_c = log(pi_c);
@@ -58,3 +52,11 @@ model {
5852
}
5953
}
6054

55+
generated quantities {
56+
// Superpopulation complier average causal effect (CACE)
57+
// in per-1000 units
58+
real CACE = (eta_c1 - eta_c0) * 10^3;
59+
}
60+
61+
62+

education/causal_iv_one-sided/stan/cace_without_exclusion.stan

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,6 @@ parameters {
1616
real<lower=0, upper=1> eta_n1;
1717
}
1818

19-
transformed parameters {
20-
// Super-population average causal effects
21-
real CACE = (eta_c1 - eta_c0) * 10^3;
22-
real NACE = (eta_n1 - eta_n0) * 10^3;
23-
}
24-
2519
model {
2620
// Define local variables for efficiency
2721
real log_pi_c = log(pi_c);
@@ -60,3 +54,10 @@ model {
6054
}
6155
}
6256

57+
generated quantities {
58+
// Super-population average causal effects
59+
real CACE = (eta_c1 - eta_c0) * 10^3;
60+
real NACE = (eta_n1 - eta_n0) * 10^3;
61+
}
62+
63+

0 commit comments

Comments
 (0)