Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 710
wonderburg Avatar asked Feb 25 '16 13:02

wonderburg


People also ask

What does lme4 do in R?

2015, with DOI 10.18637/jss. v067. i01, see https://www.jstatsoft.org/ article/view/v067i01/. The lme4 package (Bates, Maechler, Bolker, and Walker 2014a) for R (R Core Team 2015) provides functions to fit and analyze linear mixed models, generalized linear mixed models and nonlinear mixed models.

What is the difference between Glmer and LMER?

lmer() and glmer() The lmer() (pronounced el-mer) and glmer() functions are used in the examples of this article. The lmer() function is for linear mixed models and the glmer() function is for generalized mixed models.

Is lme4 Bayesian?

lme4 is fully frequentist, while rstanarm is fully Bayesian. That means there are more differences than just whether a prior is used.

What package is lme4 in R?

lme4 is the canonical package for implementing multilevel models in R, though there are a number of packages that depend on and enhance its feature set, including Bayesian extensions.


1 Answers

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) , ...)
    
like image 138
Ben Bolker Avatar answered Oct 05 '22 00:10

Ben Bolker