Regressions

Rémi Thériault

August 29, 2022

Regression example

# Load libraries
library(lavaan)
library(lavaanExtra)

# Define our regression terms
regression <- list(mpg = names(mtcars)[2:5],
                   disp = names(mtcars)[4:7])

# Write the model, and check it
mtcars.model <- write_lavaan(regression = regression)
cat(mtcars.model)
## ##################################################
## # [---------Regressions (Direct effects)---------]
## 
## mpg ~ cyl + disp + hp + drat
## disp ~ hp + drat + wt + qsec
# Fit the model with `lavaan`
fit.reg <- sem(mtcars.model, data = mtcars)

# Get regression parameters only
lavaan_reg(fit.reg)
##   Outcome Predictor      B     p
## 1     mpg       cyl -0.242 0.244
## 2     mpg      disp -0.287 0.123
## 3     mpg        hp -0.264 0.128
## 4     mpg      drat  0.192 0.159
## 5    disp        hp  0.175 0.177
## 6    disp      drat -0.177 0.034
## 7    disp        wt  0.614 0.000
## 8    disp      qsec -0.186 0.061
# We can get it prettier with the `rempsyc::nice_table` integration
lavaan_reg(fit.reg, nice_table = TRUE, highlight = TRUE)