I'm new to linear mixed effects models and I'm trying to use them for hypothesis testing.
In my data (DF
) I have two categorical/factor variables: color
(red/blue/green) and direction
(up/down). I want to see if there are significant differences in scores
(numeric values) across these factors and if there is an interaction effect, while accounting for random intercepts and random slopes for each participant
.
What is the appropriate lmer
formula for doing this?
Here's what I have...
My data is structured like so:
> str(DF)
'data.frame': 4761 obs. of 4 variables:
$ participant : Factor w/ 100 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
$ direction : Factor w/ 2 levels "down","up": 2 2 2 2 2 2 2 2 2 2 ...
$ color : Factor w/ 3 levels "red","blue",..: 3 3 3 3 3 3 3 3 3 3 ...
$ scores : num 15 -4 5 25 0 3 16 0 5 0 ...
After some reading, I figured that I could write a model with random slopes and intercepts for participants and one fixed effect like so:
model_1 <- lmer(scores ~ direction + (direction|participant), data = DF)
This gives me a fixed effect estimate and p-value for direction
, which I understand to be a meaningful assessment of the effect of direction
on scores
while individual differences across participants are accounted for as a random effect.
But how do I add in my second fixed factor, color
, and an interaction term whilst still affording each participant a random intercept and slope?
I thought maybe I could do this:
model_2 <- lmer(scores ~ direction * color + (direction|participant) + (color|participant), data = DF)
But ultimately I really don't know what exactly this formula means. Any guidance would be appreciated.