semplot: unified visualizations of structural equation...

54
semPlot: Unified visualizations of Structural Equation Models Sacha Epskamp University of Amsterdam Department of Psychological Methods Psychoco 2014 13-02-2014

Upload: others

Post on 23-Feb-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPlot: Unified visualizations of StructuralEquation Models

Sacha Epskamp

University of AmsterdamDepartment of Psychological Methods

Psychoco 201413-02-2014

Page 2: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPlot

I R package dedicated to visualizing structural equationmodels (SEM)

I fills the gap between advanced, but time-consuming,graphical software and the limited graphics producedautomatically by SEM software

I Also unifies different SEM software packages and modelframeworks in R

I General framework for extracting parameters from differentSEM software packages to different SEM modelingframeworks

I Sister package and extension to qgraph (Epskamp,Cramer, Waldorp, Schmittmann, & Borsboom, 2012)

Page 3: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Supported input

I R (R Core Team, 2013) objects:I lmI loadingsI factanalI princompI principal (Revelle, 2010)

I R package output:I lavaan (Rosseel, 2012)

I Output and modelI sem (Fox, Nie, & Byrnes, 2013)I OpenMx (Boker et al., 2011)

I Path specification onlyI String indication output file of:

I MPlus (L. K. Muthén & B. O. Muthén, 1998–2012)I Via MplusAutomation (Hallquist & Wiley, 2013)

I LISREL (Jöreskog & Sörbom, 1996)I Via lisrelToR (Epskamp, 2013)

Page 4: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPlotModel

SEM package output:

I lavaanI semI OpenMxI MPlusI LISRELI Onyx

Model specification:

I lavaan modelI lisrelModel()I ramModel()

non-SEM objects:

I lmI loadingsI factanalI principalI princomp

semPaths()

semCors()

semSyntax()

modelMatrices()

semMatrixAlgebra()

Page 5: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPlotModel

SEM package output:

I lavaanI semI OpenMxI MPlusI LISRELI Onyx

Model specification:

I lavaan modelI lisrelModel()I ramModel()

non-SEM objects:

I lmI loadingsI factanalI principalI princomp

semPaths()

semCors()

semSyntax()

modelMatrices()

semMatrixAlgebra()

Page 6: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Components of a SEM model

I Square nodes indicate manifest or observed variablesI Circular nodes indicate latent or unobserved variablesI Triangular nodes indicate constant variables (intercepts)I Directed edges indicate linear regression parametersI Bidirectional edges indicate (co)variancesI (Residual) variances can be indicated in several ways:

I Double headed selfloops (RAM style)I Incoming edge with no origin on endogenous variables only

(LISREL style)I As a latent variable (not yet supported in semPlot)

Page 7: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

λ11(y)

λ21(y) λ31

(y) λ42(y) λ52

(y) λ62(y)

θ11(ε) θ22

(ε) θ33(ε) θ44

(ε) θ55(ε) θ66

(ε)

ψ11

ψ21

ψ22

τ1(y) τ2

(y) τ3(y) τ1

(y) τ2(y) τ3

(y)y1 y2 y3 y4 y5 y6

η1 η2

1 1 1 1 1 1

y1 = τ1 + λ11η1 + ε1

...y6 = τ6 + λ62η2 + ε6

Page 8: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

λ11(y)

λ21(y) λ31

(y) λ42(y) λ52

(y) λ62(y)

θ11(ε) θ22

(ε) θ33(ε) θ44

(ε) θ55(ε) θ66

(ε)

ψ21

τ1(y) τ2

(y) τ3(y) τ1

(y) τ2(y) τ3

(y)y1 y2 y3 y4 y5 y6

η1 η2

1 1 1 1 1 1

y1 = τ1 + λ11η1 + ε1

...y6 = τ6 + λ62η2 + ε6

Page 9: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

I semPaths can be used to to plot path diagramsI The first argument can be a semPlotModel or any input

optionI The second argument specifies what the edge color and

width representI path, diagram or modI est or parI stand or stdI eq or consI col

I The third argument specifies what the edge labelsrepresent

