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)
#> ℹ Finding posterior mode.
#> ✔ Finding posterior mode. [23ms]
#>
#> ℹ Computing the Hessian.
#> ✔ Computing the Hessian. [19ms]
#>
#> ℹ Performing VB correction.
#> ✔ VB correction; mean |δ| = 0.246σ. [53ms]
#>
#> ⠙ Fitting 0/6 skew-normal marginals.
#> ✔ Fitting 6/6 skew-normal marginals. [73ms]
#>
#> ℹ Adjusting copula correlations (NORTA).
#> ✔ Adjusting copula correlations (NORTA). [21ms]
#>
#> ⠙ Posterior sampling and summarising.
#> ✔ Posterior sampling and summarising. [417ms]
#>
# Simulate one replicate dataset from the posterior
sims <- simulate(fit, nsim = 1)
head(sims[[1]]) # data frame
#> x1 x2 x3
#> 1 -0.09916336 -2.3532667 -0.1417267
#> 2 0.52478956 -0.6783174 -1.3472148
#> 3 -0.76180870 -0.1612624 0.8219097
#> 4 0.23045650 -0.6685604 0.6385479
#> 5 2.51226067 -0.7269663 1.5260502
#> 6 -1.41566295 -2.8317893 -0.6663346
attr(sims[[1]], "truth") # true lavaan-side (x-space) parameters
#> visual=~x2 visual=~x3 x1~~x1 x2~~x2 x3~~x3
#> 0.6842168 0.9006944 0.6429224 1.1979793 0.7832034
#> visual~~visual
#> 0.6777291
attr(sims[[1]], "truth_theta") # corresponding unconstrained (theta-space) parameters
#> visual=~x2 visual=~x3 x1~~x1 x2~~x2 x3~~x3
#> 0.6842168 0.9006944 -0.4417313 0.1806362 -0.2443629
#> visual~~visual
#> -0.3890076
# 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
#>
