# Load libraries
library(lavaan)
library(lavaanExtra)
# Define our regression terms
<- list(mpg = names(mtcars)[2:5],
regression disp = names(mtcars)[4:7])
# Write the model, and check it
<- write_lavaan(regression = regression)
mtcars.model cat(mtcars.model)
## ##################################################
## # [---------Regressions (Direct effects)---------]
##
## mpg ~ cyl + disp + hp + drat
## disp ~ hp + drat + wt + qsec
# Fit the model with `lavaan`
<- sem(mtcars.model, data = mtcars)
fit.reg
# 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)
Outcome | Predictor | β | p |
mpg | cyl | -0.24 | .244 |
mpg | disp | -0.29 | .123 |
mpg | hp | -0.26 | .128 |
mpg | drat | 0.19 | .159 |
disp | hp | 0.18 | .177 |
disp | drat | -0.18 | .034 |
disp | wt | 0.61 | < .001 |
disp | qsec | -0.19 | .061 |