Title: | Estimate Bayesian Multilevel Models for Compositional Data |
---|---|
Description: | Implement Bayesian Multilevel Modelling for compositional data in a multilevel framework. Compute multilevel compositional data and Isometric log ratio (ILR) at between and within-person levels, fit Bayesian multilevel models for compositional predictors and outcomes, and run post-hoc analyses such as isotemporal substitution models. References: Le, Stanford, Dumuid, and Wiley (2024) <doi:10.48550/arXiv.2405.03985>, Le, Dumuid, Stanford, and Wiley (2024) <doi:10.48550/arXiv.2411.12407>. |
Authors: | Flora Le [aut, cre] , Joshua F. Wiley [aut] |
Maintainer: | Flora Le <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.3.1 |
Built: | 2024-11-25 16:18:12 UTC |
Source: | CRAN |
complr
object.Extract amounts and compositions in conventional formats as data.frames, matrices, or arrays.
## S3 method for class 'complr' as.data.frame(x, row.names = NULL, optional = TRUE, ...) ## S3 method for class 'complr' as.matrix(x, ...)
## S3 method for class 'complr' as.data.frame(x, row.names = NULL, optional = TRUE, ...) ## S3 method for class 'complr' as.matrix(x, ...)
x |
An object of class |
row.names , optional
|
Unused and only added for consistency with
the |
... |
generic argument, not in use. |
Compute Bayes factors from marginal likelihoods
## S3 method for class 'brmcoda' bayes_factor(x1, x2, ...)
## S3 method for class 'brmcoda' bayes_factor(x1, x2, ...)
x1 |
A |
x2 |
Another |
... |
Other arguments passed to |
Fit a brm
model with multilevel ILR coordinates
brmcoda(complr, formula, ...)
brmcoda(complr, formula, ...)
complr |
A |
formula |
A object of class |
... |
Further arguments passed to |
A brmcoda
with two elements
complr |
An object of class |
model |
An object of class |
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # inspects ILRs before passing to brmcoda names(cilr$between_logratio) names(cilr$within_logratio) names(cilr$logratio) # model with compositional predictor at between and within-person levels m1 <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") # model with compositional outcome m2 <- brmcoda(complr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ Stress + Female + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # inspects ILRs before passing to brmcoda names(cilr$between_logratio) names(cilr$within_logratio) names(cilr$logratio) # model with compositional predictor at between and within-person levels m1 <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") # model with compositional outcome m2 <- brmcoda(complr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ Stress + Female + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") }
This function is an alias of substitution
to estimates the the difference in an outcome
when compositional parts are substituted for specific unit(s) at between level
using a single reference composition (e.g., compositional mean at sample level).
It is recommended that users run substitution model using the substitution
function.
bsub( object, delta, basesub, summary = TRUE, ref = "grandmean", level = "between", weight = "equal", scale = c("response", "linear"), cores = NULL, ... )
bsub( object, delta, basesub, summary = TRUE, ref = "grandmean", level = "between", weight = "equal", scale = c("response", "linear"), cores = NULL, ... )
object |
A fitted |
delta |
A integer, numeric value or vector indicating the amount of substituted change between compositional parts. |
basesub |
A base substitution.
Can be a |
summary |
A logical value.
Should the estimate at each level of the reference grid ( |
ref |
Either a character value or vector or a dataset.
Can be |
level |
A character string or vector.
Should the estimate of multilevel models focus on the |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
scale |
Either |
cores |
Number of cores to use when executing the chains in parallel,
we recommend setting the |
... |
currently ignored. |
A list containing the results of multilevel compositional substitution model. The first six lists contain the results of the substitution estimation for a compositional part.
Mean |
Posterior means. |
CI_low and CI_high |
95% credible intervals. |
Delta |
Amount substituted across compositional parts. |
From |
Compositional part that is substituted from. |
To |
Compositional parts that is substituted to. |
Level |
Level where changes in composition takes place. |
Reference |
Either |
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and between-person levels m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + Female + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- bsub(object = m, basesub = psub, delta = 5) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and between-person levels m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + Female + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- bsub(object = m, basesub = psub, delta = 5) }
This function is an alias of substitution
to estimates the the difference in an outcome
when compositional parts are substituted for specific unit(s) at between level
using cluster mean (e.g., compositional mean at individual level) as reference composition.
It is recommended that users run substitution model using the substitution
function.
bsubmargins( object, delta, basesub, ref = "clustermean", level = "between", weight = "proportional", scale = c("response", "linear"), cores = NULL, ... )
bsubmargins( object, delta, basesub, ref = "clustermean", level = "between", weight = "proportional", scale = c("response", "linear"), cores = NULL, ... )
object |
A fitted |
delta |
A integer, numeric value or vector indicating the amount of substituted change between compositional parts. |
basesub |
A base substitution.
Can be a |
ref |
Either a character value or vector or a dataset.
Can be |
level |
A character string or vector.
Should the estimate of multilevel models focus on the |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
scale |
Either |
cores |
Number of cores to use when executing the chains in parallel,
we recommend setting the |
... |
currently ignored. |
A list containing the results of multilevel compositional substitution model. The first six lists contain the results of the substitution estimation for a compositional part.
Mean |
Posterior means. |
CI_low and CI_high |
95% credible intervals. |
Delta |
Amount substituted across compositional parts. |
From |
Compositional part that is substituted from. |
To |
Compositional parts that is substituted to. |
Level |
Level where changes in composition takes place. |
Reference |
Either |
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd[ID %in% 1:10, .SD[1:3], by = ID], sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + Female + (1 | ID), chains = 1, iter = 500, backend = "cmdstanr") subm <- bsubmargins(object = m, basesub = psub, delta = 5) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd[ID %in% 1:10, .SD[1:3], by = ID], sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + Female + (1 | ID), chains = 1, iter = 500, backend = "cmdstanr") subm <- bsubmargins(object = m, basesub = psub, delta = 5) }
Make a data set of all possible pairwise substitution of a composition which can be used as the base for substitution models.
build.basesub(parts, comparison = NULL)
build.basesub(parts, comparison = NULL)
parts |
A character vector specifying the names of compositional variables to be used. |
comparison |
Either |
A data table of all possible pairwise substitution.
ps1 <- build.basesub(parts = c("TST", "WAKE", "MVPA", "LPA", "SB")) print(ps1) ps2 <- build.basesub(c("WAKE", "MVPA", "LPA", "SB"), comparison = "one-to-all") print(ps2)
ps1 <- build.basesub(parts = c("TST", "WAKE", "MVPA", "LPA", "SB")) print(ps1) ps2 <- build.basesub(c("WAKE", "MVPA", "LPA", "SB"), comparison = "one-to-all") print(ps2)
substitution
model.Build a dataset for fitted.brmcoda
used in substitution
model
build.rg(object, ref, level, weight, fill = FALSE)
build.rg(object, ref, level, weight, fill = FALSE)
object |
A fitted |
ref |
Either a character value or vector or a dataset.
Can be |
level |
A character string or vector.
Should the estimate of multilevel models focus on the |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
fill |
Logical value only relevant when |
A reference grid consisting of a combination of covariates in brmcoda
Build a default sequential binary partition for complr
object.
The default sequential binary partition is a pivot balance that allows
the effect of this first balance coordinate to be interpreted as the change
in the prediction for the dependent variable
when that given part increases while all remaining parts decrease by a common proportion.
build.sbp(parts)
build.sbp(parts)
parts |
A character vector specifying the names of compositional variables to be used. |
A matrix sequential binary partition.
sbp1 <- build.sbp(parts = c("TST", "WAKE", "MVPA", "LPA", "SB")) print(sbp1) sbp2 <- build.sbp(c("WAKE", "MVPA", "LPA", "SB")) print(sbp2)
sbp1 <- build.sbp(parts = c("TST", "WAKE", "MVPA", "LPA", "SB")) print(sbp1) sbp2 <- build.sbp(c("WAKE", "MVPA", "LPA", "SB")) print(sbp2)
Extract model coefficients, which are the sum of population-level
effects and corresponding group-level effects
of the brmsfit
object in a brmcoda
object.
## S3 method for class 'brmcoda' coef(object, ...)
## S3 method for class 'brmcoda' coef(object, ...)
object |
An object of class |
... |
Further arguments passed to |
A list of 3D arrays (one per grouping factor).
If summary
is TRUE
,
the 1st dimension contains the factor levels,
the 2nd dimension contains the summary statistics
(see posterior_summary
), and
the 3rd dimension contains the group-level effects.
If summary
is FALSE
, the 1st dimension contains
the posterior draws, the 2nd dimension contains the factor levels,
and the 3rd dimension contains the group-level effects.
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## extract population and group-level coefficients separately fixef(m) ranef(m) ## extract combined coefficients coef(m) }
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## extract population and group-level coefficients separately fixef(m) ranef(m) ## extract combined coefficients coef(m) }
Indices from a (dataset of) Multilevel Composition(s) (deprecated.)
compilr(...)
compilr(...)
... |
arguments passed to |
A complr
object with at least the following elements.
comp |
A vector of class |
between_comp |
A vector of class |
within_comp |
A vector of class |
logratio |
Log ratio transform of composition. |
between_logratio |
Log ratio transform of between-person composition. |
within_logratio |
Log ratio transform of within-person composition. |
data |
The user's dataset or imputed dataset if the input data contains zeros. |
transform |
Type of transform applied on compositional data. |
parts |
Names of compositional variables. |
idvar |
Name of the variable containing IDs. |
total |
Total amount to which the compositions is closed. |
Compute sets of compositions and log ratio transformation for multilevel compositional data
complr(data, parts, sbp = NULL, total = 1, idvar = NULL, transform = "ilr")
complr(data, parts, sbp = NULL, total = 1, idvar = NULL, transform = "ilr")
data |
A |
parts |
A character vector specifying the names of compositional variables to be used. |
sbp |
A signary matrix indicating sequential binary partition. |
total |
A numeric value of the total amount to which the compositions should be closed.
Default is |
idvar |
Only for multilevel data, a character string specifying the name of the variable containing IDs. |
transform |
A character value naming a log ratio transformation to be applied on compositional data.
Can be either |
The ilr-transform maps the D-part compositional data from the simplex into non-overlapping subgroups in the (D-1)-dimension Euclidean space isometrically by using an orthonormal basis, thereby preserving the compositional properties and yielding a full-rank covariance matrix. ilr transformation should be preferred. However, the alr and clr are alternatives. The alr-transform maps a D-part composition in the Aitchison-simplex non-isometrically to a (D-1)-dimension Euclidian vectors, commonly treating the last part as the common denominator of the others. alr transformation does not rely on distance which breaks the constraint of compositional data. clr-transform maps a D-part composition in the Aitchison-simplex isometrically to a D-dimensional Euclidian vector subspace. clr transformation is not injetive, resulting in singular covariance matrices.
A complr
object with at least the following elements.
comp |
A vector of class |
between_comp |
A vector of class |
within_comp |
A vector of class |
logratio |
Log ratio transform of composition. |
between_logratio |
Log ratio transform of between-person composition. |
within_logratio |
Log ratio transform of within-person composition. |
data |
The user's dataset or imputed dataset if the input data contains zeros. |
transform |
Type of transform applied on compositional data. |
parts |
Names of compositional variables. |
idvar |
Name of the variable containing IDs. |
total |
Total amount to which the compositions is closed. |
cilr <- complr(data = mcompd, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) str(cilr) calr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", transform = "alr") str(calr) cclr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", transform = "clr") str(cclr) cilr_wide <- complr(data = mcompd[!duplicated(ID)], sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB")) str(cilr_wide)
cilr <- complr(data = mcompd, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) str(cilr) calr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", transform = "alr") str(calr) cclr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", transform = "clr") str(cclr) cilr_wide <- complr(data = mcompd[!duplicated(ID)], sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB")) str(cilr_wide)
substitution
class.Constructor function for substitution
class.
create_substitution( between_simple_sub, within_simple_sub, simple_sub, between_avg_sub, within_avg_sub, avg_sub, delta, ref, level, weight, parts, summary )
create_substitution( between_simple_sub, within_simple_sub, simple_sub, between_avg_sub, within_avg_sub, avg_sub, delta, ref, level, weight, parts, summary )
between_simple_sub |
A list of results from |
within_simple_sub |
A list of results from |
simple_sub |
A list of results from |
between_avg_sub |
A list of results from |
within_avg_sub |
A list of results from |
avg_sub |
A list of results from |
delta |
A numeric vector of the amount of substitution |
ref |
A character value specifying the reference grid |
level |
A character value specifying the level of substitution |
weight |
The weight to use in calculation of the reference composition |
parts |
The parts of the composition |
summary |
A logical value specifying whether to summarize the results |
An object of class substitution
brmsfit
Models in brmcoda
Extract Diagnostic Quantities from brmsfit
Models in brmcoda
## S3 method for class 'brmcoda' log_posterior(object, ...) ## S3 method for class 'brmcoda' nuts_params(object, ...) ## S3 method for class 'brmcoda' rhat(x, ...) ## S3 method for class 'brmcoda' neff_ratio(object, ...)
## S3 method for class 'brmcoda' log_posterior(object, ...) ## S3 method for class 'brmcoda' nuts_params(object, ...) ## S3 method for class 'brmcoda' rhat(x, ...) ## S3 method for class 'brmcoda' neff_ratio(object, ...)
... |
Arguments passed to individual methods (if applicable). |
x , object
|
A |
The exact form of the output depends on the method.
brmcoda
objectsIndex brmcoda
objects
## S3 method for class 'brmcoda' variables(x, ...) ## S3 method for class 'brmcoda' nvariables(x, ...) ## S3 method for class 'brmcoda' niterations(x) ## S3 method for class 'brmcoda' nchains(x) ## S3 method for class 'brmcoda' ndraws(x)
## S3 method for class 'brmcoda' variables(x, ...) ## S3 method for class 'brmcoda' nvariables(x, ...) ## S3 method for class 'brmcoda' niterations(x) ## S3 method for class 'brmcoda' nchains(x) ## S3 method for class 'brmcoda' ndraws(x)
x |
An object of class |
... |
Arguments passed to individual methods. |
Compute posterior draws of the expected value of the posterior predictive
distribution of a brmsfit
model in the brmcoda
object.
Can be performed for the data used to fit the model (posterior
predictive checks) or for new data. By definition, these predictions have
smaller variance than the posterior predictions performed by the
predict.brmcoda
method. This is because only the
uncertainty in the expected value of the posterior predictive distribution is
incorporated in the draws computed by fitted
while the
residual error is ignored there. However, the estimated means of both methods
averaged across draws should be very similar.
## S3 method for class 'brmcoda' fitted(object, scale = c("linear", "response"), summary = TRUE, ...)
## S3 method for class 'brmcoda' fitted(object, scale = c("linear", "response"), summary = TRUE, ...)
object |
An object of class |
scale |
Specifically for models with compositional responses,
either |
summary |
Should summary statistics be returned
instead of the raw values? Default is |
... |
Further arguments passed to |
An array
of predicted mean response values.
If summary = FALSE
the output resembles those of
posterior_epred.brmsfit
.
If summary = TRUE
the output depends on the family: For categorical
and ordinal families, the output is an N x E x C array, where N is the
number of observations, E is the number of summary statistics, and C is the
number of categories. For all other families, the output is an N x E
matrix. The number of summary statistics E is equal to 2 +
length(probs)
: The Estimate
column contains point estimates (either
mean or median depending on argument robust
), while the
Est.Error
column contains uncertainty estimates (either standard
deviation or median absolute deviation depending on argument
robust
). The remaining columns starting with Q
contain
quantile estimates as specified via argument probs
.
In multivariate models, an additional dimension is added to the output which indexes along the different response variables.
## fit a model if(requireNamespace("cmdstanr")){ ## compute composition and ilr coordinates cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) ## fit a model m1 <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## compute expected predictions epred <- fitted(m1) head(epred) ## fit a model with compositional outcome m2 <- brmcoda(complr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ Stress + Female + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## expected predictions on compositional scale epredcomp <- fitted(m2, scale = "response") head(epredcomp) }
## fit a model if(requireNamespace("cmdstanr")){ ## compute composition and ilr coordinates cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) ## fit a model m1 <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## compute expected predictions epred <- fitted(m1) head(epred) ## fit a model with compositional outcome m2 <- brmcoda(complr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ Stress + Female + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## expected predictions on compositional scale epredcomp <- fitted(m2, scale = "response") head(epredcomp) }
Extract the population-level ('fixed') effects
from the brmsfit
object in a brmcoda
object.
## S3 method for class 'brmcoda' fixef(object, ...)
## S3 method for class 'brmcoda' fixef(object, ...)
object |
An object of class |
... |
Further arguments passed to |
If summary
is TRUE
, a matrix returned
by posterior_summary
for the population-level effects.
If summary
is FALSE
, a matrix with one row per
posterior draw and one column per population-level effect.
## fit a model if(requireNamespace("cmdstanr")){ ## fit a model m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## extract population-Level coefficients fixef(m) }
## fit a model if(requireNamespace("cmdstanr")){ ## fit a model m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## extract population-Level coefficients fixef(m) }
Helper functions used only internally to estimate substitution model
brmcoda
objectChecks if argument is a brmcoda
object
is.brmcoda(x)
is.brmcoda(x)
x |
An object of class |
complr
objectChecks if argument is a complr
object
is.complr(x)
is.complr(x)
x |
An object of class |
substitution
objectChecks if argument is a substitution
object
is.substitution(x)
is.substitution(x)
x |
An object of class |
Provide an interface to shinystan for models fitted with brms
## S3 method for class 'brmcoda' launch_shinystan(object, ...)
## S3 method for class 'brmcoda' launch_shinystan(object, ...)
object |
A fitted model object of class |
... |
Optional arguments to pass to
|
An S4 shinystan object
Perform approximate leave-one-out cross-validation based
on the posterior likelihood using the loo package.
For more details see loo
.
## S3 method for class 'brmcoda' loo(x, ...)
## S3 method for class 'brmcoda' loo(x, ...)
x |
A |
... |
More |
If just one object is provided, an object of class loo
.
If multiple objects are provided, an object of class loolist
.
Call MCMC plotting functions implemented in the bayesplot package.
## S3 method for class 'brmcoda' mcmc_plot(object, ...)
## S3 method for class 'brmcoda' mcmc_plot(object, ...)
object |
A |
... |
Further arguments passed to |
A ggplot
object
that can be further customized using the ggplot2 package.
## Not run: cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # model with compositional predictor at between and within-person levels fit <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500) mcmc_plot(fit) ## End(Not run)
## Not run: cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # model with compositional predictor at between and within-person levels fit <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500) mcmc_plot(fit) ## End(Not run)
A simulated dataset containing multiple days of compositional data.
mcompd
mcompd
A data table containing 10 variables.
A unique identifier for each individual
Recurrence time of repeated measures by individual
Self report stress measures on a 0 to 10 scale — repeated measure
Total Sleep Time (minutes) — repeated measure
Wake time while in bed, trying to sleep (minutes) — repeated measure
Moderate to Vigorous Physical Activity (minutes) — repeated measure
Light Physical Activity (minutes) — repeated measure
Sedentary Behavior (minutes) — repeated measure
Age in years — baseline measure only
Binary: whether participants identified as female (1) or not (0) — baseline measure only
complr
object.Mean amounts and mean compositions presented in a complr
object.
## S3 method for class 'complr' mean(x, weight = c("equal", "proportional"), ...)
## S3 method for class 'complr' mean(x, weight = c("equal", "proportional"), ...)
x |
An object of class |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
... |
generic argument, not in use. |
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") mean(cilr)
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") mean(cilr)
Extracting the Model Frame from a Formula or Fit from brmcoda object
## S3 method for class 'brmcoda' model.frame(formula, ...)
## S3 method for class 'brmcoda' model.frame(formula, ...)
formula |
A |
... |
Further arguments to be passed to methods. |
Provide the full results for a simulation study testing the performance of multilevelcoda
multilevelcoda_sim()
multilevelcoda_sim()
An S4 shiny object
Extract Number of Observations from brmcoda object
## S3 method for class 'brmcoda' nobs(object, ...)
## S3 method for class 'brmcoda' nobs(object, ...)
object |
A |
... |
Further arguments to be passed to methods. |
brmcoda
's brmsfit
objectA pairs
method that is customized for MCMC output.
## S3 method for class 'brmcoda' pairs(x, ...)
## S3 method for class 'brmcoda' pairs(x, ...)
x |
A |
... |
Further arguments passed to |
## Not run: cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # model with compositional predictor at between and within-person levels fit <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500) pairs(fit) ## End(Not run)
## Not run: cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # model with compositional predictor at between and within-person levels fit <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500) pairs(fit) ## End(Not run)
Estimate pivot balance coordinates
pivot_coord(object, summary = TRUE, method = c("rotate", "refit"), ...)
pivot_coord(object, summary = TRUE, method = c("rotate", "refit"), ...)
object |
An object of class |
summary |
Should summary statistics be returned instead of the raw values? Default is |
method |
A character string.
Should the pivot balance coordinates be estimated by |
... |
currently ignored. |
A list of brmcoda
for each pivot balance coordinate.
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # inspects ILRs before passing to brmcoda names(cilr$between_logratio) names(cilr$within_logratio) names(cilr$logratio) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") m_pivot_coord <- pivot_coord(m) summary(m_pivot_coord) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # inspects ILRs before passing to brmcoda names(cilr$between_logratio) names(cilr$within_logratio) names(cilr$logratio) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") m_pivot_coord <- pivot_coord(m) summary(m_pivot_coord) }
Estimate pivot balance coordinates by refitting model.
pivot_coord_refit(object, ...)
pivot_coord_refit(object, ...)
object |
An object of class |
... |
Further arguments passed to |
A list of brmcoda
for each pivot balance coordinate.
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") m_pivot_coord_refit <- pivot_coord_refit(m) summary(m_pivot_coord_refit) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") m_pivot_coord_refit <- pivot_coord_refit(m) summary(m_pivot_coord_refit) }
Estimate pivot balance coordinates by rotating sequential binary partition.
pivot_coord_rotate(object, summary = TRUE, ...)
pivot_coord_rotate(object, summary = TRUE, ...)
object |
An object of class |
summary |
Should summary statistics be returned instead of the raw values? Default is |
... |
currently ignored. |
A list of brmcoda
for each pivot balance coordinate.
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") m_pivot_coord_rotate <- pivot_coord_rotate(m) summary(m_pivot_coord_rotate) m_pivot_coord_raw <- pivot_coord_rotate(m, summary = FALSE) posterior::summarise_draws(posterior::as_draws_array(m_pivot_coord_raw$output)) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") m_pivot_coord_rotate <- pivot_coord_rotate(m) summary(m_pivot_coord_rotate) m_pivot_coord_raw <- pivot_coord_rotate(m, summary = FALSE) posterior::summarise_draws(posterior::as_draws_array(m_pivot_coord_raw$output)) }
Make a plot of brmcoda
model results.
## S3 method for class 'brmcoda' plot(x, ...)
## S3 method for class 'brmcoda' plot(x, ...)
x |
A |
... |
Further arguments passed to |
An invisible list of
gtable
objects.
## Not run: cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # model with compositional predictor at between and within-person levels fit <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500) plot(fit) ## End(Not run)
## Not run: cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # model with compositional predictor at between and within-person levels fit <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500) plot(fit) ## End(Not run)
Make a plot of substitution
model results.
## S3 method for class 'substitution' plot(x, to, ref, level, ...)
## S3 method for class 'substitution' plot(x, to, ref, level, ...)
x |
A |
to |
A character value or vector specifying the names of the compositional parts that were reallocated to in the model. |
ref |
A character value of (( |
level |
A character value of ( |
... |
Further components to the plot, followed by a plus sign (+). |
A ggplot graph object showing the estimated difference in outcome when each pair of compositional variables are substituted for a specific time.
brmcoda
ObjectsPerform posterior predictive checks with the help of the bayesplot package.
## S3 method for class 'brmcoda' pp_check(object, ...)
## S3 method for class 'brmcoda' pp_check(object, ...)
object |
An object of class |
... |
Further arguments passed to |
Compute posterior draws of the posterior predictive distribution
of a brmsfit
model in the brmcoda
object.
Can be performed for the data used to fit the model (posterior predictive checks) or
for new data. By definition, these draws have higher variance than draws
of the expected value of the posterior predictive distribution computed by
fitted.brmcoda
. This is because the residual error
is incorporated in posterior_predict
. However, the estimated means of
both methods averaged across draws should be very similar.
## S3 method for class 'brmcoda' predict(object, scale = c("linear", "response"), summary = TRUE, ...)
## S3 method for class 'brmcoda' predict(object, scale = c("linear", "response"), summary = TRUE, ...)
object |
An object of class |
scale |
Specifically for models with compositional responses,
either |
summary |
Should summary statistics be returned
instead of the raw values? Default is |
... |
Further arguments passed to |
An array
of predicted response values.
If summary = FALSE
the output resembles those of
posterior_predict.brmsfit
.
If summary = TRUE
the output depends on the family: For categorical
and ordinal families, the output is an N x C matrix, where N is the number
of observations, C is the number of categories, and the values are
predicted category probabilities. For all other families, the output is a N
x E matrix where E = 2 + length(probs)
is the number of summary
statistics: The Estimate
column contains point estimates (either
mean or median depending on argument robust
), while the
Est.Error
column contains uncertainty estimates (either standard
deviation or median absolute deviation depending on argument
robust
). The remaining columns starting with Q
contain
quantile estimates as specified via argument probs
.
if(requireNamespace("cmdstanr")){ ## fit a model cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) m1 <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## predicted responses pred <- predict(m1) head(pred) ## fit a model with compositional outcome m2 <- brmcoda(complr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ Stress + Female + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## predicted responses on compositional scale predcomp <- predict(m2, scale = "linear") head(predcomp) }
if(requireNamespace("cmdstanr")){ ## fit a model cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) m1 <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## predicted responses pred <- predict(m1) head(pred) ## fit a model with compositional outcome m2 <- brmcoda(complr = cilr, formula = mvbind(ilr1, ilr2, ilr3, ilr4) ~ Stress + Female + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## predicted responses on compositional scale predcomp <- predict(m2, scale = "linear") head(predcomp) }
brmsfit
model in a brmcoda
objectPrint a Summary for a fitted brmsfit
model in a brmcoda
object
## S3 method for class 'brmcoda' print(x, ...)
## S3 method for class 'brmcoda' print(x, ...)
x |
An object of class |
... |
Other arguments passed to |
if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") print(m) }
if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") print(m) }
complr
objectPrint a Summary for a complr
object
## S3 method for class 'complr' print(x, ...)
## S3 method for class 'complr' print(x, ...)
x |
An object of class |
... |
Other arguments passed to |
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") print(cilr)
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") print(cilr)
substitution
objectPrint a Summary for a substitution
object
## S3 method for class 'substitution' print(x, ...)
## S3 method for class 'substitution' print(x, ...)
x |
A |
... |
Additional arguments to be passed to to method |
if(requireNamespace("cmdstanr")){ ## fit a model with compositional predictor at between and between-person levels m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- substitution(object = m, delta = 5) print(subm) }
if(requireNamespace("cmdstanr")){ ## fit a model with compositional predictor at between and between-person levels m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- substitution(object = m, delta = 5) print(subm) }
complr
objectSummary for a complr
object
## S3 method for class 'summary.complr' print(x, ...)
## S3 method for class 'summary.complr' print(x, ...)
x |
An object of class |
... |
Other arguments passed to |
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") print(cilr)
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") print(cilr)
brmsfit
from a brmcoda
objectCompute Bayes factors from marginal likelihoods
## S3 method for class 'brmcoda' prior_summary(object, ...)
## S3 method for class 'brmcoda' prior_summary(object, ...)
object |
An object of class |
... |
Further arguments passed to or from other methods. |
A dataset containing possible pairwise subsitutions.
psub
psub
A data table containing 5 variables.
first compositional variable
second compositional variable
third compositional variable
fourth compositional variable
fifth compositional variable
Extract the group-level ('random') effects of each level
of the brmsfit
object in a brmcoda
object.
## S3 method for class 'brmcoda' ranef(object, ...)
## S3 method for class 'brmcoda' ranef(object, ...)
object |
An object of class |
... |
Further arguments passed to |
A list of 3D arrays (one per grouping factor).
If summary
is TRUE
,
the 1st dimension contains the factor levels,
the 2nd dimension contains the summary statistics
(see posterior_summary
), and
the 3rd dimension contains the group-level effects.
If summary
is FALSE
, the 1st dimension contains
the posterior draws, the 2nd dimension contains the factor levels,
and the 3rd dimension contains the group-level effects.
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## extract group-level coefficients ranef(m) }
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## extract group-level coefficients ranef(m) }
Compute posterior draws of residuals/predictive errors
## S3 method for class 'brmcoda' residuals(object, ...)
## S3 method for class 'brmcoda' residuals(object, ...)
object |
An object of class |
... |
Further arguments passed to |
An array
of predictive error/residual draws. If
summary = FALSE
the output resembles those of
predictive_error.brmsfit
. If summary = TRUE
the output
is an N x E matrix, where N is the number of observations and E denotes
the summary statistics computed from the draws.
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## extract residuals res <- residuals(m) head(res) }
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") ## extract residuals res <- residuals(m) head(res) }
A matrix of sequential binary partition.
sbp
sbp
A matrix with 5 columns and 4 rows.
first compositional variable
second compositional variable
third compositional variable
fourth compositional variable
fifth compositional variable
A list of 4 components
sim
sim
A list with 5 columns and 4 rows.
Simulation results for brmcoda() for tables
Simulation results for substitution() for tables
Simulation results for brmcoda() for graphs
Simulation results for substitution() for graphs
This function is an alias of substitution
to estimates the the difference in an outcome
when compositional parts are substituted for specific unit(s)
using a aggregate reference composition
(e.g., compositional mean at sample level, not seperated by between- and within effects).
It is recommended that users run substitution model using the substitution
function.
sub( object, delta, basesub, summary = TRUE, ref = "grandmean", level = "aggregate", weight = "equal", scale = c("response", "linear"), cores = NULL, ... )
sub( object, delta, basesub, summary = TRUE, ref = "grandmean", level = "aggregate", weight = "equal", scale = c("response", "linear"), cores = NULL, ... )
object |
A fitted |
delta |
A integer, numeric value or vector indicating the amount of substituted change between compositional parts. |
basesub |
A base substitution.
Can be a |
summary |
A logical value.
Should the estimate at each level of the reference grid ( |
ref |
Either a character value or vector or a dataset.
Can be |
level |
A character string or vector.
Should the estimate of multilevel models focus on the |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
scale |
Either |
cores |
Number of cores to use when executing the chains in parallel,
we recommend setting the |
... |
currently ignored. |
A list containing the results of multilevel compositional substitution model. The first six lists contain the results of the substitution estimation for a compositional part.
Mean |
Posterior means. |
CI_low and CI_high |
95% credible intervals. |
Delta |
Amount substituted across compositional parts. |
From |
Compositional part that is substituted from. |
To |
Compositional parts that is substituted to. |
Level |
Level where changes in composition takes place. |
Reference |
Either |
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ ilr1 + ilr2 + ilr3 + ilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- sub(object = m, basesub = psub, delta = 5) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ ilr1 + ilr2 + ilr3 + ilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- sub(object = m, basesub = psub, delta = 5) }
This function is an alias of substitution
to estimates the the difference in an outcome
when compositional parts are substituted for specific unit(s)
using cluster mean (e.g., compositional mean at individual level) as reference composition.
It is recommended that users run substitution model using the substitution
function.
submargins( object, delta, basesub, ref = "clustermean", level = "aggregate", weight = "proportional", scale = c("response", "linear"), cores = NULL, ... )
submargins( object, delta, basesub, ref = "clustermean", level = "aggregate", weight = "proportional", scale = c("response", "linear"), cores = NULL, ... )
object |
A fitted |
delta |
A integer, numeric value or vector indicating the amount of substituted change between compositional parts. |
basesub |
A base substitution.
Can be a |
ref |
Either a character value or vector or a dataset.
Can be |
level |
A character string or vector.
Should the estimate of multilevel models focus on the |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
scale |
Either |
cores |
Number of cores to use when executing the chains in parallel,
we recommend setting the |
... |
currently ignored. |
A list containing the results of multilevel compositional substitution model. The first six lists contain the results of the substitution estimation for a compositional part.
Mean |
Posterior means. |
CI_low and CI_high |
95% credible intervals. |
Delta |
Amount substituted across compositional parts. |
From |
Compositional part that is substituted from. |
To |
Compositional parts that is substituted to. |
Level |
Level where changes in composition takes place. |
Reference |
Either |
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ ilr1 + ilr2 + ilr3 + ilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- submargins(object = m, basesub = psub, delta = 5) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ ilr1 + ilr2 + ilr3 + ilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- submargins(object = m, basesub = psub, delta = 5) }
Estimate the difference in an outcome
when compositional parts are substituted for specific unit(s).
The substitution
output encapsulates
the substitution results for all compositional parts
present in the brmcoda
object.
substitution( object, delta, basesub, summary = TRUE, ref = c("grandmean", "clustermean"), level = c("between", "within", "aggregate"), weight = c("equal", "proportional"), scale = c("response", "linear"), cores = NULL, ... )
substitution( object, delta, basesub, summary = TRUE, ref = c("grandmean", "clustermean"), level = c("between", "within", "aggregate"), weight = c("equal", "proportional"), scale = c("response", "linear"), cores = NULL, ... )
object |
A fitted |
delta |
A integer, numeric value or vector indicating the amount of substituted change between compositional parts. |
basesub |
A base substitution.
Can be a |
summary |
A logical value.
Should the estimate at each level of the reference grid ( |
ref |
Either a character value or vector or a dataset.
Can be |
level |
A character string or vector.
Should the estimate of multilevel models focus on the |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
scale |
Either |
cores |
Number of cores to use when executing the chains in parallel,
we recommend setting the |
... |
currently ignored. |
A list containing the results of multilevel compositional substitution model. The first six lists contain the results of the substitution estimation for a compositional part.
Mean |
Posterior means. |
CI_low and CI_high |
95% credible intervals. |
Delta |
Amount substituted across compositional parts. |
From |
Compositional part that is substituted from. |
To |
Compositional parts that is substituted to. |
Level |
Level where changes in composition takes place. |
Reference |
Either |
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels fit1 <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") # one to one reallocation at between and within-person levels sub1 <- substitution(object = fit1, delta = 5, level = c("between", "within")) summary(sub1) # one to all reallocation at between and within-person levels sub2 <- substitution(object = fit1, delta = 5, level = c("between", "within"), basesub = "one-to-all") summary(sub2) # model with compositional predictor at aggregate level of variance fit2 <- brmcoda(complr = cilr, formula = Stress ~ ilr1 + ilr2 + ilr3 + ilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") sub3 <- substitution(object = fit2, delta = 5, level = c("aggregate")) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels fit1 <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") # one to one reallocation at between and within-person levels sub1 <- substitution(object = fit1, delta = 5, level = c("between", "within")) summary(sub1) # one to all reallocation at between and within-person levels sub2 <- substitution(object = fit1, delta = 5, level = c("between", "within"), basesub = "one-to-all") summary(sub2) # model with compositional predictor at aggregate level of variance fit2 <- brmcoda(complr = cilr, formula = Stress ~ ilr1 + ilr2 + ilr3 + ilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") sub3 <- substitution(object = fit2, delta = 5, level = c("aggregate")) }
brmsfit
model in a brmcoda
objectCreate a Summary of a fitted brmsfit
model in a brmcoda
object
## S3 method for class 'brmcoda' summary(object, ...)
## S3 method for class 'brmcoda' summary(object, ...)
object |
An object of class |
... |
Other arguments passed to |
if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") summary(m) }
if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") summary(m) }
complr
objectCreate a Summary of a complr
object
## S3 method for class 'complr' summary(object, weight = c("equal", "proportional"), ...)
## S3 method for class 'complr' summary(object, weight = c("equal", "proportional"), ...)
object |
An object of class |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
... |
generic argument, not in use. |
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") summary(cilr) cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB")) summary(cilr)
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") summary(cilr) cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB")) summary(cilr)
brmsfit
model from a pivot_coord
objectCreate a Summary of a fitted brmsfit
model from a pivot_coord
object
## S3 method for class 'pivot_coord' summary(object, digits = 2, ...)
## S3 method for class 'pivot_coord' summary(object, digits = 2, ...)
object |
An object of class |
digits |
A integer value used for number formatting. Default is |
... |
currently ignored. |
A data table of results.
if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") m_pb <- pivot_coord(m) summary(m_pb) }
if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") m_pb <- pivot_coord(m) summary(m_pb) }
substitution
objectCreate a Summary of a Substitution Model represented by a substitution
object
## S3 method for class 'substitution' summary(object, delta, to, from, ref, level, digits = 2, ...)
## S3 method for class 'substitution' summary(object, delta, to, from, ref, level, digits = 2, ...)
object |
A |
delta |
A integer, numeric value or vector indicating the desired |
to |
A character value or vector specifying the names of the compositional parts that were reallocated to in the model. |
from |
A character value or vector specifying the names of the compositional parts that were reallocated from in the model. |
ref |
Either a character value or vector (( |
level |
A character string or vector ( |
digits |
A integer value used for number formatting. Default is |
... |
generic argument, not in use. |
A summary of substitution
object.
Mean |
Posterior means. |
CI_low and CI_high |
95% credible intervals. |
Delta |
Amount substituted across compositional parts. |
From |
Compositional part that is substituted from. |
To |
Compositional parts that is substituted to. |
Level |
Level where changes in composition takes place. Either |
Reference |
Either |
if(requireNamespace("cmdstanr")){ ## fit a model with compositional predictor at between and between-person levels m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- substitution(object = m, delta = 5) summary(subm) }
if(requireNamespace("cmdstanr")){ ## fit a model with compositional predictor at between and between-person levels m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- substitution(object = m, delta = 5) summary(subm) }
brmcoda
modelsThis method allows for updating an existing brmcoda
object.
## S3 method for class 'brmcoda' update(object, formula. = NULL, newdata = NULL, newcomplr = NULL, ...)
## S3 method for class 'brmcoda' update(object, formula. = NULL, newdata = NULL, newcomplr = NULL, ...)
object |
A fitted |
formula. |
Changes to the formula; for details see
|
newdata |
A |
newcomplr |
A |
... |
Further arguments passed to |
A brmcoda
with two elements
complr |
An object of class |
model |
An object of class |
if(requireNamespace("cmdstanr")){ # model with compositional predictor at between and within-person levels fit <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID"), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") # removing the effect of wilr1 fit1 <- update(fit, formula. = ~ . - wilr1) # using only a subset fit2 <- update(fit, newdata = mcompd[ID != 1]) }
if(requireNamespace("cmdstanr")){ # model with compositional predictor at between and within-person levels fit <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID"), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") # removing the effect of wilr1 fit1 <- update(fit, formula. = ~ . - wilr1) # using only a subset fit2 <- update(fit, newdata = mcompd[ID != 1]) }
complr
This method allows for updating an existing complr
object.
## S3 method for class 'complr' update(object, newdata, ...)
## S3 method for class 'complr' update(object, newdata, ...)
object |
A |
newdata |
A |
... |
generic argument, not in use. |
A complr
object with at least the following elements.
comp |
A vector of class |
between_comp |
A vector of class |
within_comp |
A vector of class |
logratio |
Log ratio transform of composition. |
between_logratio |
Log ratio transform of between-person composition. |
within_logratio |
Log ratio transform of within-person composition. |
data |
The user's dataset or imputed dataset if the input data contains zeros. |
transform |
Type of transform applied on compositional data. |
parts |
Names of compositional variables. |
idvar |
Name of the variable containing IDs. |
total |
Total amount to which the compositions is closed. |
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # update with new data newdat <- mcompd[ID != 1] # excluding ID 1 cilr1 <- update(object = cilr, newdata = newdat)
cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID") # update with new data newdat <- mcompd[ID != 1] # excluding ID 1 cilr1 <- update(object = cilr, newdata = newdat)
complr
object.Variance of compositions presented in a complr
object.
## S3 method for class 'complr' var(x, weight = c("equal", "proportional"), ...)
## S3 method for class 'complr' var(x, weight = c("equal", "proportional"), ...)
x |
An object of class |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
... |
generic argument, not in use. |
Calculates the estimated standard deviations,
correlations and covariances of the group-level terms
of the brmsfit
object in a brmcoda
object.
## S3 method for class 'brmcoda' VarCorr(x, ...)
## S3 method for class 'brmcoda' VarCorr(x, ...)
x |
An object of class |
... |
Further arguments passed to |
A list of lists (one per grouping factor), each with three elements: a matrix containing the standard deviations, an array containing the correlation matrix, and an array containing the covariance matrix with variances on the diagonal.
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") VarCorr(m) }
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") VarCorr(m) }
Get a point estimate of the covariance or
correlation matrix of population-level parameters
of the brmsfit
object in a brmcoda
object.
## S3 method for class 'brmcoda' vcov(object, ...)
## S3 method for class 'brmcoda' vcov(object, ...)
object |
An object of class |
... |
Further arguments passed to |
covariance or correlation matrix of population-level parameters
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") vcov(m) }
## fit a model if(requireNamespace("cmdstanr")){ m <- brmcoda(complr = complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440), formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") vcov(m) }
This function is an alias of substitution
to estimates the the difference in an outcome
when compositional parts are substituted for specific unit(s) at within level
using a single reference composition (e.g., compositional mean at sample level).
It is recommended that users run substitution model using the substitution
function.
wsub( object, basesub, delta, summary = TRUE, ref = "grandmean", level = "within", weight = "equal", scale = c("response", "linear"), cores = NULL, ... )
wsub( object, basesub, delta, summary = TRUE, ref = "grandmean", level = "within", weight = "equal", scale = c("response", "linear"), cores = NULL, ... )
object |
A fitted |
basesub |
A base substitution.
Can be a |
delta |
A integer, numeric value or vector indicating the amount of substituted change between compositional parts. |
summary |
A logical value.
Should the estimate at each level of the reference grid ( |
ref |
Either a character value or vector or a dataset.
Can be |
level |
A character string or vector.
Should the estimate of multilevel models focus on the |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
scale |
Either |
cores |
Number of cores to use when executing the chains in parallel,
we recommend setting the |
... |
currently ignored. |
A list containing the results of multilevel compositional substitution model. The first six lists contain the results of the substitution estimation for a compositional part.
Mean |
Posterior means. |
CI_low and CI_high |
95% credible intervals. |
Delta |
Amount substituted across compositional parts. |
From |
Compositional part that is substituted from. |
To |
Compositional parts that is substituted to. |
Level |
Level where changes in composition takes place. |
Reference |
Either |
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- wsub(object = m, basesub = psub, delta = 60) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- wsub(object = m, basesub = psub, delta = 60) }
This function is an alias of substitution
to estimates the the difference in an outcome
when compositional parts are substituted for specific unit(s) at within level
using cluster mean (e.g., compositional mean at individual level) as reference composition.
It is recommended that users run substitution model using the substitution
function.
wsubmargins( object, delta, basesub, ref = "clustermean", level = "within", weight = "proportional", scale = c("response", "linear"), cores = NULL, ... )
wsubmargins( object, delta, basesub, ref = "clustermean", level = "within", weight = "proportional", scale = c("response", "linear"), cores = NULL, ... )
object |
A fitted |
delta |
A integer, numeric value or vector indicating the amount of substituted change between compositional parts. |
basesub |
A base substitution.
Can be a |
ref |
Either a character value or vector or a dataset.
Can be |
level |
A character string or vector.
Should the estimate of multilevel models focus on the |
weight |
A character value specifying the weight to use in calculation of the reference composition.
If |
scale |
Either |
cores |
Number of cores to use when executing the chains in parallel,
we recommend setting the |
... |
currently ignored. |
A list containing the results of multilevel compositional substitution model. The first six lists contain the results of the substitution estimation for a compositional part.
Mean |
Posterior means. |
CI_low and CI_high |
95% credible intervals. |
Delta |
Amount substituted across compositional parts. |
From |
Compositional part that is substituted from. |
To |
Compositional parts that is substituted to. |
Level |
Level where changes in composition takes place. |
Reference |
Either |
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- wsubmargins(object = m, basesub = psub, delta = 5) }
if(requireNamespace("cmdstanr")){ cilr <- complr(data = mcompd, sbp = sbp, parts = c("TST", "WAKE", "MVPA", "LPA", "SB"), idvar = "ID", total = 1440) # model with compositional predictor at between and within-person levels m <- brmcoda(complr = cilr, formula = Stress ~ bilr1 + bilr2 + bilr3 + bilr4 + wilr1 + wilr2 + wilr3 + wilr4 + (1 | ID), chain = 1, iter = 500, backend = "cmdstanr") subm <- wsubmargins(object = m, basesub = psub, delta = 5) }