I name, label, path or diagramI est or parI stand or stdI eq or consI no, omit, hide or invisible

I These arguments use fuzzy matchingI To visualize parameter estimates I recommend setting

edge weights to standardized estimates and edge labels toestimates

Page 10: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

library("lavaan")## The famous Holzinger and Swineford (1939) exampleHS.model <- ' visual =~ x1 + x2 + x3

textual =~ x4 + x5 + x6speed =~ x7 + x8 + x9 '

fit <- cfa(HS.model, data=HolzingerSwineford1939)

Page 11: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPaths(fit)

x1 x2 x3 x4 x5 x6 x7 x8 x9

vsl txt spd

Page 12: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPaths(fit, "Standardized", "Estimates")

0.450.37

0.17

0.360.55

0.55

0.41

0.26

0.49 0.57

1.000.73

0.84

1.08

0.80

1.181.00

1.13

0.931.00 1.11

0.380.81 0.98

x1 x2 x3 x4 x5 x6 x7 x8 x9

vsl txt spd

Page 13: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPaths(fit, "std", "est")

0.450.37

0.17

0.360.55

0.55

0.41

0.26

0.49 0.57

1.000.73

0.84

1.08

0.80

1.181.00

1.13

0.931.00 1.11

0.380.81 0.98

x1 x2 x3 x4 x5 x6 x7 x8 x9

vsl txt spd

Page 14: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPathssemPaths has quite a lot of arguments:

style, layout, intercepts, residuals, thresholds, rotation,curve, curvature, nCharNodes, nCharEdges, sizeMan,sizeLat, sizeInt, sizeMan2, sizeLat2, sizeInt2,shapeMan, shapeLat, shapeInt, ask, mar, title,title.color, title.adj, title.line, title.cex, include,combineGroups, manifests, latents, groups, color,residScale, gui, allVars, edge.color, reorder, structural,ThreshAtSide, thresholdColor, thresholdSize,fixedStyle, freeStyle, as.expression, optimizeLatRes,inheritColor, levels, nodeLabels, edgeLabels, pastel,rainbowStart, intAtSide, springLevels, nDigits, exoVar,exoCov, centerLevels, panelGroups, layoutSplit,measurementLayout, subScale, subScale2, subRes,subLinks, modelOpts, curveAdjacent, edge.label.cex,cardinal, equalizeManifests, covAtResiduals, bifactor,optimPoints

Page 15: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPaths

And even more via the qgraph backend:edge.width, node.width, node.height, esize, asize,minimum, maximum, cut, details, mar, filetype,filename, width, height, normalize, DoNotPlot, plot,rescale, label.cex, label.color, borders, border.color,border.width, polygonList, vTrans, label.prop,label.norm, label.scale, label.font, posCol, negCol,unCol, colFactor, trans, fade, loop,curvePivot,curvePivotShape, edge.label.bg,edge.label.position, edge.label.font, layout.par, bg,bgcontrol, bgres, pty, font, arrows, arrowAngle, asize,open, weighted, XKCD, . . .

Page 16: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPaths(fit, "std", "est", style = "mx")

0.450.37

0.17

0.360.55

0.55

0.41

0.26

0.49 0.57

1.000.73

0.84

1.08

0.80

1.181.00

1.13

0.931.00 1.11

0.380.81 0.98

x1 x2 x3 x4 x5 x6 x7 x8 x9

vsl txt spd

Page 17: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPaths(fit, "std", "est", style = "lisrel")

0.450.37

0.17

0.360.55

0.55

0.41

0.26

0.49 0.57

1.000.73

0.84

1.08

0.80

1.181.00

1.13

0.931.00 1.11

x1 x2 x3 x4 x5 x6 x7 x8 x9

vsl txt spd

Page 18: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Multi-level

semPaths(file.choose(), "model", "estimates",style = "lisrel", curve = 0.8, nCharNodes = 0,sizeLat = 12, sizeLat2 = 6, title = TRUE,mar = c(5, 1, 5, 1), curvePivot = FALSE,edge.label.cex = 0.5)

