Introducing AR = 2 in glmmTMB: Exploring Higher Level Autoregression

When analyzing time series data in R, incorporating autocorrelation structures into our models can significantly improve the accuracy and insights we derive. Recently, I encountered a situation where I needed to model a series with potentially second-order autocorrelation (AR(2)), rather than the more common first-order (AR(1)). Here’s a deep dive into finding a solution using the glmmTMB package, which is generally used for mixed-effects models and can handle time series data with autocorrelation structures.

Understanding the Original Dataset

The dataset I’m working with looks something like this:

testdat <- data.frame(time = as.factor(rep(c(1:10),20)),
                      y = rep(arima.sim(model = list(ar = .75), n = 20, innov = runif(100, 0, 1)),10),
                      group = rep(1:20,10))

Here, the y variable is simulated such that it has autocorrelation. Each group contains repeated measures over time which simulates the need to model this within-group temporal autocorrelation.

The Challenge with glmmTMB

The glmmTMB package is versatile and supports various correlation structures for the residuals. However, the correlation structures are mainly focused on AR(1) models when dealing with temporal autocorrelation. Many practitioners find themselves a bit stuck when they look to extend this to second-order autocorrelation (AR(2)).

Workarounds and Solutions

Currently, there isn’t a simple, direct way in glmmTMB to specify an AR(2) correlation directly in the formula interface as you would with AR(1), which would be equivalent to using ar1(time + 0|group). Nonetheless, here are a few different approaches you might consider:

  1. Using the nlme or lme4 packages: While these wouldn’t directly solve the lack of AR(2) capability in glmmTMB, they allow greater flexibility in specifying correlation structures. You might use lme from nlme with a specified correlation structure, like so:

library(nlme)
    model <- lme(y ~ 1, random = ~ 1 | group, correlation = corARMA(p=2, form = ~ time | group), data = testdat)

  1. Modifying Source Code of glmmTMB: This is a more complex and advanced approach, involving diving into the C++ source code underlying glmmTMB. If you’re comfortable with C++ and package development in R, you could potentially extend the package to include AR(2) capabilities.
  1. Using software outside R: Sometimes, it might be practical to consider other statistical software like MATLAB or Python (using statsmodels), which may have these capabilities built in.

Practical Considerations

Before moving forward with modification, it’s crucial to weigh the workload and potential risks. Adjusting the source code or switching between different packages or software entails significant time investment and could introduce new complications or bugs in your analysis pipeline.

Conclusion

Although glmmTMB does not directly support AR(2) models, the R community offers other packages that could be used alternatively to fit similar models. Whether to switch to a different package, modify existing package source code, or use a different software should be informed by the specific needs of your project and your comfort with these tools.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *