---
title: "SEM Structural Models in R - Example 2"
author:
 - name: Chong Xing
   affiliation: Center for Research Methods and Data Analysis, University of Kansas
   email: cxing@ku.edu
 - name: Paul Johnson
   affiliation: Center for Research Methods and Data Analysis, University of Kansas
   email: pauljohn@ku.edu
advertise: >
    Please  visit [http://crmda.ku.edu/guides](http://crmda.ku.edu/guides)
keywords: Structural Equation Modeling, R, lavaan
abstract:
    This guide outlines how to fit a structural equation model
    with measurement and structural components. The predictor and
    outcome variables in this example are latent factors.
checked_by: "First Last"
Note to Authors: please_dont_change_the_next 4 lines!
date: "`r format(Sys.time(), '%Y %B %d')`"
output:
  stationery::crmda_html_document:
    toc: true
    toc_depth: 2
    theme: null
    css: theme/kutils.css
    template: theme/guide-template.html
    mathjax-url: "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
logoleft: theme/logoleft.png
logoright: theme/logo-vert.png
---

```{r setup, include=FALSE}
##This Invisible Chunk is required in all CRMDA documents
outdir <- paste0("tmpout")
if (!file.exists(outdir)) dir.create(outdir, recursive = TRUE)
knitr::opts_chunk$set(echo=TRUE, comment=NA, fig.path=paste0(outdir, "/p-"))
options(width = 75)
```

#### Package and Data {.bs-callout .bs-callout-grey}

Load the lavaan package at the beginning of the session
```{r }
library(lavaan)
```
The data file is read in, columns are named, and missing values are specified.
```{r }
dat <- read.csv("../../data/insomnia.dat", header = FALSE, sep = "\t")

colnames(dat) <- c("insom1", "insom2", "insom3",
                   "insom4", "insom5", "insom6",
                   "insom7",
                   "cesd1", "cesd2", "cesd3",
                   "cesd4", "cesd5", "cesd6",
                   "cesd7", "cesd8", "cesd9",
                   "cesd10", "cesd11", "cesd12",
                   "cesd13", "cesd14", "cesd15",
                   "cesd16", "cesd17", "cesd18",
                   "cesd19", "cesd20",
                   "phy", "psy", "soc", "env")

dat[dat == 999] <- NA
```

#### A Path Diagram for the Model {.bs-callout .bs-callout-red}

<img src="importfigs/sem-02-a.png">

#### lavaan Syntax for the Model {.bs-callout .bs-callout-blue}
This builds the SEM model
```{r }
SEMModel <- ' ## the measurement model for exogenous factor Impact
              Impact =~ insom5 + insom6 + insom7

              ## the measurement model for exogenous factor Severity
              Severity =~ insom1 + insom2 + insom3

              ## the measurement model for exogenous factor Satisf
              Satisf =~ insom1 + insom4 + insom7

              ## the measurement model for endogenous factor Qol
              Qol =~ phy + psy + soc + env

              ## the structural model for regressing the
              ## endogenous onto the exogenous factors 
              Qol ~ Impact + Severity + Satisf '
```
Here the model is fitted and the summary is requested.
```{r }
output <- sem(model = SEMModel, data = dat, std.lv = TRUE,
              missing = "fiml", mimic = "Mplus")
summary(output, standardized = TRUE, fit.measures = TRUE)
```

#### Estimation Results in a Path Diagram{.bs-callout .bs-callout-red}

<img src="importfigs/sem-02-b.png">

#### Session Info {.bs-callout .bs-callout-grey}

```{r sessionInfo, echo = FALSE}
sessionInfo()
```

Available under
[Created Commons license 3.0 <img src="http://crmda.dept.ku.edu/images/cc-3.0.png" alt="CC BY"
style="width: 75px;height: 20px;"/>](http://creativecommons.org/licenses/by/3.0/)

