Generate complete synthetic datasets from a fitted INLAvaan model. For each
simulation, a single parameter vector is drawn (from the posterior or prior),
and then sample.nobs observations are generated from the model-implied
distribution at that parameter value.
Usage
# S4 method for class 'INLAvaan'
simulate(
object,
nsim = 1L,
seed = NULL,
sample.nobs = NULL,
prior = FALSE,
samp_copula = TRUE,
silent = FALSE,
...
)Arguments
- object
An object of class INLAvaan.
- nsim
Number of replicate datasets to generate (default 1).
- seed
Optional random seed (passed to
set.seed()).- sample.nobs
Number of observations per dataset. Defaults to the sample size of the original data.
- prior
Logical. When
TRUE, parameters are drawn from the prior; whenFALSE(default), from the posterior.- samp_copula
Logical. When
TRUE(default) andprior = FALSE, posterior parameter draws use the copula method. Ignored whenprior = TRUE.- silent
Logical. When
TRUE, suppresses the informational message about rejected non-PD draws. DefaultFALSE.- ...
Additional arguments (currently unused).
Value
A list of length nsim. Each element is a data frame with
sample.nobs rows and two attributes:
"truth"— named numeric vector of lavaan-side (x-space, constrained) parameter values used to generate the dataset."truth_theta"— named numeric vector of the corresponding unconstrained (theta-space) parameter values.
Details
This function is designed for tasks that require full replicate datasets
from a single parameter draw, such as simulation-based calibration (SBC) and
posterior predictive p-values. It differs from sampling() which generates
one observation per parameter draw (useful for prior/posterior predictive
density overlays).
For each simulation \(s = 1, \ldots, S\):
Draw \(\boldsymbol\theta^{(s)}\) from the posterior (or prior).
Compute the model-implied covariance \(\boldsymbol\Sigma(\boldsymbol\theta^{(s)})\). If it is not positive-definite, reject and redraw.
Generate a dataset of
sample.nobsrows from \(N(\boldsymbol\mu(\boldsymbol\theta^{(s)}),\, \boldsymbol\Sigma(\boldsymbol\theta^{(s)}))\).
Parameter draws reuse the same internal machinery as sampling()
(sample_params_prior / sample_params_posterior), so the prior
specification is consistent.
See also
sampling() for single-observation draws from the predictive
distribution (prior/posterior predictive checks).
Examples
utils::data("HolzingerSwineford1939", package = "lavaan")
fit <- acfa("visual =~ x1 + x2 + x3", HolzingerSwineford1939)
#> ℹ Mode finding and Hessian computation.
#> ✔ Posterior mode and Hessian. [43ms]
#>
#> ℹ Performing VB correction.
#> ✔ VB correction; mean |δ| = 0.247σ. [82ms]
#>
#> ⠙ Fitting 0/6 skew-normal marginals.
#> ✔ Fit 6/6 skew-normal marginals. [100ms]
#>
#> ℹ Adjusting copula correlations (NORTA).
#> ✔ Adjust copula correlations (NORTA). [20ms]
#>
#> ⠙ Posterior sampling and summarising.
#> ✔ Summarise 1000 posterior draws. [842ms]
#>
#> ℹ Fit measures: PPP, DIC, LOO, WAIC.
# Simulate one replicate dataset from the posterior
sims <- simulate(fit, nsim = 1)
head(sims[[1]]) # data frame
#> x1 x2 x3
#> 1 4.283820 6.124441 3.1648154
#> 2 5.058616 5.999174 1.9663417
#> 3 6.190429 6.245040 2.7712843
#> 4 4.325344 5.000279 0.3856629
#> 5 5.733955 2.891204 1.4226711
#> 6 6.058424 7.451181 2.8457662
attr(sims[[1]], "truth") # true lavaan-side (x-space) parameters
#> visual=~x2 visual=~x3 x1~~x1 x2~~x2 x3~~x3
#> 0.7976085 0.9776786 0.7434209 1.0159419 0.8360347
#> visual~~visual
#> 0.5438685
attr(sims[[1]], "truth_theta") # corresponding unconstrained (theta-space) parameters
#> visual=~x2 visual=~x3 x1~~x1 x2~~x2 x3~~x3
#> 0.79760845 0.97767856 -0.29649287 0.01581612 -0.17908516
#> visual~~visual
#> -0.60904784
# Simulate from the prior (e.g., for SBC)
sims_prior <- simulate(fit, nsim = 5, prior = TRUE)
lapply(sims_prior, nrow)
#> [[1]]
#> [1] 301
#>
#> [[2]]
#> [1] 301
#>
#> [[3]]
#> [1] 301
#>
#> [[4]]
#> [1] 301
#>
#> [[5]]
#> [1] 301
#>
