| simulate | R Documentation |
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.
## S4 method for signature 'INLAvaan'
simulate(
object,
nsim = 1L,
seed = NULL,
sample.nobs = NULL,
prior = FALSE,
samp_copula = TRUE,
silent = FALSE,
...
)
object |
An object of class INLAvaan. |
nsim |
Number of replicate datasets to generate (default 1). |
seed |
Optional random seed (passed to |
sample.nobs |
Number of observations per dataset. Defaults to the sample size of the original data. |
prior |
Logical. When |
samp_copula |
Logical. When |
silent |
Logical. When |
... |
Additional arguments (currently unused). |
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.nobs rows 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.
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.
sampling() for single-observation draws from the predictive
distribution (prior/posterior predictive checks).
utils::data("HolzingerSwineford1939", package = "lavaan")
fit <- acfa("visual =~ x1 + x2 + x3", HolzingerSwineford1939)
# Simulate one replicate dataset from the posterior
sims <- simulate(fit, nsim = 1)
head(sims[[1]]) # data frame
attr(sims[[1]], "truth") # true lavaan-side (x-space) parameters
attr(sims[[1]], "truth_theta") # corresponding unconstrained (theta-space) parameters
# Simulate from the prior (e.g., for SBC)
sims_prior <- simulate(fit, nsim = 5, prior = TRUE)
lapply(sims_prior, nrow)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.