| sampling | R Documentation |
Sample model parameters, latent variables, or observed variables from the
generative model underlying a fitted INLAvaan model. By default, parameters
are drawn from the posterior distribution; set prior = TRUE to draw
from the prior instead (useful for prior predictive checks).
sampling(object, ...)
## S4 method for signature 'INLAvaan'
sampling(
object,
type = c("lavaan", "theta", "latent", "observed", "implied", "all"),
nsamp = 1000L,
samp_copula = TRUE,
prior = FALSE,
silent = FALSE,
...
)
object |
An object of class INLAvaan (or |
... |
Additional arguments (currently unused). |
type |
Character string specifying what to sample:
|
nsamp |
Number of samples to draw. |
samp_copula |
Logical. When |
prior |
Logical. When |
silent |
Logical. When |
Each row of the output corresponds to a fresh parameter draw: a new
\boldsymbol\theta^{(s)} is sampled and then propagated through the
generative chain to produce one latent vector and one observed vector. This
makes sampling() ideal for prior and posterior predictive checks
(e.g., density overlays, test statistic distributions).
The generative chain is:
\boldsymbol\theta^{(s)} \sim \pi(\boldsymbol\theta \mid \mathbf{y})
\boldsymbol\eta^{(s)} \sim N((\mathbf{I} - \mathbf{B})^{-1}\boldsymbol\alpha,\,\boldsymbol\Phi)
\mathbf{y}^{*(s)} \sim N(\boldsymbol\Lambda\boldsymbol\eta^{(s)} + \boldsymbol\nu,\,\boldsymbol\Theta)
If you need complete replicate datasets (many observations from a single
parameter draw) — for example, for simulation-based calibration (SBC) — use
simulate() instead.
This is distinct from predict(), which computes individual-specific
factor scores \boldsymbol\eta \mid \mathbf{y},\boldsymbol\theta
conditional on observed data.
A matrix or named list, depending on type.
simulate() for generating complete replicate datasets (e.g.,
for SBC); predict() for individual-specific factor scores;
bfit_indices() for Bayesian fit indices.
utils::data("HolzingerSwineford1939", package = "lavaan")
fit <- acfa("visual =~ x1 + x2 + x3", HolzingerSwineford1939)
# Posterior samples of lavaan-side parameters
samps <- sampling(fit, nsamp = 500)
head(samps)
# Compare copula vs Gaussian sampling
s_cop <- sampling(fit, nsamp = 500, samp_copula = TRUE)
s_gaus <- sampling(fit, nsamp = 500, samp_copula = FALSE)
# Prior predictive samples
y_prior <- sampling(fit, type = "observed", nsamp = 500, prior = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.