Post-processing model with autoregressive correlation structure in brms

Hello

I am fitting a GAMM in brms (ver. 2.14.4), including an autoregressive correlation structure in the model. However, post-processing the model is causing me some troubles.

The model looks like this:

m1 <- brm(y ~ x + s(Time, by = x, k = 40) + s(Time, by = Plot_id, m = 1, k = 10) +
                          (1|Block + Plot_id) + ar(time = Time, gr = Plot_id, p = 1, cov = TRUE),
                      data = mydata,
                      family = hurdle_gamma())

The model appears to converge.

However, using conditional_effects gives an error message:

plot(conditional_effects(m1))

Error in h(simpleError(msg, call)) :
error in evaluating the argument ā€˜xā€™ in selecting a method for function ā€˜plotā€™: Canā€™t find by variable

I thought is could be caused by the ā€œPlot_idā€ argument being present several places in the model. Thus, I tried to construct a new variable with the same data as ā€œPlot_idā€, and used it in the ar-part.

mydata$new_id <- mydata$Plot_id

m2 <- brm(y ~ x + s(Time, by = x, k = 40) + s(Time, by = Plot_id, m = 1, k = 10) +
                          (1|Block + Plot_id) + ar(time = Time, gr = new_id, p = 1, cov = TRUE),
                      data = mydata,
                      family = hurdle_gamma())

And now plot(conditional_effects(m2)) is working.

However, next I tried the following:

pred <- with(mydata,
             expand.grid(Time = seq(min(Time), max(Time), length = max(Time)),
                         x = levels(x),
                         Block=0,
                         Plot_id = 0,
                         new_id = 0))

test <- fitted(m2, newdata = pred, re_formula = NA , summary = TRUE)

But it gives this error message:

Error: Time points within groups must be unique.

If I am remowing the ar-correlation structure from the model, everything works, so it seems to be related to this part of the model.

Can someone guide me? What did I do wrong?

Hi, sorry for not answering earlier.

The first one might a bug in conditional_effects - maybe it is worth reporting at brms GitHub.

My first guess is that the message mentions the problem - the data you are trying to predict have multiple rows with the same time (one for each level of x) and since you only have one value for new_id it looks like you have several time-series mixed together and brms canā€™t tell which observations are successors. If you need predictions with different x you need to also change the new_id.

Best of luck with your model!

Hi,

I have the same plotting problem. Have you solved the issue?

Error: Time points within groups must be unique.

Hello,
I have run across the same problem. Hereā€™s a reproducible version using a tidybayes approach to prediction.

  library(tidyverse)
  library(brms)
  library(tidybayes)
  library(modelr)

  time <- 1:100
  g <- rep(1:2, 50)
  x <- rnorm(100)
  b <- rnorm(100, mean = 1)
  y <- b * x
  d <- data.frame(y = y, x = x, g = g, time = time)
  
  m <- brm(y ~ x + ar(time = time, gr = g),
           data = d)
  d %>% 
    group_by(g) %>% 
    modelr::data_grid(time = median(time),
                      x = seq_range(x, n = 30)) %>% 
    add_epred_draws(m)

this produces
Error: Time points within groups must be unique.

Thank you,
Zack

@paul.buerkner

Thanks I will take a look.

So the error tells you exactly what the problem is. Time points within groups must be unique, but there are not since all of them are set to the same value. If you want to remove autocorrelations, use incl_autocor = FALSE in posterior_epred and friends.

4 Likes

Great, I wasnā€™t aware of the incl_autocor = F option. Thank you for the help and for BRMS!