This document gives an introduction to mixed effects models. It follows Chapter 9 of R for Psych by Glenn Williams very closely, and the toy data set we’ll use is from the same resource.

``sleep_groups <-read_csv(here("data", "sleep_study_with_sim_groups.csv")) ``
``````## Parsed with column specification:
## cols(
##   Reaction = col_double(),
##   Days = col_double(),
##   Subject = col_double(),
##   Group = col_character()
## )``````
``````if (1) { # shuffle subjects between groups
# make frame showing Group for each Subject
SubjGroups <-  sleep_groups %>%
group_by(Subject, Group) %>%
summarize()

nsubject <- max(sleep_groups\$Subject)
subjperm <- sample(nsubject)
orig_groups <- SubjGroups\$Group
sleep_groups <- sleep_groups %>%
mutate(Group= orig_groups[subjperm[Subject]])
}

sleep_groups <- sleep_groups %>%
mutate(Subject=factor(Subject))
``````## # A tibble: 6 x 4
##   Reaction  Days Subject Group
##      <dbl> <dbl> <fct>   <chr>
## 1     250.     0 1       group_1
## 2     259.     1 1       group_1
## 3     251.     2 1       group_1
## 4     321.     3 1       group_1
## 5     357.     4 1       group_1
## 6     415.     5 1       group_1``````

This data set is for an (imaginary) study that looks at the effect of days without sleep on reaction times. Suppose that there are three groups of people— for example, `group_1` could be teenagers, `group_2` twenty-year olds and `group_3` thirty-year olds.

# Linear model

Let’s first fit and plot a linear model:

``lm(Reaction ~ Days, data = sleep_groups)``
``````##
## Call:
## lm(formula = Reaction ~ Days, data = sleep_groups)
##
## Coefficients:
## (Intercept)         Days
##       371.7         15.4``````
``````ggplot(data = sleep_groups, mapping = aes(x = Days, y = Reaction)) +
geom_point(na.rm = T, aes(col = Group), alpha = 0.5) +
geom_smooth(method = "lm", na.rm = T, col = "black", se = F) +
scale_y_continuous(limits = c(180, 1020)) +
scale_x_continuous(breaks = seq(1:10) - 1) +
theme(legend.position = "top")``````