Prediction fitted.brmsfit from a model with group level effects using fitted.brmsfit

I’ve got a model with the following general formula, where there are both smooth, linear, and a group-level term (which reflects repeat observations of y, a test result, among individual patients).

brm( y ~ s( long , lat ) + c + d + ( 1 | group) )

I would like to predict this to a long / lat grid, but I’m not sure of two steps:

  1. what do I put in for group in the prediction grid
  2. in the fitted() call how should I specify how group effects are handled? Looking at the re_formula and allow_new_levels terms, but not sure what’s correct.

My goal is simply predict variation in y over long/lat, but having accounted for group level effects.

When I’ve tried to do this prediction just putting NA for the group term in the grid, I get this error:

Error in names(dat) <- object$term :
‘names’ attribute [3] must be the same length as the vector [2]

What means “account for”? If you want to ignore the group effects (i.e. set them to zero), set re_formula = NA.

I worry about repeated testing of individuals biasing the spatial prediction, so how would I do it if I did not want to ignore the group level effects?



In newdata set group to a value not present in the original data and then specify allow_new_levels = TRUE to account for the uncertainty in the group-level effect.

1 Like

Got it, that worked, thanks Paul.

I have similar issue. Following suggestion above, there is still problem:

predict(model_b, newdata = df_val, = TRUE)
Error: Levels ‘SRP-9001-102-201-901’, ‘SRP-9001-102-201-923’, ‘SRP-9001-102-201-931’, ‘SRP-9001-102-201-941’, ‘SRP-9001-102-229-902’, ‘SRP-9001-102-229-904’, ‘SRP-9001-103-201-1103’, ‘SRP-9001-103-210-1101’, ‘SRP-9001-103-210-1108’, ‘SRP-9001-103-211-1101’, ‘SRP-9001-103-211-1104’ of grouping factor ‘USUBJID’ cannot be found in the fitted model. Consider setting argument ‘allow_new_levels’ to TRUE.

However, when i predict with the original training data, it looks fine:

head(predict(model_b, newdata = df_train1, = TRUE))
Estimate Est.Error Q2.5 Q97.5
[1,] 23.09617 1.671638 19.86706 26.24665
[2,] 23.26106 1.630207 20.14168 26.48262
[3,] 23.30486 1.650554 20.04018 26.53622
[4,] 23.72881 1.577439 20.61693 26.86987
[5,] 23.90131 1.559154 20.76319 26.91970
[6,] 24.13778 1.545149 20.99758 27.14597

Any advice? Thanks.

You are passing but it should be allow_new_levels. The second example only works because there are no new levels.