Plot Posterior Predictive Densities Versus Observed Data
Description
This function draws kernel-density curves for posterior-predictive samples and observed data using ggplot2::geom_line(). Each predictive sample’s density is plotted in light blue; the observed density is overlaid in black.
plot_ppd(
ppd,
obs,
xlab = "Outcome",
ylab = "Density",
title = "Posterior Predictive Distribution",
xlim = NULL,
obs_color = NULL,
ppd_color = NULL
)Arguments
ppd: Amatrixcontaining posterior predictive samples (one column per sample).obs: A vector of observations, must have the same length as the number of columns inppd.xlab: Character: x-axis label. Default"Outcome".ylab: Character: y-axis label. Default"Density".title: Character: plot title. Default"Posterior Predictive Distribution".xlim: Numeric vector of length 2 giving the minimum and maximum x-axis values, e.g.c(0, 25). IfNULL(default) the limits arec(0, quantile(observed, 0.95)).obs_color: Color for the observed line density.ppd_color: Color for the posterior predictive distribution line density.
Returns
A ggplot2 plot object.
Examples
# Load example dataset
data(dengue_MS)
dengue_MS <- dengue_MS[dengue_MS$year %in% 2015:2016,]
# Declare formulas
formulas <- "dengue_cases ~ tmin + f(year, model='rw1')"
# Tranform formulas into a 'GHRformulas' object
ghr_formula <- as_GHRformulas(formulas)
# Fit multiple models
results <- fit_models(
formulas = ghr_formula,
data = dengue_MS,
family = "nbinomial",
name = "model",
offset_var = "pop100000",
nthreads = 2,
control_compute = list(config = TRUE),
keep_ppd = TRUE,
ppd_samples = 50
)
# Plot densities of the posterior predictive distribution and observed cases.
plot_ppd(results$ppd[[1]], obs = dengue_MS$dengue_cases)