Multilevel Models with Compositional Predictors
Source:vignettes/comp-predictor.Rmd
comp-predictor.Rmd
In this vignette, we discuss how to use multilevelcoda
to specify multilevel models where compositional data are used as
predictors.
The following table outlines the packages used and a brief description of their purpose.
Package | Purpose |
---|---|
multilevelcoda |
calculate between and within composition variables, calculate substitutions and plots |
brms |
fit Bayesian multilevel models using Stan as a backend |
bayestestR |
compute Bayes factors used to compare models |
doFuture |
parallel processing to speed up run times |
library(multilevelcoda)
library(brms)
library(bayestestR)
library(doFuture)
options(digits = 3) # reduce number of digits shown
For the examples, we make use of three built in datasets:
Dataset | Purpose |
---|---|
mcompd |
compositional sleep and wake variables and additional predictors/outcomes (simulated) |
sbp |
a pre-specified sequential binary partition, used in calculating compositional predictors |
psub |
all possible pairwise substitutions between compositional variables, used for substitution analyses |
The following table shows a few rows of data from
mcompd
.
TST | WAKE | MVPA | LPA | SB | ID | Age | Female | STRESS |
---|---|---|---|---|---|---|---|---|
542 | 99.0 | 297.4 | 460 | 41.4 | 185 | 29.7 | 0 | 3.67 |
458 | 49.4 | 117.3 | 653 | 162.3 | 185 | 29.7 | 0 | 7.21 |
271 | 41.1 | 488.7 | 625 | 14.5 | 185 | 29.7 | 0 | 2.84 |
286 | 52.7 | 106.9 | 906 | 89.2 | 184 | 22.3 | 1 | 2.36 |
281 | 18.8 | 403.0 | 611 | 126.3 | 184 | 22.3 | 1 | 1.18 |
397 | 26.5 | 39.9 | 587 | 389.8 | 184 | 22.3 | 1 | 0.00 |
The following table shows the sequential binary partition being used
in sbp
. Columns correspond to the composition variables
(TST, WAKE, MVPA, LPA, SB). Rows correspond to distinct ILR
coordinates.
1 | 1 | -1 | -1 | -1 |
1 | -1 | 0 | 0 | 0 |
0 | 0 | 1 | -1 | -1 |
0 | 0 | 0 | 1 | -1 |
The following table shows how all the possible binary substitutions contrasts are setup. Time substitutions work by taking time from the -1 variable and adding time to the +1 variable.
TST | WAKE | MVPA | LPA | SB |
---|---|---|---|---|
1 | -1 | 0 | 0 | 0 |
1 | 0 | -1 | 0 | 0 |
1 | 0 | 0 | -1 | 0 |
1 | 0 | 0 | 0 | -1 |
-1 | 1 | 0 | 0 | 0 |
0 | 1 | -1 | 0 | 0 |
0 | 1 | 0 | -1 | 0 |
0 | 1 | 0 | 0 | -1 |
-1 | 0 | 1 | 0 | 0 |
0 | -1 | 1 | 0 | 0 |
0 | 0 | 1 | -1 | 0 |
0 | 0 | 1 | 0 | -1 |
-1 | 0 | 0 | 1 | 0 |
0 | -1 | 0 | 1 | 0 |
0 | 0 | -1 | 1 | 0 |
0 | 0 | 0 | 1 | -1 |
-1 | 0 | 0 | 0 | 1 |
0 | -1 | 0 | 0 | 1 |
0 | 0 | -1 | 0 | 1 |
0 | 0 | 0 | -1 | 1 |
Multilevel model with compositional predictors
Compositions and isometric log ratio (ILR) coordinates.
Compositional data are often expressed as a set of isometric log
ratio (ILR) coordinates in regression models. We can use the
compilr()
function to calculate both between- and
within-level ILR coordinates for use in subsequent models as
predictors.
Notes: compilr()
also calculates total ILR
coordinates to be used as outcomes (or predictors) in models, if the
decomposition into a between- and within-level ILR coordinates was not
desired.
The compilr()
function for multilevel data requires four
arguments:
Argument | Description |
---|---|
data |
A long data set containing all variables needed to fit the multilevel models, |
including the repeated measure compositional predictors and outcomes, along with any additional covariates. | |
sbp |
A Sequential Binary Partition to calculate \(ilr\) coordinates. |
parts |
The name of the compositional components in data . |
idvar |
The grouping factor on data to compute the
between-person and within-person composition and \(ilr\) coordinates. |
total |
Optional argument to specify the amount to which the compositions should be closed. |
Fitting model
We now will use output from the compilr()
to fit our
brms
model, using the brmcoda()
. Here is a
model predicting STRESS
from between- and within-person
sleep-wake behaviours (expressed as ILR coordinates).
Notes: make sure you pass the correct names of the ILR
coordinates to brms
model.
m <- brmcoda(compilr = cilr,
formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 +
wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID),
cores = 8, seed = 123, backend = "cmdstanr")
#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt.
#> If your model failed to compile please run rebuild_cmdstan().
#> If the issue persists please open a bug report.
#> Error: An error occured during compilation! See the message above for more information.
Here is a summary()
of the model results.
summary(m$Model)
#> Warning: Parts of the model have not converged (some Rhats are > 1.05). Be careful when
#> analysing the results! We recommend running more iterations and/or setting stronger priors.
#> Family: gaussian
#> Links: mu = identity; sigma = identity
#> Formula: STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID)
#> Data: tmp (Number of observations: 3540)
#> Draws: 1 chains, each with iter = 500; warmup = 250; thin = 1;
#> total post-warmup draws = 250
#>
#> Group-Level Effects:
#> ~ID (Number of levels: 266)
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> sd(Intercept) 0.99 0.07 0.87 1.12 1.00 112 183
#>
#> Population-Level Effects:
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> Intercept 2.51 0.45 1.70 3.42 1.06 49 125
#> bilr1 0.20 0.30 -0.45 0.75 1.11 10 71
#> bilr2 0.44 0.30 -0.28 0.89 1.12 8 99
#> bilr3 0.12 0.22 -0.29 0.51 1.01 81 175
#> bilr4 -0.02 0.32 -0.58 0.63 1.00 85 118
#> wilr1 -0.36 0.12 -0.59 -0.12 1.01 162 155
#> wilr2 0.04 0.13 -0.22 0.27 1.00 100 154
#> wilr3 -0.11 0.08 -0.26 0.04 1.02 96 114
#> wilr4 0.25 0.09 0.07 0.41 1.01 133 113
#>
#> Family Specific Parameters:
#> Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
#> sigma 2.36 0.03 2.30 2.42 1.01 393 187
#>
#> Draws were sampled using sample(hmc). 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).
Results show that the first and forth within-person ILR coordinate
was associated with stress. The interpretation of these outputs depends
on how you construct your sequential binary partition. For the built-in
sequential binary partition sbp
(shown previously), the
resulting interpretation would be as follows:
ILR | Interpretation |
---|---|
bilr1 |
Between-person sleep (TST & WAKE ) vs
wake (MVPA , LPA , & SB )
behaviours |
bilr2 |
Between-person TST vs WAKE
|
bilr3 |
Between-person MVPA vs (LPA and
SB ) |
bilr4 |
Between-person LPA vs SB
|
wilr1 |
Within-person Sleep (TST &
WAKE ) vs wake (MVPA , LPA , &
SB ) behaviours |
wilr2 |
Within-person TST vs WAKE
|
wilr3 |
Within-person MVPA vs (LPA and
SB ) |
wilr4 |
Within-person LPA vs SB
|
Due to the nature of within-person ILR coordinates, it is often
challenging to interpret these results in great details. For example,
the significant coefficient for wilr1
shows that the
within-person change in sleep behaviours (sleep duration and time awake
in bed combined), relative to wake behaviours (moderate to vigorous
physical activity, light physical activity, and sedentary behaviour) on
a given day, was associated with stress. However, as there are several
behaviours involved in this coordinate, we don’t know the within-person
change in which of them drives the association. It could be the change
in sleep, such that people sleep more than their own average on a given
day, but it could also be the change in time awake. Further, we don’t
know about the specific changes in time spent across behaviours. That
is, if people slept more, what behaviour did they spend less time
in?
One approach to gain further insights into these relationships, and the changes in outcomes associated with changes in specific time across compositionl components is the substitution model. We will discuss the substitution model later in this vignette.
Bayes Factor for significance testing
In the frequentist approach, we usually compare the fits of models
using anova()
. In Bayesian, this can be done by comparing
the marginal likelihoods of two models. Bayes Factors (BFs) are indices
of relative evidence of one model over another. In the context of
compositional multilevel modelling, Bayes Factors provide two main
useful functions:
- Testing single parameters within a model
- Comparing models
We may utilize Bayes factors to answer the following question: “Which model (i.e., set of ILR predictors) is more likely to have produced the observed data?”
Let’s fit a series of model with brmcoda()
to predict
STRESS
from sleep-wake composition. For precise Bayes
factors, we will use 40,000 posterior draws for each model.
Notes : To use Bayes factors, brmsfit
models
must be fitted with an additional non-default argument
save_pars = save_pars(all = TRUE)
.
# intercept only model
m0 <- brmcoda(compilr = cilr,
formula = STRESS ~ 1 + (1 | ID),
iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000,
backend = "cmdstanr", save_pars = save_pars(all = TRUE))
#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt.
#> If your model failed to compile please run rebuild_cmdstan().
#> If the issue persists please open a bug report.
#> Error: An error occured during compilation! See the message above for more information.
# between-person composition only model
m1 <- brmcoda(compilr = cilr,
formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 + (1 | ID),
iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000,
backend = "cmdstanr", save_pars = save_pars(all = TRUE))
#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt.
#> If your model failed to compile please run rebuild_cmdstan().
#> If the issue persists please open a bug report.
#> Error: An error occured during compilation! See the message above for more information.
# within-person composition only model
m2 <- brmcoda(compilr = cilr,
formula = STRESS ~ wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID),
iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000,
backend = "cmdstanr", save_pars = save_pars(all = TRUE))
#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt.
#> If your model failed to compile please run rebuild_cmdstan().
#> If the issue persists please open a bug report.
#> Error: An error occured during compilation! See the message above for more information.
# full model
m <- brmcoda(compilr = cilr,
formula = STRESS ~ bilr1 + bilr2 + bilr3 + bilr4 +
wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID),
iter = 6000, chains = 8, cores = 8, seed = 123, warmup = 1000,
backend = "cmdstanr", save_pars = save_pars(all = TRUE))
#> Warning: CmdStan's precompiled header (PCH) files may need to be rebuilt.
#> If your model failed to compile please run rebuild_cmdstan().
#> If the issue persists please open a bug report.
#> Error: An error occured during compilation! See the message above for more information.
We can now compare these models with the
bayesfactor_models()
function, using the intercept-only
model as reference.
comparison <- bayesfactor_models(m$Model, m1$Model, m2$Model, denominator = m0$Model)
#> Warning: Bayes factors might not be precise.
#> For precise Bayes factors, sampling at least 40,000 posterior samples is recommended.
#> Error: Bridgesampling failed. Perhaps you did not set 'save_pars = save_pars(all = TRUE)' when fitting your model? If you are running bridge sampling on another machine than the one used to fit the model, you may need to set recompile = TRUE.
comparison
#> Bayes Factors for Model Comparison
#>
#> Model BF
#> [1] 2.90e-05
#>
#> * Against Denominator: [2]
#> * Bayes Factor Type: marginal likelihoods (bridgesampling)
We can see that model with only within-person composition is the best model - with \(BF\) = 11.86 compared to the null (intercept only).
Let’s compare these models against the full model.
update(comparison, reference = 1)
#> Bayes Factors for Model Comparison
#>
#> Model BF
#> [2] 3.45e+04
#>
#> * Against Denominator: [1]
#> * Bayes Factor Type: marginal likelihoods (bridgesampling)
Again, our data favours the within-person composition only model over the full model, giving 2.93 times more support.
Substitution model
When examining the relationships between compositional data and an
outcome, we often are also interested in the changes in an outcomes when
a fixed duration of time is reallocated from one compositional component
to another, while the other components remain constant. These changes
can be examined using the compositional isotemporal substitution model.
In multilevelcoda
, we extend this model to multilevel
approach to test both between-person and within-person changes. All
substitution models can be computed using the
substitution()
function, with the following arguments:
Argument | Description |
---|---|
object |
A fitted brmcoda object |
base |
A data.frame or data.table of possible
substitution of variables. |
This data set can be computed using function
possub
|
|
delta |
A integer, numeric value or vector indicating the amount of change in compositional parts for substitution |
level |
A character value or vector to specify whether the change in
composition should be at between -person and/or
within -person levels |
type |
A character value or vector to specify whether the estimated change
in outcome should be conditional or
marginal
|
regrid |
Optional reference grid consisting of combinations of covariates over which predictions are made. If not provided, the default reference grid is used. |
summary |
A logical value to indicate whether the prediction at each level of the reference grid or an average of them should be returned. |
... |
Additional arguments to be passed to
describe_posterior
|
Between-person substitution model
The below example examines the changes in stress for different pairwise substitution of sleep-wake behaviours for 5 minutes, at between-person level.
bsubm <- substitution(object = m, delta = 5,
level = "between", ref = "grandmean")
The output contains multiple data sets of results for all compositional components. Here are the results for changes in stress when sleep (TST) is substituted for 5 minutes, averaged across levels of covariates.
knitr::kable(bsubm$BetweenSub$TST)
Mean | CI_low | CI_high | Delta | From | To | Level | Reference |
---|---|---|---|---|---|---|---|
0.022 | -0.009 | 0.051 | 5 | WAKE | TST | between | grandmean |
0.004 | -0.013 | 0.019 | 5 | MVPA | TST | between | grandmean |
0.006 | -0.007 | 0.017 | 5 | LPA | TST | between | grandmean |
0.007 | -0.006 | 0.018 | 5 | SB | TST | between | grandmean |
-0.021 | -0.047 | 0.008 | -5 | WAKE | TST | between | grandmean |
-0.004 | -0.018 | 0.013 | -5 | MVPA | TST | between | grandmean |
-0.006 | -0.017 | 0.007 | -5 | LPA | TST | between | grandmean |
-0.007 | -0.018 | 0.006 | -5 | SB | TST | between | grandmean |
None of the results are significant, given that the credible intervals did not cross 0, showing that increasing sleep (TST) at the expense of any other behaviours was not associated in changes in stress. Notice there is no column indicating the levels of convariates, indicating that these results have been averaged.
Within-person substitution model
Let’s now take a look at how stress changes when different pairwise of sleep-wake behaviours are substituted for 5 minutes, at within-person level.
# Within-person substitution
wsubm <- substitution(object = m, delta = 5,
level = "within", ref = "grandmean")
Results for 5 minute substitution.
knitr::kable(wsubm$WithinSub$TST)
Mean | CI_low | CI_high | Delta | From | To | Level | Reference |
---|---|---|---|---|---|---|---|
0.017 | 0.002 | 0.031 | 5 | WAKE | TST | within | grandmean |
-0.003 | -0.009 | 0.003 | 5 | MVPA | TST | within | grandmean |
-0.005 | -0.009 | -0.001 | 5 | LPA | TST | within | grandmean |
-0.002 | -0.006 | 0.003 | 5 | SB | TST | within | grandmean |
-0.016 | -0.029 | -0.002 | -5 | WAKE | TST | within | grandmean |
0.003 | -0.003 | 0.009 | -5 | MVPA | TST | within | grandmean |
0.005 | 0.001 | 0.009 | -5 | LPA | TST | within | grandmean |
0.002 | -0.003 | 0.006 | -5 | SB | TST | within | grandmean |
At within-person level, there were significant results for substitution of sleep (TST) and time awake in bed (WAKE) for 5 minutes, but not other behaviours. Increasing sleep at the expense of time spent awake in bed predicted 0.02 higher stress [95% CI 0.00, 0.03], on a given day. Conversely, less sleep and more time awake in bed predicted less stress (b = -0.02 [95% CI -0.03, -0.00]).
More interesting substitution models
You can learn more about different types of substitution models
at
Compositional
Multilevel Substitution Models.