Skip to contents

Introduction

In the present vignette, we want to discuss how to specify multivariate multilevel models using brms. We call a model multivariate if it contains multiple response variables, each being predicted by its own set of predictors. Consider an example from biology. Hadfield, Nutall, Osorio, and Owens (2007) analyzed data of the Eurasian blue tit (https://en.wikipedia.org/wiki/Eurasian_blue_tit). They predicted the tarsus length as well as the back color of chicks. Half of the brood were put into another fosternest, while the other half stayed in the fosternest of their own dam. This allows to separate genetic from environmental factors. Additionally, we have information about the hatchdate and sex of the chicks (the latter being known for 94% of the animals).

data("BTdata", package = "MCMCglmm")
head(BTdata)
       tarsus       back  animal     dam fosternest  hatchdate  sex
1 -1.89229718  1.1464212 R187142 R187557      F2102 -0.6874021  Fem
2  1.13610981 -0.7596521 R187154 R187559      F1902 -0.6874021 Male
3  0.98468946  0.1449373 R187341 R187568       A602 -0.4279814 Male
4  0.37900806  0.2555847 R046169 R187518      A1302 -1.4656641 Male
5 -0.07525299 -0.3006992 R046161 R187528      A2602 -1.4656641  Fem
6 -1.13519543  1.5577219 R187409 R187945      C2302  0.3502805  Fem

Basic Multivariate Models

We begin with a relatively simple multivariate normal model.

bform1 <- 
  bf(mvbind(tarsus, back) ~ sex + hatchdate + (1|p|fosternest) + (1|q|dam)) +
  set_rescor(TRUE)

fit1 <- brm(bform1, data = BTdata, chains = 2, cores = 2)

As can be seen in the model code, we have used mvbind notation to tell brms that both tarsus and back are separate response variables. The term (1|p|fosternest) indicates a varying intercept over fosternest. By writing |p| in between we indicate that all varying effects of fosternest should be modeled as correlated. This makes sense since we actually have two model parts, one for tarsus and one for back. The indicator p is arbitrary and can be replaced by other symbols that comes into your mind (for details about the multilevel syntax of brms, see help("brmsformula") and vignette("brms_multilevel")). Similarly, the term (1|q|dam) indicates correlated varying effects of the genetic mother of the chicks. Alternatively, we could have also modeled the genetic similarities through pedigrees and corresponding relatedness matrices, but this is not the focus of this vignette (please see vignette("brms_phylogenetics")). The model results are readily summarized via

fit1 <- add_criterion(fit1, "loo")
summary(fit1)
 Family: MV(gaussian, gaussian) 
  Links: mu = identity
         mu = identity 
Formula: tarsus ~ sex + hatchdate + (1 | p | fosternest) + (1 | q | dam) 
         back ~ sex + hatchdate + (1 | p | fosternest) + (1 | q | dam) 
   Data: BTdata (Number of observations: 828) 
  Draws: 2 chains, each with iter = 2000; warmup = 1000; thin = 1;
         total post-warmup draws = 2000

Multilevel Hyperparameters:
~dam (Number of levels: 106) 
                                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
sd(tarsus_Intercept)                     0.48      0.05     0.39     0.58 1.00      862
sd(back_Intercept)                       0.25      0.07     0.10     0.39 1.02      326
cor(tarsus_Intercept,back_Intercept)    -0.52      0.22    -0.92    -0.08 1.01      556
                                     Tail_ESS
sd(tarsus_Intercept)                     1226
sd(back_Intercept)                        514
cor(tarsus_Intercept,back_Intercept)      787

~fosternest (Number of levels: 104) 
                                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
sd(tarsus_Intercept)                     0.27      0.05     0.16     0.38 1.00      697
sd(back_Intercept)                       0.35      0.06     0.23     0.46 1.00      499
cor(tarsus_Intercept,back_Intercept)     0.69      0.19     0.26     0.98 1.01      326
                                     Tail_ESS
sd(tarsus_Intercept)                     1046
sd(back_Intercept)                       1114
cor(tarsus_Intercept,back_Intercept)      648

Regression Coefficients:
                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
tarsus_Intercept    -0.41      0.07    -0.54    -0.28 1.00     2110     1855
back_Intercept      -0.01      0.07    -0.14     0.11 1.00     3074     1651
tarsus_sexMale       0.77      0.06     0.66     0.89 1.00     4418     1457
tarsus_sexUNK        0.23      0.13    -0.03     0.49 1.00     3612     1440
tarsus_hatchdate    -0.04      0.06    -0.16     0.07 1.00     1809     1484
back_sexMale         0.01      0.07    -0.12     0.14 1.00     3915     1573
back_sexUNK          0.15      0.15    -0.15     0.44 1.00     3914     1415
back_hatchdate      -0.09      0.05    -0.19     0.01 1.00     2650     1630

Further Distributional Parameters:
             Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma_tarsus     0.76      0.02     0.72     0.80 1.00     2044     1604
sigma_back       0.90      0.02     0.85     0.95 1.00     2466     1619

Residual Correlations: 
                    Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
rescor(tarsus,back)    -0.05      0.04    -0.13     0.02 1.00     2966     1662

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

The summary output of multivariate models closely resembles those of univariate models, except that the parameters now have the corresponding response variable as prefix. Across dams, tarsus length and back color seem to be negatively correlated, while across fosternests the opposite is true. This indicates differential effects of genetic and environmental factors on these two characteristics. Further, the small residual correlation rescor(tarsus, back) on the bottom of the output indicates that there is little unmodeled dependency between tarsus length and back color. Although not necessary at this point, we have already computed and stored the LOO information criterion of fit1, which we will use for model comparisons. Next, let’s take a look at some posterior-predictive checks, which give us a first impression of the model fit.

pp_check(fit1, resp = "tarsus")

pp_check(fit1, resp = "back")

This looks pretty solid, but we notice a slight unmodeled left skewness in the distribution of tarsus. We will come back to this later on. Next, we want to investigate how much variation in the response variables can be explained by our model and we use a Bayesian generalization of the \(R^2\) coefficient.

bayes_R2(fit1)
          Estimate  Est.Error      Q2.5     Q97.5
R2tarsus 0.4323065 0.02637683 0.3791434 0.4818787
R2back   0.1977172 0.02869476 0.1418642 0.2543988

Clearly, there is much variation in both animal characteristics that we can not explain, but apparently we can explain more of the variation in tarsus length than in back color.

More Complex Multivariate Models

Now, suppose we only want to control for sex in tarsus but not in back and vice versa for hatchdate. Not that this is particular reasonable for the present example, but it allows us to illustrate how to specify different formulas for different response variables. We can no longer use mvbind syntax and so we have to use a more verbose approach:

bf_tarsus <- bf(tarsus ~ sex + (1|p|fosternest) + (1|q|dam))
bf_back <- bf(back ~ hatchdate + (1|p|fosternest) + (1|q|dam))
fit2 <- brm(bf_tarsus + bf_back + set_rescor(TRUE), 
            data = BTdata, chains = 2, cores = 2)

Note that we have literally added the two model parts via the + operator, which is in this case equivalent to writing mvbf(bf_tarsus, bf_back). See help("brmsformula") and help("mvbrmsformula") for more details about this syntax. Again, we summarize the model first.

fit2 <- add_criterion(fit2, "loo")
summary(fit2)
 Family: MV(gaussian, gaussian) 
  Links: mu = identity
         mu = identity 
Formula: tarsus ~ sex + (1 | p | fosternest) + (1 | q | dam) 
         back ~ hatchdate + (1 | p | fosternest) + (1 | q | dam) 
   Data: BTdata (Number of observations: 828) 
  Draws: 2 chains, each with iter = 2000; warmup = 1000; thin = 1;
         total post-warmup draws = 2000

Multilevel Hyperparameters:
~dam (Number of levels: 106) 
                                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
sd(tarsus_Intercept)                     0.48      0.05     0.39     0.58 1.00      742
sd(back_Intercept)                       0.25      0.07     0.10     0.39 1.01      276
cor(tarsus_Intercept,back_Intercept)    -0.48      0.22    -0.90    -0.03 1.01      372
                                     Tail_ESS
sd(tarsus_Intercept)                     1087
sd(back_Intercept)                        574
cor(tarsus_Intercept,back_Intercept)      592

~fosternest (Number of levels: 104) 
                                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
sd(tarsus_Intercept)                     0.27      0.05     0.17     0.37 1.00      558
sd(back_Intercept)                       0.35      0.06     0.24     0.46 1.01      567
cor(tarsus_Intercept,back_Intercept)     0.67      0.21     0.23     0.98 1.00      251
                                     Tail_ESS
sd(tarsus_Intercept)                      944
sd(back_Intercept)                       1068
cor(tarsus_Intercept,back_Intercept)      533

Regression Coefficients:
                 Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
tarsus_Intercept    -0.41      0.07    -0.54    -0.28 1.00      907     1228
back_Intercept      -0.00      0.05    -0.11     0.10 1.00     1304     1194
tarsus_sexMale       0.77      0.06     0.66     0.88 1.00     2899     1636
tarsus_sexUNK        0.22      0.13    -0.04     0.48 1.00     2375     1662
back_hatchdate      -0.09      0.05    -0.19     0.01 1.00     1734     1310

Further Distributional Parameters:
             Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma_tarsus     0.75      0.02     0.72     0.80 1.00     1742     1445
sigma_back       0.90      0.02     0.85     0.95 1.00     2266     1381

Residual Correlations: 
                    Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
rescor(tarsus,back)    -0.05      0.04    -0.13     0.02 1.00     1606     1406

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

Let’s find out, how model fit changed due to excluding certain effects from the initial model:

loo(fit1, fit2)
Output of model 'fit1':

Computed from 2000 by 828 log-likelihood matrix.

         Estimate   SE
elpd_loo  -2125.9 33.4
p_loo       175.1  7.2
looic      4251.8 66.8
------
MCSE of elpd_loo is NA.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.5, 1.8]).

