Skip to contents

Adds a $generated object to a fit, holding random data generated from the fit's posterior (or its estimate), on either backend. ctStanGenerateFromFit is maintained as a backward-compatible alias.

Usage

ctGenerateFromFit(
  fit,
  nsamples = 200,
  fullposterior = FALSE,
  verboseErrors = FALSE,
  cores = getOption("mc.cores", 2L),
  intoverstates = "fit",
  effects = c("population", "fitted")
)

Arguments

fit

A fit from ctFit, on either backend.

nsamples

Positive integer specifying number of datasets to generate.

fullposterior

Logical indicating whether to sample from the full posterior (original nsamples) or the posterior mean.

verboseErrors

if TRUE, print verbose output when errors in generation encountered.

cores

Number of cpu cores to use: R worker processes for a stan fit, and for a julia fit the engine threads its draws are generated on.

intoverstates

For backend='julia' fits: which route generates the latent states. 'fit' (the default) mirrors the fit, so a state-explicit fit gets a state-explicit predictive and every other fit generates through the filter's one-step-ahead predictive: the density the fit worked with, and the identity that a generated dataset's llrow is the likelihood reported while generating it.

FALSE simulates the model exactly whatever the fit did: the trajectory from the process at each subject's own parameters, then each observation from its measurement model given the state at its row, as ctGenerate does. The two differ for a non-Gaussian indicator – where the filter's measurement update is an assumed-density projection – or for state-dependent dynamics. Comparing data generated each way, with the same effects, separates the fitting approximation from model misspecification, which comparing either with the observed data tests. TRUE forces the filter route.

Either way the states are regenerated rather than reused.

effects

For backend='julia' fits with random effects: 'population' (the default) draws new subjects – and groups, at every level – from the fitted population distribution at each parameter draw, so the generated data are the model's prediction for a new sample from the same design. 'fitted' keeps the fitted subjects: each at its sampled effects when the fit sampled them and saved the draws, otherwise at its conditional mode. A fit that integrates the random effects as states (intoverpop='augmented') carries no per-subject effects, so only 'population' applies there. Stan fits generate as the fit was made and refuse the argument.

Value

The fit, with a $generated element: Y, the generated data as an array of sample by data row by manifest variable, on the original time and missingness structure, and llrow, each generated row's log likelihood.

Examples

gen <- ctGenerateFromFit(ctstantestfit, nsamples=3,fullposterior=TRUE,cores=1)
plot(gen$generated$Y[3,,2],type='l') #Third random data sample, 2nd manifest var, all time points.