Learn R Programming

psychonetrics (version 0.17.8)

tolavaan: Convert a psychonetrics lvm to a lavaan model

Description

Converts a psychonetrics latent variable model (lvm) with latent = "cov" and residual = "cov" into a fitted lavaan object, or into a lavaan parameter table. Only the public lavaan API (lavaan::lavaan) is used; no lavaan source code is reused.

Usage

tolavaan(x, fit = TRUE, verbose = FALSE, ...)

Value

A fitted lavaan object when fit = TRUE, or a lavaan parameter table (data.frame) when fit = FALSE.

Arguments

x

A psychonetrics object created with lvm (or a wrapper such as lnm) using latent = "cov" and residual = "cov".

fit

Logical. If TRUE (default), the model is fitted with lavaan::lavaan and the resulting lavaan object is returned. If FALSE, the lavaan parameter table (a data.frame) is returned without fitting.

verbose

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

...

Further arguments passed to lavaan::lavaan. For example, pass do.fit = FALSE to obtain an unfitted lavaan object that uses the psychonetrics estimates as starting values.

Author

Sacha Epskamp

Details

Only models with latent = "cov" and residual = "cov" can be converted, because network (ggm/prec) and Cholesky (chol) parameterizations have no direct lavaan equivalent. The function emits one lavaan parameter-table row for every element of the psychonetrics matrices lambda, sigma_zeta, sigma_epsilon, beta, nu and nu_eta (including elements that are fixed to zero, so that lavaan adds no default parameters). Free parameters are given sequential free indices and fixed parameters their fixed value via ustart. Equality constraints (shared psychonetrics par indices) are encoded with per-row plabels and explicit == rows, which is the only encoding lavaan honors for cross-row equality.

Warm start. When the psychonetrics model has been estimated (x@computed is TRUE), the estimates are also written as starting values (ustart) for the free parameters. lavaan then typically polishes this warm start by a few optimizer steps, so the refitted log-likelihood matches to within optimizer tolerance (about \(10^{-5}\)) rather than exactly.

Data. If the psychonetrics model stored its raw data (fitted with storedata = TRUE), that raw data is passed to lavaan (with the grouping variable for multi-group models, and missing = "ml" for FIML). Otherwise the sample covariance matrix (and the sample means, when a mean structure is present) is passed with sample.cov.rescale = FALSE and likelihood = "normal", since psychonetrics covariances already use the \(n\) (maximum-likelihood) normalization. FIML conversion therefore requires the raw data: re-fit the lvm with storedata = TRUE first.

Unsupported. The following raise an informative error: non-lvm models, lvm models with latent or residual not equal to "cov", correlation-input or ordinal models, estimators other than "ML" or "FIML", and FIML models fitted without storedata = TRUE.

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

fromlavaan, lvm

Examples

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

  # A psychonetrics CFA:
  data(HolzingerSwineford1939, package = "lavaan")
  HS <- lavaan::HolzingerSwineford1939
  Lambda <- matrix(0, 9, 3)
  Lambda[1:3, 1] <- Lambda[4:6, 2] <- Lambda[7:9, 3] <- 1

  mod <- lvm(HS, lambda = Lambda, latent = "cov", residual = "cov",
             vars = paste0("x", 1:9),
             latents = c("visual", "textual", "speed"),
             identification = "loadings") %>%
    runmodel

  # Convert to a fitted lavaan object:
  lavfit <- tolavaan(mod)
  c(psychonetrics = mod@fitmeasures$logl,
    lavaan = as.numeric(logLik(lavfit)))

  # Or obtain just the parameter table:
  PT <- tolavaan(mod, fit = FALSE)
  head(PT)
}
# }

Run the code above in your browser using DataLab