Pareto k diagnostic values:
                         Count Pct.    Min. ESS
(-Inf, 0.7]   (good)     827   99.9%   82      
   (0.7, 1]   (bad)        1    0.1%   <NA>    
   (1, Inf)   (very bad)   0    0.0%   <NA>    
See help('pareto-k-diagnostic') for details.

Output of model 'fit2':

Computed from 2000 by 828 log-likelihood matrix.

         Estimate   SE
elpd_loo  -2124.5 33.7
p_loo       175.4  7.4
looic      4249.0 67.4
------
MCSE of elpd_loo is NA.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.5, 1.7]).

Pareto k diagnostic values:
                         Count Pct.    Min. ESS
(-Inf, 0.7]   (good)     826   99.8%   74      
   (0.7, 1]   (bad)        2    0.2%   <NA>    
   (1, Inf)   (very bad)   0    0.0%   <NA>    
See help('pareto-k-diagnostic') for details.

Model comparisons:
 model elpd_diff se_diff p_worse       diag_diff      diag_elpd
  fit2       0.0     0.0      NA                 2 k_psis > 0.7
  fit1      -1.4     1.4    0.85 |elpd_diff| < 4 1 k_psis > 0.7

Apparently, there is no noteworthy difference in the model fit. Accordingly, we do not really need to model sex and hatchdate for both response variables, but there is also no harm in including them (so I would probably just include them).

