Predicting with a multilevel model with BRMS

In the past I have had no problem predicting with predict() where I set allow_new_levels = T. However, now, using the exact same code as before I get the following error:

Error: NAs are not allowed in grouping variables.

I have tried to figure out what has changed, e.g. setting re_formula = NULL. Do I have to specify the grouping variable (in my case phylogeny) in newdata?


I think that one means you have an NA value somewhere in the newdata phylogeny column - nothing to do with how you’re using the predict formula itself.

Thanks for your reply! Here is an example:

n <- 100
d <- data.frame(replicate(2, rnorm(n)))
d$observation <- 1:nrow(d) 
B <- matrix(runif(n^2)*2-1, ncol=n) 
A <- t(B) %*% B
colnames(A) <- d$observation
rownames(A) <- d$observation

f1 <- brm(
  X1 ~ X2 + (1|gr(observation, cov = A)), 
  data = d, 
  family = gaussian(), 
  data2 = list(A = A),
  prior = c(
    prior(normal(0, 10), "b"),
    prior(normal(0, 50), "Intercept"),
    prior(student_t(3, 0, 20), "sd"),
    prior(student_t(3, 0, 20), "sigma")

h <- data.frame(X2=seq(-1,1, length.out=100))
predict(f1, newdata = h, allow_new_levels = TRUE, summary = F)

I get: Error: NAs are not allowed in grouping variables.

Apparently one have to specify the grouping variable in new data, this is a new feature, like this:

data.frame(X2=seq(-1,1, length.out=100), observation="whatever")