Skip to contents

Simulate draws from a statistical model to return a data frame of estimates.

Usage

simulate_model(model, iterations = 1000, ...)

# S3 method for glmmTMB
simulate_model(
  model,
  iterations = 1000,
  component = c("all", "conditional", "zi", "zero_inflated", "dispersion"),
  verbose = FALSE,
  ...
)

Arguments

model

Statistical model (no Bayesian models).

iterations

The number of draws to simulate/bootstrap.

...

Arguments passed to insight::get_varcov(), e.g. to allow simulated draws to be based on heteroscedasticity consistent variance covariance matrices.

component

Should all parameters, parameters for the conditional model, for the zero-inflation part of the model, or the dispersion model be returned? Applies to models with zero-inflation and/or dispersion component. component may be one of "conditional", "zi", "zero-inflated", "dispersion" or "all" (default). May be abbreviated.

verbose

Toggle warnings and messages.

Value

A data frame.

Details

Technical Details

simulate_model() is a computationally faster alternative to bootstrap_model(). Simulated draws for coefficients are based on a multivariate normal distribution (MASS::mvrnorm()) with mean mu = coef(model) and variance Sigma = vcov(model).

Models with Zero-Inflation Component

For models from packages glmmTMB, pscl, GLMMadaptive and countreg, the component argument can be used to specify which parameters should be simulated. For all other models, parameters from the conditional component (fixed effects) are simulated. This may include smooth terms, but not random effects.

Examples

model <- lm(Sepal.Length ~ Species * Petal.Width + Petal.Length, data = iris)
head(simulate_model(model))
#>   (Intercept) Speciesversicolor Speciesvirginica Petal.Width Petal.Length
#> 1    3.612058         -1.286187        -2.489872  -0.2908642    0.9918098
#> 2    3.546354         -1.120890        -2.668212   0.3926696    0.9041063
#> 3    3.793472         -1.433718        -1.677664   0.3411330    0.7765568
#> 4    3.432554         -1.540597        -2.022697   0.7666518    0.9567220
#> 5    3.717873         -1.381307        -3.041551  -0.3340260    0.9059531
#> 6    3.623222         -1.177965        -1.523143   0.5187997    0.8810527
#>   Speciesversicolor:Petal.Width Speciesvirginica:Petal.Width
#> 1                   -0.17089176                   0.25600625
#> 2                   -0.68021548                  -0.06306801
#> 3                   -0.16437533                  -0.23854105
#> 4                   -0.75234226                  -0.85689591
#> 5                    0.09566148                   0.79310220
#> 6                   -0.70832655                  -0.74359713
# \donttest{
if (require("glmmTMB", quietly = TRUE)) {
  model <- glmmTMB(
    count ~ spp + mined + (1 | site),
    ziformula = ~mined,
    family = poisson(),
    data = Salamanders
  )
  head(simulate_model(model))
  head(simulate_model(model, component = "zero_inflated"))
}
#>   (Intercept)   minedno
#> 1   0.7425365 -1.550774
#> 2   0.4402207 -1.523603
#> 3   0.9159846 -1.471019
#> 4   1.2169572 -2.278681
#> 5   0.6553302 -1.461300
#> 6   1.0807601 -2.122098
# }