Page 19: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

1.14 1.14 1.39 1.41 2.48 2.91 1.25 0.88 0.66 1.86 1.71 1.99 1.17 0.83 1.27 1.53

0.600.48

0.49

0.480.55

0.72

ANSWER1 ANSWER2 ANSWER3 ANSWER8 ANSWER4 ANSWER5 ANSWER6 ANSWER9 ANSWER16 ANSWER10 ANSWER11 ANSWER12 ANSWER7 ANSWER13 ANSWER14 ANSWER15

WITHDRAW SELFCARE COMPLIAN ANTISOCI

Within

0.59 0.56 0.82 0.76 1.34 1.57 0.92 0.59 0.40 0.94 0.87 1.09 0.67 0.51 0.90 1.05

0.830.71

0.78

0.730.76

0.85

0.14 0.17 0.30 0.46 0.80 0.11 0.200.23 0.19 0.13 0.23 0.21 0.13 0.20 0.180.34

ANSWER1 ANSWER2 ANSWER3 ANSWER8 ANSWER4 ANSWER5 ANSWER6 ANSWER9 ANSWER16 ANSWER10 ANSWER11 ANSWER12 ANSWER7 ANSWER13 ANSWER14 ANSWER15

BWITHDRA BSELFCAR BCOMPLIA BANTISOC

Between

Page 20: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Constraints

library("semTools")fits <- example(measurementInvariance)semPaths(fits$value$fit.intercepts, "equality",

"estimates", sizeLat = 5, title = FALSE,ask = FALSE, levels = c(1, 2, 4), edge.label.cex = 0.5,mar = c(0.1, 0.1, 0.1, 0.1))

Page 21: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Constraints

1.00 0.58 0.80 1.00 1.12 0.93 1.00 1.13 1.01

0.56 1.30 0.94 0.45 0.50 0.26 0.89 0.54 0.65

0.80 0.88 0.32

0.41

0.180.18

5.00 6.15 2.27 2.78 4.03 1.93 4.24 5.63 5.47

0.00 0.00 0.00

x1 x2 x3 x4 x5 x6 x7 x8 x9

vsl txt spd

1 1 1 1 1 1 1 1 1

1 1 1

1.00 0.58 0.80 1.00 1.12 0.93 1.00 1.13 1.01

0.65 0.96 0.64 0.34 0.38 0.44 0.63 0.43 0.52

0.71 0.87 0.51

0.43

0.330.24

5.00 6.15 2.27 2.78 4.03 1.93 4.24 5.63 5.47

−0.15 0.58 −0.18

x1 x2 x3 x4 x5 x6 x7 x8 x9

vsl txt spd

1 1 1 1 1 1 1 1 1

1 1 1

Page 22: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Structural Models# lavaan sem example:example(sem)semPaths(fit, "model", "est", style = "lisrel")

1.00 2.18 1.82

1.00 1.19 1.17 1.25 1.00 1.19 1.17 1.25

1.47 0.60

0.87

0.581.44

2.18 0.71 0.361.37

0.08 0.12 0.47

1.85 7.58 4.96 3.22 2.31 4.97 3.56 3.31

3.88 0.16

x1 x2 x3

y1 y2 y3 y4 y5 y6 y7 y8

i60

d60 d65

Page 23: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Layout Algorithms

I semPaths can use several tree-like layout algorithmstree Based on LISREL (Jöreskog & Sörbom, 1996)

tree2 Variation of the Reingold-Tilford algorithm(Reingold & Tilford, 1981)

tree3 Variation of Boker, McArdle, and Neale (2002)I Exogenous variables on top, endogenous variables at the

bottomI Can be rotated

I These layouts can be circularized (circle, circle2 andcircle3)

I Alternatively any igraph algorithm can be usedI layoutSplit can be used to split layout of measurement

models and structural model

Page 24: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

# Example 5.25 from mplus user guide:l <- "http://www.statmodel.com/usersguide/chap5/ex5.11.out"download.file(l, modfile <- tempfile(fileext = ".out"))Model <- semPlotModel(modfile)

