---
title: "Multiple Linear Regression (MLR) Example in lavaan - 01"
author:
 - name: Ben Kite
   affiliation: Center for Research Methods and Data Analysis, University of Kansas
   email: bakite@ku.edu
 - name: Chong Xing
   affiliation: Center for Research Methods and Data Analysis, University of Kansas
   email: cxing@ku.edu

advertise: >
    Please  visit [http://crmda.ku.edu/guides](http://crmda.ku.edu/guides)
keywords: Linear Regression, Multiple Outcomes, R, lavaan

abstract:
    This guide outlines how to fit two linear regression models
    in R using the lavaan package. The results of the fisrt linear
    model (seven predictors and one outcome) fitted with lavaan are
    compared to the same model fitted with the lm function. The second
    model example demonstrates how to use lavaan to estimate
    a linear regression model with two outcome variables.
    
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/job_placement.csv", header = FALSE)
colnames(dat) <- c("id", "wjcalc", "wjspl", "wratspl", "wratcalc",
                   "waiscalc", "waisspl", "edlevel", "newschl",
                   "suspend", "expelled", "haveld", "gender", "age")
dat[dat == 99999] <- NA
dat$gender <- as.factor(dat$gender)
levels(dat$gender) <- c("Male", "Female")
```
#### Regular Linear Regression Model {.bs-callout .bs-callout-red}
Using the base-R function "lm", we can fit a regression model where
wjspl is predicted by numerous observed variables.
```{r}
lmout <- lm(wjspl ~ edlevel + newschl + suspend +
                    expelled + haveld + gender +
                    age, data = dat)
summary(lmout)
```

#### Linear Regression Model in lavaan {.bs-callout .bs-callout-blue}
We can build the same model as before and save it as a character
string for lavaan.
```{r }
MLRModel <- 'wjspl ~ edlevel + newschl + suspend +
                     expelled + haveld + gender +
                     age

             wjspl ~ 1'
```

This fits the model using the sem function and returns an output
summary.
If we compare the results of this model to the previous model, we can
see that the slope and intercept estimates are the same. In addition,
"standardized = TRUE" command in summary() function will show
standardized parameter estimates. In the next example, the regression
model will include two outcome variables and the correlation between
the outcomes is estimated - in this case a standardized correlation
estimate is more intuitive to understand. 

```{r }
output <- sem(model = MLRModel, data = dat)
summary(output, standardized = TRUE)
```

#### Linear Regression Model in lavaan - Adding another Outcome Variable {.bs-callout .bs-callout-red}
We can add more outcome variables to a regression model.
```{r }
MLRModel.2 <- 'wjspl ~ edlevel + newschl + suspend +
                       expelled + haveld + gender +
                       age

               wjspl ~ 1

               wjcalc ~ edlevel + newschl + suspend +
                        expelled + haveld + gender +
                        age

               wjcalc ~ 1'
```

```{r }
output.2 <- sem(model = MLRModel.2, data = dat)
summary(output.2, standardized = TRUE)
```

#### 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/)
