fromlavaan() and tolavaan() converters are new in this release and are flagged experimental. They reproduce lavaan's estimates, fit and standard errors on the supported model classes (see the caveats below), but please report any unexpected behavior.
Contents
Overview
lavaan is the standard R package for structural equation modeling, with its familiar model syntax (=~, ~, ~~). psychonetrics offers complementary strengths: network (GGM) parameterizations, automated network search (prune() / stepup()), Ising and time-series models, and meta-analytic SEM. Version 0.16 adds two converters — validated against lavaan — so you can move a model between the two frameworks and use the best tool for each step.
fromlavaan()converts a fitted lavaan object (or model syntax + data) into an equivalent psychonetricslvmmodel.tolavaan()converts a psychonetricslvmmodel into a fitted lavaan object, or just its parameter table.
The bridge is the lvm family with latent = "cov" and residual = "cov" — the standard ML CFA/SEM parameterization that both packages share.
fromlavaan(): lavaan → psychonetrics
Specify and fit a model in lavaan as usual, then hand the fitted object to fromlavaan(). The resulting psychonetrics model reproduces the lavaan estimates, standard errors, chi-square, degrees of freedom and log-likelihood — after which the full psychonetrics toolbox (modification indices, pruning, network reparameterization) becomes available.
library("psychonetrics")
library("lavaan")
library("dplyr")
data(HolzingerSwineford1939)
# A standard three-factor CFA in lavaan syntax:
HS.model <- 'visual =~ x1 + x2 + x3
textual =~ x4 + x5 + x6
speed =~ x7 + x8 + x9'
fit_lav <- cfa(HS.model, data = HolzingerSwineford1939, meanstructure = TRUE)
# Convert to psychonetrics and (re-)estimate:
mod_pn <- fit_lav %>% fromlavaan %>% runmodel
mod_pn %>% fit
Output
Multi-group models and cross-group equality constraints carry over too. For example, a two-group CFA with equal loadings (group.equal = "loadings") reproduces exactly:
fit_mg <- cfa(HS.model, data = HolzingerSwineford1939,
group = "school", group.equal = "loadings",
meanstructure = TRUE)
mod_mg <- fit_mg %>% fromlavaan %>% runmodel
mod_mg %>% fit
Output
meanstructure = TRUE for the log-likelihood to match exactly. If the lavaan model has no mean structure, fromlavaan() still reproduces estimates, standard errors, chi-square and df, but adds a saturated mean structure and warns that the log-likelihood differs by a constant.
tolavaan(): psychonetrics → lavaan
The reverse converter takes a fitted psychonetrics lvm (with latent = "cov", residual = "cov") and returns either a fitted lavaan object (the default) or a lavaan parameter table (type = "partable"). This is handy for reusing lavaan's reporting, plotting (e.g. semPlot), or downstream tools:
# Round-trip the model from the previous example back to lavaan:
back <- tolavaan(mod_pn)
class(back) # "lavaan"
logLik(back) # -3737.745 (matches the psychonetrics fit)
# Or extract just the parameter table:
pt <- tolavaan(mod_pn, type = "partable")
Output
Estimates, standard errors, chi-square and log-likelihood round-trip exactly, so a model can travel lavaan → psychonetrics → lavaan without drift.
Supported features
The converters target standard maximum-likelihood CFA/SEM. The following are supported:
- Factor loadings, regressions, (residual) variances and covariances, intercepts and latent means.
- Multiple groups and cross-group equality constraints (e.g. measurement invariance).
- Both common identification schemes: marker loadings and
std.lv = TRUE(fixed latent variances). - FIML for missing data.
Features outside this scope (for example ordered-categorical estimators, certain nonlinear constraints, or definitions that do not map onto the lvm cov/cov parameterization) raise an informative error rather than silently producing a wrong model. For network parameterizations, fit in psychonetrics directly — see the LVM page.
Summary
fromlavaan()converts a fitted lavaan object (or syntax + data) to a psychonetricslvmmodel, reproducing estimates, SEs, chi-square, df and log-likelihood.tolavaan()converts a psychonetricslvmback to a fitted lavaan object or parameter table.- Multi-group models, equality constraints,
std.lvand FIML are all supported; unsupported features error informatively. - Use lavaan for specification and reporting, and psychonetrics for network reparameterization, automated search, and robust estimation.