Page 25: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPaths(Model, rotation = 2)

Y1

Y2

Y3

Y4

Y5

Y6

Y7

Y8

Y9

Y10

Y11

Y12

F1

F2

F3

F4

1

1

1

1

1

1

1

1

1

1

1

1

Page 26: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semPaths(Model, "col", "est", rotation = 2,groups = "latents", pastel = TRUE,edge.label.cex = 0.5, intercepts = TRUE,mar = c(1, 1, 1, 1))

1.00

1.18

0.94

1.00

0.87

0.89

1.00

0.87

0.88

1.00

0.83

0.68

0.47

0.56

0.79−0.03

0.01

0.05

0.00

0.10

0.08

0.07

0.05

0.06

0.08

−0.01

0.04

0.03

0.88

0.89

1.03

0.80

1.14

1.15

0.95

1.06

0.95

0.94

0.90

1.20

0.92

1.07

0.55

0.56

Y1

Y2

Y3

Y4

Y5

Y6

Y7

Y8

Y9

Y10

Y11

Y12

F1

F2

F3

F4

1

1

1

1

1

1

1

1

1

1

1

1

Page 27: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

LISREL style layout

semPaths(<...>, layout = "tree")

1.00

1.18

0.94

1.00

0.87

0.89

1.00

0.87

0.88

1.00

0.83

0.68

0.47

0.56

0.79−0.03

0.01

0.05

0.00

0.10

0.08

0.07

0.05

0.06

0.08

−0.01

0.04

0.03

0.88

0.89

1.03

0.80

1.14

1.15

0.95

1.06

0.95

0.94

0.90

1.20

0.92

1.07

0.55

0.56

Y1

Y2

Y3

Y4

Y5

Y6

Y7

Y8

Y9

Y10

Y11

Y12

F1

F2

F3

F4

1

1

1

1

1

1

1

1

1

1

1

1

Page 28: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Reingold-Tilford based layout

semPaths(<...>, layout = "tree2", centerLevels = FALSE)

1.00

1.18

0.94

1.00

0.87

0.89

1.00

0.87

0.88

1.00

0.83

0.68

0.47

0.56

0.79−0.03

0.01

0.05

0.00

0.10

0.08

0.07

0.05

0.06

0.08

−0.01

0.04

0.03

0.88

0.89

1.03

0.80

1.14

1.15

0.95

1.06

0.95

0.94

0.90

1.20

0.92

1.07

0.55

0.56

Y1

Y2

Y3

Y4

Y5

Y6

Y7

Y8

Y9

Y10

Y11

Y12

F1

F2

F3

F4

1

1

1

1

1

1

1

1

1

1

1

1

Page 29: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Boker-McArdle-Neale based layout

semPaths(<...>, layout = "tree3", optimizeLatRes = TRUE)

1.00

1.18

0.94

1.00

0.87

0.89

1.00

0.87

0.88

1.00

0.83

0.68

0.47

0.56

0.79

−0.03

0.01

0.05

0.00

0.10

0.08

0.07

0.05

0.06

0.08

−0.01

0.04

0.03

0.88

0.89

1.03

0.80

1.14

1.15

0.95

1.06

0.95

0.94

0.90

1.20

0.92

1.07

0.55

0.56

Y1

Y2

Y3

Y4

Y5

Y6

Y7

Y8

Y9

Y10

Y11

Y12

F1

F2

F3 F4

1

1

1

1

1

1

1

1

1

1

1

1

Page 30: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Split measurement and structural models

semPaths(<...>, layout = "tree3", layoutSplit = TRUE)

1.00

1.18

0.94

1.00

0.87

0.89

1.000.870.88

1.00

0.83

0.68

0.47

0.56

0.79

−0.03

0.01

0.05

0.00

0.10

0.08

0.07 0.050.060.08

−0.01

0.04

0.03

0.88

0.89

1.03

0.80

1.14

1.15

0.95

1.06 0.950.940.90

1.20

0.92

1.07

0.55

0.56

Y1

Y2

Y3

