Plot posterior distribution of observation generated under model's posterior parameter distribution.

plotPosteriorFit(
  trace,
  fitmodel,
  initState,
  data,
  posteriorSummary = c("sample", "median", "mean", "max"),
  summary = TRUE,
  sampleSize = 100,
  nonExtinct = NULL,
  alpha = min(1, 10/sampleSize),
  plot = TRUE,
  allVars = FALSE,
  initDate = NULL
)

Arguments

trace

a data.frame with one column per estimated parameter, as returned by burnAndThin

fitmodel

a fitmodel object

initState

named numeric vector. Initial values of the state variables. Names should match fitmodel$stateNames.

data

data frame. Observation times and observed data. The time column must be named "time" and the observation column must be named "obs".

posteriorSummary

character. Set to "sample" to plot trajectories from a sample of the posterior (default). Set to "median", "mean" or "max" to plot trajectories corresponding to the median, mean and maximum of the posterior density.

summary

logical, if TRUE trajectories are summarised by their mean, median, 50% and 95% quantile distributions. Otherwise, the trajectories are plotted.

sampleSize

number of theta sampled from posterior distribution (if posterior.summary == "sample"). Otherwise, number of replicated simulations.

nonExtinct

character vector. Names of the infected states which must be non-zero so the epidemic is still ongoing. When the names of these states are provided, the extinction probability is plotted by computing the proportion of faded-out epidemics over time. An epidemic has faded-out when all the infected states (whose names are provided) are equal to 0. This is only relevant for stochastic models. In addition, if summary == TRUE, the summaries of the trajectories conditioned on non-extinction are shown. Default to NULL.

alpha

transparency of the trajectories (between 0 and 1).

plot

if TRUE the plot is displayed, and returned otherwise.

allVars

logical, if FALSE only the observations are plotted. Otherwise, all state variables are plotted.

initDate

character. Date of the first point of the time series (default to NULL). If provided, the x-axis will be in calendar format. NB: currently only works if the unit of time is the day.

Value

If plot == FALSE, a list of 2 elements is returned:

  • theta the theta(s) used for plotting (either a vector or a data.frame)

  • traj a data.frame with the trajectories (and observations) sampled from the posterior distribution.

  • plot the plot of the fit displayed.

Examples

data(fluTdc1971)
data(epi)
data(mcmcEpi)
data(models)
initState <- c(S = 999, I = 1, R = 0)
plotPosteriorFit(
  trace = mcmcEpi1, fitmodel = sirDeter, initState = initState,
  data = epi1
 )
#> Compute posterior fit
#> Compute confidence intervals