# 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.6374319,"math_prob":0.7869361,"size":2510,"snap":"2021-31-2021-39","text_gpt3_token_len":855,"char_repetition_ratio":0.09736632,"word_repetition_ratio":0.10933941,"special_character_ratio":0.40876493,"punctuation_ratio":0.15384616,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9862895,"pos_list":[0,1,2],"im_url_duplicate_count":[null,1,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-23T23:46:53Z\",\"WARC-Record-ID\":\"<urn:uuid:5d6aa520-a31a-4634-ab08-1d3d13e913ff>\",\"Content-Length\":\"172194\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c48da63b-72c5-4e18-8727-b8d5b4e78de0>\",\"WARC-Concurrent-To\":\"<urn:uuid:98fffe85-f8d2-4c7f-80c9-04ea6a34f311>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://stats.stackexchange.com/questions/538402/difference-in-slope-in-longitudinal-study\",\"WARC-Payload-Digest\":\"sha1:7NY7F54JD6CBOD3RDYGVL35AUI7VMGFW\",\"WARC-Block-Digest\":\"sha1:X6ILU2JMH2RL7FFTOWJN2V2QWTCVGU3E\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057479.26_warc_CC-MAIN-20210923225758-20210924015758-00511.warc.gz\"}"}