Y4

Y5

Y6

Y7Y8Y9

Y10

Y11

Y12

F1

F2

F3 F4

1

1

1

1

1

1

111

1

1

1

Page 31: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

library("qgraph")data(big5)res <- factanal(big5, 5, rotation = "promax")semPaths(res, "standardized", "hide", residuals = FALSE,

sizeMan = 1, mar = c(1, 1, 1, 1), NCharNodes = 0,layout = "circle")

N1 N6 N11 N16 E17 N26 C30 N31 N36 N41 E42N56

N61N71

N76E77

N86E87

O88N91

N101N106

E107E112

N116N121

C130N131

N136

E137

O138

N146

E147

N151

C155

N161

N166

E167

N176

N181

N186

N191

N196

C205

N206

N211

N216

N221

N226

N231

N236

E2

E7

A19

N21

E27

E32

A34

C35

E37

O43

E57

A59

E62

A64

E67

O68

A74

N81

A89

C90

E92

A94

E97

A99

A104

N111

E117

E122

A124

N126

A129

O133

A134

N141

E142

A149

E152

A154

O163

A164

O168N171

E177A179

E182A184

E187O193

A194O198

E207E212

A214E217

A224E227

E237O238

C5C10C15O18C20C25C40N46E47C50C55C60C65C70C75O78C80C85C95C100C110C115C120

C125E127

C135C140

C145O153

C160C165

C170C175

C180C185

C195C200

O203C210

C215

C220

C225

O228

C230

C235

A239

C24

A4

A9

E12

A14

E22

A24

A29

A39

A44

C45

A49

N51

A54

N66

A69

E72

A79

E82

A84

N96

E102

C105

A109

A114

E132

A139

A144

C150

N156

A159

E162

A169

E172

A174

A189

E192

E197

A199

N201

E202

A204

O208

A219

E222

A229

E232

A234

O3

O8

O13

O23

O28

O33

O38

O48

E52O53

O58O63

O73O83

O93O98

O103O108

O113O118

A119O123

O128O143

O148E157

O158 O173 O178 O183 O188 C190 A209 O213 O218 O223 O233

Fc1

Fc2

Fc3

Fc4

Fc5

Page 32: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Manual specification

library('lavaan')

example(sem)

L <- matrix(c(

"y1", "y2", "y3", "y4", "y5", "y6", "y7", "y8","x1", NA, NA, "dem60", NA, NA, "dem65", NA,"x2", NA, NA, "ind60", NA, NA, NA, NA,"x3", NA, NA, NA, NA , NA, NA, NA),

,4 )

Page 33: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Graph <- semPaths(fit,layout=L,nCharNodes=0,edge.color="black",label.scale=FALSE,label.cex=1.0,residuals=FALSE,fixedStyle=1,freeStyle=1,exoVar=FALSE,sizeMan=4,sizeLat=10,DoNotPlot = TRUE

)

Graph$graphAttributes$Edges$curve <-ifelse(Graph$Edgelist$bidir, -2, 0)

Page 34: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

plot(Graph)

a

b

c

a

b

c

x1 x2 x3y1

y2

y3

y4

y5

y6

y7

y8

ind60dem60

dem65

Page 35: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Model by Janneke de Kort

θ11(ε) θ22

(ε) θ33(ε) θ44

(ε)

θ55(ε) θ66

(ε) θ77(ε) θ88

(ε)

ψ55

ψ175

ψ66

ψ186

ψ77

ψ197

ψ88

ψ208

ψ99

ψ219

ψ1010

ψ2210

ψ1111

ψ2311

ψ1212

ψ2412

ψ1717 ψ1818 ψ1919 ψ2020

ψ2121 ψ2222 ψ2323 ψ2424

β101

β141

β112

β152

β123

β163

β15

β65

β26

β76

β37

β87

β48

β19

β109

β210

β1110

β311

β1211

β412

β213

β2213

β314

β2314

β415

β2415

β1317

β1817

β1418

β1918

β1519

β2019

β1620

β1321

β2221

β1422

β2322

β1523

β2423

