Learn R Programming

DHARMa (version 0.4.7)

getSimulations: Get model simulations

Description

Wrapper to simulate from a fitted model.

Usage

getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

# S3 method for default getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

# S3 method for negbin getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

# S3 method for gam getSimulations(object, nsim = 1, type = c("normal", "refit"), mgcViz = TRUE, ...)

# S3 method for lmerMod getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

# S3 method for glmmTMB getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

# S3 method for HLfit getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

# S3 method for MixMod getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

# S3 method for phylolm getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

# S3 method for phyloglm getSimulations(object, nsim = 1, type = c("normal", "refit"), ...)

Value

A matrix with simulations.

Arguments

object

a fitted model.

nsim

number of simulations.

type

if simulations should be prepared for getQuantile or for refit.

...

additional parameters to be passed on, usually to the simulate function of the respective model class.

mgcViz

whether simulations should be created with mgcViz (if mgcViz is available)

Author

Florian Hartig

Details

The purpose of this function is to wrap or implement the simulate function of different model classes and thus return simulations from fitted models in a standardized way.

Note: GLMM and other regression packages often differ in how simulations are produced, and which parameters can be used to modify this behavior.

One important difference is how to modifiy which hierarchical levels are held constant, and which are re-simulated. In lme4, this is controlled by the re.form argument (see lme4::simulate.merMod). In glmmTMB, the package version 1.1.10 has a temporary solution to simulate conditional to all random effects (see glmmTMB::set_simcodes val = "fix", and issue #888 in glmmTMB GitHub repository. For other packages, please consult the help.

If the model was fit with weights and the respective model class does not include the weights in the simulations, getSimulations will throw a warning. The background is if weights are used on the likelihood directly, then what is fitted is effectively a pseudo likelihood, and there is no way to directly simulate from the specified likelihood. Whether or not residuals can be used in this case depends very much on what is tested and how weights are used. I'm sorry to say that it is hard to give a general recommendation, you have to consult someone that understands how weights are processed in the respective model class.

See Also

getObservedResponse, getRefit, getFixedEffects, getFitted

Examples

Run this code
testData = createData(sampleSize = 400, family = gaussian())

fittedModel <- lm(observedResponse ~ Environment1 , data = testData)

# response that was used to fit the model
getObservedResponse(fittedModel)

# predictions of the model for these points
getFitted(fittedModel)

# extract simulations from the model as matrix
getSimulations(fittedModel, nsim = 2)

# extract simulations from the model for refit (often requires different structure)
x = getSimulations(fittedModel, nsim = 2, type = "refit")

getRefit(fittedModel, x[[1]])

getRefit(fittedModel, getObservedResponse(fittedModel))

Run the code above in your browser using DataLab