# Estimating Monotonic Effects with brms

## Introduction

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 Simple Monotonic Model

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 ~ monotonic(income), data = dat) The summary methods yield summary(fit1)  Family: gaussian(identity) Formula: ls ~ monotonic(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.45 1.21 28.10 32.83 2413 1 income 45.11 1.89 41.45 48.89 2116 1 Simplex Parameters: Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat income[1] 0.69 0.04 0.62 0.76 2505 1 income[2] 0.10 0.05 0.02 0.20 2816 1 income[3] 0.20 0.04 0.11 0.28 3537 1 Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat sigma 6.84 0.5 5.95 7.89 3249 1 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 = "simplex") 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.42 2.20 17.09 25.90 3198 1 income_num 14.61 0.82 13.00 16.22 3354 1 Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat sigma 9.49 0.69 8.24 10.92 3214 1 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.45      1.22    28.05    32.85       3344    1
income2      31.20      1.85    27.48    34.80       3388    1
income3      35.96      1.94    32.14    39.69       3635    1
income4      45.24      1.84    41.60    48.81       3614    1

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma     6.85       0.5     5.96     7.89       3538    1

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        672.10 15.31
fit2        736.09 17.34
fit3        672.07 15.29
fit1 - fit2 -63.99 10.96
fit1 - fit3   0.03  0.22
fit2 - fit3  64.03 10.83

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.

## Setting Prior Distributions

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 = "simplex", coef = "income")
fit4 <- brm(ls ~ monotonic(income), data = dat,
prior = prior4, sample_prior = TRUE)
summary(fit4)
 Family: gaussian(identity)
Formula: ls ~ monotonic(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.48      1.25    28.02    32.93       2881    1
income       45.04      1.93    41.31    48.85       2730    1

Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
income[1]     0.69      0.04     0.62     0.77       3162    1
income[2]     0.11      0.05     0.02     0.20       3381    1
income[3]     0.20      0.04     0.12     0.28       4000    1

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma     6.84       0.5     5.96      7.9       3475    1

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 simplex_income.

plot(fit4, pars = "prior_simplex", N = 3)

As is visible in the plots, simplex_income[1] was a-priori on average twice as high as simplex_income[2] and simplex_income[3] as a result of setting $$\alpha_1$$ to 2.

## Modelling Monotonic Group-Level Effects

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:

fit5 <- brm(ls ~ mo(income) + (1 | city) + (mo(income) | city), data = dat)

For technical reasons, monotonic group-level effects have to be specified in separate terms in the model formula. Further, we have used the abbrevation mo for monotonic, which helps in shortening the formula. The summary output

summary(fit5)
 Family: gaussian(identity)
Formula: ls ~ mo(income) + (1 | city) + (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(Intercept)     8.76      2.72     4.84    15.01       1877 1.00
sd(income)        3.69      2.62     0.18     9.95       1528 1.01

Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
Intercept    33.45      3.10    27.34    39.59       1187    1
income       44.37      2.38    39.83    49.22       2594    1

Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
income[1]     0.69      0.04     0.61     0.77       4000    1
income[2]     0.11      0.05     0.02     0.21       4000    1
income[3]     0.20      0.04     0.12     0.28       4000    1

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Eff.Sample Rhat
sigma     6.85      0.53     5.92     7.99       4000    1

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.