# Predict.brmsfit fails with a model that has a monotonic predictor

**URL:** https://discourse.mc-stan.org/t/predict-brmsfit-fails-with-a-model-that-has-a-monotonic-predictor/20103
**Category:** brms
**Created:** [January 7, 2021, 5:51pm UTC](https://discourse.mc-stan.org/t/predict-brmsfit-fails-with-a-model-that-has-a-monotonic-predictor/20103 "2021-01-07T17:51:26Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![htrier](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/htrier/32/9397_2.png) [@htrier](https://discourse.mc-stan.org/u/htrier)
#### Post date: [January 7, 2021, 5:51pm UTC](https://discourse.mc-stan.org/t/predict-brmsfit-fails-with-a-model-that-has-a-monotonic-predictor/20103/1 "2021-01-07T17:51:26Z")

</div>

I am running the following model that has one numeric predictor, `x1`, and one ordered factor monotonic predictor, `x2`:

```no-highlight
f <- as.formula(y ~ x1 + mo(x2))
model <- brm(f, data=mydata,iter=9000,control=list(adapt_delta=0.9))

```

However, when I try to enter it into `posterior_predict`:

```no-highlight
myprediction <- as.data.frame(brms::posterior_predict(model,newdata=mydata))

```

I receive the following error:

> Error in simplex[, X + 1] : subscript out of bounds

I am able to run `posterior_predict` when I remove `mo()` from the formula or when I convert the ordered factor `x2` to numeric --which makes me believe the monotonic predictor is the problem. Am I using `mo()` the wrong way, or are these two functions simply incompatible? Thanks for your advice.

---

<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: [January 7, 2021, 6:42pm UTC](https://discourse.mc-stan.org/t/predict-brmsfit-fails-with-a-model-that-has-a-monotonic-predictor/20103/2 "2021-01-07T18:42:29Z")

</div>

Looks like a bug. Let me check.

---

<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: [January 7, 2021, 8:04pm UTC](https://discourse.mc-stan.org/t/predict-brmsfit-fails-with-a-model-that-has-a-monotonic-predictor/20103/3 "2021-01-07T20:04:01Z")

</div>

Can you please provide a minimal reproducible example, ideally with simulated data? I don’t have your data so I cannot run your code.

---

<div class="post-metadata">

### Author: ![htrier](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/htrier/32/9397_2.png) [@htrier](https://discourse.mc-stan.org/u/htrier)
#### Post date: [January 8, 2021, 1:48am UTC](https://discourse.mc-stan.org/t/predict-brmsfit-fails-with-a-model-that-has-a-monotonic-predictor/20103/4 "2021-01-08T01:48:21Z")

</div>

Hi @paul.buerkner I believe I have figured out how to fix the problem, although I’m still not entirely sure why the issue occurs. I’ve posted a reproducible example below with explanation in case other users have the same problem. To make the sample data similar to my data, I added a few random `NA` values into the factor column.

Normally I don’t need to manually exclude `NA` values from my data when running brms models because the model automatically excludes these when run; and indeed when I convert my ordered factor variable into numeric, `posterior_predict` works fine with `NA` values included in the input dataset. However, there seems to be a special case where `posterior_predict` fails to evaluate predictions for a dataset that has `NA` values in a factor using a model where `NA` values had been automatically excluded as a process of the `brm` function.

For example, the following code returns my original error:

```
# R version 4.0.2
library(brms) # version brms_2.14.4
library(ggplot2) # version ggplot2_3.3.2
library(dplyr) # version dplyr_1.0.2
mydata <- mpg
mydata$cyl <- factor(mydata$cyl,ordered=TRUE) 
mydata <- mydata %>% mutate_if(is.numeric,scale)
mydata$cyl[10:15] <- NA

f <- as.formula(hwy ~ displ + mo(cyl))
model <- brm(f, data=mydata,iter=9000,control=list(adapt_delta=0.9))
myprediction <- as.data.frame(brms::posterior_predict(model,newdata=mydata))

```

But when I add `mydata <- mydata[complete.cases(mydata),]` before running the model the error disappears.
