Learn R Programming

GHRmodel (version 0.1.1)

plot_ppd: 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.

Usage

plot_ppd(
  ppd,
  xlab = "Outcome",
  ylab = "Density",
  title = "Posterior Predictive Distribution",
  xlim = NULL,
  obs_color = NULL,
  ppd_color = NULL
)

Value

A ggplot2 plot object.

Arguments

ppd

A data.frame containing posterior-predictive samples (one column per sample) and the column with observed data.

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). If NULL (default) the limits are c(0, quantile(observed, 0.95)).

obs_color

Color for the observed line density

ppd_color

Color for the posterior predictive distribution lines density

Examples

Run this code
# \donttest{
# Load example dataset
data(dengueMS)

# Declare formulas
formulas <- c("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[dengue_MS$year %in% 2005:2010,],
  family   = "nbinomial",
  name     = "model",
  offset   = "population",
  nthreads = 2,
  control_compute = list(config = FALSE),
  pb       = TRUE
)

# Generate 100 samples from the posterior predictive distribution of the model
ppd_df <- sample_ppd( 
  results,
  mod_id = "model1", 
  s = 100,
  nthreads = 2)

# Plot densities of the posterior predictive distribution and observed cases.
plot_ppd(ppd_df, obs_color = "blue", ppd_color = "red")
# }

Run the code above in your browser using DataLab