Multiple Linear Regression (MLR) Example in R
Load the lavaan package at the beginning of the session
library(lavaan)
## Warning: package 'lavaan' was built under R version 3.1.3
## This is lavaan 0.5-18
## lavaan is BETA software! Please report any bugs.
The data file is read in, columns are named, and missing values are specified.
dat <- read.csv("../../data/job_placement.csv", header = FALSE)
colnames(dat) <- c("id", "wjcalc", "wjspl", "wratspl", "wratcalc", "waiscalc", "waisspl", "edlevel", "newschl", "suspend", "expelled", "haveld", "female", "age")
dat[dat == 99999] <- NA
This builds the model object.
MLRModel <- "wjspl ~ edlevel + newschl + suspend + expelled +
haveld + female + age
wjspl ~ 1
"
This fits the model and returns an output summary.
output <- sem(model = MLRModel, data = dat)
summary(output)
## lavaan (0.5-18) converged normally after 1 iterations
##
## Used Total
## Number of observations 313 322
##
## Estimator ML
## Minimum Function Test Statistic 0.000
## Degrees of freedom 0
##
## Parameter estimates:
##
## Information Expected
## Standard Errors Standard
##
## Estimate Std.err Z-value P(>|z|)
## Regressions:
## wjspl ~
## edlevel 1.162 0.324 3.584 0.000
## newschl 0.063 0.747 0.085 0.932
## suspend -0.052 0.773 -0.067 0.947
## expelled -2.758 1.098 -2.510 0.012
## haveld -6.974 0.987 -7.063 0.000
## female 0.720 0.792 0.909 0.363
## age 0.412 0.207 1.994 0.046
##
## Intercepts:
## wjspl 21.953 4.414 4.974 0.000
##
## Variances:
## wjspl 39.904 3.190