# 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 (Bürkner & Charpentier, in review). 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 D \sum_{i = 1}^{x_n} \zeta_i$

The parameter $$b$$ can take on any real value, while $$\zeta$$ is a simplex, which means that it satisfies $$\zeta_i \in [0,1]$$ and $$\sum_{i = 1}^D \zeta_i = 1$$ with $$D$$ being the number of elements of $$\zeta$$. Equivalently, $$D$$ is the number of categories (or highest integer in the data) minus 1, since we start counting categories from zero to simplify the notation.

## 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. fit1 <- brm(ls ~ mo(income), data = dat) The summary methods yield summary(fit1)  Family: gaussian Links: mu = identity; sigma = 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 Population-Level Effects: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS Intercept 29.62 1.38 26.88 32.35 1.00 2741 2353 moincome 15.08 0.63 13.79 16.33 1.00 2798 2585 Simplex Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS moincome1 0.68 0.04 0.61 0.76 1.00 2977 2074 moincome1 0.19 0.05 0.09 0.28 1.00 2665 1784 moincome1 0.13 0.05 0.04 0.22 1.00 2429 1224 Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 6.88 0.51 5.97 7.96 1.00 3501 2649 Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS and Tail_ESS are effective sample size measures, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat = 1). plot(fit1, pars = "simo") plot(conditional_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 Links: mu = identity; sigma = 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 Population-Level Effects: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS Intercept 23.11 2.36 18.45 27.79 1.00 4089 2780 income_num 14.20 0.88 12.45 15.99 1.00 4209 2901 Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 9.89 0.74 8.58 11.51 1.00 3542 2475 Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS and Tail_ESS are effective sample size measures, 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
Links: mu = identity; sigma = 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

Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept    29.46      1.36    26.76    32.07 1.00     3080     2272
income2      31.00      1.79    27.52    34.61 1.00     3380     2784
income3      39.39      2.12    35.22    43.52 1.00     3463     3204
income4      45.41      1.88    41.74    49.13 1.00     3163     2814

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     6.87      0.50     5.97     7.93 1.00     3699     2900

Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, 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)
Output of model 'fit1':

Computed from 4000 by 100 log-likelihood matrix

Estimate   SE
elpd_loo   -336.5  6.9
p_loo         4.8  0.7
looic       673.0 13.8
------
Monte Carlo SE of elpd_loo is 0.0.

All Pareto k estimates are good (k < 0.5).
See help('pareto-k-diagnostic') for details.

Output of model 'fit2':

Computed from 4000 by 100 log-likelihood matrix

Estimate   SE
elpd_loo   -371.7  5.6
p_loo         2.6  0.4
looic       743.4 11.3
------
Monte Carlo SE of elpd_loo is 0.0.

All Pareto k estimates are good (k < 0.5).
See help('pareto-k-diagnostic') for details.

Output of model 'fit3':

Computed from 4000 by 100 log-likelihood matrix

Estimate   SE
elpd_loo   -336.4  6.9
p_loo         4.7  0.7
looic       672.7 13.9
------
Monte Carlo SE of elpd_loo is 0.0.

All Pareto k estimates are good (k < 0.5).
See help('pareto-k-diagnostic') for details.

Model comparisons:
elpd_diff se_diff
fit3   0.0       0.0
fit1  -0.1       0.1
fit2 -35.3       6.7  

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}^D \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
Links: mu = identity; sigma = 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

Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept    29.62      1.36    26.94    32.32 1.00     2933     2767
moincome     15.05      0.63    13.84    16.32 1.00     2673     2389

Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
moincome1     0.68      0.04     0.62     0.75 1.00     3308     2578
moincome1     0.19      0.05     0.09     0.28 1.00     3628     2502
moincome1     0.13      0.05     0.04     0.22 1.00     3425     2247

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     6.86      0.51     5.98     7.94 1.01     3334     2496

Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, 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 was a-priori on average twice as high as simo_moincome1 and simo_moincome1 as a result of setting $$\alpha_1$$ to 2.

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 Links: mu = identity; sigma = 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 Population-Level Effects: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS Intercept 36.10 4.92 27.10 46.61 1.00 1311 2010 age -0.16 0.12 -0.42 0.05 1.00 1251 1923 moincome 13.48 2.65 8.17 18.78 1.00 996 1650 moincome:age 0.04 0.06 -0.08 0.17 1.00 999 1684 Simplex Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS moincome1 0.70 0.08 0.55 0.87 1.00 1329 1036 moincome1 0.17 0.07 0.02 0.31 1.00 1825 1084 moincome1 0.14 0.06 0.02 0.26 1.00 1798 1393 moincome:age1 0.42 0.25 0.02 0.89 1.00 1671 1633 moincome:age1 0.30 0.22 0.01 0.80 1.00 2315 2290 moincome:age1 0.28 0.22 0.01 0.82 1.00 2187 2434 Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 6.85 0.50 5.93 7.90 1.00 2573 2474 Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS and Tail_ESS are effective sample size measures, and Rhat is the potential scale reduction factor on split chains (at convergence, Rhat = 1). conditional_effects(fit5, "income:age") ## 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:

fit6 <- brm(ls ~ mo(income)*age + (mo(income) | city), data = dat)
summary(fit6)
 Family: gaussian
Links: mu = identity; sigma = 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

Group-Level Effects:
~city (Number of levels: 10)
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sd(Intercept)               8.76      2.67     4.90    15.53 1.00     1565     2511
sd(moincome)                0.89      0.75     0.03     2.69 1.00     1721     2224
cor(Intercept,moincome)    -0.06      0.56    -0.95     0.94 1.00     4135     2727

Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept       40.24      6.09    28.86    52.82 1.00     1301     1704
age             -0.18      0.13    -0.45     0.04 1.00     1603     1801
moincome        13.10      2.80     7.66    18.70 1.01     1292     1628
moincome:age     0.05      0.07    -0.08     0.18 1.01     1340     1586

Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
moincome1         0.69      0.08     0.53     0.86 1.00     1636     1402
moincome1         0.18      0.08     0.03     0.32 1.00     2433     2114
moincome1         0.14      0.07     0.02     0.27 1.00     2691     1830
moincome:age1     0.44      0.25     0.02     0.90 1.00     2382     2557
moincome:age1     0.30      0.22     0.01     0.79 1.00     3287     2641
moincome:age1     0.26      0.21     0.01     0.77 1.00     2904     2693

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     6.90      0.55     5.92     8.03 1.00     3720     2742

Samples were drawn using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, 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.