Posterior Predictive Checks (PPC)
Gradient Metrics
Source:vignettes/7_posterior_predictive_checks.Rmd
7_posterior_predictive_checks.RmdPremise
In a list experiment we observe real public and private answers, and the Stan model estimates posterior distributions of parameters that should reproduce those answers when we simulate from the fitted model. This vignette is about checking how well that reconstruction worked in practice.
Basics
Posterior simulation returns respondent-by-draw matrices for both response components:
- public agree/disagree answers (
sim_public$draws): each cell is a simulated 1/0 response for one respondent in one posterior draw, - list-question count answers (
sim_private$draws): each cell is a simulated count for one respondent in one posterior draw.
PPC in this package focuses on subgroup summaries rather than exact respondent-by-respondent matching. At respondent level, simulated values are inherently noisy and often weakly identified; subgroup summaries are the primary inferential target and are more stable for fit diagnostics.
To demonstrate the workflow, we treat the full sample as one subgroup and check reconstruction of the overall population mean for public responses.
Take the bundled fitted model:
model <- example_lx_modelExtract the objects used below:
# observed public responses
obs_public <- model$response_public$response
# matrix of simulated public responses
sim_public <- model$sim_public$draws
str(obs_public)
#> num [1:262] 1 0 0 0 0 0 0 0 0 0 ...
str(sim_public)
#> 'AsIs' int [1:262, 1:200] 0 0 0 0 0 0 0 0 0 1 ...
#> - attr(*, "dimnames")=List of 2
#> ..$ : NULL
#> ..$ iterations: NULLNow derive the two quantities we compare:
- Observed population mean
- Draws of simulated population means (one value per posterior draw)
Metrics
Using obs_mean and sim_mean_draws, we can
now compute PPC diagnostics one by one.
Bias distribution
Here we inspect simulated minus observed. Values above zero suggest overprediction; values below zero suggest underprediction.
bias_draws <- sim_mean_draws - obs_mean
tibble(
bias_mean = mean(bias_draws),
bias_median = median(bias_draws),
bias_lower = unname(quantile(bias_draws, 0.025)),
bias_upper = unname(quantile(bias_draws, 0.975))
)
#> # A tibble: 1 × 4
#> bias_mean bias_median bias_lower bias_upper
#> <dbl> <dbl> <dbl> <dbl>
#> 1 0.00170 -0.00382 -0.0650 0.0687Tail position of the observed value
tail_p is computed as the proportion of predictive draws
less than or equal to the observed mean. The direction of the inequality
is conventional; what matters is balance. In the ideal case, about half
of draws are below the observed value and half are above it, so
tail_p is near 0.5. Values close to
0 or 1 mean the observed value is in a
predictive tail.
tail_badness is a practical helper metric. It is just
distance from the ideal center (tail_p = 0.5), rescaled to
0(perfect) - 1 (bad), so it is easy to spot and sort the worst
tail-position cases.
CRPS
CRPS is a single-number probabilistic score that rewards both good
centering and low uncertainty. Lower values indicate better predictive
performance. The theoretical best value is 0, achieved only
when all predictive draws are exactly equal to the observed value, which
is not realistic in practice.
crps_draws(sim_mean_draws, obs_mean)
#> [1] 0.007470515To build intuition for CRPS magnitudes, it helps to evaluate a few synthetic predictive distributions on roughly the same scale as list-experiment means.
set.seed(123)
tribble(
~scenario, ~crps,
"centered, very wide (mean = 0.50, sd = 0.20)",
crps_draws(rnorm(100, mean = 0.50, sd = 0.20), observed = 0.50),
"centered, narrow (mean = 0.50, sd = 0.05)",
crps_draws(rnorm(100, mean = 0.50, sd = 0.05), observed = 0.50),
"shifted, narrow (mean = 0.60, sd = 0.05)",
crps_draws(rnorm(100, mean = 0.60, sd = 0.05), observed = 0.50),
"shifted, wider (mean = 0.60, sd = 0.10)",
crps_draws(rnorm(100, mean = 0.60, sd = 0.10), observed = 0.50)
)
#> # A tibble: 4 × 2
#> scenario crps
#> <chr> <dbl>
#> 1 centered, very wide (mean = 0.50, sd = 0.20) 0.0435
#> 2 centered, narrow (mean = 0.50, sd = 0.05) 0.0115
#> 3 shifted, narrow (mean = 0.60, sd = 0.05) 0.0793
#> 4 shifted, wider (mean = 0.60, sd = 0.10) 0.0584How to read this:
- When predictions stay centered at the observed value, a wider distribution gets a worse CRPS than a tighter one, because it is less sharp.
- A shifted predictive mean (for example 0.6 vs observed 0.5) is penalized strongly: even a very narrow distribution can have poor CRPS if it is centered in the wrong place.
- For a shifted distribution, increasing spread can slightly improve CRPS by placing more predictive mass closer to the observed value, but this does not remove the misspecification from the mean shift.
Which quantities are checked
The same PPC logic can be applied to almost any quantity that can be computed from answers, as long as you can compute it for both observed and simulated responses.
In lx_ppc(), the two component summaries are:
-
raw_p_public: mean of public responses. -
raw_p_private: mean(private responses in treatment) minus mean(private responses in control).
raw_p_private is a simple difference-in-means quantity,
commonly used as a raw analysis in list experiments.
This raw private quantity is not the same target as model-based
p_private, and in general it should not be expected to
match exactly. The point of fitting the full model is precisely to go
beyond simple difference-in-means.
Still, PPC asks a replication question: if the model reproduces the
observed data well, then these raw quantities computed on simulated
responses should be close to the same quantities computed on observed
responses. That applies to both raw_p_private and
raw_p_public.
lx_ppc()
The package provides a dedicated function, lx_ppc(), for
this workflow. It applies the same PPC logic shown above and returns fit
summaries for both raw_p_public and
raw_p_private.
In typical use, you will apply it to cohorts generated from model
predictors. The easiest option is
cohorts = "predictors":
lx_ppc(model, cohorts = "predictors")
#> Cohort Set
#> # A tibble: 11 × 7
#> column label level cohort n weighted_n ppc
#> <chr> <chr> <chr> <list> <int> <dbl> <list>
#> 1 population Overall population population <cohort> 500 500 <tibble>
#> 2 d_gender Gender male <cohort> 244 238. <tibble>
#> 3 d_gender Gender female <cohort> 256 262. <tibble>
#> 4 d_age Age group 18-30 <cohort> 104 147. <tibble>
#> 5 d_age Age group 31-45 <cohort> 159 152. <tibble>
#> 6 d_age Age group 46-64 <cohort> 144 123. <tibble>
#> 7 d_age Age group 65+ <cohort> 93 77.9 <tibble>
#> 8 d_hhincome Household income <$25k <cohort> 110 151. <tibble>
#> 9 d_hhincome Household income $25k-$50k <cohort> 151 125. <tibble>
#> 10 d_hhincome Household income $50k-$100k <cohort> 148 147. <tibble>
#> 11 d_hhincome Household income 100k+ <cohort> 91 77.4 <tibble>You can also request only selected predictors:
lx_ppc(model, cohorts = c("d_gender", "d_age"))
#> Cohort Set
#> # A tibble: 6 × 7
#> column label level cohort n weighted_n ppc
#> <chr> <chr> <chr> <list> <int> <dbl> <list>
#> 1 d_gender Gender male <cohort [244 × 2]> 244 238. <tibble>
#> 2 d_gender Gender female <cohort [256 × 2]> 256 262. <tibble>
#> 3 d_age Age group 18-30 <cohort [104 × 2]> 104 147. <tibble>
#> 4 d_age Age group 31-45 <cohort [159 × 2]> 159 152. <tibble>
#> 5 d_age Age group 46-64 <cohort [144 × 2]> 144 123. <tibble>
#> 6 d_age Age group 65+ <cohort [93 × 2]> 93 77.9 <tibble>
lx_ppc_render()
If you want a spreadsheet version of the same diagnostics, use
lx_ppc_render() on a cohort-set PPC summary.
ppc_summary <- lx_ppc(model, cohorts = "predictors")
wb <- lx_ppc_render(ppc_summary)
wb
#> A Workbook object.
#>
#> Worksheets:
#> Sheet 1: "pets"
#>
#> Custom column widths (column: width)
#> 1: 14, 2: 20, 3: 14, 4: 16, 5: 10, 6: 10, 7: 8, 8: 9, 9: 10, 10: 10, 11: 10, 12: 10, 13: 10, 14: 10, 15: 10, 16: 10, 17: 10, 18: 8, 19: 9, 20: 10, 21: 10, 22: 10, 23: 10, 24: 10, 25: 10, 26: 10
#>
#>
#>
#> Worksheet write order: 1
#> Active Sheet 1: "pets"
#> Position: 1If needed, save the workbook with
openxlsx::saveWorkbook():
openxlsx::saveWorkbook(wb, "lx_ppc_summary.xlsx", overwrite = TRUE)To build one spreadsheet across multiple models, call
lx_ppc_render() sequentially and pass the same workbook
object each time. The PPC summary keeps the experiment short code in
metadata, and by default lx_ppc_render() uses it as the
sheet name.