Last updated 2023-11-16.
This GitHub repository contains R code for conducting Structural Equation Modeling (SEM) with a focus on latent variables, specifically tailored for psychologists. The analysis includes a two-mediator serial mediation model that I applied in my MSc research on work addiction. As serial mediation studies are rare in Psychology literature, I'm uploading my code which may be of use to someone as a starting point which will hopefully give you the basic tools to construct, evaluate and compare Structural Equation Models (SEM; w/plots), using lavaan
.
The repository is organised into the following sections:
- 01 Power Analysis: Script for power analysis using the
pwr
package. - 02 SEM Model Assumptions: Script for data wrangling and assumptions testing using
dplyr
,knitr
,lavaan
,lavaanPlot
,mvnormalTest
, andtidyr
. - 03 SEM (Latent Variables) Two-Mediator Serial Mediation: Script for actual SEM analysis using
lavaan
,lavaanPlot
, andsemPlot
. - 04 Careless Longstring: Script to detect careless participants within the dataset using the
careless
package. - 05 Univariate Outliers: Script to detect univariate outliers within the dataset using z-scores the
dplyr
package. - 06 Convert IP Address to Country and plot on a map: Script for converting IP addresses to countries and plotting on a map (requires loading Python in R).**
- 07 Reliability: Script for calculating Cronbach's Alpha and McDonald's Omega coefficients using the
psych
package. - 08 Descriptive Statistics: Script for calculating descriptive statistics including min, max, mean using summary() function, as well as frequencies and correlation matricies using the
psych
package as well as the apa.cor.table() function from theapaTables
package.
This repository assumes basic competence in R (importing, structural equation, modelling, mediation, plotting, etc) and contains only materials relating to Structural Equation Modelling in R. So the focus will be generally on the application and not on the theory.
To run the code, you will need:
-
A dataset relating to one independent variable, two mediator variables and one dependent variable.
-
A fresh installation of
R
(preferably version 4.3.1 or above). -
RStudio IDE (optional but recommended).
-
Install the required packages by running:
# in alphabetical order: pkgs <- c( "careless", "dplyr", "knitr", "lavaan", "lavaanPlot", "mvnormalTest", "psych", "pwr", "semPlot", "tidyr" ) install.packages(pkgs, repos = c("https://easystats.r-universe.dev", getOption("repos")))
Package Versions
Run on Windows 11 x64 (build 22621), with R version 4.3.1.
The packages used here:
careless
1.2.2(CRAN)dplyr
1.1.3 (CRAN)knitr
1.45 (CRAN)lavaan
0.6-16 (CRAN)lavaanPlot
0.6.2 (CRAN)mvnormalTest
1.0.0 (CRAN)psych
2.3.9 (CRAN)pwr
1.3-0 (CRAN)semPlot
1.1.6 (CRAN)tidyr
1.3.0 (CRAN)
Feel free to adjust this based on your preferences and specific details about your code and setup.