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.
Models with composites Experimental (0.18)
lavaan 0.7 and later estimate composites, defined with the <~ operator. Since psychonetrics 0.18 such models convert to and from the composite variable family: fromlavaan() returns a cvm model, and tolavaan() accepts a cvm model in which latent, residual and composite are all "cov".
library("lavaan")
library("psychonetrics")
data(StarWars)
fit <- sem(' Originals <~ Q5 + Q6 + Q7
Sequels =~ Q8 + Q9 + Q10
Sequels ~ Originals ',
data = StarWars, meanstructure = TRUE, composites.cov = "free")
mod <- fromlavaan(fit, run = TRUE) # a cvm model
c(lavaan = fitMeasures(fit, "chisq"), psychonetrics = mod@fitmeasures$chisq)
# both 3.01 with 6 degrees of freedom
tolavaan(mod) # and back to lavaan
Three things to know:
- Covariances of composite indicators. By default lavaan fixes the variances and covariances of the indicators of composites to their sample values; psychonetrics estimates them, which is
composites.cov = "free"in lavaan. With complete data both give the same chi-square, degrees of freedom, weights, loadings and regressions with the same standard errors, but a different number of parameters (and so a different AIC and BIC).fromlavaan()gives a warning when the lavaan model used the default. - Starting values. When the lavaan model was not fitted or did not converge,
fromlavaan()uses the psychonetrics starting values instead of the lavaan values. On some models with composites lavaan does not converge from its own starting values; in that casetolavaan()on the estimated psychonetrics model supplies the estimates as starting values to lavaan. - Known differences. In lavaan up to at least version 0.7-3, the reported intercept of a composite that is predicted by another endogenous composite differs from the value psychonetrics computes, and a covariance between composite indicators that is fixed to zero is not counted in the degrees of freedom. The fit of the model and all other estimates are the same.
Not convertible: covariances between indicators of different composites, covariances between an indicator of a composite and another observed variable, composites without a fixed weight, and network parameterizations ("ggm", "prec"), which have no lavaan counterpart.
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.- Models with composites (
<~in lavaan 0.7 or later) convert to and from the experimentalcvmfamily. - 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.