| Title: | Bayesian Super Imposition by Translation and Rotation Growth Curve Analysis |
|---|---|
| Description: | The Super Imposition by Translation and Rotation (SITAR) model is a shape-invariant nonlinear mixed effect model that fits a natural cubic spline mean curve to the growth data and aligns individual-specific growth curves to the underlying mean curve via a set of random effects (see Cole, 2010 <doi:10.1093/ije/dyq115> for details). The non-Bayesian version of the SITAR model can be fit by using the already available R package 'sitar'. Unlike the 'sitar' package which allows modelling of a single outcome only, the 'bsitar' package offers great flexibility in fitting models of varying complexities, including joint modelling of multiple outcomes such as height and weight (multivariate model). Additionally, the 'bsitar' package allows for the simultaneous analysis of an outcome separately for subgroups defined by a factor variable such as gender. This is achieved by fitting separate models for each subgroup (for example males and females for gender variable). An advantage of this approach is that posterior draws for each subgroup are part of a single model object, making it possible to compare coefficients across subgroups and test hypotheses. Since the 'bsitar' package is a front-end to the R package 'brms', it offers excellent support for post-processing of posterior draws via various functions that are directly available from the 'brms' package. In addition, the 'bsitar' package includes various customized functions that allow for the visualization of distance (increase in size with age) and velocity (change in growth rate as a function of age), as well as the estimation of growth spurt parameters such as age at peak growth velocity and peak growth velocity. |
| Authors: | Satpal Sandhu [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-8539-6897>) |
| Maintainer: | Satpal Sandhu <[email protected]> |
| License: | GPL-2 |
| Version: | 0.3.3.9983 |
| Built: | 2026-07-03 17:29:53 UTC |
| Source: | https://github.com/sandhu-ss/bsitar |
The add_model_criterion() function is a wrapper around
brms::add_criterion() that allows adding fit criteria to a model. Note
that arguments such as compare and pointwise are relevant
only for brms::add_loo, while summary, robust, and
probs are ignored except for the brms::bayes_R2()
## S3 method for class 'bgmfit' add_model_criterion( model, criterion = c("loo", "waic"), ndraws = NULL, draw_ids = NULL, compare = TRUE, pointwise = FALSE, model_name = NULL, overwrite = FALSE, force_save = FALSE, file = NULL, summary = TRUE, robust = FALSE, probs = c(0.025, 0.975), newdata = NULL, resp = NULL, cores = 1, model_deriv = NULL, return_model = TRUE, return_criteria = FALSE, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, envir = NULL, ... ) add_model_criterion(model, ...)## S3 method for class 'bgmfit' add_model_criterion( model, criterion = c("loo", "waic"), ndraws = NULL, draw_ids = NULL, compare = TRUE, pointwise = FALSE, model_name = NULL, overwrite = FALSE, force_save = FALSE, file = NULL, summary = TRUE, robust = FALSE, probs = c(0.025, 0.975), newdata = NULL, resp = NULL, cores = 1, model_deriv = NULL, return_model = TRUE, return_criteria = FALSE, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, envir = NULL, ... ) add_model_criterion(model, ...)
model |
An object of class |
criterion |
Names of model fit criteria
to compute. Currently supported are |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
compare |
A flag indicating if the information criteria
of the models should be compared to each other
via |
pointwise |
A flag indicating whether to compute the full
log-likelihood matrix at once or separately for each observation.
The latter approach is usually considerably slower but
requires much less working memory. Accordingly, if one runs
into memory issues, |
model_name |
Optional name of the model. If |
overwrite |
Logical; Indicates if already stored fit
indices should be overwritten. Defaults to |
force_save |
Logical; only relevant if |
file |
Either |
summary |
A logical value indicating whether only the estimate should be
computed ( |
robust |
A logical value to specify the summary options. If |
probs |
The percentiles to be computed by the |
newdata |
An optional data frame for estimation. If |
resp |
A character string (default |
cores |
An integer specifying the number of cores for parallel execution.
|
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
return_model |
A logical (default |
return_criteria |
A logical (default |
verbose |
A logical argument (default |
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
envir |
The environment used for function evaluation. The default is
|
... |
Further arguments passed on to the functions from the brms |
An object of class bgmfit with the specified fit criteria
added.
Satpal Sandhu [email protected]
brms::add_loo, brms::add_ic(), brms::add_waic(),
brms::bayes_R2()
# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # Add model fit criteria (e.g., WAIC) model <- add_model_criterion(model, criterion = c("waic"))# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # Add model fit criteria (e.g., WAIC) model <- add_model_criterion(model, criterion = c("waic"))
Longitudinal growth records for 136 children.
berkeleyberkeley
A data frame with 4884 observations on the following 10 variables:
Factor variable with levels 201-278 for males and 301-385 for females.
Age in years (numeric vector).
Height in cm (numeric vector).
Weight in kg (numeric vector).
Stem length in cm (numeric vector).
Biacromial diameter in cm (numeric vector).
Bi-iliac diameter in cm (numeric vector).
Leg circumference in cm (numeric vector).
Dynamometric strength in pounds (numeric vector).
Factor variable with level 1 for male and level 2 for female.
The data was originally included as an appendix in the book Physical Growth of California Boys and Girls from Birth to Eighteen Years authored by Tuddenham and Snyder (1954). The dataset was later used as an example in the sitar (Cole 2022) package after correcting transcription errors.
A more detailed description, including the frequency of measurements per year, is provided in the sitar package (Cole 2022). Briefly, the data consists of repeated growth measurements made on 66 boys and 70 girls (ages 0 to 21). The children were born in 1928-29 in Berkeley, California, and were of northern European ancestry. Measurements were taken at the following ages:
0 years (at birth),
0.085 years,
0.25 to 2 years (every 3 months),
2 to 8 years (annually),
8 to 21 years (every 6 months).
The data includes measurements for height, weight (undressed), stem length,
biacromial diameter, bi-iliac diameter, leg circumference, and
dynamometric strength.
A data frame with 10 columns.
Satpal Sandhu [email protected]
Cole T (2022).
sitar: Super Imposition by Translation and Rotation Growth Curve Analysis.
R package version 1.3.0, https://CRAN.R-project.org/package=sitar.
Tuddenham RD, Snyder MM (1954).
“Physical growth of California boys and girls from birth to eighteen years.”
Publications in Child Development. University of California, Berkeley, 1(2), 183–364.
https://pubmed.ncbi.nlm.nih.gov/13217130/.
A subset of the berkeley data, containing longitudinal growth data for 70 females (aged 8 to 18 years).
berkeley_exdataberkeley_exdata
A data frame with the following 3 variables:
A factor variable identifying the subject.
Age in years, numeric vector.
Height in centimeters, numeric vector.
Detailed information about the full dataset can be found in the berkeley data.
A data frame with 3 columns.
Satpal Sandhu [email protected]
Bayesian SITAR model fit to the berkeley_exdata dataset (70 females, aged 8 to 18 years).
berkeley_exfitberkeley_exfit
A model fit object containing a summary of the posterior draws.
Detailed information about the data can be found in the berkeley_exdata dataset.
An object of class bgmfit containing the posterior draws.
Satpal Sandhu [email protected]
A Bayesian implementation of the Super Imposition by Translation and Rotation (SITAR) model. The SITAR model is a nonlinear mixed-effects framework widely used to summarize individual growth trajectories (e.g., height or weight) from early childhood through adulthood.
The frequentist implementation of the SITAR model is available in the sitar package (Cole 2022). In contrast, bsitar extends this framework within a Bayesian paradigm, offering increased flexibility in model specification, improved uncertainty quantification, and richer inferential capabilities.
Beyond standard Univariate analysis (i.e., modeling a single outcome), bsitar supports Univariate-by-subgroup analysis and Multivariate analysis as described below.
Univariate analysis: Modeling a single outcome, consistent with the traditional use of the sitar package.
Univariate-by-subgroup analysis: Simultaneous modeling of a single outcome across subgroups defined by a factor variable (e.g., sex). Posterior draws for all subgroups are obtained within a single model object, facilitating direct comparison of parameters and formal hypothesis testing across groups.
Multivariate analysis: Joint modeling of two or more outcomes, allowing for a more comprehensive representation of correlated growth processes.
Overall, the Bayesian implementation in bsitar provides a flexible and robust framework for growth curve modeling, extending the capabilities of the traditional frequentist approach.
bsitar( x, y, id, data, df = 4, knots = NA, knots_selection = NULL, fixed = "a + b + c", random = "a + b + c", xoffset = "mean", bstart = xoffset, cstart = 0, xfun = NULL, yfun = NULL, xfunxoffset = TRUE, bound = 0.04, stype = "rcs", terms_rhs = NULL, a_formula = ~1, b_formula = ~1, c_formula = ~1, d_formula = ~1, s_formula = ~1, a_formula_gr = ~1, b_formula_gr = ~1, c_formula_gr = ~1, d_formula_gr = ~1, a_formula_gr_str = NULL, b_formula_gr_str = NULL, c_formula_gr_str = NULL, d_formula_gr_str = NULL, d_adjusted = FALSE, sigma_formula = NULL, sigma_formula_gr = NULL, sigma_formula_gr_str = NULL, sigma_formula_manual = NULL, sigmax = NULL, sigmaid = NULL, sigmadf = 4, sigmaknots = NA, sigmafixed = NULL, sigmarandom = "", sigmaxoffset = "mean", sigmabstart = "sigmaxoffset", sigmacstart = 0, sigmaxfun = NULL, sigmabound = 0.04, sigmaxfunxoffset = TRUE, dpar_formula = NULL, autocor_formula = NULL, family = gaussian(), custom_family = NULL, custom_formula = NULL, custom_prior = NULL, custom_stanvars = NULL, group_arg = list(groupvar = NULL, by = NULL, cor = "un", cov = NULL, dist = "gaussian"), sigma_group_arg = list(groupvar = NULL, by = NULL, cor = un, cov = NULL, dist = "gaussian"), univariate_by = list(by = NA, cor = "un", terms = "subset"), multivariate = list(mvar = FALSE, cor = "un", rescor = TRUE, rcorr_by = NULL, rcorr_gr = NULL, rcorr_method = NULL, rcorr_prior = NULL), a_prior_beta = normal(ymean, ysd, autoscale = FALSE), b_prior_beta = normal(0, 2, autoscale = FALSE), c_prior_beta = normal(0, 1, autoscale = FALSE), d_prior_beta = normal(0, 1, autoscale = FALSE), s_prior_beta = normal(lm, lm, autoscale = FALSE), a_cov_prior_beta = normal(0, 50, autoscale = FALSE), b_cov_prior_beta = normal(0, 2, autoscale = FALSE), c_cov_prior_beta = normal(0, 1, autoscale = FALSE), d_cov_prior_beta = normal(0, 1, autoscale = FALSE), s_cov_prior_beta = normal(lm, lm, autoscale = FALSE), a_prior_sd = normal(0, ysd, autoscale = FALSE), b_prior_sd = normal(0, 2, autoscale = FALSE), c_prior_sd = normal(0, 1, autoscale = FALSE), d_prior_sd = normal(0, 1, autoscale = FALSE), a_cov_prior_sd = normal(0, 5, autoscale = FALSE), b_cov_prior_sd = normal(0, 1, autoscale = FALSE), c_cov_prior_sd = normal(0, 0.1, autoscale = FALSE), d_cov_prior_sd = normal(0, 1, autoscale = FALSE), a_prior_sd_str = NULL, b_prior_sd_str = NULL, c_prior_sd_str = NULL, d_prior_sd_str = NULL, a_cov_prior_sd_str = NULL, b_cov_prior_sd_str = NULL, c_cov_prior_sd_str = NULL, d_cov_prior_sd_str = NULL, sigma_prior_beta = normal(0, 1, autoscale = FALSE), sigma_cov_prior_beta = normal(0, 0.5, autoscale = FALSE), sigma_prior_sd = normal(0, 0.25, autoscale = FALSE), sigma_cov_prior_sd = normal(0, 0.15, autoscale = FALSE), sigma_prior_sd_str = NULL, sigma_cov_prior_sd_str = NULL, rsd_prior_sigma = normal(0, ysd, autoscale = FALSE), dpar_prior_sigma = normal(0, ysd, autoscale = FALSE), dpar_cov_prior_sigma = normal(0, 1, autoscale = FALSE), autocor_prior_acor = uniform(-1, 1, autoscale = FALSE), autocor_prior_unstr_acor = lkj(1), gr_prior_cor = lkj(1), gr_prior_cor_str = lkj(1), sigma_prior_cor = lkj(1), sigma_prior_cor_str = lkj(1), mvr_prior_rescor = lkj(1), init = NULL, init_r = 0.5, a_init_beta = "lm", b_init_beta = 0, c_init_beta = 0, d_init_beta = 0, s_init_beta = "lm", a_cov_init_beta = "lm", b_cov_init_beta = 0, c_cov_init_beta = 0, d_cov_init_beta = 0, s_cov_init_beta = 0, a_init_sd = "random", b_init_sd = "random", c_init_sd = "random", d_init_sd = "random", a_cov_init_sd = "random", b_cov_init_sd = "random", c_cov_init_sd = "random", d_cov_init_sd = "random", sigma_init_beta = "random", sigma_cov_init_beta = "random", sigma_init_sd = "random", sigma_cov_init_sd = "random", gr_init_cor = "random", sigma_init_cor = "random", rsd_init_sigma = "random", dpar_init_sigma = "random", dpar_cov_init_sigma = "random", autocor_init_acor = "random", autocor_init_unstr_acor = "random", mvr_init_rescor = "random", r_init_z = "random", vcov_init_0 = FALSE, jitter_init_beta = NULL, jitter_init_sd = NULL, jitter_init_cor = NULL, prior_data = NULL, init_data = NULL, init_custom = NULL, expose_function = FALSE, get_stancode = FALSE, get_standata = FALSE, get_formula = FALSE, get_stanvars = FALSE, get_priors = FALSE, get_priors_eval = FALSE, get_init_eval = FALSE, validate_priors = FALSE, set_self_priors = NULL, add_self_priors = NULL, set_replace_priors = NULL, set_same_priors_hierarchy = FALSE, outliers = NULL, unused = NULL, chains = 4, iter = 2000, warmup = floor(iter/2), thin = 1, cores = getOption("mc.cores", "optimize"), backend = getOption("brms.backend", "rstan"), threads = getOption("brms.threads", "optimize"), opencl = getOption("brms.opencl", NULL), normalize = getOption("brms.normalize", TRUE), algorithm = getOption("brms.algorithm", "sampling"), control = list(adapt_delta = 0.9, max_treedepth = 15), empty = FALSE, rename = TRUE, pathfinder_args = NULL, pathfinder_init = FALSE, data2 = NULL, data_custom = NULL, genquant_xyadj = FALSE, sample_prior = "no", save_pars = NULL, drop_unused_levels = TRUE, stan_model_args = list(), refresh = NULL, silent = 1, seed = 123, save_model = NULL, fit = NA, file = NULL, file_compress = TRUE, file_refit = getOption("brms.file_refit", "never"), future = getOption("future", FALSE), sum_zero = FALSE, global_args = NULL, parameterization = "ncp", verbose = FALSE, ... )bsitar( x, y, id, data, df = 4, knots = NA, knots_selection = NULL, fixed = "a + b + c", random = "a + b + c", xoffset = "mean", bstart = xoffset, cstart = 0, xfun = NULL, yfun = NULL, xfunxoffset = TRUE, bound = 0.04, stype = "rcs", terms_rhs = NULL, a_formula = ~1, b_formula = ~1, c_formula = ~1, d_formula = ~1, s_formula = ~1, a_formula_gr = ~1, b_formula_gr = ~1, c_formula_gr = ~1, d_formula_gr = ~1, a_formula_gr_str = NULL, b_formula_gr_str = NULL, c_formula_gr_str = NULL, d_formula_gr_str = NULL, d_adjusted = FALSE, sigma_formula = NULL, sigma_formula_gr = NULL, sigma_formula_gr_str = NULL, sigma_formula_manual = NULL, sigmax = NULL, sigmaid = NULL, sigmadf = 4, sigmaknots = NA, sigmafixed = NULL, sigmarandom = "", sigmaxoffset = "mean", sigmabstart = "sigmaxoffset", sigmacstart = 0, sigmaxfun = NULL, sigmabound = 0.04, sigmaxfunxoffset = TRUE, dpar_formula = NULL, autocor_formula = NULL, family = gaussian(), custom_family = NULL, custom_formula = NULL, custom_prior = NULL, custom_stanvars = NULL, group_arg = list(groupvar = NULL, by = NULL, cor = "un", cov = NULL, dist = "gaussian"), sigma_group_arg = list(groupvar = NULL, by = NULL, cor = un, cov = NULL, dist = "gaussian"), univariate_by = list(by = NA, cor = "un", terms = "subset"), multivariate = list(mvar = FALSE, cor = "un", rescor = TRUE, rcorr_by = NULL, rcorr_gr = NULL, rcorr_method = NULL, rcorr_prior = NULL), a_prior_beta = normal(ymean, ysd, autoscale = FALSE), b_prior_beta = normal(0, 2, autoscale = FALSE), c_prior_beta = normal(0, 1, autoscale = FALSE), d_prior_beta = normal(0, 1, autoscale = FALSE), s_prior_beta = normal(lm, lm, autoscale = FALSE), a_cov_prior_beta = normal(0, 50, autoscale = FALSE), b_cov_prior_beta = normal(0, 2, autoscale = FALSE), c_cov_prior_beta = normal(0, 1, autoscale = FALSE), d_cov_prior_beta = normal(0, 1, autoscale = FALSE), s_cov_prior_beta = normal(lm, lm, autoscale = FALSE), a_prior_sd = normal(0, ysd, autoscale = FALSE), b_prior_sd = normal(0, 2, autoscale = FALSE), c_prior_sd = normal(0, 1, autoscale = FALSE), d_prior_sd = normal(0, 1, autoscale = FALSE), a_cov_prior_sd = normal(0, 5, autoscale = FALSE), b_cov_prior_sd = normal(0, 1, autoscale = FALSE), c_cov_prior_sd = normal(0, 0.1, autoscale = FALSE), d_cov_prior_sd = normal(0, 1, autoscale = FALSE), a_prior_sd_str = NULL, b_prior_sd_str = NULL, c_prior_sd_str = NULL, d_prior_sd_str = NULL, a_cov_prior_sd_str = NULL, b_cov_prior_sd_str = NULL, c_cov_prior_sd_str = NULL, d_cov_prior_sd_str = NULL, sigma_prior_beta = normal(0, 1, autoscale = FALSE), sigma_cov_prior_beta = normal(0, 0.5, autoscale = FALSE), sigma_prior_sd = normal(0, 0.25, autoscale = FALSE), sigma_cov_prior_sd = normal(0, 0.15, autoscale = FALSE), sigma_prior_sd_str = NULL, sigma_cov_prior_sd_str = NULL, rsd_prior_sigma = normal(0, ysd, autoscale = FALSE), dpar_prior_sigma = normal(0, ysd, autoscale = FALSE), dpar_cov_prior_sigma = normal(0, 1, autoscale = FALSE), autocor_prior_acor = uniform(-1, 1, autoscale = FALSE), autocor_prior_unstr_acor = lkj(1), gr_prior_cor = lkj(1), gr_prior_cor_str = lkj(1), sigma_prior_cor = lkj(1), sigma_prior_cor_str = lkj(1), mvr_prior_rescor = lkj(1), init = NULL, init_r = 0.5, a_init_beta = "lm", b_init_beta = 0, c_init_beta = 0, d_init_beta = 0, s_init_beta = "lm", a_cov_init_beta = "lm", b_cov_init_beta = 0, c_cov_init_beta = 0, d_cov_init_beta = 0, s_cov_init_beta = 0, a_init_sd = "random", b_init_sd = "random", c_init_sd = "random", d_init_sd = "random", a_cov_init_sd = "random", b_cov_init_sd = "random", c_cov_init_sd = "random", d_cov_init_sd = "random", sigma_init_beta = "random", sigma_cov_init_beta = "random", sigma_init_sd = "random", sigma_cov_init_sd = "random", gr_init_cor = "random", sigma_init_cor = "random", rsd_init_sigma = "random", dpar_init_sigma = "random", dpar_cov_init_sigma = "random", autocor_init_acor = "random", autocor_init_unstr_acor = "random", mvr_init_rescor = "random", r_init_z = "random", vcov_init_0 = FALSE, jitter_init_beta = NULL, jitter_init_sd = NULL, jitter_init_cor = NULL, prior_data = NULL, init_data = NULL, init_custom = NULL, expose_function = FALSE, get_stancode = FALSE, get_standata = FALSE, get_formula = FALSE, get_stanvars = FALSE, get_priors = FALSE, get_priors_eval = FALSE, get_init_eval = FALSE, validate_priors = FALSE, set_self_priors = NULL, add_self_priors = NULL, set_replace_priors = NULL, set_same_priors_hierarchy = FALSE, outliers = NULL, unused = NULL, chains = 4, iter = 2000, warmup = floor(iter/2), thin = 1, cores = getOption("mc.cores", "optimize"), backend = getOption("brms.backend", "rstan"), threads = getOption("brms.threads", "optimize"), opencl = getOption("brms.opencl", NULL), normalize = getOption("brms.normalize", TRUE), algorithm = getOption("brms.algorithm", "sampling"), control = list(adapt_delta = 0.9, max_treedepth = 15), empty = FALSE, rename = TRUE, pathfinder_args = NULL, pathfinder_init = FALSE, data2 = NULL, data_custom = NULL, genquant_xyadj = FALSE, sample_prior = "no", save_pars = NULL, drop_unused_levels = TRUE, stan_model_args = list(), refresh = NULL, silent = 1, seed = 123, save_model = NULL, fit = NA, file = NULL, file_compress = TRUE, file_refit = getOption("brms.file_refit", "never"), future = getOption("future", FALSE), sum_zero = FALSE, global_args = NULL, parameterization = "ncp", verbose = FALSE, ... )
x |
Predictor variable (typically age in years). For a |
y |
Response variable (e.g., repeated height measurements). For
|
id |
A factor variable uniquely identifying the groups (e.g.,
individuals) in the data frame. For |
data |
A data frame containing variables such as |
df |
Degrees of freedom for the natural cubic spline design matrix
(default |
knots |
A numeric vector specifying the knots for the natural cubic
spline design matrix (default |
knots_selection |
A named list to control knot optimization during model
fitting. This feature explores a larger candidate set (typically
Note that when both Example usage:
|
fixed |
A character string specifying the fixed effects structure
(default |
random |
A character string specifying the random effects structure
(default |
xoffset |
An optional character string or numeric value to set the
origin of the predictor variable,
For |
bstart |
An optional character string or numeric value to set the origin
of the fixed effect parameter
The default is |
cstart |
An optional character string or numeric value to set the origin
of the fixed effect parameter
Note that since parameter |
xfun |
An optional character string specifying the transformation of the
predictor variable
For |
yfun |
An optional character string specifying the transformation of the
response variable
For |
xfunxoffset |
Transformation applied to |
bound |
An optional real number specifying the value by which the span
of the predictor variable |
stype |
A character string or a named list specifying the spline type to be used. The available options are:
The For more details, see |
terms_rhs |
An optional character string (default For example, when fitting a model with measurement error in the response
variable, the formula in For a Note that |
a_formula |
Formula for the fixed effect parameter, For example, The formula can include a combination of continuous and factor variables, as well as their interactions. |
b_formula |
Formula for the fixed effect parameter, |
c_formula |
Formula for the fixed effect parameter, |
d_formula |
Formula for the fixed effect parameter, |
s_formula |
Formula for the fixed effect parameter, |
a_formula_gr |
Formula for the random effect parameter In addition to defining the design matrix for the random effect parameter
For example, to include only random intercepts for parameters
To include a grouping variable (e.g.,
Here, Alternatively, the grouping structure and correlation can be specified
using the
together with |
b_formula_gr |
Formula for the random effect parameter |
c_formula_gr |
Formula for the random effect parameter |
d_formula_gr |
Formula for the random effect parameter |
a_formula_gr_str |
Formula for the random effect parameter For
In this setup, More generally, users can specify models with additional hierarchical levels and include covariates within the random effect formulas as needed. |
b_formula_gr_str |
Formula for the random effect parameter |
c_formula_gr_str |
Formula for the random effect parameter |
d_formula_gr_str |
Formula for the random effect parameter |
d_adjusted |
Logical indicator for adjusting the scale of the predictor
variable If This adjusted scale represents individual developmental age rather than
chronological age, making the parameter |
sigma_formula |
Formula for the distributional (fixed effect) parameter
By default, An alternative approach is to use the The arguments External functions (e.g.,
|
sigma_formula_gr |
Formula for the random effect parameter |
sigma_formula_gr_str |
Formula for the random effect parameter
As with See |
sigma_formula_manual |
A custom formula for advanced modeling of the
distributional parameter Use Cases: The primary use cases for
Basic variance modelling:
A simple example of modeling sigma_formula_manual = list(nlf(sigma ~ z) + lf(z ~ 1 + (1 | gr(id)))) Note that use can specify any external function in the linear predictor
part of the formula sigma_formula_manual = list(nlf(sigma ~ z) + lf(z ~ 1 + splines2::nsk(age) + (1 | gr(id)))) Automatic Prior Assignment: Advanced variance modelling: This approach models heteroscedasticity
using an explicit variance function. The 'bsitar' package provides six
different methods for variance modeling, five of which are implemented in the
'nlme' package. The variance modeling approach uses a helper function
For multivariate models, the Available Methods (short hands in parentheses):
Below are examples demonstrating how to use All variance model specifications are provided through the
Note that the predictor used within these variance functions should not be
identical to Instead, define a separate predictor variable (even if it contains identical values). For example:
and then use For 1. varpower: nlf(sigma ~ vf(param1, param2, predictor), method = 'vp') + lf(param1 + param2 ~ 1) 2. varConstPower: nlf(sigma ~ vf(param1, param2, param3, predictor), method = 'cp') + lf(param1 + param2 + param3 ~ 1) 3. varExp: nlf(sigma ~ vf(param1, param2, predictor), method = 've') + lf(param1 + param2 ~ 1) 4. fitted: nlf(sigma ~ vf(param1, param2, identity()), method = 'fi') + lf(param1 + param2 ~ 1) 5. residual: nlf(sigma ~ vf(param1, param2, identity(), outcome), method = 're') + lf(param1 + param2 ~ 1) 6. mean: nlf(sigma ~ vf(param1, param2, identity()), method = 'me') + lf(param1 + param2 ~ 1) Internal Predictor Transformations: The function applies internal transformations based on the chosen method:
Note that the default Similar to the Like Covariates and Random Effects:
The linear predictor, lf(param1 + param2 ~ 1 + covariate + (1 || gr(id, by = groupid))) Automatic Prior Assignment:
Priors for these linear predictors are assigned automatically for both mean
and group-level random effects. The function uses priors specified via
arguments like To disable this automatic prior assignment, user can add the argument
nlf(..., method = 'vp', prior = 'self') Note: If user disables the automatic prior assignment, it is
advised to first get the required prior structure by calling the For Location-Scale Models: Another important use case for
Here, The first argument The growth parameters Similarly, the number of spline parameters are based on the The other relevant information that is passed to the Note that for the |
sigmax |
Predictor for the distributional parameter |
sigmaid |
A factor variable uniquely identifying the groups (e.g.,
individuals) in the data frame. If |
sigmadf |
Degree of freedom for the spline function used for
|
sigmaknots |
Knots for the spline function used for |
sigmafixed |
Fixed effect formula for the |
sigmarandom |
Random effect formula for the |
sigmaxoffset |
Offset for the |
sigmabstart |
Starting value for the |
sigmacstart |
Starting value for the |
sigmaxfun |
Transformation of |
sigmabound |
Bounds for the |
sigmaxfunxoffset |
Transformation applied to Note that |
dpar_formula |
Formula for the distributional fixed effect parameter,
|
autocor_formula |
Formula to set up the autocorrelation structure of
residuals (default
See Note that for This internal variable is required because the unstructured autocorrelation covariance matrix is indexed by time points treated as discrete, ordered indices. The underlying Stan implementation expects time to be represented as unique positive integers in increasing order (e.g., 1, 2, 3, ...) for each individual so that:
The predictor variable |
family |
Family distribution (default |
custom_family |
Specifies custom families (i.e., response distribution).
Default is |
custom_formula |
Specifies custom formula. Default is |
custom_prior |
Specifies custom prior Default is |
custom_stanvars |
Allows the passing of user-defined variables to be
added to Stan's program blocks (default |
group_arg |
Specify the grouping variable for group-level random
effects. The
All components of For The |
sigma_group_arg |
Specify arguments for modeling distributional-level
random effects for |
univariate_by |
Set up the univariate-by-subgroup model fitting (default
|
multivariate |
Set up the multivariate model fitting (default
|
a_prior_beta |
Prior specification for the fixed effect parameter
|
b_prior_beta |
Prior specification for the fixed effect parameter
|
c_prior_beta |
Prior specification for the fixed effect parameter
|
d_prior_beta |
Prior specification for the fixed effect parameter
|
s_prior_beta |
Prior specification for the fixed effect parameter
Scale Parameter Variants for
Note: Location-scale priors (such as |
a_cov_prior_beta |
Prior specification for covariate effects included
in the fixed effect parameter
|
b_cov_prior_beta |
Prior specification for covariate effects included in
the fixed effect parameter |
c_cov_prior_beta |
Prior specification for covariate effects included
in the fixed effect parameter |
d_cov_prior_beta |
Prior specification for covariate effects included
in the fixed effect parameter |
s_cov_prior_beta |
Prior specification for covariate effects included in
the fixed effect parameter |
a_prior_sd |
Prior specification for the random effect parameter,
|
b_prior_sd |
Prior specification for the random effect parameter,
|
c_prior_sd |
Prior specification for the random effect parameter,
|
d_prior_sd |
Prior specification for the random effect parameter,
|
a_cov_prior_sd |
Prior specification for covariate effects included in
the random effect parameter |
b_cov_prior_sd |
Prior specification for covariate effects included in
the random effect parameter |
c_cov_prior_sd |
Prior specification for covariate effects included in
the random effect parameter |
d_cov_prior_sd |
Prior specification for covariate effects included in
the random effect parameter |
a_prior_sd_str |
Prior specification for the random effect parameter
|
b_prior_sd_str |
Prior specification for the random effect parameter
|
c_prior_sd_str |
Prior specification for the random effect parameter
|
d_prior_sd_str |
Prior specification for the random effect parameter
|
a_cov_prior_sd_str |
Prior specification for covariate effects in the
random effect parameter |
b_cov_prior_sd_str |
Prior specification for covariate effects in the
random effect parameter |
c_cov_prior_sd_str |
Prior specification for covariate effects in the
random effect parameter |
d_cov_prior_sd_str |
Prior specification for covariate effects in the
random effect parameter |
sigma_prior_beta |
Prior specification for the fixed effect
distributional parameter |
sigma_cov_prior_beta |
Prior specification for covariate effects in the
fixed effect distributional parameter |
sigma_prior_sd |
Prior specification for the random effect
distributional parameter |
sigma_cov_prior_sd |
Prior specification for covariate effects in the
random effect distributional parameter |
sigma_prior_sd_str |
Prior specification for the random effect
distributional parameter |
sigma_cov_prior_sd_str |
Prior specification for covariate effects in
the random effect distributional parameter |
rsd_prior_sigma |
Prior specification for the residual standard
deviation parameter |
dpar_prior_sigma |
Prior specification for the fixed effect
distributional parameter |
dpar_cov_prior_sigma |
Prior specification for covariate effects in the
fixed effect distributional parameter |
autocor_prior_acor |
Prior specification for autocorrelation parameters
when fitting models with |
autocor_prior_unstr_acor |
Prior specification for autocorrelation
parameters when fitting models with the unstructured ( |
gr_prior_cor |
Prior specification for correlation parameters of
group-level random effects (default |
gr_prior_cor_str |
Prior specification for correlation parameters of
group-level random effects when fitting a hierarchical model with three or
more levels of hierarchy (default |
sigma_prior_cor |
Prior specification for correlation parameters of
distributional random effects |
sigma_prior_cor_str |
Prior specification for correlation parameters of
distributional random effects |
mvr_prior_rescor |
Prior specification for the residual correlation
matrix in multivariate models (default |
init |
Initial values for the sampler. Options include:
When |
init_r |
Positive real value specifying the range for random initial
values (default |
a_init_beta |
Initial specification for the fixed effect parameter
Note that |
b_init_beta |
Initial specification for the fixed effect parameter
|
c_init_beta |
Initial specification for the fixed effect parameter
|
d_init_beta |
Initial specification for the fixed effect parameter
|
s_init_beta |
Initial specification for the fixed effect parameter
|
a_cov_init_beta |
Initial specification for covariate effects in the
fixed effect parameter
Note that |
b_cov_init_beta |
Initial specification for covariate effects in the
fixed effect parameter |
c_cov_init_beta |
Initial specification for covariate effects in the
fixed effect parameter |
d_cov_init_beta |
Initial specification for covariate effects in the
fixed effect parameter |
s_cov_init_beta |
Initial specification for covariate effects in the
fixed effect parameter |
a_init_sd |
Initial specification for the standard deviation of the
group-level random effect parameter
Note that When fitting |
b_init_sd |
Initial specification for the standard deviation of the
group-level random effect parameter |
c_init_sd |
Initial specification for the standard deviation of the
group-level random effect parameter |
d_init_sd |
Initial specification for the standard deviation of the
group-level random effect parameter |
a_cov_init_sd |
Initial specification for covariate effects in the
group-level random effect parameter
|
b_cov_init_sd |
Initial specification for covariate effects in the
group-level random effect parameter |
c_cov_init_sd |
Initial specification for covariate effects in the
group-level random effect parameter |
d_cov_init_sd |
Initial specification for covariate effects in the
group-level random effect parameter |
sigma_init_beta |
Initial specification for the fixed effect
distributional parameter
|
sigma_cov_init_beta |
Initial specification for covariate effects in the
fixed effect distributional parameter |
sigma_init_sd |
Initial specification for the standard deviation of the
distributional random effect parameter |
sigma_cov_init_sd |
Initial specification for covariate effects in the
distributional random effect parameter |
gr_init_cor |
Initial specification for the correlation parameters of
group-level random effects (default
Note that when |
sigma_init_cor |
Initial specification for the correlation parameters of
distributional random effects
|
rsd_init_sigma |
Initial specification for the residual standard
deviation parameter
If |
dpar_init_sigma |
Initial specification for the distributional parameter
|
dpar_cov_init_sigma |
Initial specification for covariate effects in the
distributional parameter |
autocor_init_acor |
Initial specification for autocorrelation parameters
(see |
autocor_init_unstr_acor |
Initial specification for unstructured
residual autocorrelation parameters (default |
mvr_init_rescor |
Initial specification for the residual correlation
matrix in |
r_init_z |
Initial specification for standardized group-level random
effect parameters (default |
vcov_init_0 |
Logical indicator controlling initial values for variance
(standard deviation) and covariance (correlation) parameters. When
|
jitter_init_beta |
Named list or numeric value used to add a small amount of noise to initial values for population-level (fixed effect) parameters. When specified as a numeric value, The default Jitter is applied proportionally to the specified initial value, not as an
absolute amount. For example, if the initial value is When To use the defaults of |
jitter_init_sd |
Named list or numeric value used to add a small amount
of noise to initial values for the standard deviations of random-effect
parameters. A pragmatic choice that has worked well in early testing is
|
jitter_init_cor |
Named list or numeric value used to add a small amount
of noise to initial values for the correlations of random-effect
parameters. A pragmatic choice that has worked well in early testing is
|
prior_data |
Optional named list (default
|
init_data |
Optional named list (default |
init_custom |
Custom initialization object supplied as a named list. The
list is passed directly to the underlying |
expose_function |
Logical (default |
get_stancode |
Logical (default |
get_standata |
Logical (default |
get_formula |
Logical (default |
get_stanvars |
Logical (default |
get_priors |
Logical or character (default |
get_priors_eval |
Logical (default |
get_init_eval |
Logical (default |
validate_priors |
Logical (default |
set_self_priors |
Optional object (default |
add_self_priors |
Optional object (default |
set_replace_priors |
Optional object (default |
set_same_priors_hierarchy |
Optional object (default |
outliers |
Optional specification (default |
unused |
Optional formula defining variables that are unused in the model but should still be stored in the model frame, for example when they are required for post-processing. |
chains |
Number of Markov chains (default |
iter |
Total number of iterations per chain, including warmup (default
|
warmup |
Positive integer specifying the number of warmup (burn-in)
iterations. Warmup iterations are also used for step-size adaptation and
should not be used for inference. The number of warmup iterations must not
exceed |
thin |
Positive integer specifying the thinning interval. Set |
cores |
Number of cores used to execute chains in parallel. See
Alternatively, |
backend |
Character string specifying the interface used to execute the
Stan model. Available options are |
threads |
Number of threads used for within-chain parallelization.
Unlike An alternative is Alternatively, threads can be specified directly using
|
opencl |
Numeric vector of length two specifying the platform and
device IDs for OpenCL-based GPU support during model fitting. If unsure,
|
normalize |
Logical indicating whether normalization constants should be
included in the Stan code (default |
algorithm |
Character string specifying the estimation method. Available options are:
This argument can be set globally via the |
control |
Named |
empty |
Logical. If |
rename |
For internal use only. |
pathfinder_args |
Named |
pathfinder_init |
Logical (default |
data2 |
A named |
data_custom |
A |
genquant_xyadj |
A logical (default |
sample_prior |
A character string indicating whether to draw samples
from the priors in addition to the posterior draws. Options are |
save_pars |
An object generated by |
drop_unused_levels |
A logical value indicating whether unused factor
levels in the data should be dropped. The default is |
stan_model_args |
A |
refresh |
An integer specifying the frequency of printing every nth
iteration. By default, |
silent |
A verbosity level between |
seed |
An integer or |
save_model |
A character string or |
fit |
An instance of class |
file |
Either |
file_compress |
Logical or a character string, specifying one of the
compression algorithms supported by |
file_refit |
Modifies when the fit stored via the
If user believe a false positive occurred, the
|
future |
Logical; If |
sum_zero |
Currently ignored. Placeholder for future development. |
global_args |
Currently ignored. Placeholder for future development. |
parameterization |
A character string specifying the type of
parameterization to use for drawing group-level random effects. Options
are: The NCP is generally recommended when the likelihood is weak (e.g., few
observations per individual) and is the default approach (and only option)
in The CP parameterization is typically more efficient when a relatively large
number of observations are available across individuals. We consider a
'relatively large number' as at least 10 repeated measurements per
individual. If there are fewer than 10, NCP is used automatically. This
behavior applies only when Note that since |
verbose |
An optional argument (logical, default |
... |
Further arguments passed to These internal arguments are typically not used in regular model fitting but can be relevant for certain testing scenarios or advanced customization. Users are generally not expected to interact with these unless working on debugging or testing specific features of the model fitting process. |
The SITAR is a shape-invariant nonlinear mixed-effects growth
curve model that fits a population average (i.e., mean) curve to the data and
aligns each individual's growth trajectory to the underlying population curve
via a set of (typically) three random effects: size, timing,
and intensity. Additionally, a slope parameter can be included as a
random effect to estimate the variability in adult growth rate (see
sitar::sitar() for details).
The concept of a shape-invariant model (SIM) was first introduced by Lindstrom (1995), and later used by Beath (2007) to model infant growth data (birth to 2 years). The current version of the SITAR model was developed by Cole et al. (2010) and has been extensively used for modeling growth data (see Nembidzane et al. 2020 and Sandhu 2020).
The frequentist version of the SITAR model can be fit using the
already available R package, sitar (Cole 2022). The
framework of the Bayesian implementation of the SITAR model in the
bsitar package is similar to the sitar package, with the main
difference being that sitar uses splines::ns() to construct the
B-splines based natural cubic spline design matrix, whereas bsitar
implements a different strategy to create natural cubic splines. The
bsitar offers three different types of splines: nsp, nsk,
and rcs. Both nsp and nsk use the B-splines basis to
generate the natural cubic spline design matrix as implemented in
splines2::nsp() and splines2::nsk(), whereas rcs is based on the
truncated power basis approach (see
Harrell and others (2001) and
Harrell Jr. (2022) for details) to construct the spline
design matrix. While all approaches produce the same growth curves, the
model-estimated spline coefficients differ from each other.
A key challenge in spline based regression methods is to select the number
and location of knots. The standard approach is to place knots by a regular
sequence of quantiles between the outer boundaries. A regression curve can
easily be fitted to the sample using a relatively high number of knots. The
problem is then over fitting, where a regression model has a good fit to the
given sample but does not generalize well to other samples. A low knot count
is thus preferred. However, the standard knot selection process can lead to
under performance in the sparser regions of the predictor variable,
especially when using a low number of knots. It can also lead to over fitting
in the denser regions. The sitar package offers an option (see
knots_selection argument) to use a search algorithm that implements a
backward method for knot selection that shows reduced prediction error and
a lower information criterion (IC) or cross-validation scores compared to the
standard knot selection process in simulation experiments. See
(Arnes et al. 2023) for details. The approach suggested by
(Arnes et al. 2023) is implemented via option method = 'bs'
when setting up the knots_selection argument. We strongly recommend
that users carefully consider the placement of knots—whether using the
approach suggested here or another method of their choice—to ensure optimal
model performance.
Like sitar, (Cole et al. 2010), the bsitar package
fits the SITAR model with (usually) three random effects: size
(parameter a), timing (parameter b), and intensity (parameter
c). Additionally, there is a slope parameter (parameter d) that
models the variability in the adult slope of the growth curve (see
sitar::sitar() for details).
Note that the sitar package (Cole et al. 2010) enforces the
inclusion of d parameter as a random effect only, excluding it from
the fixed structure of the model. However, the bsitar package allows
inclusion of the d parameter in both the fixed and/or random effects
structures of the SITAR model.
For the three-parameter version of the SITAR model (default), the
fixed effects structure (i.e., population average trajectory) is specified as
fixed = 'a+b+c', and the random effects structure, capturing the
deviation of individual trajectories from the population average curve, is
specified as random = 'a+b+c'.
The bsitar package offers flexibility in model specification. For example:
A fixed-effect version of the SITAR model can be fit by
setting random = ''.
The fixed-effect structure can include a
subset of parameters,
such as size and timing (fixed = 'a+b') or size and intensity
(fixed = 'a+c').
For a four-parameter version of the SITAR model, parameter
d is included in the fixed and/or random arguments.
The bsitar package internally depends on the brms package (see Bürkner 2022; Bürkner 2021), which fits a wide range of hierarchical linear and nonlinear regression models, including multivariate models. The brms package itself depends on Stan for full Bayesian inference (see Stan Development Team 2023; Gelman et al. 2015). Like brms, the bsitar package allows flexible prior specifications based on user's knowledge of growth processes (e.g., timing and intensity of growth spurts).
The brms package uses a combination of normal and
student_t distributions for regression coefficients, group-level
random effects, and the distributional parameter (sigma), while
rstanarm uses normal distributions for regression coefficients
and group-level random effects, but sets exponential for the
distributional parameter (sigma). By default, bsitar uses
normal distributions for all parameters, including regression
coefficients, standard deviations of group-level random effects, and the
distributional parameter. Additionally, bsitar provides flexibility in
choosing scale parameters for location-scale distributions (such as
normal and student_t).
The bsitar package also allows three types of model specifications:
'univariate', 'univariate_by', and 'multivariate':
'univariate' fits a single model to an outcome variable.
'univariate_by' fits two or more sub-models to an outcome
defined by a factor variable (e.g., sex).
'multivariate' fits a joint model to multiple outcomes with
shared random effects.
The bsitar package provides full flexibility in specifying predictors,
degrees of freedom for design matrices, priors, and initial values. It also
supports unquoted argument evaluation for convenience (e.g.,
univariate_by = sex is treated equivalently to
univariate_by = 'sex').
Priors
The following guidelines apply to all prior specifications. For full details,
see brms::prior():
Allowed distributions: normal, student_t,
cauchy, lognormal, uniform, exponential,
gamma, and inv_gamma.
Bounds: Upper and lower bounds can be defined for any
distribution using lb and ub (default: NA). Strictly
positive distributions (exponential, gamma, inv_gamma)
automatically set their lower bound to zero.
Scaling: Location-scale distributions (normal,
student_t, cauchy, lognormal) support an
autoscale argument (default: FALSE) that multiplies the scale
parameter by a numeric value. While brms typically restricts scaling
factors to 1.0 or 2.5, bsitar accepts any real number (e.g.,
autoscale = 5.0).
Transformations: For location-scale distributions,
fxl (function location) and fxs (function scale) apply
transformations to their respective parameters. For example,
normal(2, 5, fxl = 'log', fxs = 'sqrt') translates to
normal(log(2), sqrt(5)). Alternatively, fxls (function
location scale) simultaneously transforms both parameters when they are
interdependent (e.g., log-normal transformations). This can be passed as a
character string or list of functions.
Uniform distributions: The addrange argument
symmetrically widens the prior range. For instance, uniform(a, b,
addrange = 5) adjusts the bounds to uniform(a - 5, b + 5).
Exponential distributions: The specified rate parameter is
internally inverted. Therefore, exponential(10.0) is evaluated as
exponential(1.0 / 10.0) = exponential(0.1).
Syntax defaults: Missing arguments automatically default to
their standard positional values. For example, normal(location = 5,
scale = 1) simplifies to normal(5, 1).
Multivariate/Univariate-by models: Priors can be applied
globally across all submodels (e.g., normal(5, 1)) or specified
individually via a list (e.g., list(normal(5, 1), normal(10, 5))).
Data-driven parameters: For location-scale distributions,
parameters can be derived directly from the response variable. The location
can be set to the mean (ymean) or median (ymedian), and the
scale to the standard deviation (ysd) or median absolute deviation
(ymad). Alternatively, coefficients from a simple linear model
(lm(y ~ age)) can be used. Examples include normal(ymean,
ysd) or normal(lm, ysd).
Note: Data-driven options (ymean, ymedian, ysd,
ymad, and lm) are exclusive to the fixed effect parameter
a and cannot be used for b, c, or d.
Initials Initial values for the fixed effect parameter a
(default: 'random'). Available options include:
'0': Initializes the parameter to zero.
'random': Initializes with random values within a
specified range.
'prior': Draws initial values directly from the prior
distribution.
'ymean': Initializes using the mean of the response
variable.
'ymedian': Initializes using the median of the
response variable.
'lm': Initializes using coefficients from a simple
linear model fitted to the data.
Note: Data-driven initial values ('ymean', 'ymedian',
and 'lm') are exclusive to the fixed effect parameter a. For
univariate_by and multivariate models, initial values can be
applied globally across all submodels (e.g., a_init_beta = '0') or
specified individually via a list (e.g., a_init_beta = list('0',
'lm')).
An object of class brmsfit, bsitar, which contains the
posterior draws, model coefficients, and other useful information related
to the model fitting. This object includes details such as the fitted
model, the data used, prior distributions, and any other relevant outputs
from the Stan model fitting process. The resulting object can be used for
further analysis, diagnostics, and post-processing, including model summary
statistics, predictions, and visualizations.
The package is under continuous development, and new models, post-processing features, and improvements are being actively worked on. Keep an eye on future releases for additional functionality and updates to enhance model fitting, diagnostics, and analysis capabilities.
Satpal Sandhu [email protected]
Arnes JI, Hapfelmeier A, Horsch A, Braaten T (2023).
“Greedy knot selection algorithm for restricted cubic spline regression.”
Frontiers in Epidemiology, Volume 3 - 2023.
ISSN 2674-1199.
doi:10.3389/fepid.2023.1283705.
https://www.frontiersin.org/journals/epidemiology/articles/10.3389/fepid.2023.1283705.
Beath KJ (2007).
“Infant growth modelling using a shape invariant model with random effects.”
Statistics in Medicine, 26(12), 2547–2564.
doi:10.1002/sim.2718.
Type: Journal article.
Bürkner P (2021).
“Bayesian Item Response Modeling in R with brms and Stan.”
Journal of Statistical Software, 100(5), 1–54.
doi:10.18637/jss.v100.i05.
Bürkner P (2022).
brms: Bayesian Regression Models using Stan.
R package version 2.18.0, https://CRAN.R-project.org/package=brms.
Cole T (2022).
sitar: Super Imposition by Translation and Rotation Growth Curve Analysis.
R package version 1.3.0, https://CRAN.R-project.org/package=sitar.
Cole TJ, Donaldson MDC, Ben-Shlomo Y (2010).
“SITAR—a useful instrument for growth curve analysis.”
International Journal of Epidemiology, 39(6), 1558–1566.
ISSN 0300-5771.
doi:10.1093/ije/dyq115.
tex.eprint: https://academic.oup.com/ije/article-pdf/39/6/1558/18480886/dyq115.pdf.
Gelman A, Lee D, Guo J (2015).
“Stan: A Probabilistic Programming Language for Bayesian Inference and Optimization.”
Journal of Educational and Behavioral Statistics, 40(5), 530-543.
doi:10.3102/1076998615606113.
Harrell FE, others (2001).
Regression modeling strategies: with applications to linear models, logistic regression, and survival analysis, volume 608.
Springer.
Harrell Jr. FE (2022).
Hmisc: Harrell Miscellaneous.
R package version 4.7-2, https://hbiostat.org/R/Hmisc/.
Lindstrom MJ (1995).
“Self-modelling with random shift and scale parameters and a free-knot spline shape function.”
Statistics in Medicine, 14(18), 2009-2021.
doi:10.1002/sim.4780141807.
https://pubmed.ncbi.nlm.nih.gov/8677401/.
Nembidzane C, Lesaoana M, Monyeki KD, Boateng A, Makgae PJ (2020).
“Using the SITAR Method to Estimate Age at Peak Height Velocity of Children in Rural South Africa: Ellisras Longitudinal Study.”
Children, 7(3), 17.
ISSN 2227-9067.
doi:10.3390/children7030017.
Sandhu SS (2020).
Analysis of longitudinal jaw growth data to study sex differences in timing and intensity of the adolescent growth spurt for normal growth and skeletal discrepancies.
Thesis, University of Bristol.
Stan Development Team (2023).
Stan Reference Manual version 2.31.
https://mc-stan.org/docs/reference-manual/.
brms::brm() brms::brmsformula() brms::prior()
# Below, we fit a SITAR model to a subset of the Berkley height data, # specifically the data for 70 girls between the ages of 8 and 18. # This subset is used as an example in the vignette for the 'sitar' package. # # The original Berkley height data contains repeated growth measurements for # 66 boys and 70 girls (ages 0-21). For this example, we use a subset of the # data for 70 girls aged 8 to 18 years. # # A detailed description of Berkley height dataset is provided in the 'sitar' # package documentation (help file: ?sitar::berkeley). Details on the subset # of the data used in the 'bsitar' package can be found in the vignette # ('Fitting_models_with_SITAR', package = 'sitar'). # Load the 'berkeley_exdata' that has been pre-saved berkeley_exdata <- getNsObject(berkeley_exdata) # Fit frequentist SITAR model with df = 3 using the sitar package model_ml <- sitar::sitar(x = age, y = height, id = id, df = 3, data = berkeley_exdata, xoffset = 'mean', fixed = 'a+b+c', random = 'a+b+c', a.formula = ~1, b.formula = ~1, c.formula = ~1 ) # Fit Bayesian SITAR model # To avoid time-consuming model estimation, the Bayesian SITAR model fit has # been saved as an example fit ('berkeley_exfit'). This model was fit using # 2 chains (2000 iterations per chain) with thinning set to 6 for memory # efficiency. Users are encouraged to refit the model using default settings # (4 chains, 2000 iterations per chain, thin = 1) as suggested by the Stan # team. Note that with thinning set to 6 (thin = 6), only one sixth of total # draws will be saved and hence the effective sample size is expected to be # small. # Check if the pre-saved model 'berkeley_exfit' exists # berkeley_exfit <- bsitar:::berkeley_exfit berkeley_exfit <- getNsObject(berkeley_exfit) if(exists('berkeley_exfit')) { model <- berkeley_exfit } else { # Fit model with default priors # Refer to the documentation for prior on each parameter model <- bsitar(x = age, y = height, id = id, df = 3, data = berkeley_exdata, xoffset = 'mean', fixed = 'a+b+c', random = 'a+b+c', a_formula = ~1, b_formula = ~1, c_formula = ~1, threads = NULL, chains = 2, cores = 2, iter = 1000, thin = 6) } # Model summary summary(model) # Model summary for the frequentist SITAR model fite using 'sitar' package print(model_ml) # Evaluate model fit using the posterior predictive checks (PPC) plot. # plot_ppc() is a wrapper for the pp_check() from 'brms' package. plot_ppc(model, ndraws = NULL) # Plot distance and velocity curves using plot_conditional_effects. # plot_conditional_effects() is a wrapper for conditional_effects() # from 'brms' package. However, unlike conditional_effects() which plots # only the distance curve, plot_conditional_effects() plot velocity curve # in addition to the distance curve # Distance curve plot_conditional_effects(model, deriv = 0) # Velocity curve plot_conditional_effects(model, deriv = 1) # A custom function plot_curves() can also be used to plot distance and # velocity curves along with the growth parameter such as APGV # plot_curves() is similar to plot() from the sitar package. plot_curves(model, apv = TRUE) # Compare plots with the frequentist SITAR model plot(model_ml)# Below, we fit a SITAR model to a subset of the Berkley height data, # specifically the data for 70 girls between the ages of 8 and 18. # This subset is used as an example in the vignette for the 'sitar' package. # # The original Berkley height data contains repeated growth measurements for # 66 boys and 70 girls (ages 0-21). For this example, we use a subset of the # data for 70 girls aged 8 to 18 years. # # A detailed description of Berkley height dataset is provided in the 'sitar' # package documentation (help file: ?sitar::berkeley). Details on the subset # of the data used in the 'bsitar' package can be found in the vignette # ('Fitting_models_with_SITAR', package = 'sitar'). # Load the 'berkeley_exdata' that has been pre-saved berkeley_exdata <- getNsObject(berkeley_exdata) # Fit frequentist SITAR model with df = 3 using the sitar package model_ml <- sitar::sitar(x = age, y = height, id = id, df = 3, data = berkeley_exdata, xoffset = 'mean', fixed = 'a+b+c', random = 'a+b+c', a.formula = ~1, b.formula = ~1, c.formula = ~1 ) # Fit Bayesian SITAR model # To avoid time-consuming model estimation, the Bayesian SITAR model fit has # been saved as an example fit ('berkeley_exfit'). This model was fit using # 2 chains (2000 iterations per chain) with thinning set to 6 for memory # efficiency. Users are encouraged to refit the model using default settings # (4 chains, 2000 iterations per chain, thin = 1) as suggested by the Stan # team. Note that with thinning set to 6 (thin = 6), only one sixth of total # draws will be saved and hence the effective sample size is expected to be # small. # Check if the pre-saved model 'berkeley_exfit' exists # berkeley_exfit <- bsitar:::berkeley_exfit berkeley_exfit <- getNsObject(berkeley_exfit) if(exists('berkeley_exfit')) { model <- berkeley_exfit } else { # Fit model with default priors # Refer to the documentation for prior on each parameter model <- bsitar(x = age, y = height, id = id, df = 3, data = berkeley_exdata, xoffset = 'mean', fixed = 'a+b+c', random = 'a+b+c', a_formula = ~1, b_formula = ~1, c_formula = ~1, threads = NULL, chains = 2, cores = 2, iter = 1000, thin = 6) } # Model summary summary(model) # Model summary for the frequentist SITAR model fite using 'sitar' package print(model_ml) # Evaluate model fit using the posterior predictive checks (PPC) plot. # plot_ppc() is a wrapper for the pp_check() from 'brms' package. plot_ppc(model, ndraws = NULL) # Plot distance and velocity curves using plot_conditional_effects. # plot_conditional_effects() is a wrapper for conditional_effects() # from 'brms' package. However, unlike conditional_effects() which plots # only the distance curve, plot_conditional_effects() plot velocity curve # in addition to the distance curve # Distance curve plot_conditional_effects(model, deriv = 0) # Velocity curve plot_conditional_effects(model, deriv = 1) # A custom function plot_curves() can also be used to plot distance and # velocity curves along with the growth parameter such as APGV # plot_curves() is similar to plot() from the sitar package. plot_curves(model, apv = TRUE) # Compare plots with the frequentist SITAR model plot(model_ml)
A wrapper around brms::loo_compare() to compare models using fit criteria.
## S3 method for class 'bgmfit' compare_models( model, ..., criterion = "loo", model_names = NULL, check_criterion = TRUE, add_criterion_args = list(), expose_function = FALSE, verbose = FALSE ) compare_models(model, ...) ## S3 method for class 'list' compare_models(model, ...) compare_model(model, ...)## S3 method for class 'bgmfit' compare_models( model, ..., criterion = "loo", model_names = NULL, check_criterion = TRUE, add_criterion_args = list(), expose_function = FALSE, verbose = FALSE ) compare_models(model, ...) ## S3 method for class 'list' compare_models(model, ...) compare_model(model, ...)
model |
An |
... |
More |
criterion |
The name of the criterion to be extracted
from |
model_names |
If |
check_criterion |
A logical (default |
add_criterion_args |
A named list to set up the arguments for the
|
expose_function |
A logical value or
Note: In the |
verbose |
A logical (default |
All models should have pre computed fit criterion See add_model_criterion()
for more help.
An object of class "compare.loo".
Satpal Sandhu [email protected]
# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # For illustration purposes, we compare model with itself # In the example below, compare_models() should indicate no difference # between model_1 and model_2 as both these models are exactly identical # Add model fit criteria (e.g., WAIC). model_1 <- add_model_criterion(model, criterion = c("waic")) model_2 <- add_model_criterion(model, criterion = c("waic")) # compare models model_1 and model_2 compare_model_12 <- compare_models(model_1, model_2, criterion = c("waic"))# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # For illustration purposes, we compare model with itself # In the example below, compare_models() should indicate no difference # between model_1 and model_2 as both these models are exactly identical # Add model fit criteria (e.g., WAIC). model_1 <- add_model_criterion(model, criterion = c("waic")) model_2 <- add_model_criterion(model, criterion = c("waic")) # compare models model_1 and model_2 compare_model_12 <- compare_models(model_1, model_2, criterion = c("waic"))
The expose_model_functions() function is a wrapper
around rstan::expose_stan_functions() that exposes user-defined Stan
function(s). These functions are necessary for post-processing the
posterior draws.
## S3 method for class 'bgmfit' expose_model_functions( model, scode = NULL, expose = TRUE, select_model = NULL, returnobj = TRUE, vectorize = FALSE, verbose = FALSE, sigmafun = FALSE, backend = NULL, path = NULL, envir = NULL, ... ) expose_model_functions(model, ...)## S3 method for class 'bgmfit' expose_model_functions( model, scode = NULL, expose = TRUE, select_model = NULL, returnobj = TRUE, vectorize = FALSE, verbose = FALSE, sigmafun = FALSE, backend = NULL, path = NULL, envir = NULL, ... ) expose_model_functions(model, ...)
model |
An object of class |
scode |
A character string containing the user-defined Stan function(s)
in |
expose |
A logical (default |
select_model |
A character string (default |
returnobj |
A logical (default |
vectorize |
A logical (default |
verbose |
A logical argument (default |
sigmafun |
A logical (default |
backend |
A character string (default |
path |
A character string to set up the path of installed
|
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the
|
An object of class bgmfit if returnobj = TRUE;
otherwise, it returns NULL invisibly.
Satpal Sandhu [email protected]
rstan::expose_stan_functions()
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # To save time, argument expose is set as FALSE, which runs a dummy test # and avoids model compilation that often takes time. expose_model_functions(model, expose = FALSE)# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # To save time, argument expose is set as FALSE, which runs a dummy test # and avoids model compilation that often takes time. expose_model_functions(model, expose = FALSE)
The fitted_draws() function is a wrapper around the
brms::fitted.brmsfit() function, which allows users to obtain fitted
values (and their summaries) from the posterior draws. For more details,
refer to the documentation for brms::fitted.brmsfit(). An alternative
approach is to get_predictions() function which is based on the
marginaleffects.
## S3 method for class 'bgmfit' fitted_draws( model, newdata = NULL, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, re_formula = NA, allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, numeric_cov_at = NULL, levels_id = NULL, avg_reffects = NULL, aux_variables = NULL, grid_add = NULL, ipts = NULL, deriv = 0, model_deriv = TRUE, summary = TRUE, robust = FALSE, transform_draws = NULL, scale = c("response", "linear"), probs = c(0.025, 0.975), xrange = NULL, xrange_search = NULL, parms_eval = FALSE, parms_method = "getPeak", idata_method = NULL, verbose = FALSE, fullframe = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) fitted_draws(model, ...)## S3 method for class 'bgmfit' fitted_draws( model, newdata = NULL, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, re_formula = NA, allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, numeric_cov_at = NULL, levels_id = NULL, avg_reffects = NULL, aux_variables = NULL, grid_add = NULL, ipts = NULL, deriv = 0, model_deriv = TRUE, summary = TRUE, robust = FALSE, transform_draws = NULL, scale = c("response", "linear"), probs = c(0.025, 0.975), xrange = NULL, xrange_search = NULL, parms_eval = FALSE, parms_method = "getPeak", idata_method = NULL, verbose = FALSE, fullframe = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) fitted_draws(model, ...)
model |
An object of class |
newdata |
An optional data frame for estimation. If |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
incl_autocor |
A flag indicating if correlation structures originally
specified via |
numeric_cov_at |
An optional (named list) argument to specify the value
of continuous covariate(s). The default |
levels_id |
An optional argument to specify the |
avg_reffects |
An optional argument (default |
aux_variables |
An optional argument to specify the variable(s) that can
be passed to the |
grid_add |
An optional argument to specify the variable(s) that can be
passed to the |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
deriv |
An integer indicating whether to estimate the distance curve or
its derivative (velocity curve). The default |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
summary |
A logical value indicating whether only the estimate should be
computed ( |
robust |
A logical value to specify the summary options. If |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
scale |
Either |
probs |
The percentiles to be computed by the |
xrange |
An integer to set the predictor range (e.g., age) when
executing the interpolation via |
xrange_search |
A vector of length two or a character string
|
parms_eval |
A logical value to specify whether or not to compute growth parameters on the fly. This is for internal use only and is mainly needed for compatibility across internal functions. |
parms_method |
A character string specifying the method used when
evaluating |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
verbose |
A logical argument (default |
fullframe |
A logical value indicating whether to return a
|
dummy_to_factor |
A named list (default
|
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
difx |
A character string (default |
idvar |
A character string (default |
itransform |
A character string (default |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The fitted_draws() function computes the fitted values from
the posterior draws. While the brms::fitted.brmsfit() function from the
brms package can be used to obtain fitted (distance) values when the
outcome (e.g., height) is untransformed, it returns fitted values on the
log or square root scale if the outcome is transformed. In contrast,
fitted_draws() returns fitted values on the original scale.
Additionally, fitted_draws() computes the first derivative
(velocity) on the original scale, after applying the necessary
back-transformation. Apart from these differences, both
functions—brms::fitted.brmsfit() and fitted_draws()—operate in the same
manner, allowing users to specify all options available in
brms::fitted.brmsfit().
An array of predicted mean response values when summarise =
FALSE, or a data.frame when summarise = TRUE. For further
details, refer to brms::fitted.brmsfit.
Satpal Sandhu [email protected]
# Fit Bayesian SITAR model # To avoid time-consuming model estimation, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check if the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance curve fitted_draws(model, deriv = 0, re_formula = NA) # Individual-specific distance curves fitted_draws(model, deriv = 0, re_formula = NULL) # Population average velocity curve fitted_draws(model, deriv = 1, re_formula = NA) # Individual-specific velocity curves fitted_draws(model, deriv = 1, re_formula = NULL)# Fit Bayesian SITAR model # To avoid time-consuming model estimation, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check if the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance curve fitted_draws(model, deriv = 0, re_formula = NA) # Individual-specific distance curves fitted_draws(model, deriv = 0, re_formula = NULL) # Population average velocity curve fitted_draws(model, deriv = 1, re_formula = NA) # Individual-specific velocity curves fitted_draws(model, deriv = 1, re_formula = NULL)
The get_comparisons() function estimates and
compares growth curves such as distance and velocity. This function is a
wrapper around marginaleffects::comparisons() and
marginaleffects::avg_comparisons(). The marginaleffects::comparisons()
function computes unit-level (conditional) estimates, whereas
marginaleffects::avg_comparisons() returns average (marginal) estimates.
A detailed explanation is available here.
Note that the marginaleffects package is highly flexible, and users
are expected to have a strong understanding of its workings. Additionally,
since the marginaleffects package is evolving rapidly, results from
the current implementation should be considered experimental.
## S3 method for class 'bgmfit' get_comparisons( model, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, datagrid = NULL, re_formula = NA, newdata2 = NULL, allow_new_levels = FALSE, sample_new_levels = "gaussian", xrange = 1, digits = 2, numeric_cov_at = NULL, aux_variables = NULL, grid_add = NULL, levels_id = NULL, avg_reffects = NULL, idata_method = NULL, ipts = NULL, seed = 123, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, usedtplyr = FALSE, usecollapse = TRUE, cores = NULL, fullframe = FALSE, average = FALSE, plot = FALSE, mapping_facet = NULL, showlegends = NULL, variables = NULL, condition = NULL, deriv = 0, model_deriv = TRUE, method = "custom", method_call = NULL, marginals = NULL, pdrawso = FALSE, pdrawsp = FALSE, pdrawsh = FALSE, comparison = NULL, type = NULL, by = FALSE, conf_level = 0.95, transform = NULL, transform_draws = NULL, cross = FALSE, wts = NULL, hypothesis = NULL, equivalence = NULL, eps = NULL, constrats_by = NULL, constrats_at = NULL, reformat = NULL, estimate_center = NULL, estimate_interval = NULL, dummy_to_factor = NULL, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) get_comparisons(model, ...) marginal_comparison(model, ...) marginal_comparisons(model, ...)## S3 method for class 'bgmfit' get_comparisons( model, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, datagrid = NULL, re_formula = NA, newdata2 = NULL, allow_new_levels = FALSE, sample_new_levels = "gaussian", xrange = 1, digits = 2, numeric_cov_at = NULL, aux_variables = NULL, grid_add = NULL, levels_id = NULL, avg_reffects = NULL, idata_method = NULL, ipts = NULL, seed = 123, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, usedtplyr = FALSE, usecollapse = TRUE, cores = NULL, fullframe = FALSE, average = FALSE, plot = FALSE, mapping_facet = NULL, showlegends = NULL, variables = NULL, condition = NULL, deriv = 0, model_deriv = TRUE, method = "custom", method_call = NULL, marginals = NULL, pdrawso = FALSE, pdrawsp = FALSE, pdrawsh = FALSE, comparison = NULL, type = NULL, by = FALSE, conf_level = 0.95, transform = NULL, transform_draws = NULL, cross = FALSE, wts = NULL, hypothesis = NULL, equivalence = NULL, eps = NULL, constrats_by = NULL, constrats_at = NULL, reformat = NULL, estimate_center = NULL, estimate_interval = NULL, dummy_to_factor = NULL, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) get_comparisons(model, ...) marginal_comparison(model, ...) marginal_comparisons(model, ...)
model |
An object of class |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
newdata |
An optional data frame for estimation. If |
datagrid |
A data frame or named list for setting up a custom grid of
predictor values to evaluate the quantities of interest. If |
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
newdata2 |
A named |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
xrange |
An integer to set the predictor range (e.g., age) when
executing the interpolation via |
digits |
An integer (default |
numeric_cov_at |
An optional (named list) argument to specify the value
of continuous covariate(s). The default |
aux_variables |
An optional argument to specify the variable(s) that can
be passed to the |
grid_add |
An optional argument to specify the variable(s) that can be
passed to the |
levels_id |
An optional argument to specify the |
avg_reffects |
An optional argument (default |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
seed |
An integer (default |
future |
A logical value (default |
future_session |
A character string or a named list specifying the
parallel plan when
|
future_splits |
A list, numeric vector, or logical value (default
Input options:
Note: On Windows with |
future_method |
A character string (default
|
future_re_expose |
A logical value (default
|
usedtplyr |
A logical (default |
usecollapse |
A logical (default |
cores |
An integer specifying the number of cores for parallel execution.
|
fullframe |
A logical value indicating whether to return a
|
average |
A logical indicating whether to call
|
plot |
A logical indicating whether to plot the comparisons using
|
mapping_facet |
A named list that can be used to pass the aesthetic
|
showlegends |
A logical value to specify whether to show legends
( |
variables |
A named list specifying the level 1 predictor, such as
|
condition |
Conditional slopes
|
deriv |
A numeric value indicating whether to estimate parameters based on the differentiation of the distance curve or the model's first derivative. |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
method |
A character string specifying whether to compute comparisons
using the marginaleffects machinery ( |
method_call |
A character string specifying whether to compute
comparisons using |
marginals |
A |
pdrawso |
A character string (default
When |
pdrawsp |
A character string (default
When |
pdrawsh |
A character string (default
The summary of posterior draws for parameters is the default returned
object. The |
comparison |
A character string specifying the comparison type for
growth parameter estimation. Options are |
type |
string indicates the type (scale) of the predictions used to
compute contrasts or slopes. This can differ based on the model
type, but will typically be a string such as: "response", "link", "probs",
or "zero". When an unsupported string is entered, the model-specific list of
acceptable values is returned in an error message. When |
by |
Aggregate unit-level estimates (aka, marginalize, average over). Valid inputs:
|
conf_level |
numeric value between 0 and 1. Confidence level to use to build a confidence interval. |
transform |
string or function. Transformation applied to unit-level estimates and confidence intervals just before the function returns results. Functions must accept a vector and return a vector of the same length. Support string shortcuts: "exp", "ln" |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
cross |
|
wts |
logical, string or numeric: weights to use when computing average predictions, contrasts or slopes. These weights only affect the averaging in
|
hypothesis |
specify a hypothesis test or custom contrast using a number , formula, string equation, vector, matrix, or function.
|
equivalence |
Numeric vector of length 2: bounds used for the two-one-sided test (TOST) of equivalence, and for the non-inferiority and non-superiority tests. For bayesian models, this report the proportion of posterior draws in the interval and the ROPE. See Details section below. |
eps |
NULL or numeric value which determines the step size to use when
calculating numerical derivatives: (f(x+eps)-f(x))/eps. When |
constrats_by |
A character vector (default |
constrats_at |
A character vector (default |
reformat |
A logical (default |
estimate_center |
A character string (default |
estimate_interval |
A character string (default |
dummy_to_factor |
A named list (default
|
verbose |
A logical argument (default |
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
difx |
A character string (default |
idvar |
A character string (default |
itransform |
A character string (default |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
A data frame with estimates and confidence intervals for the
specified parameters, or a list when method = 'custom' and
hypothesis is not NULL.
Satpal Sandhu [email protected]
There are no references for Rd macro \insertAllCites on this help page.
marginaleffects::comparisons(),
marginaleffects::avg_comparisons(),
marginaleffects::plot_comparisons()
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Note: Since no covariates are included, the 'get_comparisons' # function is being shown here as a dummy example. In practice, comparisons # may not make sense without relevant covariates. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Call get_comparisons to demonstrate the function # Note: since model has no covariate, the example is for illustration purposes # Comparisons at 1 SD of age get_comparisons(model, variables = list(age = "sd"), re_formula = NA, draw_ids = 1:2) # Comparisons between individuals get_comparisons(model, variables = list(id = "sequential"), re_formula = NULL, draw_ids = 1:2)# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Note: Since no covariates are included, the 'get_comparisons' # function is being shown here as a dummy example. In practice, comparisons # may not make sense without relevant covariates. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Call get_comparisons to demonstrate the function # Note: since model has no covariate, the example is for illustration purposes # Comparisons at 1 SD of age get_comparisons(model, variables = list(age = "sd"), re_formula = NA, draw_ids = 1:2) # Comparisons between individuals get_comparisons(model, variables = list(id = "sequential"), re_formula = NULL, draw_ids = 1:2)
The get_growthparameters() function estimates and
compares growth parameters, such as peak growth velocity and the age at
peak growth velocity. This function serves as a wrapper around
marginaleffects::comparisons() and marginaleffects::avg_comparisons().
The marginaleffects::comparisons() function computes unit-level
(conditional) estimates, whereas marginaleffects::avg_comparisons()
returns average (marginal) estimates. A detailed explanation is available
here. Note that for the current use case—
estimating and comparing growth parameters—the arguments variables
and comparison in marginaleffects::comparisons() and
marginaleffects::avg_comparisons() are modified (see below). Furthermore,
comparisons of growth parameters are performed via the hypothesis
argument of both the marginaleffects::comparisons() and
marginaleffects::avg_comparisons() functions. Please note that the
marginaleffects package is highly flexible, and users are expected to
have a strong understanding of its workings. Additionally, since the
marginaleffects package is rapidly evolving, results obtained from
the current implementation should be considered experimental.
## S3 method for class 'bgmfit' get_growthparameters( model, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, datagrid = NULL, re_formula = NA, newdata2 = NULL, allow_new_levels = FALSE, sample_new_levels = "gaussian", parameter = NULL, xrange = 1, acg_velocity = 0.1, digits = 2, numeric_cov_at = NULL, aux_variables = NULL, grid_add = NULL, levels_id = NULL, avg_reffects = NULL, idata_method = NULL, ipts = NULL, seed = 123, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, usedtplyr = FALSE, usecollapse = TRUE, parallel = FALSE, cores = NULL, fullframe = FALSE, average = FALSE, plot = FALSE, mapping_facet = NULL, showlegends = NULL, variables = NULL, condition = NULL, deriv = NULL, model_deriv = NULL, method = "custom", marginals = NULL, preparms = NULL, pdraws = FALSE, pdrawso = FALSE, pdrawsp = FALSE, pdrawsh = FALSE, comparison = "difference", type = NULL, by = FALSE, bys = NULL, conf_level = 0.95, transform = NULL, transform_draws = NULL, cross = FALSE, wts = NULL, hypothesis = NULL, equivalence = NULL, equivalence_test = NULL, p_direction = NULL, eps = NULL, constrats_by = FALSE, constrats_at = FALSE, constrats_subset = FALSE, reformat = NULL, estimate_center = NULL, estimate_interval = NULL, dummy_to_factor = NULL, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) get_growthparameters(model, ...) get_gp(model, ...) growthparameters_comparison(model, ...) marginal_growthparameters(model, ...)## S3 method for class 'bgmfit' get_growthparameters( model, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, datagrid = NULL, re_formula = NA, newdata2 = NULL, allow_new_levels = FALSE, sample_new_levels = "gaussian", parameter = NULL, xrange = 1, acg_velocity = 0.1, digits = 2, numeric_cov_at = NULL, aux_variables = NULL, grid_add = NULL, levels_id = NULL, avg_reffects = NULL, idata_method = NULL, ipts = NULL, seed = 123, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, usedtplyr = FALSE, usecollapse = TRUE, parallel = FALSE, cores = NULL, fullframe = FALSE, average = FALSE, plot = FALSE, mapping_facet = NULL, showlegends = NULL, variables = NULL, condition = NULL, deriv = NULL, model_deriv = NULL, method = "custom", marginals = NULL, preparms = NULL, pdraws = FALSE, pdrawso = FALSE, pdrawsp = FALSE, pdrawsh = FALSE, comparison = "difference", type = NULL, by = FALSE, bys = NULL, conf_level = 0.95, transform = NULL, transform_draws = NULL, cross = FALSE, wts = NULL, hypothesis = NULL, equivalence = NULL, equivalence_test = NULL, p_direction = NULL, eps = NULL, constrats_by = FALSE, constrats_at = FALSE, constrats_subset = FALSE, reformat = NULL, estimate_center = NULL, estimate_interval = NULL, dummy_to_factor = NULL, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) get_growthparameters(model, ...) get_gp(model, ...) growthparameters_comparison(model, ...) marginal_growthparameters(model, ...)
model |
An object of class |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
newdata |
An optional data frame for estimation. If |
datagrid |
A grid of user-specified values to be used in the
|
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
newdata2 |
A named |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
parameter |
A single character string or character vector specifying the growth parameter(s) to estimate. Must be one of the following options, ordered by growth curve progression:
Multiple parameters can be requested simultaneously as a vector, e.g.,
|
xrange |
An integer to set the predictor range (e.g., age) when
executing the interpolation via |
acg_velocity |
A real number specifying the percentage of peak growth
velocity to be used as the cessation velocity when estimating the
|
digits |
An integer (default |
numeric_cov_at |
An optional (named list) argument to specify the value
of continuous covariate(s). The default |
aux_variables |
An optional argument to specify the variable(s) that can
be passed to the |
grid_add |
An optional argument to specify the variable(s) that can be
passed to the |
levels_id |
An optional argument to specify the |
avg_reffects |
An optional argument (default |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
seed |
An integer (default |
future |
A logical value (default |
future_session |
A character string or a named list specifying the
parallel plan when
|
future_splits |
A list, numeric vector, or logical value (default
Input options:
Note: On Windows with |
future_method |
A character string (default
|
future_re_expose |
A logical value (default
|
usedtplyr |
A logical (default |
usecollapse |
A logical (default |
parallel |
A logical (default |
cores |
A positive integer (default |
fullframe |
A logical value indicating whether to return a
|
average |
A logical value indicating whether to internally call the
|
plot |
A logical value specifying whether to plot comparisons by calling
the |
mapping_facet |
A named list that can be used to pass the aesthetic
|
showlegends |
A logical value to specify whether to show legends
( |
variables |
A named list specifying the level 1 predictor, such as
|
condition |
Conditional slopes
|
deriv |
A numeric value specifying whether to estimate parameters based
on the differentiation of the distance curve or the model's first
derivative. Please refer to the |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
method |
A character string indicating whether to compute estimates
using the |
marginals |
A |
preparms |
A |
pdraws |
A character string (default
The |
pdrawso |
A character string (default
When |
pdrawsp |
A character string (default
When |
pdrawsh |
A character string (default
The summary of posterior draws for parameters is the default returned
object. The |
comparison |
A character string specifying the comparison type for
growth parameter estimation. Options are |
type |
string indicates the type (scale) of the predictions used to
compute contrasts or slopes. This can differ based on the model
type, but will typically be a string such as: "response", "link", "probs",
or "zero". When an unsupported string is entered, the model-specific list of
acceptable values is returned in an error message. When |
by |
Aggregate unit-level estimates (aka, marginalize, average over). Valid inputs:
|
bys |
A character string (default |
conf_level |
numeric value between 0 and 1. Confidence level to use to build a confidence interval. |
transform |
string or function. Transformation applied to unit-level estimates and confidence intervals just before the function returns results. Functions must accept a vector and return a vector of the same length. Support string shortcuts: "exp", "ln" |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
cross |
|
wts |
logical, string or numeric: weights to use when computing average predictions, contrasts or slopes. These weights only affect the averaging in
|
hypothesis |
specify a hypothesis test or custom contrast using a number , formula, string equation, vector, matrix, or function.
|
equivalence |
Numeric vector of length 2: bounds used for the two-one-sided test (TOST) of equivalence, and for the non-inferiority and non-superiority tests. For bayesian models, this report the proportion of posterior draws in the interval and the ROPE. See Details section below. |
equivalence_test |
A named arguments list (default
Additional package-specific controls:
|
p_direction |
A named arguments list (default
Additional package-specific controls:
|
eps |
NULL or numeric value which determines the step size to use when
calculating numerical derivatives: (f(x+eps)-f(x))/eps. When |
constrats_by |
A character vector (default |
constrats_at |
A named list (default |
constrats_subset |
A named list (default |
reformat |
A logical (default |
estimate_center |
A character string (default |
estimate_interval |
A character string (default |
dummy_to_factor |
A named list (default
|
verbose |
A logical argument (default |
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
difx |
A character string (default |
idvar |
A character string (default |
itransform |
A character string (default |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The get_growthparameters function estimates and
returns the following growth parameters:
pgv - peak growth velocity
apgv - age at peak growth velocity
tgv - takeoff growth velocity
atgv - age at takeoff growth velocity
cgv - cessation growth velocity
acgv - age at cessation growth velocity
The takeoff growth velocity is the lowest velocity just before the peak
begins, indicating the start of the pubertal growth spurt. The cessation
growth velocity marks the end of the active pubertal growth spurt and is
calculated as a certain percentage of the peak velocity (pgv).
Typically, 10 percent of pgv is considered a good indicator for the
cessation of the active pubertal growth spurt (Hardin et al. 2022).
This percentage is controlled via the acg_velocity argument, which
accepts a positive real value bounded between 0 and 1 (with the default value
being 0.1, indicating 10 percent).
A data.frame containing posterior estimates and credible
intervals (CIs). The output includes the parameter estimates and their
corresponding bounds (e.g., Q2.5 and Q97.5 for a 95%
interval). The specific column names and structure may vary depending on
the computation method.
Satpal Sandhu [email protected]
Hardin AM, Knigge RP, Oh HS, Valiathan M, Duren DL, McNulty KP, Middleton KM, Sherwood RJ (2022). “Estimating Craniofacial Growth Cessation: Comparison of Asymptote- and Rate-Based Methods.” The Cleft Palate Craniofacial Journal, 59(2), 230-238. doi:10.1177/10556656211002675. PMID: 33998905.
marginaleffects::comparisons()
marginaleffects::avg_comparisons()
marginaleffects::plot_comparisons()
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Note that since no covariate is part of the model fit, the below example # doesn't make sense and is included here only for the purpose of completeness. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit get_growthparameters(model, parameter = 'apgv', ndraws = 10)# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Note that since no covariate is part of the model fit, the below example # doesn't make sense and is included here only for the purpose of completeness. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit get_growthparameters(model, parameter = 'apgv', ndraws = 10)
The get_predictions() function estimates and plots
growth curves (distance and velocity) by using the marginaleffects
package as a back-end. This function computes growth curves
(marginaleffects::predictions()), average growth curves
(marginaleffects::avg_predictions()), and plots growth them via the
marginaleffects::plot_predictions(). See
here for details. Note that the
marginaleffects package is highly flexible, and therefore, it is
expected that the user has a strong understanding of its workings.
Furthermore, since marginaleffects is rapidly evolving, the results
obtained from the current implementation should be considered experimental.
Also note that unlike get_predictions(), the fitted_draws() and
predict_draws() uses the brms package as a back-end.
## S3 method for class 'bgmfit' get_predictions( model, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, datagrid = NULL, re_formula = NA, newdata2 = NULL, allow_new_levels = FALSE, sample_new_levels = "gaussian", parameter = NULL, xrange = 1, acg_velocity = 0.1, digits = 2, numeric_cov_at = NULL, aux_variables = NULL, grid_add = NULL, levels_id = NULL, avg_reffects = NULL, idata_method = NULL, ipts = NULL, seed = 123, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, usedtplyr = FALSE, usecollapse = TRUE, cores = NULL, fullframe = FALSE, average = FALSE, plot = FALSE, mapping_facet = NULL, showlegends = NULL, variables = NULL, condition = NULL, deriv = 0, model_deriv = TRUE, method = "custom", marginals = NULL, pdrawso = FALSE, pdrawsp = FALSE, pdrawsh = FALSE, type = NULL, by = NULL, conf_level = 0.95, transform = NULL, transform_draws = NULL, byfun = NULL, wts = NULL, hypothesis = NULL, equivalence = NULL, eps = NULL, constrats_by = NULL, constrats_at = NULL, reformat = NULL, estimate_center = NULL, estimate_interval = NULL, dummy_to_factor = NULL, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) get_predictions(model, ...) marginal_draws(model, ...)## S3 method for class 'bgmfit' get_predictions( model, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, datagrid = NULL, re_formula = NA, newdata2 = NULL, allow_new_levels = FALSE, sample_new_levels = "gaussian", parameter = NULL, xrange = 1, acg_velocity = 0.1, digits = 2, numeric_cov_at = NULL, aux_variables = NULL, grid_add = NULL, levels_id = NULL, avg_reffects = NULL, idata_method = NULL, ipts = NULL, seed = 123, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, usedtplyr = FALSE, usecollapse = TRUE, cores = NULL, fullframe = FALSE, average = FALSE, plot = FALSE, mapping_facet = NULL, showlegends = NULL, variables = NULL, condition = NULL, deriv = 0, model_deriv = TRUE, method = "custom", marginals = NULL, pdrawso = FALSE, pdrawsp = FALSE, pdrawsh = FALSE, type = NULL, by = NULL, conf_level = 0.95, transform = NULL, transform_draws = NULL, byfun = NULL, wts = NULL, hypothesis = NULL, equivalence = NULL, eps = NULL, constrats_by = NULL, constrats_at = NULL, reformat = NULL, estimate_center = NULL, estimate_interval = NULL, dummy_to_factor = NULL, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) get_predictions(model, ...) marginal_draws(model, ...)
model |
An object of class |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
newdata |
An optional data frame for estimation. If |
datagrid |
A grid of user-specified values to be used in the
|
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
newdata2 |
A named |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
parameter |
A single character string or character vector specifying the growth parameter(s) to estimate. Must be one of the following options, ordered by growth curve progression:
Multiple parameters can be requested simultaneously as a vector, e.g.,
|
xrange |
An integer to set the predictor range (e.g., age) when
executing the interpolation via |
acg_velocity |
A real number specifying the percentage of peak growth
velocity to be used as the cessation velocity when estimating the
|
digits |
An integer (default |
numeric_cov_at |
An optional (named list) argument to specify the value
of continuous covariate(s). The default |
aux_variables |
An optional argument to specify the variable(s) that can
be passed to the |
grid_add |
An optional argument to specify the variable(s) that can be
passed to the |
levels_id |
An optional argument to specify the |
avg_reffects |
An optional argument (default |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
seed |
An integer (default |
future |
A logical value (default |
future_session |
A character string or a named list specifying the
parallel plan when
|
future_splits |
A list, numeric vector, or logical value (default
Input options:
Note: On Windows with |
future_method |
A character string (default
|
future_re_expose |
A logical value (default
|
usedtplyr |
A logical (default |
usecollapse |
A logical (default |
cores |
An integer specifying the number of cores for parallel execution.
|
fullframe |
A logical value indicating whether to return a
|
average |
A logical indicating whether to internally call the
|
plot |
A logical specifying whether to plot predictions by calling
|
mapping_facet |
A named list that can be used to pass the aesthetic
|
showlegends |
A logical value to specify whether to show legends
( |
variables |
A named list specifying the level 1 predictor, such as
|
condition |
Conditional slopes
|
deriv |
An integer to indicate whether to estimate the distance curve or
its derivative (i.e., velocity curve). The |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
method |
A character string specifying the computation method: whether
to use the marginaleffects machinery at the post-draw stage, i.e.,
|
marginals |
A |
pdrawso |
A character string (default
When |
pdrawsp |
A character string (default
When |
pdrawsh |
A character string (default
The summary of posterior draws for parameters is the default returned
object. The |
type |
string indicates the type (scale) of the predictions used to
compute contrasts or slopes. This can differ based on the model
type, but will typically be a string such as: "response", "link", "probs",
or "zero". When an unsupported string is entered, the model-specific list of
acceptable values is returned in an error message. When |
by |
Aggregate unit-level estimates (aka, marginalize, average over). Valid inputs:
|
conf_level |
numeric value between 0 and 1. Confidence level to use to build a confidence interval. |
transform |
string or function. Transformation applied to unit-level estimates and confidence intervals just before the function returns results. Functions must accept a vector and return a vector of the same length. Support string shortcuts: "exp", "ln" |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
byfun |
A function such as |
wts |
logical, string or numeric: weights to use when computing average predictions, contrasts or slopes. These weights only affect the averaging in
|
hypothesis |
specify a hypothesis test or custom contrast using a number , formula, string equation, vector, matrix, or function.
|
equivalence |
Numeric vector of length 2: bounds used for the two-one-sided test (TOST) of equivalence, and for the non-inferiority and non-superiority tests. For bayesian models, this report the proportion of posterior draws in the interval and the ROPE. See Details section below. |
eps |
NULL or numeric value which determines the step size to use when
calculating numerical derivatives: (f(x+eps)-f(x))/eps. When |
constrats_by |
A character vector (default |
constrats_at |
A character vector (default |
reformat |
A logical (default |
estimate_center |
A character string (default |
estimate_interval |
A character string (default |
dummy_to_factor |
A named list (default
|
verbose |
A logical argument (default |
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
difx |
A character string (default |
idvar |
A character string (default |
itransform |
A character string (default |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The get_predictions() function estimates fitted values (via
brms::fitted.brmsfit()) or the posterior draws from the posterior
distribution (via brms::predict.brmsfit()) depending on the type
argument.
An array of predicted mean response values. See brms::fitted.brmsfit for details.
Satpal Sandhu [email protected]
marginaleffects::predictions()
marginaleffects::avg_predictions()
marginaleffects::plot_predictions()
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance curve get_predictions(model, deriv = 0, re_formula = NA, draw_ids = 1:2) # Individual-specific distance curves get_predictions(model, deriv = 0, re_formula = NULL, draw_ids = 1:2) # Population average velocity curve get_predictions(model, deriv = 1, re_formula = NA, draw_ids = 1:2) # Individual-specific velocity curves get_predictions(model, deriv = 1, re_formula = NULL, draw_ids = 1:2)# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance curve get_predictions(model, deriv = 0, re_formula = NA, draw_ids = 1:2) # Individual-specific distance curves get_predictions(model, deriv = 0, re_formula = NULL, draw_ids = 1:2) # Population average velocity curve get_predictions(model, deriv = 1, re_formula = NA, draw_ids = 1:2) # Individual-specific velocity curves get_predictions(model, deriv = 1, re_formula = NULL, draw_ids = 1:2)
This function checks if an object exists within a specified namespace and returns the object if it exists. The object must be provided as a symbol (not a character string). The function is designed to facilitate the retrieval of model or other objects from a specified environment or namespace. This function is mainly for internal purposes.
getNsObject(object, namespace = NULL, envir = NULL)getNsObject(object, namespace = NULL, envir = NULL)
object |
A symbol representing the object to be retrieved. The input must be a symbol (i.e., not a character string) corresponding to an existing object within the specified namespace. |
namespace |
A character string specifying the namespace to check for the object. If the object exists within the given namespace, it will be returned. |
envir |
An environment in which to search for the object. If set to
|
The object of the same class as the input object, if it
exists. If the object doesn't exist in the specified namespace or
environment, an error is raised.
Satpal Sandhu [email protected]
# Check whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit)# Check whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit)
The growthparameters() function estimates both
population-average and individual-specific growth parameters (e.g., age at
peak growth velocity). It also provides measures of uncertainty, including
standard errors (SE) and credible intervals (CIs). For a more advanced
analysis, consider using the get_growthparameters() function, which not
only estimates adjusted parameters but also enables comparisons of these
parameters across different groups.The get_growthparameters() function is
based on the marginaleffects package.
## S3 method for class 'bgmfit' growthparameters( model, newdata = NULL, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, summary = FALSE, robust = FALSE, transform_draws = NULL, scale = c("response", "linear"), re_formula = NA, peak = TRUE, takeoff = FALSE, trough = FALSE, acgv = FALSE, acgv_velocity = 0.1, estimation_method = "fitted", allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, numeric_cov_at = NULL, levels_id = NULL, avg_reffects = NULL, aux_variables = NULL, grid_add = NULL, ipts = NULL, model_deriv = TRUE, conf = 0.95, xrange = NULL, xrange_search = NULL, digits = 2, seed = 123, future = FALSE, future_session = "multisession", cores = NULL, parms_eval = FALSE, idata_method = NULL, parms_method = "getPeak", verbose = FALSE, fullframe = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) growthparameters(model, ...)## S3 method for class 'bgmfit' growthparameters( model, newdata = NULL, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, summary = FALSE, robust = FALSE, transform_draws = NULL, scale = c("response", "linear"), re_formula = NA, peak = TRUE, takeoff = FALSE, trough = FALSE, acgv = FALSE, acgv_velocity = 0.1, estimation_method = "fitted", allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, numeric_cov_at = NULL, levels_id = NULL, avg_reffects = NULL, aux_variables = NULL, grid_add = NULL, ipts = NULL, model_deriv = TRUE, conf = 0.95, xrange = NULL, xrange_search = NULL, digits = 2, seed = 123, future = FALSE, future_session = "multisession", cores = NULL, parms_eval = FALSE, idata_method = NULL, parms_method = "getPeak", verbose = FALSE, fullframe = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) growthparameters(model, ...)
model |
An object of class |
newdata |
An optional data frame for estimation. If |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
summary |
A logical value indicating whether only the estimate should be
computed ( |
robust |
A logical value to specify the summary options. If |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
scale |
Either |
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
peak |
A logical value (default |
takeoff |
A logical value (default |
trough |
A logical value (default |
acgv |
A logical value (default |
acgv_velocity |
The percentage of the peak growth velocity to use when
estimating |
estimation_method |
A character string specifying the estimation method
when calculating the velocity from the posterior draws. The |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
incl_autocor |
A flag indicating if correlation structures originally
specified via |
numeric_cov_at |
An optional (named list) argument to specify the value
of continuous covariate(s). The default |
levels_id |
An optional argument to specify the |
avg_reffects |
An optional argument (default |
aux_variables |
An optional argument to specify the variable(s) that can
be passed to the |
grid_add |
An optional argument to specify the variable(s) that can be
passed to the |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
conf |
A numeric value (default |
xrange |
An integer to set the predictor range (e.g., age) when
executing the interpolation via |
xrange_search |
A vector of length two or a character string
|
digits |
An integer (default |
seed |
An integer (default |
future |
A logical value (default |
future_session |
A character string or a named list specifying the
parallel plan when
|
cores |
An integer specifying the number of cores for parallel execution.
|
parms_eval |
A logical value to specify whether or not to compute growth parameters on the fly. This is for internal use only and is mainly needed for compatibility across internal functions. |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
parms_method |
A character string specifying the method used when
evaluating |
verbose |
A logical argument (default |
fullframe |
A logical value indicating whether to return a
|
dummy_to_factor |
A named list (default
|
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
difx |
A character string (default |
idvar |
A character string (default |
itransform |
A character string (default |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The growthparameters() function internally calls either the
fitted_draws() or the predict_draws() function to estimate
first-derivative growth parameters for each posterior draw. The estimated
growth parameters include:
Age at Peak Growth Velocity (APGV)
Peak Growth Velocity (PGV)
Age at Takeoff Growth Velocity (ATGV)
Takeoff Growth Velocity (TGV)
Age at Cessation of Growth Velocity (ACGV)
Cessation Growth Velocity (CGV)
APGV and PGV are estimated using the sitar::getPeak() function, while
ATGV and TGV are estimated using the sitar::getTakeoff() function. The
sitar::getTrough() function is employed to estimate ACGV and CGV. The
parameters from each posterior draw are then summarized to provide
estimates along with uncertainty measures (SEs and CIs).
Please note that estimating cessation and takeoff growth parameters may not be possible if there are no distinct pre-peak or post-peak troughs in the data.
A data frame with either five columns (when summary = TRUE) or
two columns (when summary = FALSE, assuming re_formual =
NULL). The first two columns, common to both scenarios, are
'Parameter' and 'Estimate', representing the growth parameter
(e.g., APGV, PGV) and its estimate. When summary = TRUE, three
additional columns are included: 'Est.Error' and two columns
representing the lower and upper bounds of the confidence intervals, named
Q.2.5 and Q.97.5 (for the 95% CI). If re_formual =
NULL, an additional column with individual identifiers (e.g., id)
is included.
Satpal Sandhu [email protected]
# Fit Bayesian SITAR Model # To avoid mode estimation, which takes time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check if the model fit object 'berkeley_exfit' exists and load it berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average age and velocity during the peak growth spurt growthparameters(model, re_formula = NA) # Population average age and velocity during the take-off and peak # growth spurt (APGV, PGV, ATGV, TGV) growthparameters(model, re_formula = NA, peak = TRUE, takeoff = TRUE) # Individual-specific age and velocity during the take-off and peak # growth spurt (APGV, PGV, ATGV, TGV) growthparameters(model, re_formula = NULL, peak = TRUE, takeoff = TRUE)# Fit Bayesian SITAR Model # To avoid mode estimation, which takes time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check if the model fit object 'berkeley_exfit' exists and load it berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average age and velocity during the peak growth spurt growthparameters(model, re_formula = NA) # Population average age and velocity during the take-off and peak # growth spurt (APGV, PGV, ATGV, TGV) growthparameters(model, re_formula = NA, peak = TRUE, takeoff = TRUE) # Individual-specific age and velocity during the take-off and peak # growth spurt (APGV, PGV, ATGV, TGV) growthparameters(model, re_formula = NULL, peak = TRUE, takeoff = TRUE)
Performs non-linear hypothesis testing via brms::hypothesis(), Region of
Practical Equivalence (ROPE) via bayestestR::equivalence_test(),
probability of direction (pd) via bayestestR::p_direction(), and
pairwise and contrast-style comparisons via marginaleffects::comparisons(),
using a unified interface.
The ROPE evaluates whether the parameter values should be accepted or
rejected against an explicitly formulated "null hypothesis". It checks the
percentage of the posterior credible intervals such as the High Density
Intervals (HDI) defined as the null region (the ROPE). If this
percentage is sufficiently low, the null hypothesis is rejected. If this
percentage is sufficiently high, the null hypothesis is accepted. If the HDI
is completely outside the ROPE, the "null hypothesis" for this
parameter is "rejected". If the ROPE completely covers the HDI, i.e.,
all most credible values of a parameter are inside the region of practical
equivalence, the null hypothesis is accepted. Else, it is undecided whether
to accept or reject the null hypothesis. See bayestestR::equivalence_test()
for further details.
The PD, also known as the Maximum Probability of Effect (MPE)
is the probability that a parameter (described by its posterior distribution)
is strictly positive or negative (whichever is the most probable). Although
differently expressed, this index is fairly similar (i.e., is strongly
correlated) to the frequentest p-value (see details). See
bayestestR::p_direction() for further details.
## S3 method for class 'bgmfit' hypothesis_test( model, by = NULL, hypothesis = NULL, hypothesis_str = NULL, parameters = NULL, parameter = NULL, class = "b", group = "", scope = c("standard", "ranef", "coef"), robust = FALSE, seed = NULL, equivalence_test = NULL, p_direction = NULL, rope_test = NULL, pd_test = NULL, range = "default", method = "direct", method_call = "pkg", null = NULL, as_p = FALSE, remove_na = TRUE, rvar_col = NULL, effects = "fixed", component = "conditional", evaluate_comparison = NULL, comparison_by = NULL, comparison_range_null = NULL, comparison_range = NULL, comparison_null = NULL, evaluate_hypothesis = NULL, hypothesis_by = NULL, hypothesis_range_null = NULL, hypothesis_range = NULL, hypothesis_null = NULL, rope_as_percent = TRUE, pd_as_percent = TRUE, format = NULL, reformat = TRUE, estimate_center = NULL, estimate_interval = NULL, na.rm = TRUE, alpha = 0.05, ci = 0.95, conf_level = NULL, probs = c(0.025, 0.975), get_range_null_form = NULL, get_range_null_value = NULL, plot = FALSE, digits = 2, engine = NULL, usesavedfuns = FALSE, drop_probtitles = NULL, verbose = FALSE, envir = NULL, ... ) hypothesis_test(model, ...)## S3 method for class 'bgmfit' hypothesis_test( model, by = NULL, hypothesis = NULL, hypothesis_str = NULL, parameters = NULL, parameter = NULL, class = "b", group = "", scope = c("standard", "ranef", "coef"), robust = FALSE, seed = NULL, equivalence_test = NULL, p_direction = NULL, rope_test = NULL, pd_test = NULL, range = "default", method = "direct", method_call = "pkg", null = NULL, as_p = FALSE, remove_na = TRUE, rvar_col = NULL, effects = "fixed", component = "conditional", evaluate_comparison = NULL, comparison_by = NULL, comparison_range_null = NULL, comparison_range = NULL, comparison_null = NULL, evaluate_hypothesis = NULL, hypothesis_by = NULL, hypothesis_range_null = NULL, hypothesis_range = NULL, hypothesis_null = NULL, rope_as_percent = TRUE, pd_as_percent = TRUE, format = NULL, reformat = TRUE, estimate_center = NULL, estimate_interval = NULL, na.rm = TRUE, alpha = 0.05, ci = 0.95, conf_level = NULL, probs = c(0.025, 0.975), get_range_null_form = NULL, get_range_null_value = NULL, plot = FALSE, digits = 2, engine = NULL, usesavedfuns = FALSE, drop_probtitles = NULL, verbose = FALSE, envir = NULL, ... ) hypothesis_test(model, ...)
model |
A fitted model object of class |
by |
Optional grouping variable(s) used to stratify summaries or comparisons. The exact meaning depends on the selected engine and methods. |
hypothesis |
specify a hypothesis test or custom contrast using a number , formula, string equation, vector, matrix, or function.
|
hypothesis_str |
A character vector specifying one or more non-linear
hypothesis concerning parameters of the model. Note that the argument
|
parameters |
Regular expression pattern that describes the parameters
that should be returned. Meta-parameters (like |
parameter |
A single character string or character vector specifying the growth parameter(s) to estimate. Must be one of the following options, ordered by growth curve progression:
Multiple parameters can be requested simultaneously as a vector, e.g.,
|
class |
A string specifying the class of parameters being tested.
Default is "b" for population-level effects.
Other typical options are "sd" or "cor".
If |
group |
Name of a grouping factor to evaluate only group-level effects parameters related to this grouping factor. |
scope |
Indicates where to look for the variables specified in
|
robust |
If |
seed |
A single numeric value passed to |
equivalence_test |
A named arguments list (default
Additional package-specific controls:
|
p_direction |
A named arguments list (default
Additional package-specific controls:
|
rope_test |
Logical; if |
pd_test |
Logical; if |
range |
ROPE's lower and higher bounds. Should be
In multivariate models, |
method |
Can be |
method_call |
A character string to pass |
null |
The value considered as a "null" effect. Traditionally 0, but could also be 1 in the case of ratios of change (OR, IRR, ...). |
as_p |
If |
remove_na |
Should missing values be removed before computation? Note
that |
rvar_col |
A single character - the name of an |
effects |
Should variables for fixed effects ( For models of from packages brms or rstanarm there are additional options:
|
component |
Which type of parameters to return, such as parameters for the conditional model, the zero-inflated part of the model, the dispersion term, etc. See details in section Model Components. May be abbreviated. Note that the conditional component also refers to the count or mean component - names may differ, depending on the modeling package. There are three convenient shortcuts (not applicable to all model classes):
|
evaluate_comparison |
Logical indicating whether to compute and return
comparisons from |
comparison_by |
Character string specifying the |
comparison_range_null |
Data frame with columns |
comparison_range |
Like |
comparison_null |
Like |
evaluate_hypothesis |
Logical indicating whether to compute and return
hypothesis from |
hypothesis_by |
Character string specifying the |
hypothesis_range_null |
Data frame with columns |
hypothesis_range |
Like |
hypothesis_null |
Like |
rope_as_percent |
Logical indicating whether to return ROPE results as
percentages ( |
pd_as_percent |
Logical indicating whether to return PD results as
percentages ( |
format |
Logical; if |
reformat |
Logical; if |
estimate_center |
A character string (default |
estimate_interval |
A character string (default |
na.rm |
Logical; if |
alpha |
Alpha level for hypothesis tests used in |
ci |
Confidence level used in |
conf_level |
Confidence level used in |
probs |
Probability levels for credible intervals. Defaults to
|
get_range_null_form |
Logical; if |
get_range_null_value |
Logical; if |
plot |
Logical or a character string |
digits |
Number of digits to use when printing numeric results. Default
|
engine |
Optional engine selector specifying which backend(s) to use:
|
usesavedfuns |
A logical value (default |
drop_probtitles |
A logical indicating whether to drop the |
verbose |
Toggle off warnings. |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments forwarded to |
This function collects arguments from multiple upstream packages:
hypothesis_test arguments (e.g., hypothesis,
class, scope, alpha, robust, seed) map
to brms::hypothesis() and, in particular,
scope controls where hypothesis components referenced in
hypothesis are looked up. Note that to avoid conflicts with the
hypothesis argument from
marginaleffects::comparisons(),
the hypothesis argument for the brms package is renamed as
hypothesis_str. In other words, the hypothesis_str (see
below) is used to setup the
hypothesis argument when calling the
brms::hypothesis().
equivalence_test arguments (e.g., range, ci,
remove_na, parameters) map to
bayestestR::equivalence_test()
for ROPE-based practical equivalence decisions.
p_direction arguments (e.g., method, null,
ci, remove_na, parameters) map to
bayestestR::p_direction() for
probability of direction based practical decisions.
Additional arguments in ... are forwarded to
marginaleffects::comparisons()
via get_growthparameters to compute contrasts between
predictions under different predictor values.
Users should supply either hypothesis_str (brms hypothesis),
parameters (bayestestR), or parameter
(growth-parameter selector used by this package), but not multiple
overlapping selectors.
Supported inputs: bgmfit objects (via all three packages),
posterior data frames (via brms and bayestestR methods), or
brmsfit objects (via brms).
An object (typically a list) containing some or all of the following components, depending on the requested analyses and available methods:
A brms::hypothesis() result object for hypothesis tests.
A bayestestR::equivalence_test() result for ROPE and practical
equivalence.
A bayestestR::p_direction() result for probability of direction.
A marginaleffects::comparisons() result for contrasts between
predictions.
Satpal Sandhu [email protected]
brms::hypothesis() for hypothesis testing.
bayestestR::equivalence_test() for ROPE tests.
bayestestR::p_direction() for probability of direction tests.
marginaleffects::comparisons() for prediction contrasts.
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists model <- getNsObject(berkeley_exfit) # Speed up examples by using a subset of posterior draws draw_ids <- 1:5 ## Example 1: Hypothesis testing (brms-style) set_hypothesis <- c("a_Intercept = 0", "b_Intercept > 1") # brms reference hyp_ex1 <- brms::hypothesis(model, hypothesis = set_hypothesis, draw_ids = draw_ids) # hypothesis_test (auto-detects brms engine) hyp_ex11 <- hypothesis_test(model, draw_ids = draw_ids, hypothesis_str = set_hypothesis) # If see package is installed (install.packages("see")), then you can plot it # plot(hyp_ex1) # plot(hyp_ex11) ## Example 2: ROPE equivalence testing (bayestestR) set_parameters <- c("b_a_Intercept", "b_b_Intercept") set_range <- list(b_a_Intercept = c(100, 150), b_b_Intercept = c(-2, 2)) # bayestestR reference - If you installed bayestestR package # hyp_ex1 <- bayestestR::equivalence_test(model, # parameters = set_parameters, # range = set_range, # draw_ids = draw_ids) # hypothesis_test (auto-detects bayestestR engine) # If see package is installed (install.packages("see")), then you can plot # the results by setting plot = TRUE hyp_ex11 <- hypothesis_test(model, draw_ids = draw_ids, parameters = set_parameters, range = set_range, plot = FALSE) # print(hyp_ex1) print(hyp_ex11) # Note: bayestestR often ignores parameters/effects for nonlinear bsitar models. # hypothesis_test correctly respects user-specified parameters. ## Example 3: p-direction testing (bayestestR) set_parameters <- c("b_a_Intercept", "b_b_Intercept") set_null <- list(b_a_Intercept = 0, b_b_Intercept = 0) # bayestestR reference - If you installed bayestestR package # hyp_ex1 <- bayestestR::p_direction(model, # parameters = set_parameters, # null = set_null, # draw_ids = draw_ids) # hypothesis_test (auto-detects bayestestR engine) # If see package is installed (install.packages("see")), then you can plot # the results by setting plot = TRUE hyp_ex11 <- hypothesis_test(model, draw_ids = draw_ids, parameters = set_parameters, null = set_null, plot = FALSE, equivalence_test = FALSE, p_direction = TRUE) # print(hyp_ex1) print(hyp_ex11) ## Example 4: Growth parameter hypothesis testing (marginaleffects) set_parameter <- c("apgv", "pgv") set_range <- list(apgv = 1, pgv = c(0, 0.5)) set_null <- list(apgv = 1, pgv = 0) # Directly using get_growthparameters() hyp_ex1 <- get_growthparameters(model, parameter = set_parameter, draw_ids = draw_ids) # Uncomment for ROPE/p-direction tests: # equivalence_test = list(range = set_range), # p_direction = list(null = set_null) ## Example 5: hypothesis_test() with growth parameters hyp_ex1 <- hypothesis_test(model, parameter = set_parameter, equivalence_test = list(range = set_range), p_direction = list(null = set_null), draw_ids = draw_ids) ## Example 6: Pass posterior draws from get_growthparameters() # pdrawsp = TRUE returns draws for downstream hypothesis testing hyp_draws <- get_growthparameters(model, parameter = set_parameter, pdrawsp = TRUE, equivalence_test = list(range = set_range), p_direction = list(null = set_null), draw_ids = draw_ids) # Use draws directly in hypothesis_test() hyp_ex1 <- hypothesis_test(hyp_draws, parameter = set_parameter, equivalence_test = list(range = set_range), p_direction = list(null = set_null), draw_ids = draw_ids)# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists model <- getNsObject(berkeley_exfit) # Speed up examples by using a subset of posterior draws draw_ids <- 1:5 ## Example 1: Hypothesis testing (brms-style) set_hypothesis <- c("a_Intercept = 0", "b_Intercept > 1") # brms reference hyp_ex1 <- brms::hypothesis(model, hypothesis = set_hypothesis, draw_ids = draw_ids) # hypothesis_test (auto-detects brms engine) hyp_ex11 <- hypothesis_test(model, draw_ids = draw_ids, hypothesis_str = set_hypothesis) # If see package is installed (install.packages("see")), then you can plot it # plot(hyp_ex1) # plot(hyp_ex11) ## Example 2: ROPE equivalence testing (bayestestR) set_parameters <- c("b_a_Intercept", "b_b_Intercept") set_range <- list(b_a_Intercept = c(100, 150), b_b_Intercept = c(-2, 2)) # bayestestR reference - If you installed bayestestR package # hyp_ex1 <- bayestestR::equivalence_test(model, # parameters = set_parameters, # range = set_range, # draw_ids = draw_ids) # hypothesis_test (auto-detects bayestestR engine) # If see package is installed (install.packages("see")), then you can plot # the results by setting plot = TRUE hyp_ex11 <- hypothesis_test(model, draw_ids = draw_ids, parameters = set_parameters, range = set_range, plot = FALSE) # print(hyp_ex1) print(hyp_ex11) # Note: bayestestR often ignores parameters/effects for nonlinear bsitar models. # hypothesis_test correctly respects user-specified parameters. ## Example 3: p-direction testing (bayestestR) set_parameters <- c("b_a_Intercept", "b_b_Intercept") set_null <- list(b_a_Intercept = 0, b_b_Intercept = 0) # bayestestR reference - If you installed bayestestR package # hyp_ex1 <- bayestestR::p_direction(model, # parameters = set_parameters, # null = set_null, # draw_ids = draw_ids) # hypothesis_test (auto-detects bayestestR engine) # If see package is installed (install.packages("see")), then you can plot # the results by setting plot = TRUE hyp_ex11 <- hypothesis_test(model, draw_ids = draw_ids, parameters = set_parameters, null = set_null, plot = FALSE, equivalence_test = FALSE, p_direction = TRUE) # print(hyp_ex1) print(hyp_ex11) ## Example 4: Growth parameter hypothesis testing (marginaleffects) set_parameter <- c("apgv", "pgv") set_range <- list(apgv = 1, pgv = c(0, 0.5)) set_null <- list(apgv = 1, pgv = 0) # Directly using get_growthparameters() hyp_ex1 <- get_growthparameters(model, parameter = set_parameter, draw_ids = draw_ids) # Uncomment for ROPE/p-direction tests: # equivalence_test = list(range = set_range), # p_direction = list(null = set_null) ## Example 5: hypothesis_test() with growth parameters hyp_ex1 <- hypothesis_test(model, parameter = set_parameter, equivalence_test = list(range = set_range), p_direction = list(null = set_null), draw_ids = draw_ids) ## Example 6: Pass posterior draws from get_growthparameters() # pdrawsp = TRUE returns draws for downstream hypothesis testing hyp_draws <- get_growthparameters(model, parameter = set_parameter, pdrawsp = TRUE, equivalence_test = list(range = set_range), p_direction = list(null = set_null), draw_ids = draw_ids) # Use draws directly in hypothesis_test() hyp_ex1 <- hypothesis_test(hyp_draws, parameter = set_parameter, equivalence_test = list(range = set_range), p_direction = list(null = set_null), draw_ids = draw_ids)
bgmfit
Checks if object is of class bgmfit
is.bgmfit(x)is.bgmfit(x)
x |
An R object |
Satpal Sandhu [email protected]
The loo_validation() function is a wrapper around the
brms::loo() function to perform approximate leave-one-out cross-validation
based on the posterior likelihood. See brms::loo() for more details.
## S3 method for class 'bgmfit' loo_validation( model, compare = TRUE, resp = NULL, dpar = NULL, pointwise = FALSE, moment_match = FALSE, reloo = FALSE, k_threshold = 0.7, save_psis = FALSE, moment_match_args = list(), reloo_args = list(), model_names = NULL, ndraws = NULL, draw_ids = NULL, cores = 1, model_deriv = NULL, verbose = FALSE, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, envir = NULL, ... ) loo_validation(model, ...)## S3 method for class 'bgmfit' loo_validation( model, compare = TRUE, resp = NULL, dpar = NULL, pointwise = FALSE, moment_match = FALSE, reloo = FALSE, k_threshold = 0.7, save_psis = FALSE, moment_match_args = list(), reloo_args = list(), model_names = NULL, ndraws = NULL, draw_ids = NULL, cores = 1, model_deriv = NULL, verbose = FALSE, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, envir = NULL, ... ) loo_validation(model, ...)
model |
An object of class |
compare |
A logical flag indicating if the information criteria of the
models should be compared using |
resp |
Optional names of response variables. If specified, predictions are performed only for the specified response variables. |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
pointwise |
A flag indicating whether to compute the full
log-likelihood matrix at once or separately for each observation.
The latter approach is usually considerably slower but
requires much less working memory. Accordingly, if one runs
into memory issues, |
moment_match |
A logical flag to indicate whether
|
reloo |
A logical flag indicating whether |
k_threshold |
The Pareto |
save_psis |
Should the |
moment_match_args |
An optional |
reloo_args |
An optional |
model_names |
If |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
cores |
An integer specifying the number of cores for parallel execution.
|
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
verbose |
A logical argument (default |
dummy_to_factor |
A named list (default
|
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The function supports model comparisons using loo::loo_compare()
for comparing information criteria across models. For bgmfit
objects, LOO is simply an alias for loo. Additionally, you
can use brms::add_criterion() to store information criteria in the fitted
model object for later use.
If only one model object is provided, an object of class loo
is returned. If multiple objects are provided, an object of class
loolist is returned.
Satpal Sandhu [email protected]
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Perform leave-one-out cross-validation loo_validation(model, cores = 1)# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Perform leave-one-out cross-validation loo_validation(model, cores = 1)
The modelbased_growthparameters() function estimates
individual growth parameters by mapping population average estimate of age
of interest (such as age at peak growth velocity or age at take off) on to
the individual velocity curves defined by individual level random effects.
Note that option 'dpar' can not be used along with 'nlpar' in
brms::posterior_linpred().
## S3 method for class 'bgmfit' modelbased_growthparameters( model, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, datagrid = NULL, re_formula = NA, newdata2 = NULL, allow_new_levels = FALSE, sample_new_levels = "gaussian", parameter = NULL, xrange = 1, acg_velocity = 0.1, digits = 2, numeric_cov_at = NULL, aux_variables = NULL, levels_id = NULL, avg_reffects = NULL, idata_method = NULL, ipts = FALSE, seed = 123, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, usedtplyr = FALSE, usecollapse = TRUE, parallel = FALSE, cores = NULL, fullframe = FALSE, average = FALSE, plot = FALSE, showlegends = NULL, variables = NULL, deriv = NULL, model_deriv = NULL, method = "custom", marginals = NULL, preparms = NULL, pdraws = FALSE, pdrawso = FALSE, pdrawsp = FALSE, pdrawsh = FALSE, comparison = "difference", type = NULL, by = FALSE, bys = NULL, conf_level = 0.95, transform = NULL, transform_draws = NULL, cross = FALSE, wts = NULL, hypothesis = NULL, equivalence = NULL, eps = NULL, constrats_by = FALSE, constrats_at = FALSE, constrats_subset = FALSE, reformat = NULL, estimate_center = NULL, estimate_interval = NULL, dummy_to_factor = NULL, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, idvar = NULL, difx = NULL, itransform = NULL, incl_autocor = TRUE, parameter_method = 1, subset_by = NULL, add_xtm = FALSE, call_function = "R", newdata_fixed = NULL, envir = NULL, ... ) modelbased_growthparameters(model, ...)## S3 method for class 'bgmfit' modelbased_growthparameters( model, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, datagrid = NULL, re_formula = NA, newdata2 = NULL, allow_new_levels = FALSE, sample_new_levels = "gaussian", parameter = NULL, xrange = 1, acg_velocity = 0.1, digits = 2, numeric_cov_at = NULL, aux_variables = NULL, levels_id = NULL, avg_reffects = NULL, idata_method = NULL, ipts = FALSE, seed = 123, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, usedtplyr = FALSE, usecollapse = TRUE, parallel = FALSE, cores = NULL, fullframe = FALSE, average = FALSE, plot = FALSE, showlegends = NULL, variables = NULL, deriv = NULL, model_deriv = NULL, method = "custom", marginals = NULL, preparms = NULL, pdraws = FALSE, pdrawso = FALSE, pdrawsp = FALSE, pdrawsh = FALSE, comparison = "difference", type = NULL, by = FALSE, bys = NULL, conf_level = 0.95, transform = NULL, transform_draws = NULL, cross = FALSE, wts = NULL, hypothesis = NULL, equivalence = NULL, eps = NULL, constrats_by = FALSE, constrats_at = FALSE, constrats_subset = FALSE, reformat = NULL, estimate_center = NULL, estimate_interval = NULL, dummy_to_factor = NULL, verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, idvar = NULL, difx = NULL, itransform = NULL, incl_autocor = TRUE, parameter_method = 1, subset_by = NULL, add_xtm = FALSE, call_function = "R", newdata_fixed = NULL, envir = NULL, ... ) modelbased_growthparameters(model, ...)
model |
An object of class |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
newdata |
An optional data frame for estimation. If |
datagrid |
A grid of user-specified values to be used in the
|
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
newdata2 |
A named |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
parameter |
A single character string or a character vector specifying
the growth parameter(s) to be estimated. Options include age at peak growth
velocity ( |
xrange |
An integer to set the predictor range (e.g., age) when
executing the interpolation via |
acg_velocity |
A real number specifying the percentage of peak growth
velocity to be used as the cessation velocity when estimating the
|
digits |
An integer (default |
numeric_cov_at |
An optional (named list) argument to specify the value
of continuous covariate(s). The default |
aux_variables |
An optional argument to specify the variable(s) that can
be passed to the |
levels_id |
An optional argument to specify the |
avg_reffects |
An optional argument (default |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
seed |
An integer (default |
future |
A logical value (default |
future_session |
A character string or a named list specifying the
parallel plan when
|
future_splits |
A list, numeric vector, or logical value (default
Input options:
Note: On Windows with |
future_method |
A character string (default
|
future_re_expose |
A logical value (default
|
usedtplyr |
A logical (default |
usecollapse |
A logical (default |
parallel |
A logical (default |
cores |
An integer specifying the number of cores for parallel execution.
|
fullframe |
A logical value indicating whether to return a
|
average |
A logical value indicating whether to internally call the
|
plot |
A logical value specifying whether to plot comparisons by calling
the |
showlegends |
A logical value to specify whether to show legends
( |
variables |
A named list specifying the level 1 predictor, such as
|
deriv |
A numeric value specifying whether to estimate parameters based
on the differentiation of the distance curve or the model's first
derivative. Please refer to the |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
method |
A character string indicating whether to compute estimates
using the |
marginals |
A |
preparms |
A |
pdraws |
A character string (default
The |
pdrawso |
A character string (default
When |
pdrawsp |
A character string (default
When |
pdrawsh |
A character string (default
The summary of posterior draws for parameters is the default returned
object. The |
comparison |
A character string specifying the comparison type for
growth parameter estimation. Options are |
type |
string indicates the type (scale) of the predictions used to
compute contrasts or slopes. This can differ based on the model
type, but will typically be a string such as: "response", "link", "probs",
or "zero". When an unsupported string is entered, the model-specific list of
acceptable values is returned in an error message. When |
by |
Aggregate unit-level estimates (aka, marginalize, average over). Valid inputs:
|
bys |
A character string (default |
conf_level |
numeric value between 0 and 1. Confidence level to use to build a confidence interval. |
transform |
string or function. Transformation applied to unit-level estimates and confidence intervals just before the function returns results. Functions must accept a vector and return a vector of the same length. Support string shortcuts: "exp", "ln" |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
cross |
|
wts |
logical, string or numeric: weights to use when computing average predictions, contrasts or slopes. These weights only affect the averaging in
|
hypothesis |
specify a hypothesis test or custom contrast using a number , formula, string equation, vector, matrix, or function.
|
equivalence |
Numeric vector of length 2: bounds used for the two-one-sided test (TOST) of equivalence, and for the non-inferiority and non-superiority tests. For bayesian models, this report the proportion of posterior draws in the interval and the ROPE. See Details section below. |
eps |
NULL or numeric value which determines the step size to use when
calculating numerical derivatives: (f(x+eps)-f(x))/eps. When |
constrats_by |
A character vector (default |
constrats_at |
A named list (default |
constrats_subset |
A named list (default |
reformat |
A logical (default |
estimate_center |
A character string (default |
estimate_interval |
A character string (default |
dummy_to_factor |
A named list (default
|
verbose |
A logical argument (default |
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
idvar |
A character string (default |
difx |
A character string (default |
itransform |
A character string (default |
incl_autocor |
A flag indicating if correlation structures originally
specified via |
parameter_method |
An integer (default =
|
subset_by |
A logical or character string (default = When This option is useful when multiple rows per For |
add_xtm |
A logical (default |
call_function |
A character string indicating the source of the function
used for computation—either a native Though |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the function. |
Since SITAR is a shape-invariant model, each individual curve has a peak velocity point that can be mapped by knowing the population average age at peak velocity. This hold true even when a individual lacks measurements at the expected turning point.
A data frame comprising growth parameter estimates for age, distance and velocity.
Satpal Sandhu [email protected]
marginaleffects::predictions()
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit modelbased_growthparameters(model, ndraws = 2, parameter = 'apgv')# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit modelbased_growthparameters(model, ndraws = 2, parameter = 'apgv')
The optimization process for selecting the best-fitting SITAR
model involves choosing the optimal degrees of freedom (df) for the
natural cubic spline curve, as well as determining the appropriate
transformations for the predictor (x) and/or outcome (y)
variables.
## S3 method for class 'bgmfit' optimize_model( model, newdata = NULL, optimize_df = NULL, optimize_x = list(NULL, log, sqrt), optimize_y = list(NULL, log, sqrt), transform_prior_class = NULL, transform_beta_coef = NULL, transform_sd_coef = NULL, exclude_default = TRUE, add_fit_criteria = c("loo"), byresp = FALSE, model_name = NULL, overwrite = FALSE, file = NULL, force_save = FALSE, save_each = FALSE, digits = 2, cores = 1, verbose = FALSE, expose_function = FALSE, usesavedfuns = FALSE, clearenvfuns = NULL, envir = NULL, ... ) optimize_model(model, ...)## S3 method for class 'bgmfit' optimize_model( model, newdata = NULL, optimize_df = NULL, optimize_x = list(NULL, log, sqrt), optimize_y = list(NULL, log, sqrt), transform_prior_class = NULL, transform_beta_coef = NULL, transform_sd_coef = NULL, exclude_default = TRUE, add_fit_criteria = c("loo"), byresp = FALSE, model_name = NULL, overwrite = FALSE, file = NULL, force_save = FALSE, save_each = FALSE, digits = 2, cores = 1, verbose = FALSE, expose_function = FALSE, usesavedfuns = FALSE, clearenvfuns = NULL, envir = NULL, ... ) optimize_model(model, ...)
model |
An object of class |
newdata |
An optional data frame for estimation. If |
optimize_df |
A list of integers specifying the degrees of freedom
( |
optimize_x |
A list specifying the transformations for the predictor
variable (i.e., |
optimize_y |
A list specifying the transformations of the response
variable (i.e., |
transform_prior_class |
A character vector (default |
transform_beta_coef |
A character vector (default |
transform_sd_coef |
A character vector (default |
exclude_default |
A logical indicating whether transformations for
( |
add_fit_criteria |
A character vector specifying the fit criteria to
be added. See |
byresp |
A logical (default |
model_name |
Optional name of the model. If |
overwrite |
Logical; Indicates if already stored fit
indices should be overwritten. Defaults to |
file |
Either |
force_save |
Logical; only relevant if |
save_each |
A logical (default |
digits |
An integer (default |
cores |
The number of cores to use in parallel processing (default
|
verbose |
A logical argument (default |
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
envir |
The environment used for function evaluation. The default is
|
... |
Other arguments passed to |
A list containing the optimized models of class bgmfit, and
the the summary statistics if add_fit_criteria are specified.
Satpal Sandhu [email protected]
# Fit Bayesian SITAR model # To avoid model estimation, which takes time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # The following example shows a dummy call for optimization to save time. # Note that if the degree of freedom, and both the \code{optimize_x} and # \code{optimize_y} are \code{NULL} (i.e., nothing to optimize), the original # model object is returned. # To explicitly check whether the model is being optimized or not, # the user can set \code{verbose = TRUE}. This is useful for getting # information about what arguments have changed compared to the # original model. model2 <- optimize_model(model, optimize_df = NULL, optimize_x = NULL, optimize_y = NULL, verbose = TRUE)# Fit Bayesian SITAR model # To avoid model estimation, which takes time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # The following example shows a dummy call for optimization to save time. # Note that if the degree of freedom, and both the \code{optimize_x} and # \code{optimize_y} are \code{NULL} (i.e., nothing to optimize), the original # model object is returned. # To explicitly check whether the model is being optimized or not, # the user can set \code{verbose = TRUE}. This is useful for getting # information about what arguments have changed compared to the # original model. model2 <- optimize_model(model, optimize_df = NULL, optimize_x = NULL, optimize_y = NULL, verbose = TRUE)
Display conditional effects of one or more numeric and/or categorical predictors including two-way interaction effects.
## S3 method for class 'bgmfit' plot_conditional_effects( model, effects = NULL, conditions = NULL, int_conditions = NULL, re_formula = NA, spaghetti = FALSE, surface = FALSE, categorical = FALSE, ordinal = FALSE, method = NULL, allow_new_levels = FALSE, estimation_method = "fitted", transform = NULL, transform_draws = NULL, resolution = 100, select_points = 0, too_far = 0, probs = c(0.025, 0.975), robust = TRUE, newdata = NULL, ndraws = NULL, dpar = NULL, draw_ids = NULL, levels_id = NULL, resp = NULL, ipts = NULL, deriv = 0, summary = FALSE, model_deriv = NULL, idata_method = NULL, verbose = FALSE, label.x = NULL, label.y = NULL, label.title = NULL, label.subtitle = NULL, legendpos = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) plot_conditional_effects(model, ...)## S3 method for class 'bgmfit' plot_conditional_effects( model, effects = NULL, conditions = NULL, int_conditions = NULL, re_formula = NA, spaghetti = FALSE, surface = FALSE, categorical = FALSE, ordinal = FALSE, method = NULL, allow_new_levels = FALSE, estimation_method = "fitted", transform = NULL, transform_draws = NULL, resolution = 100, select_points = 0, too_far = 0, probs = c(0.025, 0.975), robust = TRUE, newdata = NULL, ndraws = NULL, dpar = NULL, draw_ids = NULL, levels_id = NULL, resp = NULL, ipts = NULL, deriv = 0, summary = FALSE, model_deriv = NULL, idata_method = NULL, verbose = FALSE, label.x = NULL, label.y = NULL, label.title = NULL, label.subtitle = NULL, legendpos = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) plot_conditional_effects(model, ...)
model |
An object of class |
effects |
An optional character vector naming effects (main effects or
interactions) for which to compute conditional plots. Interactions are
specified by a |
conditions |
An optional |
int_conditions |
An optional named |
re_formula |
A formula containing group-level effects to be considered
in the conditional predictions. If |
spaghetti |
Logical. Indicates if predictions should
be visualized via spaghetti plots. Only applied for numeric
predictors. If |
surface |
Logical. Indicates if interactions or
two-dimensional smooths should be visualized as a surface.
Defaults to |
categorical |
Logical. Indicates if effects of categorical
or ordinal models should be shown in terms of probabilities
of response categories. Defaults to |
ordinal |
(Deprecated) Please use argument |
method |
Method used to obtain predictions. Can be set to
|
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
estimation_method |
A character string specifying the estimation method
when calculating the velocity from the posterior draws. The |
transform |
A function or a character string naming
a function to be applied on the predicted responses
before summary statistics are computed. Only allowed
if |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
resolution |
Number of support points used to generate
the plots. Higher resolution leads to smoother plots.
Defaults to |
select_points |
Positive number.
Only relevant if |
too_far |
Positive number.
For surface plots only: Grid points that are too
far away from the actual data points can be excluded from the plot.
|
probs |
(Deprecated) The quantiles to be used in the computation of
uncertainty intervals. Please use argument |
robust |
If |
newdata |
An optional data frame for estimation. If |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
levels_id |
An optional argument to specify the |
resp |
A character string (default |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
deriv |
An integer indicating whether to estimate the distance curve or
its derivative (velocity curve). The default |
summary |
A logical value indicating whether only the estimate should be
computed ( |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
verbose |
A logical argument (default |
label.x |
An optional character string to label the x-axis. If
|
label.y |
An optional character string to label the y-axis. If
|
label.title |
An optional character string to label the title. Default
|
label.subtitle |
An optional character string to label the title. Default
|
legendpos |
A character string to specify the position of the legend.
If, the legend position is set to 'bottom' for distance and velocity curves
in the |
dummy_to_factor |
A named list (default
|
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
difx |
A character string (default |
idvar |
A character string (default |
itransform |
A character string (default |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The plot_conditional_effects() is a wrapper around the
brms::conditional_effects(). The brms::conditional_effects() function
from the brms package can be used to plot the fitted (distance) curve
when response (e.g., height) is not transformed. However, when the outcome
is log or square root transformed, the brms::conditional_effects() will
return the fitted curve on the log or square root scale, whereas the
plot_conditional_effects() will return the fitted curve on the
original scale. Furthermore, the plot_conditional_effects() also
plots the velocity curve on the original scale after making the required
back-transformation. Apart from these differences, both these functions
(brms::conditional_effects and plot_conditional_effects()) work
in the same manner. In other words, the user can specify all the arguments
which are available in the brms::conditional_effects(). An alternative
approach is to get_predictions() function (with plot = TRUE) which
is based on the marginaleffects.
An object of class 'brms_conditional_effects', which is a
named list with one data.frame per effect containing all information
required to generate conditional effects plots. See
brms::conditional_effects() for details.
Satpal Sandhu [email protected]
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance curve plot_conditional_effects(model, deriv = 0, re_formula = NA) # Individual-specific distance curves plot_conditional_effects(model, deriv = 0, re_formula = NULL) # Population average velocity curve plot_conditional_effects(model, deriv = 1, re_formula = NA) # Individual-specific velocity curves plot_conditional_effects(model, deriv = 1, re_formula = NULL)# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance curve plot_conditional_effects(model, deriv = 0, re_formula = NA) # Individual-specific distance curves plot_conditional_effects(model, deriv = 0, re_formula = NULL) # Population average velocity curve plot_conditional_effects(model, deriv = 1, re_formula = NA) # Individual-specific velocity curves plot_conditional_effects(model, deriv = 1, re_formula = NULL)
The plot_curves() function visualizes six different
types of growth curves using the ggplot2 package. Additionally, it
allows users to create customized plots from the data returned as a
data.frame. For an alternative approach, the get_predictions()
function can be used, which not only estimates adjusted curves but also
enables comparison across groups using the hypotheses argument.
## S3 method for class 'bgmfit' plot_curves( model, opt = "dv", apv = FALSE, bands = NULL, conf = 0.95, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, summary = FALSE, digits = 2, re_formula = NULL, numeric_cov_at = NULL, aux_variables = NULL, plot_cov = NULL, grid_add = NULL, levels_id = NULL, avg_reffects = NULL, ipts = NULL, model_deriv = TRUE, xrange = NULL, xrange_search = NULL, takeoff = FALSE, trough = FALSE, acgv = FALSE, acgv_velocity = 0.1, seed = 123, estimation_method = "fitted", allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, robust = FALSE, transform_draws = NULL, xaxis_breaks_fun = NULL, scale = c("response", "linear"), future = FALSE, future_session = "multisession", cores = NULL, trim = 0, layout = "single", linecolor = NULL, linecolor1 = NULL, linecolor2 = NULL, label.x = NULL, label.y = NULL, label.title = NULL, label.subtitle = NULL, legendpos = "bottom", linetype.apv = NULL, linewidth.main = NULL, linewidth.apv = NULL, linetype.groupby = NA, color.groupby = NULL, fill.groupby = NULL, band.alpha = NULL, band.legends = FALSE, show_age_takeoff = TRUE, show_age_peak = TRUE, show_age_cessation = TRUE, show_vel_takeoff = FALSE, show_vel_peak = FALSE, show_vel_cessation = FALSE, each_object = FALSE, ncol = NULL, print = TRUE, returndata = FALSE, returndata_add_parms = FALSE, parms_eval = FALSE, idata_method = NULL, parms_method = "getPeak", verbose = FALSE, fullframe = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) plot_curves(model, ...)## S3 method for class 'bgmfit' plot_curves( model, opt = "dv", apv = FALSE, bands = NULL, conf = 0.95, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, newdata = NULL, summary = FALSE, digits = 2, re_formula = NULL, numeric_cov_at = NULL, aux_variables = NULL, plot_cov = NULL, grid_add = NULL, levels_id = NULL, avg_reffects = NULL, ipts = NULL, model_deriv = TRUE, xrange = NULL, xrange_search = NULL, takeoff = FALSE, trough = FALSE, acgv = FALSE, acgv_velocity = 0.1, seed = 123, estimation_method = "fitted", allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, robust = FALSE, transform_draws = NULL, xaxis_breaks_fun = NULL, scale = c("response", "linear"), future = FALSE, future_session = "multisession", cores = NULL, trim = 0, layout = "single", linecolor = NULL, linecolor1 = NULL, linecolor2 = NULL, label.x = NULL, label.y = NULL, label.title = NULL, label.subtitle = NULL, legendpos = "bottom", linetype.apv = NULL, linewidth.main = NULL, linewidth.apv = NULL, linetype.groupby = NA, color.groupby = NULL, fill.groupby = NULL, band.alpha = NULL, band.legends = FALSE, show_age_takeoff = TRUE, show_age_peak = TRUE, show_age_cessation = TRUE, show_vel_takeoff = FALSE, show_vel_peak = FALSE, show_vel_cessation = FALSE, each_object = FALSE, ncol = NULL, print = TRUE, returndata = FALSE, returndata_add_parms = FALSE, parms_eval = FALSE, idata_method = NULL, parms_method = "getPeak", verbose = FALSE, fullframe = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) plot_curves(model, ...)
model |
An object of class |
opt |
A character string containing one or more of the following plotting options:
|
apv |
A logical value (default |
bands |
A character string containing one or more of the following
options, or
The |
conf |
A numeric value (default |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
newdata |
An optional data frame for estimation. If |
summary |
A logical value indicating whether only the estimate should be
computed ( |
digits |
An integer (default |
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
numeric_cov_at |
An optional (named list) argument to specify the value
of continuous covariate(s). The default |
aux_variables |
An optional argument to specify variables passed to the
|
plot_cov |
An optional character string to specify the |
grid_add |
An optional argument to specify the variable(s) that can be
passed to the |
levels_id |
An optional argument to specify the |
avg_reffects |
An optional argument (default |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
xrange |
An integer to set the predictor range (e.g., age) when
executing the interpolation via |
xrange_search |
A vector of length two or a character string
|
takeoff |
A logical value (default |
trough |
A logical value (default |
acgv |
A logical value (default |
acgv_velocity |
The percentage of the peak growth velocity to use when
estimating |
seed |
An integer (default |
estimation_method |
A character string specifying the estimation method
when calculating the velocity from the posterior draws. The |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
incl_autocor |
A flag indicating if correlation structures originally
specified via |
robust |
A logical value to specify the summary options. If |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
xaxis_breaks_fun |
A function (default |
scale |
Either |
future |
A logical value (default |
future_session |
A character string or a named list specifying the
parallel plan when
|
cores |
An integer specifying the number of cores for parallel execution.
|
trim |
A numeric value (default |
layout |
A character string defining the plot layout. The default
|
linecolor |
The color of the lines when the layout is |
linecolor1 |
The color of the first line when the layout is
|
linecolor2 |
The color of the second line when the layout is
|
label.x |
An optional character string to label the x-axis. If
|
label.y |
An optional character string to label the y-axis. If
|
label.title |
An optional character string to label the title. Default
|
label.subtitle |
An optional character string to label the title. Default
|
legendpos |
A character string to specify the position of the legend.
If, the legend position is set to 'bottom' for distance and velocity curves
in the |
linetype.apv |
A character string to specify the type of the vertical
line marking the APGV. Default |
linewidth.main |
A numeric value to specify the line width for distance
and velocity curves. The default |
linewidth.apv |
A numeric value to specify the width of the vertical
line marking the APGV. The default |
linetype.groupby |
A character string specifying the line type of
distance and velocity curves for |
color.groupby |
A character string specifying the line color of distance
and velocity curves for |
fill.groupby |
A character string specifying the fill color of distance
and velocity curves for |
band.alpha |
A numeric value to specify the transparency of the CI bands
around the curves. The default |
band.legends |
A logical value (default |
show_age_takeoff |
A logical value (default |
show_age_peak |
A logical value (default |
show_age_cessation |
A logical value (default |
show_vel_takeoff |
A logical value (default |
show_vel_peak |
A logical value (default |
show_vel_cessation |
A logical value (default |
each_object |
Optional logical (default |
ncol |
Optional integer (default |
print |
A logical value (default |
returndata |
A logical value (default |
returndata_add_parms |
A logical value (default |
parms_eval |
A logical value to specify whether or not to compute growth parameters on the fly. This is for internal use only and is mainly needed for compatibility across internal functions. |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
parms_method |
A character string specifying the method used when
evaluating |
verbose |
A logical argument (default |
fullframe |
A logical value indicating whether to return a
|
dummy_to_factor |
A named list (default
|
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
difx |
A character string (default |
idvar |
A character string (default |
itransform |
A character string (default |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The plot_curves() function is a generic tool for visualizing the following six curves:
Population average distance curve
Population average velocity curve
Individual-specific distance curves
Individual-specific velocity curves
Unadjusted individual growth curves (i.e., observed growth curves)
Adjusted individual growth curves (adjusted for the model-estimated random effects)
Internally, plot_curves() calls the growthparameters() function
to estimate and summarize the distance and velocity curves, as well as to
compute growth parameters such as the age at peak growth velocity (APGV).
The function also calls fitted_draws() or predict_draws() to make
inferences based on posterior draws. As a result, plot_curves()
can plot either fitted or predicted curves. For more details, see
fitted_draws() and predict_draws() to understand the difference between
fitted and predicted values.
A plot object (default) or a data.frame when returndata
= TRUE.
Satpal Sandhu [email protected]
growthparameters() fitted_draws() predict_draws()
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model is fit to # the 'berkeley_exdata' and saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance and velocity curves with default options plot_curves(model, opt = 'dv') # Individual-specific distance and velocity curves with default options # Note that \code{legendpos = 'none'} will suppress the legend positions. # This suppression is useful when plotting individual-specific curves plot_curves(model, opt = 'DV') # Population average distance and velocity curves with APGV plot_curves(model, opt = 'dv', apv = TRUE) # Individual-specific distance and velocity curves with APGV plot_curves(model, opt = 'DV', apv = TRUE) # Population average distance curve, velocity curve, and APGV with CI bands # To construct CI bands, growth parameters are first calculated for each # posterior draw and then summarized across draws. Therefore,summary # option must be set to FALSE plot_curves(model, opt = 'dv', apv = TRUE, bands = 'dvp', summary = FALSE) # Adjusted and unadjusted individual curves # Note ipts = NULL (i.e., no interpolation of predictor (i.e., age) to plot a # smooth curve). This is because it does not a make sense to interploate data # when estimating adjusted curves. Also, layout = 'facet' (and not default # layout = 'single') is used for the ease of visualizing the plotted # adjusted and unadjusted individual curves. However, these lines can be # superimposed on each other by setting the set layout = 'single'. # For other plots shown above, layout can be set as 'single' or 'facet' # Separate plots for adjusted and unadjusted curves (layout = 'facet') plot_curves(model, opt = 'au', ipts = NULL, layout = 'facet') # Superimposed adjusted and unadjusted curves (layout = 'single') plot_curves(model, opt = 'au', ipts = NULL, layout = 'single')# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model is fit to # the 'berkeley_exdata' and saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance and velocity curves with default options plot_curves(model, opt = 'dv') # Individual-specific distance and velocity curves with default options # Note that \code{legendpos = 'none'} will suppress the legend positions. # This suppression is useful when plotting individual-specific curves plot_curves(model, opt = 'DV') # Population average distance and velocity curves with APGV plot_curves(model, opt = 'dv', apv = TRUE) # Individual-specific distance and velocity curves with APGV plot_curves(model, opt = 'DV', apv = TRUE) # Population average distance curve, velocity curve, and APGV with CI bands # To construct CI bands, growth parameters are first calculated for each # posterior draw and then summarized across draws. Therefore,summary # option must be set to FALSE plot_curves(model, opt = 'dv', apv = TRUE, bands = 'dvp', summary = FALSE) # Adjusted and unadjusted individual curves # Note ipts = NULL (i.e., no interpolation of predictor (i.e., age) to plot a # smooth curve). This is because it does not a make sense to interploate data # when estimating adjusted curves. Also, layout = 'facet' (and not default # layout = 'single') is used for the ease of visualizing the plotted # adjusted and unadjusted individual curves. However, these lines can be # superimposed on each other by setting the set layout = 'single'. # For other plots shown above, layout can be set as 'single' or 'facet' # Separate plots for adjusted and unadjusted curves (layout = 'facet') plot_curves(model, opt = 'au', ipts = NULL, layout = 'facet') # Superimposed adjusted and unadjusted curves (layout = 'single') plot_curves(model, opt = 'au', ipts = NULL, layout = 'single')
This function generate various model diagnostic plots including residual-based diagnostics, MCMC convergence diagnostics, and posterior predictive checks. When multiple plots are requested, patch-able plots are combined into a single display using patchwork.
## S3 method for class 'bgmfit' plot_diagnostics( model, newdata = NULL, plots = c("rvf", "rvp", "qq", "qqn", "qqp", "pairs", "acf", "acfp", "acfr", "trace", "dens_overlay", "rhat", "rhat_hist", "neff", "ppc_overlay", "ppc_hist", "ppc_scatter", "ppc_stat"), set_draws = "epred", resp = NULL, ndraws = NULL, draw_ids = NULL, seed = 123, re_formula = NULL, dpar = NULL, variable = NULL, combine = TRUE, ncol = NULL, point_alpha = 0.15, bins = 30, regex = FALSE, smooth_se = FALSE, smooth_method = "lm", ppc_stat = "mean", rank_overlay = FALSE, add_plot_df = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, category = ".category", wrap_title = NULL, title_size = 12, each_object = FALSE, qq_plot_args = list(draw_ids = NULL, draw_ids_select = 1, summary = "mean", qq_type = "qq", seed = 123), patch_plot.margin = ggplot2::margin(10, 0, 10, 0), funlist = NULL, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, transform = NULL, transform_draws = NULL, itransform = NULL, model_deriv = NULL, dummy_to_factor = NULL, xvar = NULL, difx = NULL, idvar = NULL, levels_id = NULL, conf_level = 0.95, deriv = 0, plot = FALSE, method = "pkg", ipts = FALSE, idata_method = NULL, newdata_fixed = NULL, verbose = FALSE, envir = NULL, ... ) plot_diagnostics(model, ...)## S3 method for class 'bgmfit' plot_diagnostics( model, newdata = NULL, plots = c("rvf", "rvp", "qq", "qqn", "qqp", "pairs", "acf", "acfp", "acfr", "trace", "dens_overlay", "rhat", "rhat_hist", "neff", "ppc_overlay", "ppc_hist", "ppc_scatter", "ppc_stat"), set_draws = "epred", resp = NULL, ndraws = NULL, draw_ids = NULL, seed = 123, re_formula = NULL, dpar = NULL, variable = NULL, combine = TRUE, ncol = NULL, point_alpha = 0.15, bins = 30, regex = FALSE, smooth_se = FALSE, smooth_method = "lm", ppc_stat = "mean", rank_overlay = FALSE, add_plot_df = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, category = ".category", wrap_title = NULL, title_size = 12, each_object = FALSE, qq_plot_args = list(draw_ids = NULL, draw_ids_select = 1, summary = "mean", qq_type = "qq", seed = 123), patch_plot.margin = ggplot2::margin(10, 0, 10, 0), funlist = NULL, future = FALSE, future_session = "multisession", future_splits = TRUE, future_method = "future", future_re_expose = NULL, transform = NULL, transform_draws = NULL, itransform = NULL, model_deriv = NULL, dummy_to_factor = NULL, xvar = NULL, difx = NULL, idvar = NULL, levels_id = NULL, conf_level = 0.95, deriv = 0, plot = FALSE, method = "pkg", ipts = FALSE, idata_method = NULL, newdata_fixed = NULL, verbose = FALSE, envir = NULL, ... ) plot_diagnostics(model, ...)
model |
An object of class |
newdata |
Optional |
plots |
Character vector specifying which diagnostics to produce. Supported values are:
|
set_draws |
Character scalar indicating which fitted-value draw type to use for the residual-versus-fitted plot. Must be one of:
The tidybayes documentation recommends using expectation-scale
predictions from |
resp |
Optional name of the response variable to plot. This is primarily useful for multivariate models, where a specific response must often be selected. |
ndraws |
Optional integer specifying the number of posterior draws to use in residual-based diagnostics and posterior predictive checks. |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
seed |
Optional random seed used when subsampling posterior draws. |
re_formula |
Optional formula passed to prediction methods to control whether group-level effects are included. |
dpar |
Optional distributional parameter name passed to the underlying draw function when relevant. |
variable |
Optional character vector of parameter names for MCMC diagnostic plots such as trace, autocorrelation, and density overlays. |
combine |
Logical; if |
ncol |
Optional integer giving the number of columns to use when
combining multiple plots with patchwork. If |
point_alpha |
Numeric transparency level used for points in the residual-versus-fitted plot. |
bins |
Numeric value passed to |
regex |
Logical; Indicates whether |
smooth_se |
Logical; should the residual smoothing line display a standard-error ribbon. |
smooth_method |
Smoother passed to |
ppc_stat |
Summary statistic to use when |
rank_overlay |
Logical; if |
add_plot_df |
Optional logical (default |
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
category |
For some ordinal, multinomial, and multivariate models (notably, |
wrap_title |
Optional logical indicating whether to wrap plot title. If
|
title_size |
Text size of plot title. |
each_object |
Optional logical (default |
qq_plot_args |
A named list of arguments passed on to the
|
patch_plot.margin |
Optional setting the margins for the patchwork. |
funlist |
Currently ignored and serves as a placeholder. It will be implemented in future updates. |
future |
Currently ignored and serves as a placeholder. |
future_session |
Currently ignored and serves as a placeholder. |
future_splits |
Currently ignored and serves as a placeholder. |
future_method |
Currently ignored and serves as a placeholder. |
future_re_expose |
Currently ignored and serves as a placeholder. |
transform |
Currently ignored and serves as a placeholder. |
transform_draws |
Currently ignored and serves as a placeholder. |
itransform |
Currently ignored and serves as a placeholder. |
model_deriv |
Currently ignored and serves as a placeholder. |
dummy_to_factor |
Currently ignored and serves as a placeholder. |
xvar |
Currently ignored and serves as a placeholder. |
difx |
Currently ignored and serves as a placeholder. |
idvar |
Currently ignored and serves as a placeholder. |
levels_id |
An optional argument to specify the |
conf_level |
Currently ignored and serves as a placeholder. |
deriv |
Currently ignored and serves as a placeholder. |
plot |
Currently ignored and serves as a placeholder. |
method |
Currently ignored and serves as a placeholder. |
ipts |
Currently ignored and serves as a placeholder. |
idata_method |
Currently ignored and serves as a placeholder. |
newdata_fixed |
Currently ignored and serves as a placeholder. |
verbose |
A logical argument (default |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
This function provides a single interface to several common Bayesian model
diagnostics. Residual-based plots are constructed with tidybayes, using
posterior residual draws together with fitted-value draws from
tidybayes::add_epred_draws(), tidybayes::add_linpred_draws(), or
tidybayes::add_predicted_draws() MCMC diagnostics are produced through brms::mcmc_plot(), which is a wrapper around plotting functionality from
bayesplot. Posterior predictive checks are produced through
brms::pp_check()
Residual-based diagnostics are created by joining posterior residual draws
from tidybayes::add_residual_draws() with fitted-value draws from one of
tidybayes::add_epred_draws(), tidybayes::add_linpred_draws(), or
tidybayes::add_predicted_draws(). The resulting joined draws are used for
the residual-versus-fitted plot, and the residuals are summarized with
tidybayes::median_qi() before creating the Q-Q plot. The tidybayes
documentation notes that the default output column names depend on the draw
function: ".epred", ".linpred", ".prediction", and
".residual".
MCMC diagnostic plots are delegated to brms::mcmc_plot(), which provides a convenient wrapper around bayesplot diagnostics such as trace plots, autocorrelation plots, density overlays, R-hat, and effective sample size summaries.
Posterior predictive checks are delegated to brms::pp_check(), which
exposes bayesplot PPC types such as density overlays,
histograms, average scatter checks, and statistic-based checks.
A list. The returned structure depends on combine and the number of
plots requested:
combine = FALSE or only one plot is requestedA named list of plot objects.
combine = TRUE and multiple compatible plots are requestedA list with components:
combinedA patchwork object combining all patchable
plots.
plotsA named list containing all individual plot objects.
plot_dfThe joined residual/fitted draws data frame used for
residual-based plots, or NULL if no residual-based plots were
requested.
For Gaussian models, residual-versus-fitted plots and Q-Q plots are often useful summaries of residual behavior.
set_draws = "epred" is usually the best default because expected
predictions are often easier to interpret than linear predictors or raw
posterior predictive draws.
Satpal Sandhu [email protected]
brms::pp_check(), brms::mcmc_plot(),
tidybayes::add_residual_draws(), tidybayes::add_epred_draws(),
tidybayes::add_linpred_draws(), tidybayes::add_predicted_draws(),
patchwork::wrap_plots()
# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # Residual vs Fitted plot res1 <- plot_diagnostics( model = model, plots = "rvf" ) # MCMC diagnostics for selected parameters res2 <- plot_diagnostics( model = model, plots = c("trace", "dens_overlay"), variable = c("b_a_Intercept", "b_b_Intercept") ) res2$combined # Posterior predictive checks res3 <- plot_diagnostics( model = model, plots = c("ppc_overlay", "ppc_scatter"), ndraws = 10, ppc_stat = "sd" ) res3$combined # Use linear predictor instead of expected predictions res4 <- plot_diagnostics( model = model, plots = "rvf", set_draws = "linpred" )# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # Residual vs Fitted plot res1 <- plot_diagnostics( model = model, plots = "rvf" ) # MCMC diagnostics for selected parameters res2 <- plot_diagnostics( model = model, plots = c("trace", "dens_overlay"), variable = c("b_a_Intercept", "b_b_Intercept") ) res2$combined # Posterior predictive checks res3 <- plot_diagnostics( model = model, plots = c("ppc_overlay", "ppc_scatter"), ndraws = 10, ppc_stat = "sd" ) res3$combined # Use linear predictor instead of expected predictions res4 <- plot_diagnostics( model = model, plots = "rvf", set_draws = "linpred" )
Perform posterior predictive checks with the help of the bayesplot package.
## S3 method for class 'bgmfit' plot_ppc( model, type, ndraws = NULL, dpar = NULL, draw_ids = NULL, prefix = c("ppc", "ppd"), group = NULL, x = NULL, newdata = NULL, resp = NULL, size = 0.25, alpha = 0.7, trim = FALSE, bw = "nrd0", adjust = 1, kernel = "gaussian", n_dens = 1024, pad = TRUE, discrete = FALSE, binwidth = NULL, bins = NULL, breaks = NULL, freq = TRUE, y_draw = c("violin", "points", "both"), y_size = 1, y_alpha = 1, y_jitter = 0.1, verbose = FALSE, model_deriv = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, newdata_fixed = NULL, envir = NULL, ... ) plot_ppc(model, ...)## S3 method for class 'bgmfit' plot_ppc( model, type, ndraws = NULL, dpar = NULL, draw_ids = NULL, prefix = c("ppc", "ppd"), group = NULL, x = NULL, newdata = NULL, resp = NULL, size = 0.25, alpha = 0.7, trim = FALSE, bw = "nrd0", adjust = 1, kernel = "gaussian", n_dens = 1024, pad = TRUE, discrete = FALSE, binwidth = NULL, bins = NULL, breaks = NULL, freq = TRUE, y_draw = c("violin", "points", "both"), y_size = 1, y_alpha = 1, y_jitter = 0.1, verbose = FALSE, model_deriv = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, newdata_fixed = NULL, envir = NULL, ... ) plot_ppc(model, ...)
model |
An object of class |
type |
Type of the ppc plot as given by a character string.
See |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
prefix |
The prefix of the bayesplot function to be applied. Either '"ppc"' (posterior predictive check; the default) or '"ppd"' (posterior predictive distribution), the latter being the same as the former except that the observed data is not shown for '"ppd"'. |
group |
Optional name of a factor variable in the model
by which to stratify the ppc plot. This argument is required for
ppc |
x |
Optional name of a variable in the model.
Only used for ppc types having an |
newdata |
An optional data frame for estimation. If |
resp |
A character string (default |
size, alpha
|
Passed to the appropriate geom to control the appearance of the predictive distributions. |
trim |
A logical scalar passed to |
bw, adjust, kernel, n_dens
|
Optional arguments passed to
|
pad |
A logical scalar passed to |
discrete |
For |
binwidth |
Passed to |
bins |
Passed to |
breaks |
Passed to |
freq |
For histograms, |
y_draw |
For |
y_jitter, y_size, y_alpha
|
For |
verbose |
A logical argument (default |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
dummy_to_factor |
A named list (default
|
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The plot_ppc() function is a wrapper around the
brms::pp_check() function, which allows for the visualization of
posterior predictive checks.
A ggplot object that can be further customized using the
ggplot2 package.
Satpal Sandhu [email protected]
# Fit Bayesian SITAR model # To avoid mode estimation, which takes time, the Bayesian SITAR model is fit to # the 'berkeley_exdata' and saved as an example fit ('berkeley_exfit'). # See the 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit plot_ppc(model, ndraws = NULL)# Fit Bayesian SITAR model # To avoid mode estimation, which takes time, the Bayesian SITAR model is fit to # the 'berkeley_exdata' and saved as an example fit ('berkeley_exfit'). # See the 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit plot_ppc(model, ndraws = NULL)
The predict_draws() function is a wrapper around the
brms::predict.brmsfit() function, which obtains predicted values (and
their summary) from the posterior distribution. See
brms::predict.brmsfit() for details. An alternative approach is to
get_predictions() function which is based on the marginaleffects.
## S3 method for class 'bgmfit' predict_draws( model, newdata = NULL, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, re_formula = NA, allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, numeric_cov_at = NULL, levels_id = NULL, avg_reffects = NULL, aux_variables = NULL, grid_add = NULL, ipts = NULL, deriv = 0, model_deriv = TRUE, summary = TRUE, robust = FALSE, transform_draws = NULL, probs = c(0.025, 0.975), xrange = NULL, xrange_search = NULL, parms_eval = FALSE, parms_method = "getPeak", idata_method = NULL, verbose = FALSE, fullframe = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) predict_draws(model, ...)## S3 method for class 'bgmfit' predict_draws( model, newdata = NULL, resp = NULL, dpar = NULL, ndraws = NULL, draw_ids = NULL, re_formula = NA, allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, numeric_cov_at = NULL, levels_id = NULL, avg_reffects = NULL, aux_variables = NULL, grid_add = NULL, ipts = NULL, deriv = 0, model_deriv = TRUE, summary = TRUE, robust = FALSE, transform_draws = NULL, probs = c(0.025, 0.975), xrange = NULL, xrange_search = NULL, parms_eval = FALSE, parms_method = "getPeak", idata_method = NULL, verbose = FALSE, fullframe = NULL, dummy_to_factor = NULL, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, funlist = NULL, xvar = NULL, difx = NULL, idvar = NULL, itransform = NULL, newdata_fixed = NULL, envir = NULL, ... ) predict_draws(model, ...)
model |
An object of class |
newdata |
An optional data frame for estimation. If |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
ndraws |
A positive integer indicating the number of posterior draws to
use in estimation. If |
draw_ids |
An integer specifying the specific posterior draw(s) to use
in estimation (default |
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
incl_autocor |
A flag indicating if correlation structures originally
specified via |
numeric_cov_at |
An optional (named list) argument to specify the value
of continuous covariate(s). The default |
levels_id |
An optional argument to specify the |
avg_reffects |
An optional argument (default |
aux_variables |
An optional argument to specify the variable(s) that can
be passed to the |
grid_add |
An optional argument to specify the variable(s) that can be
passed to the |
ipts |
An integer specifying the number of points for interpolating the
predictor variable (e.g., age) to generate smooth curves for predictions
and plots. This value is used as the
This argument affects the following post-processing functions: |
deriv |
An integer indicating whether to estimate the distance curve or
its derivative (velocity curve). The default |
model_deriv |
A logical value specifying whether to estimate the
velocity curve from the derivative function or by differentiating the
distance curve. Set |
summary |
A logical value indicating whether only the estimate should be
computed ( |
robust |
A logical value to specify the summary options. If |
transform_draws |
A function applied to individual draws from the
posterior distribution before computing summaries (default |
probs |
The percentiles to be computed by the |
xrange |
An integer to set the predictor range (e.g., age) when
executing the interpolation via |
xrange_search |
A vector of length two or a character string
|
parms_eval |
A logical value to specify whether or not to compute growth parameters on the fly. This is for internal use only and is mainly needed for compatibility across internal functions. |
parms_method |
A character string specifying the method used when
evaluating |
idata_method |
A character string specifying the interpolation method
(default Available options:
If |
verbose |
A logical argument (default |
fullframe |
A logical value indicating whether to return a
|
dummy_to_factor |
A named list (default
|
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
funlist |
A list (default |
xvar |
A character string (default |
difx |
A character string (default |
idvar |
A character string (default |
itransform |
A character string (default |
newdata_fixed |
An indicator specifying how to handle the format and
structure of the user-provided
It is strongly recommended to leave both |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to the |
The predict_draws() function computes the expected values
from the posterior distribution. The brms::predict.brmsfit() function
from the brms package can be used to obtain predicted (distance)
values when the outcome (e.g., height) is untransformed. However, when the
outcome is log or square root transformed, the brms::predict.brmsfit()
function will return the expected curve on the log or square root scale. In
contrast, the predict_draws() function returns the expected values
on the original scale. Furthermore, predict_draws() also computes
the first derivative (velocity), again on the original scale, after making
the necessary back-transformation. Aside from these differences, both
functions (brms::predict.brmsfit() and predict_draws()) work
similarly. In other words, the user can specify all the options available
in brms::predict.brmsfit().
An array of predicted response values. See brms::predict.brmsfit()
for details.
Satpal Sandhu [email protected]
# Fit Bayesian SITAR model # To avoid mode estimation, which takes time, the Bayesian SITAR model is fit # to the 'berkeley_exdata' and saved as an example fit ('berkeley_exfit'). # See the 'bsitar' function for details on 'berkeley_exdata' and # berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance curve predict_draws(model, deriv = 0, re_formula = NA) # Individual-specific distance curves predict_draws(model, deriv = 0, re_formula = NULL) # Population average velocity curve predict_draws(model, deriv = 1, re_formula = NA) # Individual-specific velocity curves predict_draws(model, deriv = 1, re_formula = NULL)# Fit Bayesian SITAR model # To avoid mode estimation, which takes time, the Bayesian SITAR model is fit # to the 'berkeley_exdata' and saved as an example fit ('berkeley_exfit'). # See the 'bsitar' function for details on 'berkeley_exdata' and # berkeley_exfit'. # Check and confirm whether the model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Population average distance curve predict_draws(model, deriv = 0, re_formula = NA) # Individual-specific distance curves predict_draws(model, deriv = 0, re_formula = NULL) # Population average velocity curve predict_draws(model, deriv = 1, re_formula = NA) # Individual-specific velocity curves predict_draws(model, deriv = 1, re_formula = NULL)
Screen prior_sensitivity() results for parameters showing notable
prior-data conflict, likelihood conflict, or both. Parameters flagged for
both forms of sensitivity may indicate possible prior-data conflict in the
priorsense framework, while parameters flagged primarily for prior
sensitivity may reflect weak likelihood informativeness. See
prior_sensitivity() for details.
## S3 method for class 'bgmfit' prior_conflict( x, threshold_prior = 0.05, threshold_lik = 0.05, empty = "-", print = FALSE, return_table = TRUE, return_file = NULL, flex_table = FALSE, path = NULL, title = NULL, align = "center", sheet_name = "table", ... ) prior_conflict(x, ...)## S3 method for class 'bgmfit' prior_conflict( x, threshold_prior = 0.05, threshold_lik = 0.05, empty = "-", print = FALSE, return_table = TRUE, return_file = NULL, flex_table = FALSE, path = NULL, title = NULL, align = "center", sheet_name = "table", ... ) prior_conflict(x, ...)
x |
An object returned by |
threshold_prior |
Numeric threshold used to flag prior sensitivity. |
threshold_lik |
Numeric threshold used to flag likelihood sensitivity. |
empty |
A string that is used to replace |
print |
A logical to print the object. |
return_table |
A logical indicating whether to return the table
|
return_file |
Optional character string specifying the output type.
Supported values are |
flex_table |
A logical indicating whether to return the data frame
|
path |
Optional output file path. If |
title |
Optional title used in exported output where supported.
For Word and HTML output, named |
align |
Alignment used for Word export. Must be one of
|
sheet_name |
Character string giving the worksheet name for Excel
output. Default is |
... |
Ignored. |
This helper is designed as a lightweight screening step after
prior_sensitivity(). It does not replace graphical inspection. The
recommended workflow is:
Run prior_sensitivity() to compute sensitivity.
Use prior_conflict() to identify parameters of
potential concern.
Re-run prior_sensitivity() with plot = "dens",
plot = "ecdf", or plot = "quantities" for the
flagged variables.
Else, user can call prior_conflict() directly from within the
prior_sensitivity() by setting the argument return_conflicts as
TRUE. See prior_sensitivity() for details and examples.
A list when return_table = FALSE or a flextable if
return_table = TRUE
Character vector of variables flagged for prior sensitivity.
Character vector of all variables flagged for likelihood sensitivity.
Character vector of variables flagged for both prior and likelihood sensitivity
# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # Note return_table = FALSE which ensures correct output is returned from # the prior_sensitivity() ps <- prior_sensitivity( model = model, variable = c("b_a_Intercept", "sigma"), return_table = FALSE ) conflict <- prior_conflict(ps, return_table = TRUE) print(conflict) # Note that you can call prior_conflict() from within the prior_sensitivity() # with return_conflict = TRUE that resturns the same results as above, conflict2 <- prior_sensitivity( model = model, variable = c("b_a_Intercept", "sigma"), return_conflict = TRUE, return_table = TRUE ) stopifnot(identical(conflict, conflict2))# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # Note return_table = FALSE which ensures correct output is returned from # the prior_sensitivity() ps <- prior_sensitivity( model = model, variable = c("b_a_Intercept", "sigma"), return_table = FALSE ) conflict <- prior_conflict(ps, return_table = TRUE) print(conflict) # Note that you can call prior_conflict() from within the prior_sensitivity() # with return_conflict = TRUE that resturns the same results as above, conflict2 <- prior_sensitivity( model = model, variable = c("b_a_Intercept", "sigma"), return_conflict = TRUE, return_table = TRUE ) stopifnot(identical(conflict, conflict2))
Perform power-scaling sensitivity analysis for a fitted bsitar model.
Supports appending derived quantities to a posterior draws object as the
recommended priorsense workflow. Furthermore, prior_sensitivity
also allows visualization of sensitivity analysis plot(s).
## S3 method for class 'bgmfit' prior_sensitivity( model, variable = NULL, lower_alpha = 0.99, upper_alpha = 1.01, div_measure = "cjs_dist", measure_args = list(), component = c("prior", "likelihood"), sensitivity_threshold = 0.05, moment_match = FALSE, k_threshold = 0.5, resample = FALSE, transform = NULL, prediction = NULL, prior_selection = NULL, likelihood_selection = NULL, num_args = NULL, include_criterion = NULL, include_jointlik = NULL, include_predictor = NULL, name_predictor = NULL, plot = NULL, plot_variable = NULL, plot_print = FALSE, plot_return = FALSE, facet = NULL, empty = "-", print = FALSE, return_table = NULL, return_file = NULL, flex_table = FALSE, return_conflict = FALSE, digits = NULL, path = NULL, title = NULL, align = "center", sheet_name = "table", criterion = "bayes_R2", pointwise = FALSE, model_name = NULL, overwrite = FALSE, force_save = FALSE, resp = NULL, dpar = NULL, re_formula = NULL, allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, summary = FALSE, robust = FALSE, scale = c("response", "linear"), probs = c(0.025, 0.975), verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, xvar = NULL, envir = NULL, ... ) prior_sensitivity(model, ...)## S3 method for class 'bgmfit' prior_sensitivity( model, variable = NULL, lower_alpha = 0.99, upper_alpha = 1.01, div_measure = "cjs_dist", measure_args = list(), component = c("prior", "likelihood"), sensitivity_threshold = 0.05, moment_match = FALSE, k_threshold = 0.5, resample = FALSE, transform = NULL, prediction = NULL, prior_selection = NULL, likelihood_selection = NULL, num_args = NULL, include_criterion = NULL, include_jointlik = NULL, include_predictor = NULL, name_predictor = NULL, plot = NULL, plot_variable = NULL, plot_print = FALSE, plot_return = FALSE, facet = NULL, empty = "-", print = FALSE, return_table = NULL, return_file = NULL, flex_table = FALSE, return_conflict = FALSE, digits = NULL, path = NULL, title = NULL, align = "center", sheet_name = "table", criterion = "bayes_R2", pointwise = FALSE, model_name = NULL, overwrite = FALSE, force_save = FALSE, resp = NULL, dpar = NULL, re_formula = NULL, allow_new_levels = FALSE, sample_new_levels = "uncertainty", incl_autocor = TRUE, summary = FALSE, robust = FALSE, scale = c("response", "linear"), probs = c(0.025, 0.975), verbose = FALSE, expose_function = FALSE, usesavedfuns = NULL, clearenvfuns = NULL, xvar = NULL, envir = NULL, ... ) prior_sensitivity(model, ...)
model |
A fitted model object from |
variable |
Optional character vector of parameter or quantity names to
assess. Passed to |
lower_alpha |
Lower alpha value for gradient calculation. |
upper_alpha |
Upper alpha value for gradient calculation. |
div_measure |
Character (case sensitive) specifying the divergence measure to use. The following methods are implemented:
|
measure_args |
Named list of further arguments passed to divergence measure functions. |
component |
Character vector specifying component(s) to scale (default is both "prior" and "likelihood"). |
sensitivity_threshold |
Threshold for flagging variable as sensitive to power-scaling. |
moment_match |
Logical; Indicate whether or not moment
matching should be performed. Can only be TRUE if |
k_threshold |
Threshold value for Pareto k values above which the moment matching algorithm is used. Default is 0.5. |
resample |
Logical; Indicate whether or not draws should be resampled based on calculated importance weights. |
transform |
Indicate a transformation of posterior draws to perform before sensitivity analysis. Either "scale" or "whiten". |
prediction |
Function taking the model fit and returning a draws_df of predictions to be appended to the posterior draws |
prior_selection |
Vector specifying partitions of component to be
included in power-scaling. Default is NULL, which takes all
partitions. If this is a character, then it is appended to the
variable name (specified by |
likelihood_selection |
Vector specifying partitions of component to be
included in power-scaling. Default is NULL, which takes all
partitions. If this is a character, then it is appended to the
variable name (specified by |
num_args |
(named list) Optional arguments passed to
num() for pretty printing of summaries. Can be
controlled globally via the |
include_criterion |
Logical; if |
include_jointlik |
Logical; if |
include_predictor |
A character string or named list specifying
predictor-based quantities to append as |
name_predictor |
A character string or |
plot |
One of |
plot_variable |
Optional character string naming the variable to plot.
If |
plot_print |
Optional logical indicating whether to print plot object. |
plot_return |
Optional logical indicating whether to print plot object. |
facet |
Optional value passed to |
empty |
A string that is used to replace |
print |
A logical to print the object. |
return_table |
Optional logical indicating whether to return the raw
sensitivity object from |
return_file |
Optional character string specifying the output type.
Supported values are |
flex_table |
A logical indicating whether to return the data frame
|
return_conflict |
Optional logical indicating whether to return the raw
sensitivity results |
digits |
Optional integer to pass on to |
path |
Optional output file path. If |
title |
Optional title used in exported output where supported.
For Word and HTML output, named |
align |
Alignment used for Word export. Must be one of
|
sheet_name |
Character string giving the worksheet name for Excel
output. Default is |
criterion |
Names of model fit criteria
to compute. Currently supported are |
pointwise |
A flag indicating whether to compute the full
log-likelihood matrix at once or separately for each observation.
The latter approach is usually considerably slower but
requires much less working memory. Accordingly, if one runs
into memory issues, |
model_name |
Optional name of the model. If |
overwrite |
Logical; Indicates if already stored fit
indices should be overwritten. Defaults to |
force_save |
Logical; only relevant if |
resp |
A character string (default |
dpar |
Optional name of a predicted distributional parameter. If specified, expected predictions of this parameters are returned. |
re_formula |
Option to indicate whether or not to include
individual/group-level effects in the estimation. When |
allow_new_levels |
A flag indicating if new levels of group-level
effects are allowed (defaults to |
sample_new_levels |
Indicates how to sample new levels for grouping
factors specified in |
incl_autocor |
A flag indicating if correlation structures originally
specified via |
summary |
A logical value indicating whether only the estimate should be
computed ( |
robust |
A logical value to specify the summary options. If |
scale |
Either |
probs |
The percentiles to be computed by the |
verbose |
A logical argument (default |
expose_function |
A logical value or
Note: In the |
usesavedfuns |
A logical value (default |
clearenvfuns |
A logical value indicating whether to clear the exposed
Stan functions from the environment ( |
xvar |
A character string (default |
envir |
The environment used for function evaluation. The default is
|
... |
Additional arguments passed to
|
The prior_sensitivity function is intended as a package-level
convenience wrapper around priorsense::powerscale_sensitivity() and the
associated plotting tools.
If requested, the function also constructs an augmented draws object using
fit criterion (see argument include_criterion), joint log-likelihood
(see argument include_jointlik), or posterior expected predictions for
representative covariate values (see argument include_predictor). This
follows the general workflow recommended in the priorsense package.
The prior_sensitivity supports two closely related workflows for prior sensitivity analysis.
Standard workflow: The function calls
priorsense::powerscale_sensitivity() directly on the fitted bsitar
model and returns the sensitivity results. This is the simpler approach when
you only need sensitivity assessments for the model parameters themselves.
Extended workflow: A posterior draws object is created using
posterior::as_draws_df() which allows computing the additional derived
quantities. This extended draws object is useful when user wants to explore
prior sensitivity not only for model parameters but also for model-based fit
criteria and predictive quantities. The posterior draws object can be
extended with the following quantities:
Fit criteria via add_model_criterion(). Use the
include_criterion argument to specify which criteria to append.
Available options include "bayes_R2", "loo_R2",
"jointlik", and "margliklik". Note that for
"margliklik", the model should have fitted using save_pars =
save_pars(all = TRUE). See bsitar() for details on the
save_pars argument.
Joint log-likelihood (jointlik) quantity computed
using the custom jointlik_fun function. This captures the
joint likelihood of all observations under each posterior draw. See
include_jointlik argument for details.
Posterior expected predictions via
brms::posterior_epred(). Use the include_predictor argument
to specify predictor values for which to compute expected predictions.
See include_predictor argument for details.
Point-wise log-likelihood draws via
priorsense::log_lik_draws() (currently ignored)
Log-prior draws via priorsense::log_prior_draws()
(currently ignored)
Interpretation of sensitivity analysis results: In the priorsense framework, the pattern of sensitivity to prior and likelihood perturbations provides diagnostic information about the Prior-data conflict and the strength of likelihood:
Prior-data conflict: When sensitivity to both prior perturbations and likelihood perturbations is strong, it indicates a prior-data conflict. The prior information is inconsistent with what the data suggest.
Weakly informative likelihood: When sensitivity to prior perturbations is strong but sensitivity to likelihood perturbations is weak, this suggests that the likelihood is only weakly informative for that quantity. The data provide limited information relative to the prior.
These diagnostics help user to assess whether prior choices are appropriate and whether certain quantities are well-identified by the data.
When return_table = TRUE, a table of sensitivity values for each
specified variable is returned. If return_table = FALSE, a
"prior_sensitivity" object is returned as a list with the following
components:
The extracted bsitar object.
The augmented draws object if any of
include_criterion, include_jointlik, or
include_predictor are specified, otherwise NULL.
The object returned by
priorsense::powerscale_sensitivity().
A plot object, or list of plot objects when
plot = "both", otherwise NULL.
The requested plot type, if any.
The plotted variable, if any.
The matched function call.
priorsense::powerscale_sensitivity(),
priorsense::powerscale_plot_dens(),
priorsense::powerscale_plot_ecdf(),
priorsense::powerscale_plot_quantities(),
priorsense::predictions_as_draws(),
prior_conflict()
# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # Basic workflow: use brmsfit support directly ps <- prior_sensitivity( model = model, variable = c("b_a_Intercept", "sigma") ) # Directly request a density plot ps2 <- prior_sensitivity( model = model, variable = c("sigma"), plot = "dens" ) ps2$plot # Extended workflow with derived predictive quantities # In `variable` and `plot_variable`, the custom variables `bayes_R2`, # `jointlik`, `agemin`, and `agemax` are requested via the `criterion` # and `include_predictor` arguments. Here, the predictor of interest is # `age`, so `min` and `max` are prefixed with `age`, giving `agemin` # and `agemax`. The predictor `age` is automatically inferred from the # `model` object, but it can also be specified explicitly using `xvar`. ps3 <- prior_sensitivity( model = model, variable = c("sigma", "bayes_R2", "jointlik", "agemin", "agemax"), criterion = c("bayes_R2", "jointlik"), include_predictor = c("min", "max"), plot = "ecdf", plot_variable = c("sigma", "bayes_R2", "jointlik", "agemin", "agemax") ) # Note that user can also get conflict by call `prior_conflict()` from within # the `prior_sensitivity()` by setting return_conflict = TRUE conflict <- prior_sensitivity( model = model, variable = c("b_a_Intercept", "sigma"), return_conflict = TRUE, return_table = TRUE )# Fit Bayesian SITAR model # To avoid model estimation which can take time, the Bayesian SITAR model fit # to the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. model <- getNsObject(berkeley_exfit) # Basic workflow: use brmsfit support directly ps <- prior_sensitivity( model = model, variable = c("b_a_Intercept", "sigma") ) # Directly request a density plot ps2 <- prior_sensitivity( model = model, variable = c("sigma"), plot = "dens" ) ps2$plot # Extended workflow with derived predictive quantities # In `variable` and `plot_variable`, the custom variables `bayes_R2`, # `jointlik`, `agemin`, and `agemax` are requested via the `criterion` # and `include_predictor` arguments. Here, the predictor of interest is # `age`, so `min` and `max` are prefixed with `age`, giving `agemin` # and `agemax`. The predictor `age` is automatically inferred from the # `model` object, but it can also be specified explicitly using `xvar`. ps3 <- prior_sensitivity( model = model, variable = c("sigma", "bayes_R2", "jointlik", "agemin", "agemax"), criterion = c("bayes_R2", "jointlik"), include_predictor = c("min", "max"), plot = "ecdf", plot_variable = c("sigma", "bayes_R2", "jointlik", "agemin", "agemax") ) # Note that user can also get conflict by call `prior_conflict()` from within # the `prior_sensitivity()` by setting return_conflict = TRUE conflict <- prior_sensitivity( model = model, variable = c("b_a_Intercept", "sigma"), return_conflict = TRUE, return_table = TRUE )
The update_model() function is a wrapper around the
update() function from the brms package, which refits the
model based on the user-specified updated arguments.
## S3 method for class 'bgmfit' update_model( model, newdata = NULL, recompile = NULL, expose_function = FALSE, verbose = FALSE, check_newargs = FALSE, new_threads = FALSE, envir = NULL, ... ) update_model(model, ...)## S3 method for class 'bgmfit' update_model( model, newdata = NULL, recompile = NULL, expose_function = FALSE, verbose = FALSE, check_newargs = FALSE, new_threads = FALSE, envir = NULL, ... ) update_model(model, ...)
model |
An object of class |
newdata |
An optional |
recompile |
A logical value indicating whether the Stan model should be
recompiled. When |
expose_function |
A logical value or
Note: In the |
verbose |
A logical argument (default |
check_newargs |
A logical value (default |
new_threads |
A logical (default |
envir |
The environment used for function evaluation. The default is
|
... |
Other arguments passed to |
This function is an adapted version of the update() function from the brms package.
An updated object of class bgmfit.
Satpal Sandhu [email protected]
# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Update model # Note that in case all arguments supplied to the update_model() call are # same as the original model fit (checked via check_newargs = TRUE), then # original model object is returned. # To explicitly get this information whether model is being updated or not, # user can set verbose = TRUE. The verbose = TRUE also useful in getting the # information regarding what all arguments have been changed as compared to # the original model. model2 <- update_model(model, df = 5, check_newargs = TRUE, verbose = TRUE)# Fit Bayesian SITAR model # To avoid mode estimation which takes time, the Bayesian SITAR model fit to # the 'berkeley_exdata' has been saved as an example fit ('berkeley_exfit'). # See 'bsitar' function for details on 'berkeley_exdata' and 'berkeley_exfit'. # Check and confirm whether model fit object 'berkeley_exfit' exists berkeley_exfit <- getNsObject(berkeley_exfit) model <- berkeley_exfit # Update model # Note that in case all arguments supplied to the update_model() call are # same as the original model fit (checked via check_newargs = TRUE), then # original model object is returned. # To explicitly get this information whether model is being updated or not, # user can set verbose = TRUE. The verbose = TRUE also useful in getting the # information regarding what all arguments have been changed as compared to # the original model. model2 <- update_model(model, df = 5, check_newargs = TRUE, verbose = TRUE)