get_simulated: Simulate values from fitted models

View source: R/get_simulated.R

get_simulatedR Documentation

Simulate values from fitted models

Description

Simulate responses from fitted statistical models.

Usage

get_simulated(x, ...)

## S3 method for class 'lm'
get_simulated(
  x,
  data = NULL,
  iterations = 1,
  include_data = FALSE,
  seed = NULL,
  ...
)

## S3 method for class 'glmmTMB'
get_simulated(
  x,
  data = NULL,
  iterations = 1,
  include_data = FALSE,
  seed = NULL,
  centrality = NULL,
  re.form = NULL,
  ...
)

## S3 method for class 'merMod'
get_simulated(
  x,
  data = NULL,
  iterations = 1,
  include_data = FALSE,
  seed = NULL,
  use.u = FALSE,
  re.form = NA,
  newparams = NULL,
  family = NULL,
  cluster.rand = stats::rnorm,
  allow.new.levels = FALSE,
  na.action = stats::na.pass,
  ...
)

## Default S3 method:
get_simulated(x, ...)

## S3 method for class 'data.frame'
get_simulated(x, data = NULL, include_data = FALSE, ...)

Arguments

x

A model.

...

Additional arguments passed to the underlying prediction or simulation methods.

data

An optional data frame in which to evaluate predictions before simulation. This can be a data grid created with get_datagrid().

iterations

Number of response vectors to simulate.

include_data

Logical, if TRUE, data is returned alongside with the simulated draws. If data = NULL, the data used to fit the model is included.

seed

An optional integer random seed.

centrality

Function, indicating how to summarize aggregated simulated values when data is a data grid. Only applies to models from package glmmTMB, because a special handling for the data argument is required here. simulate.glmmTMB() always returns simulated draws for the full data that was used to fit the model. If a data grid is passed via data, get_simulated.glmmTMB() internally, first, filters the results using datawizard::data_match() with the model data and data. This may return more than one row of simulated draws per group defined in data, thus, in a second step, the filtered data are aggregated by the groups defined in data. Resulting estimates of simulated values are summarized using centrylity, which, by default is the mode for categorical, ordinal or count response variables, or the mean otherwise.

re.form

For glmmTMB and merMod models, random effects formula passed to simulation (NULL, NA or ~0).

use.u

For merMod models, logical indicating whether to condition on the current conditional modes of the random effects when simulating.

newparams

For merMod models, optional list or vector of alternative parameter values (e.g., fixed- and random-effect parameters) to be used for simulation instead of those from the fitted model.

family

For merMod models, optional family object or function to override the model's family when simulating responses.

cluster.rand

For merMod models, function used to generate random draws for the random effects (e.g., rnorm).

allow.new.levels

For merMod models, logical indicating whether new levels of grouping variables are allowed in data when simulating.

na.action

For merMod models, function specifying how to handle missing values in data before simulation (e.g., "na.pass").

Value

A data frame with one column per simulation (iter_1, iter_2, ...). The attribute seed contains information about the RNG state used.

Examples

data(mtcars)
m <- lm(mpg ~ wt + cyl, data = mtcars)

# Simulations on the original data
get_simulated(m, iterations = 2, seed = 123)

# Simulations on a data grid
dg <- get_datagrid(m, wt = c(2, 3), cyl = c(4, 6))
get_simulated(dg, m, iterations = 2, seed = 123)


insight documentation built on April 14, 2026, 5:06 p.m.