Incorporating time series into a mixed effects model in R (using lme4)

wonderburg picture wonderburg · Feb 25, 2016 · Viewed 8.3k times · Source

I've had a search for similar questions and come up short so apologies if there are related questions that I've missed.

I'm looking at the amount of time spent on feeders (dependent variable) across various conditions with each subject visiting feeders 30 times.

Subjects are exposed to feeders of one type which will have a different combination of being scented/unscented, having visual patterns/being blank, and having these visual or scented patterns presented in one of two spatial arrangements.

So far my model is:

mod<-lmer(timeonfeeder ~ scent_yes_no + visual_yes_no + 
    pattern_one_or_two + (1|subject), data=data)

How can I incorporate the visit numbers into the model to see if these factors have an effect on the time spent on the feeders over time?

Answer

Ben Bolker picture Ben Bolker · Feb 25, 2016

You have a variety of choices (this question might be marginally better for CrossValidated).

  • as @Dominix suggests, you can allow for a linear increase or decrease in time on feeder over time. It probably makes sense to allow this change to vary across birds:

    timeonfeeder ~ time + ... + (time|subject)
    
  • you could allow for an arbitrary pattern of change over time (i.e. not just linear):

    timeonfeeder ~ factor(time) + ... + (1|subject)
    

    this probably doesn't make sense in your case, because you have a large number of observations, so it would require many parameters (it would be more sensible if you had, say, 3 time points per individual)

  • you could allow for a more complex pattern of change over time via an additive model, i.e. modeling change over time with a cubic spline. For example:

    library(mgcv)
    gamm(timeonfeeder ~ s(time) + ... , random = ~1|subject
    

    (1) this assumes the temporal pattern is the same across subjects; (2) because gamm() uses lme rather than lmer under the hood you have to specify the random effect as a separate argument. (You could also use the gamm4 package, which uses lmer under the hood.)

  • You might want to allow for temporal autocorrelation. For example,

    lme(timeonfeeder ~ time + ... ,
        random = ~ time|subject,
        correlation = corAR1(form= ~time|subject) , ...)