This function fits a Bayesian latent variable model by approximating the posterior distributions of the model parameters using various methods, including skew-normal, asymmetric Gaussian, marginal Gaussian, or sampling-based approaches. It leverages the lavaan package for model specification and estimation.
inlavaan(
model,
data,
model.type = "sem",
dp = priors_for(),
test = "standard",
vb_correction = TRUE,
marginal_method = c("skewnorm", "asymgaus", "marggaus", "sampling"),
marginal_correction = c("shortcut", "shortcut_fd", "hessian", "none"),
nsamp = 1000,
samp_copula = TRUE,
sn_fit_ngrid = 21,
sn_fit_logthresh = -6,
sn_fit_temp = 1,
sn_fit_sample = TRUE,
control = list(),
verbose = TRUE,
debug = FALSE,
add_priors = TRUE,
optim_method = c("nlminb", "ucminf", "optim"),
numerical_grad = FALSE,
cores = NULL,
...
)An S4 object of class INLAvaan which is a subclass of the
lavaan::lavaan class.
A description of the user-specified model. Typically, the model
is described using the lavaan model syntax. See
model.syntax for more information. Alternatively, a
parameter table (eg. the output of the lavParTable() function) is also
accepted.
An optional data frame containing the observed variables used in the model. If some variables are declared as ordered factors, lavaan will treat them as ordinal variables.
Set the model type: possible values
are "cfa", "sem" or "growth". This may affect
how starting values are computed, and may be used to alter the terminology
used in the summary output, or the layout of path diagrams that are
based on a fitted lavaan object.
Default prior distributions on different types of
parameters, typically the result of a call to dpriors().
See the dpriors() help file for more information.
Character indicating whether to compute posterior fit indices. Defaults to "standard". Change to "none" to skip these computations.
Logical indicating whether to apply a variational Bayes
correction for the posterior mean vector of estimates. Defaults to TRUE.
The method for approximating the marginal posterior
distributions. Options include "skewnorm" (skew-normal), "asymgaus"
(two-piece asymmetric Gaussian), "marggaus" (marginalising the Laplace
approximation), and "sampling" (sampling from the joint Laplace
approximation).
Which type of correction to use when fitting the
skew-normal or two-piece Gaussian marginals. "hessian" computes the full
"shortcut" (default) computes only diagonals via central differences
(full z-trace plus Schur complement correction), "shortcut_fd" is the
same formula using forward differences (roughly half the cost, less
accurate), "hessian" computes the full Hessian-based correction (slow),
and "none" (or FALSE) applies no correction.
The number of samples to draw for all sampling-based approaches (including posterior sampling for model fit indices).
Logical. When TRUE (default), posterior samples are
drawn using the copula method with the fitted marginals (e.g.
skew-normal or asymmetric Gaussian), with NORTA correlation adjustment.
When FALSE, samples are drawn from the Gaussian (Laplace)
approximation. Only re
Number of grid points to lay out per dimension when
fitting the skew-normal marginals. A finer grid gives a better fit at the
cost of more joint-log-posterior evaluations. Defaults to 21.
The log-threshold for fitting the skew-normal. Points
with log-posterior drop below this threshold (relative to the maximum) will
be excluded from the fit. Defaults to -6.
Temperature parameter for fitting the skew-normal.
Defaults to 1 (weights are the density values themselves). If
NA, the temperature is included as an additional optimisation parameter.
Logical. When TRUE (default), a parametric
skew-normal is fitted to the posterior samples for covariance and defined
parameters. When FALSE, these are summarised using kernel density
estimation instead.
A list of control parameters for the optimiser.
Logical indicating whether to print progress messages.
Logical indicating whether to return debug information.
Logical indicating whether to include prior densities in the posterior computation.
The optimisation method to use for finding the posterior
mode. Options include "nlminb" (default), "ucminf", and "optim"
(BFGS).
Logical indicating whether to use numerical gradients
for the optimisation. Defaults to FALSE to use analytical gradients.
Integer or NULL. Number of cores for parallel marginal
fitting. When NULL (default), serial execution is used unless the number
of free parameters exceeds 120, in which case parallelisation is enabled
automatically using all available physical cores. Set to 1L to force
serial execution. If cores > 1, marginal fits are distributed across
cores using parallel::mclapply() (fork-based; no parallelism on
Windows).
Additional arguments to be passed to the lavaan::lavaan model fitting function.
Typically, users will interact with the specific latent variable
model functions instead, including acfa(), asem(), and agrowth().
# The Holzinger and Swineford (1939) example
HS.model <- "
visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9
"
utils::data("HolzingerSwineford1939", package = "lavaan")
fit <- inlavaan(
HS.model,
data = HolzingerSwineford1939,
auto.var = TRUE,
auto.fix.first = TRUE,
auto.cov.lv.x = TRUE
)
summary(fit)
Run the code above in your browser using DataLab