# lmer for repeated measures

I have several animal pairs pair. For each, I repeatedly measured daily proportions of time they spent in contact time.con (30-60 measurements for each group, 1 measurement per date). I want to compare how much time different pairs spent in contact using lmer and controlling for repeated measures. The pairs are permanent, so essentially pair = individual. Here is a simplified example:

      pair   date     time.con

[1,] "1"  "01.06.17" "0.12"
[2,] "1"  "02.06.17" "0"
[3,] "1"  "03.06.17" "0.11"
[4,] "2"  "04.06.17" "0.34"
[5,] "2"  "05.06.17" "0.02"
[6,] "2"  "06.06.17" "0.07"
[7,] "3"  "01.06.17" "0.14"
[8,] "3"  "02.06.17" "0.26"
[9,] "3"  "03.06.17" "0.1"


So, the fixed effect would be pair. The question is, how do I control for repeated measures? If I use pair as both fixed and random effect, the model, obviously, fails to converge:

lmer(time.con ~ pair + (1|pair))


I guess that's where I'm meant to use date somehow (as nested in pair?), but I cannot get the syntax right:

lmer(time.con ~ pair + (1+pair|date)) (doesn't work)


I'm probably missing something simple, as I'm new both to R and lmm. Would appreciate any advice!

• If you monitor a pair daily over time for 30 to 60 days, is it true that you get a single measurement for that pair every day? Also, are the animal pairs different from each other? In other words, if you pair animal A with animal B in your study, these two animals will remain paired until the end of the study and will not be paired with any other animals? Why did you monitor each pair for such a long time? Did you anticipate that the pair would spend increasingly more/less time together as the study progressed? Feb 1 '19 at 3:04
• @Isabella Ghement Yes, for each day I have a single measurement: the proportion of time the pair spent in contact. The pairs are permanent, so we might as well say that pair = individual. Will add that to the post.Yes, time spent in contact is different for different life periods of the pair. My original question was to compare periods with/without an infant, but now I also want to see how pairs are different from each other. Feb 1 '19 at 11:01
• Regarding the gap between your measurements, this is not a problem for mixed models. However, as I mentioned in my answer, you should think what is time 0 for your experiment. Regarding the zeros in the time.con you may want to consider a two-part mixed model, for example, check the GLMMadaptive package: drizopoulos.github.io/GLMMadaptive/articles/… Feb 2 '19 at 19:17
• @Dimitris Rizopoulos there is no time 0: there is no start or end of the experiment because there is no experiment. It wouldn't matter if we go back or forth in time: we just measure the same thing over and over again. That's why I don't really understand how follow_up_time would be meaningful... Thanks very much for the link! Feb 4 '19 at 11:05

This seems to be a longitudinal study, with measurements over time for each pair. As a first step, based on the date variable you could construct the follow-up time variable, which is the time from the first measurement. Think however carefully if the first measurement really is the time 0 for each pair for your experiment or perhaps another date.

Then, you include random effects for the pair grouping variable, but not include it also as a fixed effect. You could start with a random intercepts model, e.g.,

fm1 <- lmer(time.con ~ follow_up_time + (1 | pair),
data = your_data)


This model postulates that the correlations over time within a pair remain constant. You could extend the model by assume that the correlations decrease with the time span between measurements using a random intercepts and random slopes model, e.g.,

fm2 <- lmer(time.con ~ follow_up_time + (follow_up_time | pair),
data = your_data)


To evaluate if you need the random slopes, you could do a likelihood ratio test, i.e.,

anova(fm1, fm2)

• Prior to modelling, it's worth plotting the time.con variable vs. follow_up_time for several pairs just to see what kind of relationship between these variables might make sense. Feb 1 '19 at 16:08
• If there are missing data in the time.con (which is often the case) that are of the missing at random type (again often the case) such a descriptive plot can be misleading and not reveal the true relationship. Feb 1 '19 at 16:14
• @Dimitris Rizopoulos By follow-up variable, you mean "days from the first observation day"? Something like this: follow_up <- c(0, 1, 2, 0, 1, 2, 0, 1, 2) in case of my example data? Feb 2 '19 at 16:52
• @Dimitris Rizopoulos there are a lot of gaps between observation days and a lot of zeros for time.con. The problem is I don't see how the time from the first measurement is relevant: it doesn't matter if I took the 2nd measuremen in 5 or 25 days after the 1st one, right? Feb 2 '19 at 16:59

(my first attempted answer on stackexchange...fingers crossed that this works)

I'm not an expert, but I'll offer some feeback. I don't have enough reputation to put this in a comment, so here's an answer.

I guess the first question to ask is, what do you wish to learn from the data? You say you want to compare them. By that, do you just want to see the means for each pair? If so, you could look at either the unadjusted means or shrunk means:

1. The unadjusted means by using lm(). That is, without pooling you could use:

lm(time.con ~ Pair)

To get the coefficients:

coef( lm(time.con ~ Pair) )

1. The shrunk means by using lmer(). That is, with pooling you could use:

lmer(time.con ~ (1|Pair))

To get the coefficients:

coef( lmer(time.con ~ (1|Pair)) )

If that's not what you have in mind when you say you want to compare them, then what does "compare" mean to you in this context?

Finally, since your response variable has a hard lower boundary at 0, and some values that are close to it including one right at 0, then the typical assumption of the error term being Gaussian might not be the best. If you want to account for this by using generalized learn models, glm() and glmer() might be helpful. Hopefully someone else can add more details on whether or not it'd be worth it to go to a generalized linear model.

For more background, I think this is a good resource freely available on the web:

https://bookdown.org/roback/bookdown-bysh/ch-MLRreview.html

I worked through an old version, but it looks like they updated it a few days ago.

• @ MichiganWater I want to see if, after correcting for the effect of infant presence, season etc, the pairs spend significantly different time in contact (I have all reasons to expect they do). I don't quite understand about the means, though. I want to compare variances, not just means, otherwise why would I need to use lmm? But if we talk about means, I guess I need shrunk means: I have a lot of noise and little signal (lots of zeros outliers). This  lmer(time.con ~ (1|Pair)) looks promising! To clarify: does 1|Pair tell the model that observations from the same pair are not independent? Feb 2 '19 at 16:37
• @loir_loir Hmm. I’m confused. In your original question and this comment you say that you “want to compare how much time different pairs spent in contact” - that, to me, indicates that you want to see how the mean contact times for the different pairs vary. However, in this comment you also say you want to compare variances. Which variances do you want to compare? Are you looking to see if any particular pair has a large within-pair day-to-day variance compared to all the other pairs? Feb 3 '19 at 18:25
• @loir_loir And, yes, the (1|Pair) term does tell the model that observations from the same Pair are not independent. I like to think about it this way, the (1|grouping_variable) term, for example using group “3” says: “Hey, group “3”, you don’t have to use the same intercept as any other group. You get your own intercept! But, everyone in your group must work together to come up with your group’s intercept. You individuals with the group don’t get to act independently.” Feb 3 '19 at 18:26
• Ah sorry, I got confused myself! Yes, I just want to compare the means. It seems this (1|Pair) is exactly what I need. Thanks very much! As for zeros you mentioned above, this link from @Dimitris Rizopoulos might have a solution: drizopoulos.github.io/GLMMadaptive/articles/… Feb 4 '19 at 11:14