This vignette is about monotonic effects, a special way of handling discrete predictors that are on an ordinal or higher scale. The idea was brought to me by Emmanuel Charpentier, who deserves full credit for it. A predictor, which we want to model as monotonic (i.e., having a monotonically increasing or decreasing relationship with the response), must either be integer valued or an ordered factor. As opposed to a continuous predictor, predictor categories (or integers) are not assumend to be equidistant with respect to their effect on the response variable. Instead, the distance between adjacent predictor categories (or integers) is estimated from the data and may vary across categories. This is realized by parameterizing as follows: One parameter, \(b\), takes care of the direction and size of the effect similar to an ordinary regression parameter, while an additional parameter vector, \(\zeta\), estimates the normalized distances between consecutive predictor categories. For a single monotonic predictor, \(x\), the linear predictor term of observation \(n\) looks as follows:

\[\eta_n = b \sum_{i = 1}^{x_n} \zeta_i\]

The parameter \(b\) can take on any real value, while \(\zeta\) is a simplex, which means that is it satisfies \(\zeta_i \in [0,1]\) and \(\sum_{i = 1}^C \zeta_i = 1\) with \(C\) being the number of categories (or highest integer in the data).

A main application of monotonic effects are ordinal predictors that can be modeled this way without falsely treating them either as continuous or as unordered categorical predictors. In Psychology, for instance, this kind of data is omnipresent in the form of Likert scale items, which are often treated as being continuous for convenience without ever testing this assumption. As an example, suppose we are interested in the relationship of yearly income (in $) and life satisfaction measured on an arbitrary scale from 0 to 100. Usually, people are not asked for the exact income. Instead, they are asked to rank themselves in one of certain classes, say: ‘below 20k’, ‘between 20k and 40k’, ‘between 40k and 100k’ and ‘above 100k’. We use some simulated data for illustration purposes.

```
income_options <- c("below_20", "20_to_40", "40_to_100", "greater_100")
income <- factor(sample(income_options, 100, TRUE),
levels = income_options, ordered = TRUE)
mean_ls <- c(30, 60, 70, 75)
ls <- mean_ls[income] + rnorm(100, sd = 7)
dat <- data.frame(income, ls)
```

We now proceed with analyzing the data modeling `income`

as a monotonic effect.

```
library(brms)
fit1 <- brm(ls ~ mo(income), data = dat)
```

The summary methods yield

`summary(fit1)`

```
Family: gaussian(identity)
Formula: ls ~ mo(income)
Data: dat (Number of observations: 100)
Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup samples = 4000
ICs: LOO = NA; WAIC = NA; R2 = NA
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept 30.47 1.19 28.14 32.86 2709 1.00
moincome 45.12 1.85 41.44 48.76 2601 1.00
Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
moincome1[1] 0.69 0.04 0.61 0.77 3129 1.00
moincome1[2] 0.11 0.04 0.02 0.19 2655 1.00
moincome1[3] 0.20 0.04 0.12 0.28 4000 1.00
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma 6.83 0.49 5.93 7.88 3297 1.00
Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
is a crude measure of effective sample size, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
```

`plot(fit1, pars = "simo")`

`plot(marginal_effects(fit1))`

The distributions of the simplex parameter of `income`

, as shown in the `plot`

method, demonstrate that the largest difference (about 70% of the difference between minimum and maximum category) is between the first two categories.

Now, let’s compare of monotonic model with two common alternative models. (a) Assume `income`

to be continuous:

```
dat$income_num <- as.numeric(dat$income)
fit2 <- brm(ls ~ income_num, data = dat)
```

`summary(fit2)`

```
Family: gaussian(identity)
Formula: ls ~ income_num
Data: dat (Number of observations: 100)
Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup samples = 4000
ICs: LOO = NA; WAIC = NA; R2 = NA
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept 21.39 2.17 17.11 25.72 3476 1.00
income_num 14.62 0.81 12.96 16.22 3587 1.00
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma 9.49 0.67 8.26 10.92 3551 1.00
Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
is a crude measure of effective sample size, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
```

