Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mattansb/structural-equation-modeling-for-psychologists

Lesson files used in the Structural Equation Modeling for Psychologists.
https://github.com/mattansb/structural-equation-modeling-for-psychologists

bgu-university lavaan psychologists rstats sem statistics teaching-materials

Last synced: 10 days ago
JSON representation

Lesson files used in the Structural Equation Modeling for Psychologists.

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r setup, include=FALSE}
library(knitr)

opts_chunk$set(echo = TRUE)

`%>%` <- dplyr::`%>%`
```

# Structural Equation Modeling foR Psychologists

[![](https://img.shields.io/badge/Open%20Educational%20Resources-Compatable-brightgreen)](https://creativecommons.org/about/program-areas/education-oer/)
[![](https://img.shields.io/badge/CC-BY--NC%204.0-lightgray)](http://creativecommons.org/licenses/by-nc/4.0/)
[![](https://img.shields.io/badge/Language-R-blue)](http://cran.r-project.org/)

*Last updated `r Sys.Date()`.*

This Github repo contains all lesson files for *SEM - Practical Applications in R*. The goal is to impart students with the basic tools to construct, evaluate and compare **Structural Equation Models (SEM; w/ plots), using [`lavaan`](http://lavaan.ugent.be/)**.

These topics were taught in the graduate-level course ***Structural Equation Modeling*** (Psych Dep., Ben-Gurion University of the Negev). This course assumes basic competence in R (importing, regression modeling, plotting, etc.), along the lines of [*Practical Applications in R for Psychologists*](https://github.com/mattansb/Practical-Applications-in-R-for-Psychologists).

**Notes:**

- This repo contains only materials relating to *Practical Applications in R*, and does not contain any theoretical or introductory materials.
- Please note that some code does not work *on purpose*, to force students to learn to debug.

## Setup

```{r, echo=FALSE}
extract_pkgs <- function(fl) {
`%>%` <- dplyr::`%>%`

if (length(fl) == 1) {
txt <- read.delim(fl, header = FALSE) %>%
.[[1]] %>%
paste0(collapse = "\n")

pkg_lib <- stringr::str_extract_all(txt, pattern = "(?<=library\\().{1,}(?=\\))")

pkg_req <- stringr::str_extract_all(txt, pattern = "(?<=require\\().{1,}(?=\\))")

pkg_name <- stringr::str_extract_all(txt, pattern = "[a-z|A-Z|0-9]{1,}(?=\\:\\:)")

pkgs <- c(pkg_lib, pkg_req, pkg_name)

} else if (length(fl) > 1) {
pkgs <- sapply(fl, extract_pkgs)
}


pkgs %>%
unlist(recursive = TRUE) %>%
unique()
}

make_pkg_table <- function(pkgs) {
`%>%` <- dplyr::`%>%`

pkgs <- pkgs[sapply(pkgs, function(x) length(x) > 0)]

ps <- sapply(pkgs, function(x){
paste0(
glue::glue("[`{x}`](https://CRAN.R-project.org/package={x})"),
collapse = ", "
)
})

glue::glue("|[{folder}](/{folder})|{ps}|\n\n",
folder = names(pkgs)) %>%
c("|Lesson|Packages|\n|----|----|\n", .) %>% # header
paste0(collapse = "")
}
```

You will need:

1. A fresh installation of [**`R`**](https://cran.r-project.org/) (preferably version 4.0 or above).
2. [RStudio IDE](https://www.rstudio.com/products/rstudio/download/) (optional, but recommended).
3. The following packages, listed by lesson:

```{r, echo=FALSE, message=FALSE, warning=FALSE}
r_list <- list.files(pattern = ".(R|r)$", recursive = TRUE, full.names = TRUE) %>%
.[!stringr::str_detect(., pattern = "(SOLUTION|logo)")] %>%
.[stringr::str_detect(., pattern = "^./[0-9]")]

lesson_names <- stringr::str_extract(r_list, pattern = "(?<=(/)).{1,}(?=(/))")

r_list <- split(r_list, lesson_names)

pkgs <- lapply(r_list, extract_pkgs)

print_pkgs <- make_pkg_table(pkgs)

```

`r print_pkgs`

You can install all the packages used by running:

```{r echo=FALSE, comment = "", warning=FALSE}

pkgs <- pkgs %>%
unlist(recursive = TRUE) %>%
unique() %>% sort()

cat("# in alphabetical order:")

pkgs %>% {
capture.output(dput(.))
} %>% {
capture.output(cat("pkgs <-", ., fill = 80))
} %>%
styler::style_text()

cat("install.packages(pkgs, dependencies = TRUE)")
```

Package Versions
The package versions used here:
```{r, echo=FALSE}
packinfo <- installed.packages(fields = c("Package", "Version"))

get_src <- function(pkg) {
pd <- packageDescription(pkg)
if (is.null(src <- pd$Repository)) {
if (!is.null(src <- pd$GithubRepo)) {
src <- paste0("Github: ",pd$GithubUsername,"/",src)
} else {
src <- "Dev"
}
}
return(src)
}

V <- packinfo[pkgs,"Version"]
src <- sapply(pkgs, get_src)
# setNames(paste0(V, " (", src,")"), pkgs)

v_info <- paste0(glue::glue(" - `{pkgs}` {V} (*{src}*)"), collapse = "\n")
```

`r v_info`

## Other Useful Resources

### Other Courses

In addition to [`lavaan`'s toutorials](http://lavaan.ugent.be/tutorial/index.html), you might find the following online courses useful:

- Sacha Epskamp's [online course](http://sachaepskamp.com/SEM2020) and [YouTube lectures](https://www.youtube.com/playlist?list=PLliBbGBc5nn3m8bXQ4CmOep3UmQ_5tVlC).
- See also the [`psychonetrics`](http://psychonetrics.org/) package for psychometric network modeling.
- Michael Hallquist's [course](https://psu-psychology.github.io/psy-597-SEM/).

### Selected Quantitude (podcast) Episods

These are selected episodes from the [Quantitude podcast](https://quantitudepod.org/) related to SEM:

- [S2E08: **Mediation** and the Art of Squid Spleening](https://www.buzzsprout.com/639103/5817541-s2e08-mediation-and-the-art-of-squid-spleening)
- [S2E24: The **Equivalent Models** Problem](https://www.buzzsprout.com/639103/7983649-s2e24-the-equivalent-models-problem)
- [S2E26: MLM vs. **SEM**: Opportunities for Growth](https://www.buzzsprout.com/639103/8084578-s2e26-mlm-vs-sem-opportunities-for-growth)