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

**URL:** <https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084>\
**Category:** brms\
**Created:** [October 18, 2018, 3:54pm UTC](https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084 "2018-10-18T15:54:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Lantos](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul_lantos/32/6105_2.png) [@Paul\_Lantos](https://discourse.mc-stan.org/u/Paul_Lantos)\
**Post date:** [October 18, 2018, 3:54pm UTC](https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084/1 "2018-10-18T15:54:57Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 18, 2018, 4:23pm UTC](https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084/2 "2018-10-18T16:23:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_Lantos](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul_lantos/32/6105_2.png) [@Paul\_Lantos](https://discourse.mc-stan.org/u/Paul_Lantos)\
**Post date:** [October 18, 2018, 4:50pm UTC](https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084/3 "2018-10-18T16:50:34Z")

</div>

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?

Thanks,

Paul

---

<div class="post-metadata">

**Author:** ![paul.buerkner](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul.buerkner/32/3303_2.png) [@paul.buerkner](https://discourse.mc-stan.org/u/paul.buerkner)\
**Post date:** [October 18, 2018, 5:22pm UTC](https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084/4 "2018-10-18T17:22:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul\_Lantos](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/paul_lantos/32/6105_2.png) [@Paul\_Lantos](https://discourse.mc-stan.org/u/Paul_Lantos)\
**Post date:** [October 18, 2018, 7:06pm UTC](https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084/5 "2018-10-18T19:06:55Z")

</div>

Got it, that worked, thanks Paul.

---

<div class="post-metadata">

**Author:** ![xiaolongluo1979](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/xiaolongluo1979/32/13668_2.png) [@xiaolongluo1979](https://discourse.mc-stan.org/u/xiaolongluo1979)\
**Post date:** [February 11, 2022, 8:34pm UTC](https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084/6 "2022-02-11T20:34:25Z")

</div>

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

> predict(model\_b, newdata = df\_val, allow.new.levels = 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, allow.new.levels = 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.

---

<div class="post-metadata">

**Author:** ![Ax3man](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ax3man/32/11894_2.png) [@Ax3man](https://discourse.mc-stan.org/u/Ax3man)\
**Post date:** [February 14, 2022, 8:08pm UTC](https://discourse.mc-stan.org/t/prediction-fitted-brmsfit-from-a-model-with-group-level-effects-using-fitted-brmsfit/6084/7 "2022-02-14T20:08:33Z")

</div>

> [@xiaolongluo1979](#):
>
> allow.new.levels

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