or (b) Assume `income`

to be an unordered factor:

```
contrasts(dat$income) <- contr.treatment(4)
fit3 <- brm(ls ~ income, data = dat)
```

`summary(fit3)`

```
Family: gaussian(identity)
Formula: ls ~ income
Data: dat (Number of observations: 100)
Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup samples = 4000
ICs: LOO = NA; WAIC = NA; R2 = NA
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept 30.41 1.22 27.99 32.72 3483 1.00
income2 31.24 1.85 27.56 34.74 3706 1.00
income3 35.99 1.92 32.19 39.84 3619 1.00
income4 45.26 1.86 41.61 48.95 3810 1.00
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma 6.85 0.50 5.94 7.88 4000 1.00
Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
is a crude measure of effective sample size, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
```

We can easily compare the fit of the three models using leave-one-out cross-validation.

`LOO(fit1, fit2, fit3)`

```
LOOIC SE
fit1 671.86 15.39
fit2 735.88 17.30
fit3 672.01 15.27
fit1 - fit2 -64.01 10.84
fit1 - fit3 -0.15 0.21
fit2 - fit3 63.87 10.87
```

The monotonic model fits better than the continuous model, which is not surprising given that the relationship between `income`

and `ls`

is non-linear. The monotonic and the unorderd factor model have almost identical fit in this example, but this may not be the case for other data sets.

In the previous monotonic model, we have implicitly assumed that all differences between adjacent categories were a-priori the same, or formulated correctly, had the same prior distribution. In the following, we want to show how to change this assumption. The canonical prior distribution of a simplex parameter is the Dirchlet distribution, a multivariate generalization of the beta distribution. It is non-zero for all valid simplexes (i.e., \(\zeta_i \in [0,1]\) and \(\sum_{i = 1}^K \zeta_i = 1\)) and zero otherwise. The Dirichlet prior has a single parameter \(\alpha\) of the same length as \(\zeta\). The higher \(\alpha_i\) the higher the a-priori probability of higher values of \(\zeta_i\). Suppose that, before looking at the data, we expected that the same amount of additional money matters more for people who generally have less money. This translates into a higher a-priori values of \(\zeta_1\) (difference between ‘below_20’ and ‘20_to_40’) and hence into higher values of \(\alpha_1\). We choose \(\alpha_1 = 2\) and \(\alpha_2 = \alpha_3 = 1\), the latter being the default value of \(\alpha\). To fit the model we write:

```
prior4 <- prior(dirichlet(c(2, 1, 1)), class = "simo", coef = "moincome1")
fit4 <- brm(ls ~ mo(income), data = dat,
prior = prior4, sample_prior = TRUE)
```

The `1`

at the end of `"moincome1"`

may appear strange when first working with monotonic effects. However, it is necessary as one monotonic term may be associated with multiple simplex parameters, if interactions of multiple monotonic variables are included in the model.

`summary(fit4)`

```
Family: gaussian(identity)
Formula: ls ~ mo(income)
Data: dat (Number of observations: 100)
Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup samples = 4000
ICs: LOO = NA; WAIC = NA; R2 = NA
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept 30.44 1.20 28.03 32.77 2696 1.00
moincome 45.10 1.86 41.59 48.81 2317 1.00
Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
moincome1[1] 0.69 0.04 0.62 0.77 2644 1.00
moincome1[2] 0.10 0.05 0.02 0.19 2431 1.00
moincome1[3] 0.20 0.04 0.11 0.28 3217 1.00
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma 6.84 0.49 5.97 7.87 3247 1.00
Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
is a crude measure of effective sample size, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
```

We have used `sample_prior = TRUE`

to also obtain samples from the prior distribution of `simo_moincome1`

so that we can visualized it.

`plot(fit4, pars = "prior_simo", N = 3)`