β1624

IQ11 IQ12 IQ14IQ13

IQ21 IQ22 IQ24IQ23

A11 A12 A13 A14

E11 E12 E13 E14

A21 A22 A23 A24

E21 E22 E23 E24

Page 36: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semCors() can be used to plot implied and observedcovariances using the qgraph framework (Epskamp et al.,2012). For example:

library("lavaan")

# Simulate 2 factor model with correlated residual:Mod <- 'A =~ 1*a1 + 0.6*a2 + 0.8*a3B =~ 1*b1 + 0.7*b2 + 0.9*b3a1 ~~ 1*b1A ~~ -0.3* B'

set.seed(5)Data <- simulateData(Mod)

# Fit regular 2 factor model:Mod <- 'A =~ a1 + a2 + a3B =~ b1 + b2 + b3'fit <- cfa(Mod, data=Data)

Page 37: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Fit it in lavaan and look at the covariance matrices:

semCors(fit, layout = "spring", cut = 0.3,esize = 20, titles = TRUE)

a1

a2

a3b1

b2

b3

Observed

a1

a2

a3b1

b2

b3

Implied

Page 38: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

The modelMatrices() function can be used to obtain a list ofall matrices in one of three modeling frameworks:

I RAMI LISRELI Mplus

Page 39: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

names(modelMatrices(fit, "ram"))

## [1] "A" "S" "F"

names(modelMatrices(fit, "lisrel"))

## [1] "LY" "TE" "PS" "BE" "LX" "TD"## [7] "PH" "GA" "TY" "TX" "AL" "KA"

names(modelMatrices(fit, "mplus"))

## [1] "Nu" "Lambda" "Theta"## [4] "Kappa" "Alpha" "Beta"## [7] "Gamma" "Psi"

Page 40: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

modelMatrices

str(modelMatrices(fit, "ram")$A)

## List of 1## $ :List of 4## ..$ est : num [1:8, 1:8] 0 0 0 0 0 0 0 0 0 0 ...## .. ..- attr(*, "dimnames")=List of 2## .. .. ..$ : chr [1:8] "a1" "a2" "a3" "b1" ...## .. .. ..$ : chr [1:8] "a1" "a2" "a3" "b1" ...## ..$ std : num [1:8, 1:8] 0 0 0 0 0 0 0 0 0 0 ...## .. ..- attr(*, "dimnames")=List of 2## .. .. ..$ : chr [1:8] "a1" "a2" "a3" "b1" ...## .. .. ..$ : chr [1:8] "a1" "a2" "a3" "b1" ...## ..$ par : num [1:8, 1:8] 0 0 0 0 0 0 0 0 0 0 ...## .. ..- attr(*, "dimnames")=List of 2## .. .. ..$ : chr [1:8] "a1" "a2" "a3" "b1" ...## .. .. ..$ : chr [1:8] "a1" "a2" "a3" "b1" ...## ..$ fixed: logi [1:8, 1:8] FALSE FALSE FALSE FALSE FALSE FALSE ...## .. ..- attr(*, "dimnames")=List of 2## .. .. ..$ : chr [1:8] "a1" "a2" "a3" "b1" ...## .. .. ..$ : chr [1:8] "a1" "a2" "a3" "b1" ...

Page 41: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

modelMatrices(fit, "ram")$A[[1]]$est

## a1 a2 a3 b1 b2 b3 A B## a1 0 0 0 0 0 0 1.0000 0.000## a2 0 0 0 0 0 0 0.7335 0.000## a3 0 0 0 0 0 0 1.0390 0.000## b1 0 0 0 0 0 0 0.0000 1.000## b2 0 0 0 0 0 0 0.0000 0.765## b3 0 0 0 0 0 0 0.0000 1.012## A 0 0 0 0 0 0 0.0000 0.000## B 0 0 0 0 0 0 0.0000 0.000

Page 42: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

The semMatrixAlgebra() function makes extractingmatrices easier:

semMatrixAlgebra(fit, A)

## model set to ’ram’

