# Mixed model with random slope and Intercept syntax?

I posted a similar question but I am still struggling to make sure my syntax is accurate. I used the Cosinor package in R to determine the amplitude value of each participant, based on heart rate per day across a 24 hour period.

Data was collected for every day for 6 weeks per subject.

• However, some participants have less days than other due to data loss - range is 1-42 days with average of 16

my data sample:

structure(list(User.ID = c(21256L, 21256L, 21256L, 21256L, 21256L,
21256L, 21256L, 21258L, 21258L, 21258L, 21258L, 21258L, 21258L,
21259L), HR = c(93.12272727, 95.60333333, 98.29333333, 118.9666667,
84.46666667, 78.36666667, 95.03333333, 97.80333333, 80.03333333,
94.03333333, 97.88, 82.86, 85.86333333, 81.59666667), Weekday = structure(c(7L,
5L, 1L, 3L, 4L, 2L, 1L, 5L, 1L, 3L, 4L, 2L, 5L, 7L), .Label = c("Fri",
"Mon", "Sat", "Sun", "Thu", "Tue", "Wed"), class = "factor"),
YearDay = c(45L, 46L, 47L, 48L, 49L, 50L, 54L, 46L, 47L,
48L, 49L, 50L, 53L, 45L), Whoop_ID = c(6003L, 6003L, 6003L,
6003L, 6003L, 6003L, 6003L, 6004L, 6004L, 6004L, 6004L, 6004L,
6004L, 6001L), MESOR = c(127.8198669, 82.53790266, 80.196328,
87.94691954, 78.24139419, 85.76274123, 85.24568034, 78.76045577,
78.13040009, 82.96056761, 80.7242717, 81.09227575, 79.80820793,
76.53899579), amp = c(39.62989516, 15.90807964, 17.40106027,
20.93586176, 11.64670832, 17.36869616, 12.79682425, 15.60118873,
18.1036377, 17.95736176, 14.5964711, 12.81719691, 19.65582833,
19.85491777), acr = c(-2.096524329, -0.531214487, -0.419872151,
-0.885908809, -0.614661956, -0.498619211, -0.347860598, -0.424370362,
-0.09063145, -0.414970962, -0.390604738, -0.415772215, -6.021379968,
-0.627923711), Group.x = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Decrease",
"Increase"), class = "factor"), DayofIntervention = c(0,
1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 0), MRI.Day.of.Year = c(54,
54, 54, 54, 54, 54, 54, 53, 53, 53, 53, 53, 53, 57), Intervention = c("PRE",
"PRE", "PRE", "PRE", "PRE", "PRE", "POST", "PRE", "PRE",
"PRE", "PRE", "PRE", "POST", "PRE")), row.names = c(NA, -14L
), groups = structure(list(User.ID = c(21256L, 21258L, 21259L
), .rows = list(1:7, 8:13, 14L)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))


Which looks like:

# A tibble: 14 x 12
# Groups:   User.ID [3]
User.ID    HR Weekday YearDay Whoop_ID MESOR   amp     acr Group.x  DayofIntervention MRI.Day.of.Year Intervention
<int> <dbl> <fct>     <int>    <int> <dbl> <dbl>   <dbl> <fct>                <dbl>           <dbl> <chr>
1   21256  93.1 Wed          45     6003 128.   39.6 -2.10   Decrease                 0              54 PRE
2   21256  95.6 Thu          46     6003  82.5  15.9 -0.531  Decrease                 1              54 PRE
3   21256  98.3 Fri          47     6003  80.2  17.4 -0.420  Decrease                 2              54 PRE
4   21256 119.  Sat          48     6003  87.9  20.9 -0.886  Decrease                 3              54 PRE
5   21256  84.5 Sun          49     6003  78.2  11.6 -0.615  Decrease                 4              54 PRE
6   21256  78.4 Mon          50     6003  85.8  17.4 -0.499  Decrease                 5              54 PRE
7   21256  95.0 Fri          54     6003  85.2  12.8 -0.348  Decrease                 6              54 POST
8   21258  97.8 Thu          46     6004  78.8  15.6 -0.424  Decrease                 0              53 PRE
9   21258  80.0 Fri          47     6004  78.1  18.1 -0.0906 Decrease                 1              53 PRE
10   21258  94.0 Sat          48     6004  83.0  18.0 -0.415  Decrease                 2              53 PRE
11   21258  97.9 Sun          49     6004  80.7  14.6 -0.391  Decrease                 3              53 PRE
12   21258  82.9 Mon          50     6004  81.1  12.8 -0.416  Decrease                 4              53 PRE
13   21258  85.9 Thu          53     6004  79.8  19.7 -6.02   Decrease                 5              53 POST
14   21259  81.6 Wed          45     6001  76.5  19.9 -0.628  Decrease                 0              57 PRE


