Learn R Programming

psychonetrics (version 0.17.8)

fromlavaan: Convert a lavaan model to a psychonetrics lvm

Description

Converts a lavaan model into an equivalent psychonetrics latent variable model (lvm) with latent = "cov" and residual = "cov". The input can be a fitted lavaan object (e.g., from lavaan::cfa or lavaan::sem), or lavaan model syntax together with a data frame. Only the public lavaan API is used; no lavaan source code is reused.

Usage

fromlavaan(x, data = NULL, run = FALSE, estimator = "default",
           baseline_saturated = TRUE, verbose = FALSE, ...)

Value

An object of class psychonetrics (see psychonetrics-class).

Arguments

x

A fitted lavaan object, or a character string with lavaan model syntax. When syntax is supplied, the model is set up internally with lavaan::sem(model = x, data = data, meanstructure = TRUE, fixed.x = FALSE, do.fit = FALSE).

data

A data frame. Required when x is model syntax; ignored when x is a fitted lavaan object.

run

Logical. If TRUE, the resulting psychonetrics model is estimated with runmodel before being returned.

estimator

The estimator passed to lvm. The default ("default") uses "ML", or "FIML" when lavaan was fitted with full-information maximum likelihood for missing data.

baseline_saturated

Logical, passed to lvm; should the baseline and saturated models be included? Mostly used internally.

verbose

Logical. If TRUE, an experimental-feature note is shown and progress is printed.

...

Further arguments passed to lvm.

Author

Sacha Epskamp

Details

The converted model reproduces the lavaan specification using the LISREL-style model matrices that lavaan exposes through lavInspect(fit, "free") and lavInspect(fit, "est"): factor loadings (lambda), the latent variance--covariance matrix (sigma_zeta, lavaan's psi), the residual variance--covariance matrix (sigma_epsilon, lavaan's theta), structural regressions (beta), observed intercepts (nu) and latent intercepts (nu_eta, lavaan's alpha). Observed variables that appear as structural (regression) variables are carried as phantom latent columns; any latent whose name clashes with an observed variable is renamed to eta_<name>. Equality constraints are detected via shared parameter labels and simple == rows and mapped onto shared psychonetrics parameter (par) indices.

The following are guaranteed to match lavaan (for a standard ML fit, see the caveats below): the parameter estimates, the (expected-information) standard errors, the chi-square test statistic and degrees of freedom, the number of free parameters, and the log-likelihood.

Caveats. (1) If the lavaan model has no meanstructure, psychonetrics adds a saturated mean structure (free nu, nu_eta fixed to 0): estimates, standard errors, chi-square and df still match, but the log-likelihood differs by a constant; a warning is issued. (2) With fixed.x = TRUE and exogenous covariates, psychonetrics treats the covariates as random (endogenous): estimates, standard errors and chi-square match, but df-based fit measures and the log-likelihood do not (lavaan conditions on the covariates); a warning is issued. (3) If lavaan used likelihood = "wishart" (the \(n-1\) normalization), psychonetrics uses the normal (\(n\)) likelihood, so the log-likelihood differs; a warning is issued. (4) Robust standard errors and robust/scaled test statistics are not transferred; the model is converted as plain ML, with a warning. (5) For FIML, psychonetrics uses expected information for standard errors, whereas lavaan defaults to observed information, so FIML standard errors may differ slightly even though the log-likelihood matches.

Unsupported. The following lavaan features cannot be converted and raise an informative error: two-level / multilevel models (cluster=, level:), defined parameters (:=), inequality constraints (<, >), general (non-simple) equality constraints, formative indicators (<~), conditional.x = TRUE, and categorical (ordinal) endogenous variables.

References

Rosseel, Y. (2012). lavaan: An R Package for Structural Equation Modeling. Journal of Statistical Software, 48(2), 1-36. tools:::Rd_expr_doi("10.18637/jss.v048.i02")

See Also

tolavaan, lvm

Examples

Run this code
# \donttest{
if (requireNamespace("lavaan", quietly = TRUE)){
  library("lavaan")

  # A confirmatory factor analysis fitted in lavaan:
  data(HolzingerSwineford1939)
  HS <- HolzingerSwineford1939
  fit <- cfa(' visual  =~ x1 + x2 + x3
               textual =~ x4 + x5 + x6
               speed   =~ x7 + x8 + x9 ',
             data = HS, meanstructure = TRUE)

  # Convert to a psychonetrics lvm and run it:
  mod <- fromlavaan(fit, run = TRUE)
  fit(mod)

  # The log-likelihood matches lavaan:
  c(lavaan = as.numeric(logLik(fit)), psychonetrics = mod@fitmeasures$logl)

  # Equivalent starting from syntax + data (without fitting in lavaan first):
  mod2 <- fromlavaan(' visual  =~ x1 + x2 + x3
                       textual =~ x4 + x5 + x6
                       speed   =~ x7 + x8 + x9 ',
                     data = HS, run = TRUE)
}
# }

Run the code above in your browser using DataLab