\[ \newcommand{\bv}[1]{\boldsymbol{\mathbf{#1}}} \]

Click here to download the Rmd file: week7-mlm-exp.Rmd

```
# To install a package, run the following ONCE (and only once on your computer)
# install.packages("psych")
library(here) # makes reading data more consistent
library(tidyverse) # for data manipulation and plotting
library(haven) # for importing SPSS/SAS/Stata data
library(lme4) # for multilevel analysis
library(lmerTest) # for testing coefficients
library(MuMIn) # for R^2
library(sjPlot) # for plotting effects
library(emmeans) # for marginal means
library(modelsummary) # for making tables
theme_set(theme_bw()) # Theme; just my personal preference
```

This note focused mostly on within-subjects experimental designs, as between-subjects designs are generally easier to handle with the treatment condition at the upper level. When clusters of people are randomly assigned, the design is called a cluster-randomized trial. See an example here: https://www.sciencedirect.com/science/article/abs/pii/S0022103117300860

```
# Data example of Hoffman & Atchley (2001)
# Download from the Internet, unzip, and read in
zip_path <- here("data_files", "MLM_for_Exp_Appendices.zip")
if (!file.exists(zip_path)) {
download.file(
"http://www.lesahoffman.com/Research/MLM_for_Exp_Appendices.zip",
zip_path
)
}
driving_dat <- read_sav(unz(zip_path, "MLM_for_Exp_Appendices/Ex1.sav"))
# Convert `sex` and `oldage` to factor
# Note: also convert id and Item to factor (just for plotting)
driving_dat <- driving_dat %>%
mutate(
sex = as_factor(sex),
oldage = factor(oldage,
levels = c(0, 1),
labels = c("Below Age 40", "Over Age 40")
),
# id = factor(id),
Item = factor(Item)
)
# Show data
rmarkdown::paged_table(driving_dat)
```