semSyntax {semPlot}R Documentation

Produce model syntax for various SEM software

Description

This function produces a model object or model syntax for SEM software based on a semPlotModel-class object. If the input is not a "semPlotModel" object the semPlotModel function is run on the input. This allows to create model syntax for one program based on the output of another program.

Currently only the R packages 'lavaan' (Rosseel, 2012) and 'sem' (Fox, Nie & Byrnes, 2012) are supported.

Usage

semSyntax(object, syntax = "lavaan", file)

Arguments

object

A "semPlotModel" object or any of the input possibilities for semPlotModel.

syntax

A string indicating which syntax to be used for the output. Currently supported are 'lavaan' and 'sem'.

file

Path of a file the model should be written to.

Value

A string containing the lavaan model syntax or a "semmod" object for the sem package.

Author(s)

Sacha Epskamp <mail@sachaepskamp.com>

References

Yves Rosseel (2012). lavaan: An R Package for Structural Equation Modeling. Journal of Statistical Software, 48(2), 1-36. URL http://www.jstatsoft.org/v48/i02/.

John Fox, Zhenghua Nie and Jarrett Byrnes (2012). sem: Structural Equation Models. R package version 3.0-0. http://CRAN.R-project.org/package=sem

See Also

semPlotModel semPlotModel-class semPaths

Examples


# MIMIC model, example 5.8 from mplus user guide:
Data <- read.table("http://www.statmodel.com/usersguide/chap5/ex5.8.dat")
names(Data) <- c(paste("y", 1:6, sep=""),
                 paste("x", 1:3, sep=""))

# Data <- Data[,c(7:9,1:6)]

# Model:
model.Lavaan <- 'f1 =~ y1 + y2 + y3
f2 =~ y4 + y5 + y6
f1 + f2 ~ x1 + x2 + x3 '

# Run Lavaan:
library("lavaan")
fit.Lavaan <- lavaan:::cfa(model.Lavaan, data=Data, std.lv=TRUE)

# Obtain Lavaan syntax:
model.Lavaan2 <- semSyntax(fit.Lavaan, "lavaan")
## 
## Model <- '
## f1 =~ y1
## f1 =~ y2
## f1 =~ y3
## f2 =~ y4
## f2 =~ y5
## f2 =~ y6
## f1 ~ x1
## f1 ~ x2
## f1 ~ x3
## f2 ~ x1
## f2 ~ x2
## f2 ~ x3
## y1 ~~ y1
## y2 ~~ y2
## y3 ~~ y3
## y4 ~~ y4
## y5 ~~ y5
## y6 ~~ y6
## f1 ~~ 1*f1
## f2 ~~ 1*f2
## f1 ~~ f2
## x1 ~~ 2.92060364355652*x1
## x1 ~~ 0.175914393445822*x2
## x1 ~~ -0.150978787074975*x3
## x2 ~~ 2.08178484352461*x2
## x2 ~~ 0.125476432507783*x3
## x3 ~~ 1.04155105145699*x3
## '

# Run Lavaan again:
fit.Lavaan2 <- lavaan:::lavaan(model.Lavaan2, data=Data)
## Warning: lavaan WARNING: model syntax contains
## variance/covariance/intercept formulas involving (an) exogenous
## variable(s): [x1 x2 x3]; Please use fixed.x=FALSE or leave them alone

# Compare models:
layout(t(1:2))
semPaths(fit.Lavaan,"std",title=FALSE)
title("Lavaan model 1",line=3)
semPaths(fit.Lavaan2, "std",title=FALSE)
title("Lavaan model 2",line=3)

plot of chunk unnamed-chunk-1


# Convert to sem model:
model.sem <- semSyntax(fit.Lavaan, "sem")
## 
## Model <- specifyModel()
## f1 -> y1,par1,NA
## f1 -> y2,par2,NA
## f1 -> y3,par3,NA
## f2 -> y4,par4,NA
## f2 -> y5,par5,NA
## f2 -> y6,par6,NA
## x1 -> f1,par7,NA
## x2 -> f1,par8,NA
## x3 -> f1,par9,NA
## x1 -> f2,par10,NA
## x2 -> f2,par11,NA
## x3 -> f2,par12,NA
## y1 <-> y1,par13,NA
## y2 <-> y2,par14,NA
## y3 <-> y3,par15,NA
## y4 <-> y4,par16,NA
## y5 <-> y5,par17,NA
## y6 <-> y6,par18,NA
## f1 <-> f1,NA,1
## f2 <-> f2,NA,1
## f1 <-> f2,par19,NA
## x1 <-> x1,NA,2.92060364355652
## x1 <-> x2,NA,0.175914393445822
## x1 <-> x3,NA,-0.150978787074975
## x2 <-> x2,NA,2.08178484352461
## x2 <-> x3,NA,0.125476432507783
## x3 <-> x3,NA,1.04155105145699

# Run sem:
library("sem")
fit.sem <- sem:::sem(model.sem, data = Data)
## Warning: NA//Inf replaced by maximum positive value

# Compare models:
layout(t(1:2))
semPaths(fit.Lavaan,"std",title=FALSE)
title("Lavaan",line=3)
semPaths(fit.sem, "std",title=FALSE)
title("sem",line=3)

plot of chunk unnamed-chunk-1



[Package semPlot version 0.3 Index]