## a1 a2 a3 b1 b2 b3 A B## a1 0 0 0 0 0 0 1.0000 0.000## a2 0 0 0 0 0 0 0.7335 0.000## a3 0 0 0 0 0 0 1.0390 0.000## b1 0 0 0 0 0 0 0.0000 1.000## b2 0 0 0 0 0 0 0.0000 0.765## b3 0 0 0 0 0 0 0.0000 1.012## A 0 0 0 0 0 0 0.0000 0.000## B 0 0 0 0 0 0 0.0000 0.000

Note how using the term A caused the function to automaticallyidentify we were interested in the RAM model.

Page 43: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semMatriAlgebra() can also be used to easily performalgebraic computations:

semMatrixAlgebra(fit, Lambda %*% Psi %*% t(Lambda) + Theta)

## model set to ’mplus’

## a1 a2 a3 b1 b2 b3## a1 2.02879 0.60113 0.85151 -0.12520 -0.09578 -0.12674## a2 0.60113 1.52291 0.62456 -0.09183 -0.07025 -0.09296## a3 0.85151 0.62456 1.63260 -0.13008 -0.09951 -0.13168## b1 -0.12520 -0.09183 -0.13008 1.95964 0.66839 0.88447## b2 -0.09578 -0.07025 -0.09951 0.66839 1.53194 0.67661## b3 -0.12674 -0.09296 -0.13168 0.88447 0.67661 1.78813

Page 44: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Also works for multi-group analyses:

l <- "http://www.statmodel.com/examples/continuous/cont12.html"download.file(l, modfile <- tempfile(fileext = ".out"))

semMatrixAlgebra(modfile, Theta)

## model set to ’mplus’

## Reading model: C:\Users\sacha\AppData\Local\Temp\RtmpeqxVk5\file9483af417f3.out## [[1]]## Y6 Y7 Y8 Y9## Y6 0.354 0.000 0.000 0.000## Y7 0.000 0.268 0.000 0.000## Y8 0.000 0.000 1.374 0.000## Y9 0.000 0.000 0.000 2.528#### [[2]]## Y6 Y7 Y8 Y9## Y6 0.354 0.000 0.000 0.000## Y7 0.000 0.268 0.000 0.000## Y8 0.000 0.000 1.374 0.000## Y9 0.000 0.000 0.000 2.528

Page 45: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

semSyntax can be used to translate any input to semPlot intolavaan codes. This has two advantages:

I Easily fit a model based on an output file in lavaanI Simulate data based on an estimated model using

lavaan’s simulateDataTranslating lavaan syntax to MPlus syntax can be attemptedusing lavaan:::lav2mplus. sem is also supported but a bitbugged at the moment. Mail me for a lavaan to OpenMxtranslator.

Page 46: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Translate MPlus to lavaan:

l <- "http://www.statmodel.com/usersguide/chap5/ex5.1.out"download.file(l, modfile <- tempfile(fileext = ".out"))Model <- semPlotModel(modfile)

## Reading model: C:\Users\sacha\AppData\Local\Temp\RtmpeqxVk5\file9486caadb1.out

lavMod <- semSyntax(Model)

#### Model <- '## F1 =~ 1*Y1## F1 =~ Y2## F1 =~ Y3## F2 =~ 1*Y4## F2 =~ Y5## F2 =~ Y6## F2 ~~ F1## Y1 ~ 1## Y2 ~ 1## Y3 ~ 1## Y4 ~ 1## Y5 ~ 1## Y6 ~ 1## F1 ~~ F1## F2 ~~ F2## Y1 ~~ Y1## Y2 ~~ Y2## Y3 ~~ Y3## Y4 ~~ Y4## Y5 ~~ Y5## Y6 ~~ Y6## '

Page 47: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Simulate data:

l <- "http://www.statmodel.com/usersguide/chap5/ex5.1.out"download.file(l, modfile <- tempfile(fileext = ".out"))Model <- semPlotModel(modfile)lavMod <- semSyntax(Model, allFixed = TRUE)

Page 48: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Simulate data:

library("lavaan")head(simulateData(lavMod))

