Simulates response vectors from the marginal distribution implied by a fitted model. For mixed models, this means integrating over random effects (i.e. predicting at level 0).

simulate0(object, nsim = 1, seed = NULL, ...)

Arguments

object

A fitted model object. Supports objects of class lme (from nlme), gls (from nlme), or lmerMod (from lme4).

nsim

Number of simulated response vectors to generate. Default is 1.

seed

Optional seed for reproducibility.

...

Currently ignored.

Value

A data frame with length(mu) rows and nsim columns. Each column is one simulated response vector.

Details

For gls models, the mean is the fitted values. For lme models, the mean is the level-0 prediction (no random effects). For lmerMod models, the mean is the marginal mean with random effects set to zero (re.form=NA).

The covariance matrix is obtained via cov_matrix(), representing the implied marginal covariance structure.

Examples

if (require(nlme) && require(lme4) && require(MASS)) {
  data(Orthodont, package = "nlme")
  NSIM <- 10
  # GLS model
  fit_gls <- gls(distance ~ age, data = Orthodont)
  sim_gls <- simulate0(fit_gls, nsim = NSIM, seed = 123)
  head(sim_gls)

  # LME model
  fit_lme <- lme(distance ~ age, random = ~1 | Subject, data = Orthodont)
  sim_lme <- simulate0(fit_lme, nsim = NSIM, seed = 123)
  head(sim_lme)

  # lmerMod model
  fit_lmer <- lme4::lmer(distance ~ age + (1 | Subject), data = Orthodont)
  sim_lmer <- simulate0(fit_lmer, nsim = NSIM, seed = 123)
  head(sim_lmer)

  # lm
  fit_lm <- lm(distance ~ age, data = Orthodont)
  sim_lm <- simulate0(fit_lm, nsim = NSIM, seed = 123)
  head(sim_lm) 
}
#> Loading required package: MASS
#> Error in simulate0(fit_gls, nsim = NSIM, seed = 123): could not find function "simulate0"