Skip to contents

Create lavaan model syntax from matrix coefficients

Usage

matrix2lavaan(
  measurement_model = NULL,
  structural_model = NULL,
  covariances = NULL
)

Arguments

measurement_model

A matrix or data.frame with measurement model loadings. Column names are latent variables. Row names or the first column of a data.frame are indicator variables.

structural_model

A matrix or data.frame with structural model coefficients (i.e., regressions). Column names are "causal" variables. Row names or the first column of a data.frame are "effect" variables.

covariances

A matrix or data.frame with model covariances. Column names must match the row names. If a data.frame, row variable names can be specified in the first column.

Value

a character string with lavaan syntax

Examples

library(simstandard)
# Specifying the measurement model:
# For a data.frame, the column names are latent variables,
# and the indicators can be specified as rownames.
m <- data.frame(X = c(0.7,0.8,0,0),
                Y = c(0,0,0.8,0.9))
rownames(m) <- c("A", "B", "C", "D")
# Indicator variables can also be specified
# as the first column variable
# with subsequent column names as latent variables
m <- data.frame(Indicators = c("A", "B", "C", "D"),
                X = c(0.7,0.8,0,0),
                Y = c(0,0,0.8,0.9))
# Alternately, a matrix can be used:
m <- matrix(c(0.7,0.8,0,0,
              0,0,0.8,0.9),
              ncol = 2,
              dimnames = list(c("A", "B", "C", "D"),
                              c("X", "Y")))
# Specifying the structural coefficients:
# The regression coefficients of the structural model can be
# specified as either a data.frame or a matrix. Column names
# are the predictors and row names are the criterion variables.
# With a data.frame, criterion variables can alternataly be
# specified with as the first column.
s <- matrix(0.5, nrow = 1, ncol = 1, dimnames = list("Y", "X"))
# The covariance matrix must be symmetric. Can also be specified
# as a data. frame.
Sigma <- matrix(c(1, 0.3,
                  0.3, 1),
                nrow = 2,
                ncol = 2,
                dimnames = list(c("B","C"),
                                c("B","C")) )
model <- matrix2lavaan(measurement_model = m,
                       structural_model = s,
                       covariances = Sigma)
cat(model)
#> X =~ 0.7 * A + 0.8 * B
#> Y =~ 0.8 * C + 0.9 * D
#> Y ~ 0.5 * X
#> B ~~ 0.3 * C