## Y1 Y2 Y3 Y4 Y5 Y6## 1 -0.1812 -0.86023 -0.26249 0.8436 1.3738 -0.2065## 2 0.4026 -1.42322 -0.03974 0.6176 0.5889 0.6993## 3 1.2055 0.37841 1.44397 0.7376 0.9466 -0.8903## 4 2.1490 -0.67511 0.07165 0.1718 -0.4993 -2.1682## 5 0.3397 -0.09025 -0.06618 -1.2264 0.0610 -1.2726## 6 -1.5069 -0.81482 -1.58714 1.1065 -0.4947 0.2997

Page 49: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Future directions

I (Better) support for:I OnyxI AmosI EQSI lava

I Extension to different models:I LKAI IRTI Bayesian models

Page 50: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

In the spirit of Valentine

library("lavaan")example(sem)semPaths(fit, "std", "hide", sizeLat = 15, shapeLat = "star", shapeMan = "heart",

col = list(man = "pink", lat = "yellow"), residuals = FALSE, borders = FALSE,edge.color = "purple", XKCD = TRUE, edge.width = 2, rotation = 2, layout = "tree2",fixedStyle = 1, mar = c(1, 3, 1, 3))

x1

x2

x3

y1

y2

y3

y4

y5

y6

y7

y8

i60

d60

d65

Page 51: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

Thank you for your attention!

Page 52: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

References I

Boker, S. M., McArdle, J., & Neale, M. (2002). An algorithm forthe hierarchical organization of path diagrams andcalculation of components of expected covariance.Structural Equation Modeling, 9(2), 174–194.

Boker, S. M., Neale, M., Maes, H., Wilde, M., Spiegel, M.,Brick, T., . . . Fox, J. (2011). OpenMx: an open sourceextended structural equation modeling framework.Psychometrika, 76(2), 306–317.

Epskamp, S. (2013). lisrelToR: import output from LISREL intoR. R package version 0.1.4. Retrieved fromhttp://CRAN.R-project.org/package=lisrelToR

Epskamp, S., Cramer, A. O. J., Waldorp, L. J.,Schmittmann, V. D., & Borsboom, D. (2012). qgraph:network visualizations of relationships in psychometricdata. Journal of Statistical Software, 48(4), 1–18.Retrieved from http://www.jstatsoft.org/v48/i04/

Page 53: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

References II

Fox, J., Nie, Z., & Byrnes, J. (2013). sem: structural equationmodels. R package version 3.1-1. Retrieved fromhttp://CRAN.R-project.org/package=sem

Hallquist, M. & Wiley, J. (2013). MplusAutomation: automatingmplus model estimation and interpretation. R packageversion 0.5-4. Retrieved fromhttp://CRAN.R-project.org/package=MplusAutomation

Jöreskog, K. G. & Sörbom, D. (1996). LISREL 8: user’sreference guide. Scientific Software.

Muthén, L. K. & Muthén, B. O. (1998–2012). Mplus user’sguide. (Seventh Edition). Los Angeles, CA: Muthén &Muthén.

R Core Team. (2013). R: a language and environment forstatistical computing. ISBN 3-900051-07-0. R Foundationfor Statistical Computing. Vienna, Austria. Retrieved fromhttp://www.R-project.org/

Page 54: semPlot: Unified visualizations of Structural Equation Modelssachaepskamp.com/files/semPlot.pdfsemPlot I R package dedicated to visualizing structural equation models (SEM) I fills

References III

Reingold, E. M. & Tilford, J. S. (1981). Tidier drawings of trees.Software Engineering, IEEE Transactions on, SE-7(2),223–228.

Revelle, W. (2010). PSYCH: procedures for psychological,psychometric, and personality research. R packageversion 1.0-93. Northwestern University. Evanston,Illinois. Retrieved fromhttp://personality-project.org/r/psych.manual.pdf

Rosseel, Y. (2012). lavaan: an R package for structuralequation modeling. Journal of Statistical Software, 48(2),1–36. Retrieved from http://www.jstatsoft.org/v48/i02/