# Difference in slope in longitudinal study

I have a longitudinal study where subjects are measured at given time points and a response is recorded. Each subject belongs to one of three groups (A, B, C) and I'm interested in whether the slope of Response ~ Time is different between any pair of the three groups.

I use here the sleepstudy dataset in the lme4 R package as an example:

Are the slopes of the blue lines different from each other?

library(ggplot2)
library(lme4)
library(emmeans)

sleepstudy\$Group <- rep(c('A', 'B', 'C'), each= nrow(sleepstudy)/3)

Reaction Days Subject Group
1   249.5600    0     308     A
2   258.7047    1     308     A
3   250.8006    2     308     A
4   321.4398    3     308     A
5   356.8519    4     308     A
6   414.6901    5     308     A
7   382.2038    6     308     A
8   290.1486    7     308     A
9   430.5853    8     308     A
10  466.3535    9     308     A
11  222.7339    0     309     A
12  205.2658    1     309     A
13  202.9778    2     309     A
14  204.7070    3     309     A
...
178 343.2199    7     372     C
179 369.1417    8     372     C
180 364.1236    9     372     C

gg <- ggplot(data= sleepstudy, aes(x= Days, y= Reaction, by= Subject)) +
geom_line(colour= 'grey60') +
geom_point(colour= 'grey60', size= 0.5) +
geom_smooth(method= 'lm', formula= y ~ x, aes(by= NULL), se= FALSE) +
facet_wrap(~Group)
print(gg)


I'd like to check with you whether my reasoning here is correct.

The simplest option would fit a linear model with a Day-by-Group interaction:

fit <- lm(Reaction ~ Days * Group, data= sleepstudy)
trend <- emtrends(fit, 'Group', var= 'Days')
pp <- pairs(trend)
print(pp)

contrast estimate   SE  df t.ratio p.value
A - B      -3.759 2.98 174  -1.260  0.4199
A - C      -3.651 2.98 174  -1.224  0.4409
B - C       0.108 2.98 174   0.036  0.9993

P value adjustment: tukey method for comparing a family of 3 estimates


However, this model ignores that it is the same subject measured across time points; I have 18 Subjects, not 180.

A better model fits Subject as random effect and allows the slope for Days to vary by Subject each Subject to vary randomly across Days:

fit <- lmer(Reaction ~ Days * Group + (1 + Days|Subject), data= sleepstudy, REML= FALSE)
trend <- emtrends(fit, 'Group', var= 'Days')
pp <- pairs(trend)
print(pp)

contrast estimate   SE   df t.ratio p.value
A - B      -3.759 3.88 21.6  -0.970  0.6032
A - C      -3.651 3.88 21.6  -0.942  0.6203
B - C       0.108 3.88 21.6   0.028  0.9996

Degrees-of-freedom method: kenward-roger
P value adjustment: tukey method for comparing a family of 3 estimates


Is this latter a sensible approach? Am I correct in using emtrends for testing differences between group-slopes?

The estimates of the differences in slopes (output of pairs()) is the same, I guess this is expected, right?

Are the slopes of the blue lines different from each other?

Yes, the slopes for groups B and C are approximately 3.7 larger than for group A, but there is very little difference between B and C

I'd like to check with you whether my reasoning here is correct. The simplest option would fit a linear model with a Day-by-Group interaction ....However, this model ignores that it is the same subject measured across time points; I have 18 Subjects, not 180.

Correct. You need to handle the non-indepence of observations within subjects.

A better model fits Subject as random effect and allows each Subject to vary randomly across Days...Is this latter a sensible approach?

I think you mean "allows the slope for Days to vary by Subject", but yes, this is a better model and a sensible approach.

Am I correct in using emtrends for testing differences between group-slopes?

Yes, if you want estimated marginal means.

The estimates of the differences in slopes (output of pairs()) is the same, I guess this is expected, right?

Yes, that's expected, provided that emtrends is not including random effects when it makes predictions accross the cells for the mixed model.

• Thanks for checking this! I've edited my question regarding slope for days and subject so it's clearer. Jul 29 at 14:52
• You're welcome. I assumed it was just a typo - it doesn't change my answer :) Jul 29 at 15:41
• When you have a balanced experiment, it's not unusual for the EMMs to be the same with a mixed model as with lm(). But note the SEs are different because they are based on different error models. Jul 30 at 13:56