Setup

This page helps you get R, packages, and the course repository running smoothly.

1) Install/update software

  • R (recommended: recent CRAN release)
  • RStudio (recommended)
  • Quarto (needed only if you want to render slides/labs locally)

2) Get the course materials

Option B — Download ZIP

  • Download the ZIP from GitHub
  • Unzip and open the project folder in RStudio

3) Install packages

Run this once (edit if you already have everything):

pkgs <- c(
  "lavaan",
  "semTools",
  "psych",
  "MVN",
  "mice",
  "modsem",
  "ggplot2",
  "dplyr",
  "tidyr"
)

to_install <- setdiff(pkgs, rownames(installed.packages()))
if (length(to_install) > 0) install.packages(to_install)

invisible(lapply(pkgs, library, character.only = TRUE))
This is lavaan 0.6-19
lavaan is FREE software! Please report any bugs.
 
###############################################################################
This is semTools 0.5-7
All users of R (or SEM) are invited to submit functions or ideas for functions.
###############################################################################

Caricamento pacchetto: 'psych'
I seguenti oggetti sono mascherati da 'package:semTools':

    reliability, skew
Il seguente oggetto è mascherato da 'package:lavaan':

    cor2cov
Warning: il pacchetto 'MVN' è stato creato con R versione 4.5.2

Caricamento pacchetto: 'MVN'
Il seguente oggetto è mascherato da 'package:psych':

    mardia
Warning: il pacchetto 'mice' è stato creato con R versione 4.5.2

Caricamento pacchetto: 'mice'
Il seguente oggetto è mascherato da 'package:stats':

    filter
I seguenti oggetti sono mascherati da 'package:base':

    cbind, rbind
This is modsem (1.0.11). Please report any bugs!

Caricamento pacchetto: 'ggplot2'
I seguenti oggetti sono mascherati da 'package:psych':

    %+%, alpha

Caricamento pacchetto: 'dplyr'
I seguenti oggetti sono mascherati da 'package:stats':

    filter, lag
I seguenti oggetti sono mascherati da 'package:base':

    intersect, setdiff, setequal, union

5) Quick test: can you run lavaan?

library(lavaan)

m <- 'f =~ x1 + x2 + x3'
fit <- cfa(m, data = HolzingerSwineford1939)

inspect(fit, "converged")
[1] TRUE
fitMeasures(fit, c("cfi","rmsea","srmr"))
  cfi rmsea  srmr 
    1     0     0 

Troubleshooting

“Package X is not available”

  • Update R
  • Try a different CRAN mirror
  • Install from source if needed (rare)

“Quarto not found”

  • Install Quarto and restart RStudio

“Rendering fails”

  • Re-run package installation
  • Try rendering a single file first (e.g., one lab)
  • Check for path issues (especially on Windows)
Tip

If you run into errors during class, copy-paste the error message + the code you ran into your notes (or send it). It speeds up debugging a lot.