1

The code below shows how the random effects (intercepts) of mixed models without autocorrelation terms can be extracted and plotted. However, this approach does not work when modelling autocorrelation in glmmTMB.

Use reproducible example data from this question: glmmTMB with autocorrelation of irregular times

library(dplyr)
library(lubridate)
library(glmmTMB)

# Make data frame according to example linked above:
library(dplyr)
library(lubridate)
library(glmmTMB)

df <- structure(list(DayYear = c(234, 220, 234, 231, 243, 229, 228, 
                                 223, 220, 218, 234, 237, 234, 231, 241, 237, 241, 241, 233, 234, 
                                 234, 232, 218, 227, 232, 229, 220, 223, 228, 224), 
                     DateTime =     structure(c(1495477980, 
                      1399590540, 1495479780, 1495225920, 1464631980, 1495052760, 1463324460, 
                      1494525780, 1494256560, 1494088440, 1495471320, 1495730940, 1495476960, 
                      1495225200, 1432919940, 1495725900, 1432924200, 1432918860, 1495384020, 
                      1495479900, 1463848140, 1495298820, 1399420080, 1463253000, 1463692920, 
                     1495037040, 1494275160, 1494510780, 1463348220, 1494597180),
                     class =     c("POSIXct", "POSIXt"), tzone = ""), 
                     Year = c(2017, 2014, 2017, 2017, 2016, 2017, 2016, 2017, 2017, 2017, 
                              2017, 2017, 2017, 2017, 2015, 2017, 2015, 2015, 2017, 2017, 
                              2016, 2017, 2014, 2016, 2016, 2017, 2017, 
                              2017, 2016, 2017),
                     N = c(2, 2, 7, 2, 6, 4, 1, 4, 1, 3, 1, 6, 
                     2, 2, 2, 2, 5, 5, 3, 5, 3, 2, 4, 1, 6, 2, 2, 3, 5, 2)), row.names = c(NA, 
                     -30L), class = c("tbl_df", "tbl", "data.frame"))

df2 <- (df
        %>% arrange(DateTime)
        %>% group_by(Year)
        %>% mutate(times = lubridate::decimal_date(DateTime) %% 1)
        %>% ungroup()
)

df3 <- (df2
        %>% mutate(YearF = as.factor(Year),
                   times = glmmTMB::numFactor(times))
        %>% select(N, DayYear, YearF, times)
)

# Construct a model without autocorrelation:

mod <- glmmTMB(N ~ DayYear + YearF + 
                (1 | YearF),
                family = nbinom2,
                data = df3)

# extract random intercepts in 2 ways:
glmmTMB::ranef(mod, condVar = T)
broom.mixed::tidy(mod, effects = "ran_vals")

# plot random intercepts
library(sjPlot)
plot_model(mod, type = "re")

# Now fit a covariance structure:

mod2 <- glmmTMB(N ~ DayYear + YearF + 
                ou(times + 0 | YearF),
                family = nbinom2,
                data = df3)

# extract random intercepts in 2 ways:
glmmTMB::ranef(mod2, condVar = T)
broom.mixed::tidy(mod2, effects = "ran_vals")

# plot random intercepts
library(sjPlot)
plot_model(mod2, type = "re")

Autocorrelation of irregular time series is modelled with the ou(times + 0 | group) structure, where numeric times are encoded in the factor levels of 'times'. When trying to extract the random effects, there is a random effect associated with each of the 'times' in the time series.

How do I produce a plot for random intercepts while incorporating autocorrelation?

Chris0083
  • 11
  • 1

0 Answers0