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 assumed 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. If the monotonic effect is used in a linear model, \(b\) can be interpreted as the expected average difference between two adjacent categories of the ordinal predictor. An additional parameter vector, \(\zeta\), estimates the normalized distances between consecutive predictor categories which thus defines the shape of the monotonic effect. 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 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.
<- c("below_20", "20_to_40", "40_to_100", "greater_100")
income_options <- factor(sample(income_options, 100, TRUE),
income levels = income_options, ordered = TRUE)
<- c(30, 60, 70, 75)
mean_ls <- mean_ls[income] + rnorm(100, sd = 7)
ls <- data.frame(income, ls) dat
We now proceed with analyzing the data modeling income
as a monotonic effect.
<- brm(ls ~ mo(income), data = dat) fit1
The summary methods yield
summary(fit1)
Family: gaussian
Links: mu = identity; sigma = identity
Formula: ls ~ mo(income)
Data: dat (Number of observations: 100)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 4000
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept 31.59 1.54 28.65 34.57 1.00 2779 2530
moincome 14.92 0.74 13.45 16.37 1.00 2598 2549
Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
moincome1[1] 0.63 0.04 0.55 0.72 1.00 3253 2289
moincome1[2] 0.23 0.05 0.13 0.33 1.00 3497 2414
moincome1[3] 0.14 0.05 0.04 0.24 1.00 2600 1587
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma 7.76 0.59 6.69 9.03 1.00 3267 1921
Draws were sampled 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, variable = "simo", regex = TRUE)
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:
$income_num <- as.numeric(dat$income)
dat<- brm(ls ~ income_num, data = dat) fit2
summary(fit2)
Family: gaussian
Links: mu = identity; sigma = identity
Formula: ls ~ income_num
Data: dat (Number of observations: 100)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 4000
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept 23.16 2.38 18.41 27.85 1.00 3955 2629
income_num 14.67 0.89 12.91 16.45 1.00 4098 2618
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma 9.76 0.71 8.53 11.23 1.00 3332 2791
Draws were sampled 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)
<- brm(ls ~ income, data = dat) fit3
summary(fit3)
Family: gaussian
Links: mu = identity; sigma = identity
Formula: ls ~ income
Data: dat (Number of observations: 100)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 4000
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept 31.40 1.54 28.39 34.43 1.00 2865 2479
income2 28.52 2.12 24.35 32.68 1.00 3318 3056
income3 38.64 2.23 34.12 42.88 1.00 3438 2926
income4 45.09 2.26 40.59 49.62 1.00 3201 3031
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma 7.75 0.56 6.76 8.93 1.00 4412 3193
Draws were sampled 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 -348.9 6.7
p_loo 4.8 0.7
looic 697.7 13.4
------
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 -370.7 6.5
p_loo 2.8 0.5
looic 741.4 13.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.
Output of model 'fit3':
Computed from 4000 by 100 log-likelihood matrix
Estimate SE
elpd_loo -348.6 6.7
p_loo 4.7 0.7
looic 697.3 13.4
------
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.2 0.2
fit2 -22.0 5.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 unordered 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 Dirichlet 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:
<- prior(dirichlet(c(2, 1, 1)), class = "simo", coef = "moincome1")
prior4 <- brm(ls ~ mo(income), data = dat,
fit4 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)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 4000
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept 31.58 1.51 28.62 34.54 1.00 3214 2944
moincome 14.92 0.76 13.47 16.45 1.00 2553 2310
Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
moincome1[1] 0.64 0.04 0.56 0.72 1.00 3117 2425
moincome1[2] 0.22 0.05 0.12 0.32 1.00 3932 2526
moincome1[3] 0.14 0.05 0.04 0.23 1.00 2974 1667
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma 7.77 0.57 6.74 8.98 1.00 2863 2227
Draws were sampled 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 draws from the prior distribution of simo_moincome1
so that we can visualized it.
plot(fit4, variable = "prior_simo", regex = TRUE, 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.
$age <- rnorm(100, mean = 40, sd = 10) dat
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:
<- brm(ls ~ mo(income)*age, data = dat) fit5
summary(fit5)
Family: gaussian
Links: mu = identity; sigma = identity
Formula: ls ~ mo(income) * age
Data: dat (Number of observations: 100)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 4000
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept 38.45 6.37 26.86 51.66 1.00 1130 1469
age -0.17 0.16 -0.50 0.10 1.00 1060 1434
moincome 10.88 2.77 5.20 16.09 1.00 1031 1383
moincome:age 0.10 0.07 -0.03 0.24 1.00 1041 1319
Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
moincome1[1] 0.63 0.13 0.32 0.84 1.01 1410 1080
moincome1[2] 0.23 0.11 0.04 0.50 1.00 1588 1393
moincome1[3] 0.14 0.08 0.01 0.33 1.00 1858 1584
moincome:age1[1] 0.54 0.25 0.05 0.92 1.00 1591 2111
moincome:age1[2] 0.25 0.20 0.01 0.76 1.00 1897 2049
moincome:age1[3] 0.22 0.18 0.01 0.68 1.00 1803 2336
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma 7.74 0.57 6.75 8.97 1.00 2825 2476
Draws were sampled 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")
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
.
$city <- rep(1:10, each = 10)
dat<- rnorm(10, sd = 10)
var_city $ls <- dat$ls + var_city[dat$city] dat
With the following code, we fit a multilevel model assuming the intercept and the effect of income
to vary by city:
<- brm(ls ~ mo(income)*age + (mo(income) | city), data = dat) fit6
summary(fit6)
Family: gaussian
Links: mu = identity; sigma = identity
Formula: ls ~ mo(income) * age + (mo(income) | city)
Data: dat (Number of observations: 100)
Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
total post-warmup draws = 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) 13.20 4.00 7.28 22.85 1.00 1412 1948
sd(moincome) 1.40 1.06 0.07 4.09 1.00 1401 2303
cor(Intercept,moincome) -0.09 0.52 -0.93 0.90 1.00 3643 2895
Population-Level Effects:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept 37.48 8.65 21.22 54.89 1.00 947 1268
age -0.22 0.18 -0.57 0.12 1.00 1037 1666
moincome 10.26 3.27 4.08 16.77 1.00 1021 1724
moincome:age 0.11 0.08 -0.04 0.26 1.00 976 1824
Simplex Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
moincome1[1] 0.56 0.15 0.16 0.79 1.00 1402 1091
moincome1[2] 0.28 0.13 0.06 0.60 1.00 2148 1901
moincome1[3] 0.16 0.10 0.01 0.39 1.00 1714 1819
moincome:age1[1] 0.61 0.25 0.05 0.94 1.00 1384 1886
moincome:age1[2] 0.21 0.19 0.01 0.70 1.00 2494 2182
moincome:age1[3] 0.19 0.17 0.00 0.68 1.00 1992 2554
Family Specific Parameters:
Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma 7.55 0.59 6.48 8.78 1.00 3900 2921
Draws were sampled 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.
Bürkner P. C. & Charpentier, E. (in review). Monotonic Effects: A Principled Approach for Including Ordinal Predictors in Regression Models. PsyArXiv preprint.