As is visible in the plots, `simo_moincome1[1]`

was a-priori on average twice as high as `simo_moincome1[2]`

and `simo_moincome1[3]`

as a result of setting \(\alpha_1\) to 2.

Suppose, we have additionally asked participants for their age.

`dat$age <- rnorm(100, mean = 40, sd = 10)`

We are not only interested in the main effect of age but also in the interaction of income and age. Interactions with monotonic variables can be specified in the usual way using the `*`

operator:

`fit5 <- brm(ls ~ mo(income)*age, data = dat)`

`summary(fit5)`

```
Family: gaussian(identity)
Formula: ls ~ mo(income) * age
Data: dat (Number of observations: 100)
Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup samples = 4000
ICs: LOO = NA; WAIC = NA; R2 = NA
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept 32.59 4.32 24.20 41.02 1514 1.00
age -0.05 0.11 -0.26 0.16 1424 1.00
moincome 50.07 7.49 35.61 64.94 966 1.00
moincome:age -0.12 0.18 -0.48 0.23 868 1.01
Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
moincome1[1] 0.66 0.07 0.52 0.79 1183 1.00
moincome1[2] 0.12 0.05 0.02 0.23 2335 1.00
moincome1[3] 0.22 0.06 0.11 0.36 1516 1.00
moincome:age1[1] 0.31 0.23 0.01 0.82 2273 1.00
moincome:age1[2] 0.30 0.23 0.01 0.80 2533 1.00
moincome:age1[3] 0.39 0.25 0.01 0.87 2070 1.00
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma 6.81 0.51 5.91 7.92 3078 1.00
Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
is a crude measure of effective sample size, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
```

`marginal_effects(fit5, "income:age")`

Suppose that the 100 people in our sample data were drawn from 10 different cities; 10 people per city. Thus, we add an identifier for `city`

to the data and add some city-related variation to `ls`

.

```
dat$city <- rep(1:10, each = 10)
var_city <- rnorm(10, sd = 10)
dat$ls <- dat$ls + var_city[dat$city]
```

With the following code, we fit a multilevel model assuming the intercept and the effect of `income`

to vary by city:

`fit6 <- brm(ls ~ mo(income)*age + (mo(income) | city), data = dat)`

`summary(fit6)`

```
Family: gaussian(identity)
Formula: ls ~ mo(income) * age + (mo(income) | city)
Data: dat (Number of observations: 100)
Samples: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup samples = 4000
ICs: LOO = NA; WAIC = NA; R2 = NA
Group-Level Effects:
~city (Number of levels: 10)
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sd(moincome) 3.94 2.97 0.17 11.19 1584 1.00
sd(Intercept) 9.02 2.91 4.69 16.17 1165 1.00
cor(moincome,Intercept) 0.01 0.50 -0.89 0.92 563 1.00
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept 36.30 5.72 25.38 47.86 1624 1.00
age -0.07 0.12 -0.31 0.16 2012 1.00
moincome 46.87 8.83 30.77 65.23 1403 1.00
moincome:age -0.06 0.21 -0.50 0.32 1396 1.00
Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
moincome1[1] 0.67 0.08 0.51 0.81 1880 1.00
moincome1[2] 0.12 0.06 0.01 0.24 4000 1.00
moincome1[3] 0.22 0.07 0.09 0.36 2426 1.00
moincome:age1[1] 0.34 0.24 0.01 0.85 4000 1.00
moincome:age1[2] 0.29 0.22 0.01 0.80 4000 1.00
moincome:age1[3] 0.37 0.25 0.02 0.87 4000 1.00
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma 6.86 0.54 5.91 8.00 4000 1.00
Samples were drawn using sampling(NUTS). For each parameter, Eff.Sample
is a crude measure of effective sample size, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).
```

reveals that the effect of `income`

varies only little across cities. For the present data, this is not overly surprising given that, in the data simulations, we assumed `income`

to have the same effect across cities.