To give you a glimpse of the capabilities of brms’ multivariate syntax, we change our model in various directions at the same time. Remember the slight left skewness of tarsus, which we will now model by using the skew_normal family instead of the gaussian family. Since we do not have a multivariate normal (or student-t) model, anymore, estimating residual correlations is no longer possible. We make this explicit using the set_rescor function. Further, we investigate if the relationship of back and hatchdate is really linear as previously assumed by fitting a non-linear spline of hatchdate. On top of it, we model separate residual variances of tarsus for male and female chicks.

bf_tarsus <- bf(tarsus ~ sex + (1|p|fosternest) + (1|q|dam)) +
  lf(sigma ~ 0 + sex) + skew_normal()
bf_back <- bf(back ~ s(hatchdate) + (1|p|fosternest) + (1|q|dam)) +
  gaussian()

fit3 <- brm(
  bf_tarsus + bf_back + set_rescor(FALSE),
  data = BTdata, chains = 2, cores = 2,
  control = list(adapt_delta = 0.95)
)

Again, we summarize the model and look at some posterior-predictive checks.

fit3 <- add_criterion(fit3, "loo")
summary(fit3)
 Family: MV(skew_normal, gaussian) 
  Links: mu = identity; sigma = log
         mu = identity 
Formula: tarsus ~ sex + (1 | p | fosternest) + (1 | q | dam) 
         sigma ~ 0 + sex
         back ~ s(hatchdate) + (1 | p | fosternest) + (1 | q | dam) 
   Data: BTdata (Number of observations: 828) 
  Draws: 2 chains, each with iter = 2000; warmup = 1000; thin = 1;
         total post-warmup draws = 2000

