Skip to contents

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.

Usage

inlavaan(
  model,
  data,
  model.type = "sem",
  dp = priors_for(),
  vb_correction = TRUE,
  marginal_method = c("skewnorm", "asymgaus", "marggaus", "sampling"),
  marginal_correction = c("shortcut", "hessian", "none"),
  nsamp = 500,
  test = "standard",
  sn_fit_logthresh = -6,
  sn_fit_temp = NA,
  control = list(),
  verbose = TRUE,
  debug = FALSE,
  add_priors = TRUE,
  optim_method = c("nlminb", "ucminf", "optim"),
  numerical_grad = FALSE,
  ...
)

Arguments

model

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.

data

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.

model.type

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.

dp

Default prior distributions on different types of parameters, typically the result of a call to dpriors(). See the dpriors() help file for more information.

vb_correction

Logical indicating whether to apply a variational Bayes correction for the posterior mean vector of estimates. Defaults to TRUE.

marginal_method

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).

marginal_correction

Which type of correction to use when fitting the skew normal or two-piece Gaussian marginals. "hessian" computes the full Hessian-based correction (slow), "shortcut" (default) computes only diagonals, and "none" (or FALSE) applies no correction.

nsamp

The number of samples to draw for all sampling-based approaches (including posterior sampling for model fit indices).

test

Character indicating whether to compute posterior fit indices. Defaults to "standard". Change to "none" to skip these computations.

sn_fit_logthresh

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.

sn_fit_temp

Temperature parameter for fitting the skew normal. If NA, the temperature will be included in the optimisation during the skew normal fit.

control

A list of control parameters for the optimiser.

verbose

Logical indicating whether to print progress messages.

debug

Logical indicating whether to return debug information.

add_priors

Logical indicating whether to include prior densities in the posterior computation.

optim_method

The optimisation method to use for finding the posterior mode. Options include "nlminb" (default), "ucminf", and "optim" (BFGS).

numerical_grad

Logical indicating whether to use numerical gradients for the optimisation.

...

Additional arguments to be passed to the lavaan::lavaan model fitting function.

Value

An S4 object of class INLAvaan which is a subclass of the lavaan::lavaan class.

See also

Typically, users will interact with the specific latent variable model functions instead, including acfa(), asem(), and agrowth().

Examples

# 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
)
#>  Finding posterior mode.
#>  Finding posterior mode. [69ms]
#> 
#>  Computing the Hessian.
#>  Computing the Hessian. [146ms]
#> 
#>  Performing VB correction.
#>  VB correction; mean |δ| = 0.025σ. [122ms]
#> 
#> ⠙ Fitting skew normal to 0/21 marginals.
#>  Fitting skew normal to 21/21 marginals. [719ms]
#> 
#>  Sampling covariances and defined parameters.
#>  Sampling covariances and defined parameters. [89ms]
#> 
#> ⠙ Computing ppp and DIC.
#>  Computing ppp and DIC. [362ms]
#> 
summary(fit)
#> INLAvaan 0.2.3.9004 ended normally after 73 iterations
#> 
#>   Estimator                                      BAYES
#>   Optimization method                           NLMINB
#>   Number of model parameters                        21
#> 
#>   Number of observations                           301
#> 
#> Model Test (User Model):
#> 
#>    Marginal log-likelihood                   -3823.645 
#>    PPP (Chi-square)                              0.000 
#> 
#> Information Criteria:
#> 
#>    Deviance (DIC)                             7675.898 
#>    Effective parameters (pD)                   100.019 
#> 
#> Parameter Estimates:
#> 
#>    Marginalisation method                     SKEWNORM
#>    VB correction                                  TRUE
#> 
#> Latent Variables:
#>                    Estimate       SD     2.5%    97.5%     NMAD    Prior       
#>   visual =~                                                                    
#>     x1                1.000                                                    
#>     x2                0.577    0.115    0.367    0.819    0.041    normal(0,10)
#>     x3                0.764    0.126    0.538    1.034    0.065    normal(0,10)
#>   textual =~                                                                   
#>     x4                1.000                                                    
#>     x5                1.121    0.067    0.995    1.257    0.006    normal(0,10)
#>     x6                0.934    0.058    0.825    1.051    0.005    normal(0,10)
#>   speed =~                                                                     
#>     x7                1.000                                                    
#>     x8                1.224    0.162    0.937    1.571    0.016    normal(0,10)
#>     x9                1.166    0.222    0.798    1.663    0.030    normal(0,10)
#> 
#> Covariances:
#>                    Estimate       SD     2.5%    97.5%     NMAD    Prior       
#>   visual ~~                                                                    
#>     textual           0.445    0.078    0.246    0.552    0.001       beta(1,1)
#>     speed             0.479    0.052    0.145    0.349    0.022       beta(1,1)
#>   textual ~~                                                                   
#>     speed             0.279    0.048    0.259    0.070    0.002       beta(1,1)
#> 
#> Variances:
#>                    Estimate       SD     2.5%    97.5%     NMAD    Prior       
#>    .x1                0.563    0.116    1.368    0.328    0.010 gamma(1,.5)[sd]
#>    .x2                1.140    0.105    0.948    1.360    0.001 gamma(1,.5)[sd]
#>    .x3                0.838    0.096    1.254    0.656    0.007 gamma(1,.5)[sd]
#>    .x4                0.378    0.049    0.480    0.288    0.002 gamma(1,.5)[sd]
#>    .x5                0.450    0.059    0.573    0.342    0.002 gamma(1,.5)[sd]
#>    .x6                0.360    0.044    0.452    0.279    0.002 gamma(1,.5)[sd]
#>    .x7                0.831    0.091    0.668    1.024    0.004 gamma(1,.5)[sd]
#>    .x8                0.503    0.090    1.014    0.332    0.030 gamma(1,.5)[sd]
#>    .x9                0.535    0.094    1.148    0.336    0.019 gamma(1,.5)[sd]
#>     visual            0.786    0.148    1.495    0.523    0.036 gamma(1,.5)[sd]
#>     textual           0.978    0.113    1.214    0.773    0.003 gamma(1,.5)[sd]
#>     speed             0.341    0.090    0.993    0.178    0.047 gamma(1,.5)[sd]
#>