Assumptions:

1. I want to assume that measurements that are further apart in time are less correlated than measurements that closer (random slope).

2. I want to assume that amplitude measurements from the same participants on the same weekday are more correlated than measurements on different weekdays, ( random intercept for Weekday grouping factor nested with User.ID.)

• There are 2 groups (Group.x) "Decrease" and "Increase" that can be thought of as "control and intervention," respectively.

• The DayofIntervention variable counts the number of days data was collected, sequentially, per subject. The Intervention variable determines if the subject is performing a task PRE or POST having intervention.

3. I would like to account for Pre/Post intervention timepoints as well but I'm not sure how, syntactically.

my model (with what I think is random slope + intercepts:

mixed.model = lmer (amp ~ Group.x * DayofIntervention + (1 | Weekday) + (1 + DayofIntervention | User.ID) + (User.ID / Weekday), data = DF)



Some thoughts on each of your assumptions:

1. I want to assume that measurements that are further apart in time are less correlated than measurements that closer (random slope).

In theory, the random slope can help you with this, but the way you describe it here, it sounds like you believe there to be further residual autocorrelation (measurements closer together in time are more correlated measurements farther apart in time) beyond what is accounted for by the varying/random slope for DaysofIntervention. You would have to switch to nlme in R to get an AR1 residual covariance structure. But given only 6 time points, my bet is that the slope should take care of this.

1. I want to assume that amplitude measurements from the same participants on the same weekday are more correlated than measurements on different weekdays, ( random intercept for Weekday grouping factor nested with User.ID.)

If the data is setup such that Weekday is perfectly nested within User.ID, then lmer will treat the following syntax as a three level model lmer(dv ~ iv1 + iv2 + (1|Weekday) + (1|User.ID), data=df). The last bit of your lmer syntax (User.ID / Weekday) is thus not necessary.

1. I would like to account for Pre/Post intervention timepoints as well but I'm not sure how, syntactically.

Based on your description of the variable Intervention, it seems as if you have coded it to reflect this distinction. So if you code that variable so that Pre==0 and Post==1 and add Intervention as a fixed effect predictor to the lmer model (i.e., lmer(amp ~ Group.x * DayofIntervention + Intervention + (1 | Weekday) +...), the coefficient on Intervention would indicate the mean difference between pre and post. I would further recode Intervention to a factor variable myself df$Intervention <- as.factor(df$Intervention) rather than keep it as a character.

The distinction between Group.x and Intervention is a bit confusing, but perhaps that is an issue of what you've called your variables.

• Thanks. When you say "Weekday is perfectly nested within User.ID" do you mean if every User.ID has every day of the week as a data point? – Shai Jan 9 at 0:01
• If there is missing data, then some users might not have all such data points, but that would not change the idea. Taking the varying slope out of the equation, you can examine whether the results of (1 | Weekday) + (1 | User.ID) leads to the same variance estimates as when you specify the nesting explicitly with only (1 | User.ID / Weekday). If your data is set up correctly, they should be exactly the same. Also make sure to read carefully the information about the number of groups in each. They should match. – Erik Ruzek Jan 9 at 0:16
• This post by @Robert Long does a great job of explaining the lmer syntax and the difference between nested and crossed designs. stats.stackexchange.com/questions/228800/… I am pretty sure you have a nested design and thus my points about syntax remain, but @Robert Long's post walks through the cases when the two syntaxes are equivalent. – Erik Ruzek Jan 9 at 18:13