Smoothing Spline Hyperparameters:
                       Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sds(back_shatchdate_1)     2.02      1.06     0.37     4.47 1.00      446      390

Multilevel Hyperparameters:
~dam (Number of levels: 106) 
                                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
sd(tarsus_Intercept)                     0.47      0.05     0.38     0.58 1.00      760
sd(back_Intercept)                       0.23      0.07     0.10     0.37 1.01      297
cor(tarsus_Intercept,back_Intercept)    -0.52      0.23    -0.94    -0.06 1.00      435
                                     Tail_ESS
sd(tarsus_Intercept)                     1298
sd(back_Intercept)                        515
cor(tarsus_Intercept,back_Intercept)      490

~fosternest (Number of levels: 104) 
                                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS
sd(tarsus_Intercept)                     0.26      0.05     0.15     0.36 1.00      539
sd(back_Intercept)                       0.31      0.06     0.20     0.43 1.01      400
cor(tarsus_Intercept,back_Intercept)     0.62      0.23     0.12     0.98 1.02      162
                                     Tail_ESS
sd(tarsus_Intercept)                      815
sd(back_Intercept)                        680
cor(tarsus_Intercept,back_Intercept)      586

Regression Coefficients:
                     Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
tarsus_Intercept        -0.41      0.07    -0.54    -0.28 1.00     1143     1488
back_Intercept           0.00      0.05    -0.09     0.11 1.00     1547     1425
tarsus_sexMale           0.77      0.06     0.65     0.88 1.00     2953     1464
tarsus_sexUNK            0.21      0.13    -0.03     0.45 1.00     2483     1508
sigma_tarsus_sexFem     -0.30      0.04    -0.38    -0.22 1.00     2754     1765
sigma_tarsus_sexMale    -0.25      0.04    -0.33    -0.16 1.00     1706     1150
sigma_tarsus_sexUNK     -0.39      0.12    -0.63    -0.14 1.00     2221     1895
back_shatchdate_1       -0.15      3.31    -5.93     7.37 1.00     1049     1109

Further Distributional Parameters:
             Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma_back       0.90      0.02     0.86     0.95 1.00     2417     1458
alpha_tarsus    -1.23      0.41    -1.86    -0.14 1.00     1545      746

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 see that the (log) residual standard deviation of tarsus is somewhat larger for chicks whose sex could not be identified as compared to male or female chicks. Further, we see from the negative alpha (skewness) parameter of tarsus that the residuals are indeed slightly left-skewed. Lastly, running

conditional_effects(fit3, "hatchdate", resp = "back")

reveals a non-linear relationship of hatchdate on the back color, which seems to change in waves over the course of the hatch dates.

There are many more modeling options for multivariate models, which are not discussed in this vignette. Examples include autocorrelation structures, Gaussian processes, or explicit non-linear predictors (e.g., see help("brmsformula") or vignette("brms_multilevel")). In fact, nearly all the flexibility of univariate models is retained in multivariate models.

References

Hadfield JD, Nutall A, Osorio D, Owens IPF (2007). Testing the phenotypic gambit: phenotypic, genetic and environmental correlations of colour. Journal of Evolutionary Biology, 20(2), 549-557.