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!