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 27.81 1.70 24.49 31.24 1.00 2457 2282 moincome 15.65 0.77 14.19 17.18 1.00 2217 2048 Simplex Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS moincome1 0.69 0.04 0.61 0.76 1.00 2419 2136 moincome1 0.24 0.04 0.16 0.31 1.00 3392 2967 moincome1 0.08 0.04 0.01 0.15 1.00 1924 1236 Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 7.07 0.52 6.13 8.18 1.00 3121 1931 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 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 22.50 2.75 17.08 27.96 1.00 4242 2975 income_num 15.15 1.02 13.13 17.16 1.00 4193 2682 Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 10.07 0.71 8.80 11.52 1.00 4327 3223 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
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    27.46      1.69    24.12    30.78 1.00     2383     2213
income2      32.56      2.13    28.47    36.72 1.00     2484     2731
income3      43.84      2.07    39.65    47.89 1.00     2513     2407
income4      47.29      2.40    42.54    52.02 1.00     2645     2634

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     7.07      0.52     6.14     8.17 1.00     4304     3159

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)
Output of model 'fit1':

Computed from 4000 by 100 log-likelihood matrix

Estimate   SE
elpd_loo   -339.4  7.4
p_loo         4.7  0.8
looic       678.7 14.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   -373.9  5.8
p_loo         2.8  0.4
looic       747.9 11.6
------
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   -339.5  7.5
p_loo         4.8  0.8
looic       678.9 15.0
------
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
fit1   0.0       0.0
fit3  -0.1       0.2
fit2 -34.6       6.8

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    27.71      1.65    24.59    30.92 1.00     2175     1988
moincome     15.67      0.76    14.18    17.17 1.00     1821     2232

Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
moincome1     0.69      0.04     0.61     0.76 1.00     2921     2540
moincome1     0.24      0.04     0.16     0.31 1.00     4083     3217
moincome1     0.07      0.04     0.01     0.15 1.00     2239     1465

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     7.04      0.51     6.11     8.16 1.00     3122     2425

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 was a-priori on average twice as high as simo_moincome1 and simo_moincome1 as a result of setting $$\alpha_1$$ to 2.

Modeling interactions of monotonic variables

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 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 26.44 5.56 16.23 38.03 1.00 1100 1260 age 0.03 0.13 -0.24 0.28 1.00 1067 1309 moincome 16.08 2.80 10.56 21.52 1.01 895 1266 moincome:age -0.01 0.07 -0.15 0.13 1.01 885 1185 Simplex Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS moincome1 0.67 0.08 0.51 0.83 1.00 1199 1309 moincome1 0.24 0.06 0.11 0.35 1.00 1863 1267 moincome1 0.10 0.06 0.01 0.24 1.00 1380 1292 moincome:age1 0.35 0.24 0.01 0.87 1.00 1788 2092 moincome:age1 0.31 0.23 0.01 0.83 1.00 2335 2262 moincome:age1 0.34 0.24 0.01 0.85 1.00 1791 2890 Family Specific Parameters: Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS sigma 7.12 0.51 6.21 8.20 1.00 2923 2670 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") 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)              10.64      3.35     5.89    18.74 1.00     1528     2363
sd(moincome)                1.37      1.03     0.06     3.90 1.00     1034     1860
cor(Intercept,moincome)    -0.17      0.50    -0.93     0.87 1.00     3483     2304

Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept       30.59      7.85    16.37    47.97 1.00     1068     1165
age             -0.02      0.17    -0.40     0.28 1.00     1159     1152
moincome        15.28      3.07     8.68    21.27 1.00      994     1367
moincome:age     0.02      0.07    -0.13     0.18 1.00      972     1189

Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
moincome1         0.67      0.08     0.52     0.84 1.00     1615     1734
moincome1         0.24      0.07     0.09     0.37 1.01     2338     1951
moincome1         0.09      0.05     0.01     0.22 1.00     1927     1347
moincome:age1     0.39      0.25     0.01     0.88 1.00     2233     2466
moincome:age1     0.32      0.23     0.01     0.82 1.00     3306     2667
moincome:age1     0.29      0.23     0.01     0.82 1.00     2271     2503

Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     7.13      0.56     6.12     8.34 1.00     3974     2813

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.