| Title: | Implementation of Logistic Box-Cox Regression |
|---|---|
| Description: | Implements a logistic Box-Cox model that adds a shape parameter to a routine logistic regression model to flexibly estimate the shape and strength of the relationship between a binary outcome and a continuous predictor, adjusting for covariates and survey weights. This model is fully described in Xing, L. et al. (2021) <doi:10.1002/cjs.11587>. This version extends the original 'lboxcox' package (1.1) with numerically stabilized likelihood/gradient calculations, vectorized data preprocessing, and a bootstrap-ensemble estimator. |
| Authors: | Li Xing [cre, aut], Shiyu Xu [aut], Jing Wang [aut], Kohlton Booth [aut], Xuekui Zhang [aut], Igor Burstyn [aut], Paul Gustafson [aut] |
| Maintainer: | Li Xing <[email protected]> |
| License: | GPL-3 |
| Version: | 2.0.1 |
| Built: | 2026-11-04 07:53:13 UTC |
| Source: | https://github.com/cran/lboxcox |
The depress data frame contains 8,893 adults aged 20 years or older from the 2005-2006 and 2007-2008 National Health and Nutrition Examination Survey (NHANES) cycles.
depressdepress
Sample survey data with columns:
binary response variable equal to 1 for a Patient Health Questionnaire-9 (PHQ-9) score of at least 10 and 0 otherwise
total blood mercury concentration in micrograms per litre
age of the participant in years
0 if the participant is female and 1 if the participant is male
four-year Day 1 dietary sampling weight, formed as WTDRD1 / 2 for the two combined NHANES cycles
National Center for Health Statistics, NHANES 2005-2006 and 2007-2008. The analytic data were adapted from the data used by Xing, L., Zhang, X., Burstyn, I., & Gustafson, P. (2021). On logistic Box-Cox regression for flexibly estimating the shape and strength of exposure-disease relationships. Canadian Journal of Statistics, 49(3), 808-825.
Computes the sum of absolute signed deviance residuals between binary
outcomes and predicted probabilities. Used as the cross-validation
criterion in lboxcox_cv.fit and, more generally, as a
prediction-error summary for comparing fitted models.
devr(y.bin, y.pred)devr(y.bin, y.pred)
y.bin |
binary (0/1) outcome vector. |
y.pred |
predicted probabilities, same length as |
a single numeric value: the sum of absolute deviance residuals.
Trains the given formula using a logistic Box-Cox model whose parameters
(intercept, slope, Box-Cox shape, and covariate coefficients) are found by
maximizing the log-likelihood directly with maxLik::maxLik. If the
resulting lambda estimate falls outside , falls back to the
best of a grid of svyglm fits at different lambda values (see
lbc_train_ms for a version that always searches the grid).
lbc_maxlik( formula, weight_column_name, data, init = NULL, svy_lambda_vector = seq(0, 2, length = 4), init_lambda_vector = seq(0, 2, length = 100), num_cores = 1, seed = NULL )lbc_maxlik( formula, weight_column_name, data, init = NULL, svy_lambda_vector = seq(0, 2, length = 4), init_lambda_vector = seq(0, 2, length = 100), num_cores = 1, seed = NULL )
formula |
a formula of the form |
weight_column_name |
the name of the column in |
data |
dataframe containing the dataset to train on. |
init |
initial estimates for the coefficients. If |
svy_lambda_vector |
values of lambda used in training the |
init_lambda_vector |
values of lambda used, as a fallback, to find the
best-log-likelihood starting point if the direct maximization does not
converge to a lambda in |
num_cores |
the number of cores used when searching |
seed |
deprecated compatibility argument; ignored because the BFGS
optimizer is deterministic. Control bootstrap reproducibility by calling
|
object of class maxLik from the maxLik package (or,
on fallback, of class svyglm). Contains the coefficient estimates
in $estimate (named Beta_0, Beta_1, Lambda,
then covariates) and a convergence flag in $error.
This is reliant on the following work:
Henningsen, A., Toomet, O. (2011). maxLik: A package for maximum likelihood estimation in R. Computational Statistics, 26(3), 443-458.
lbc_train_ms
Fits lbc_train_ms on each of 100 bootstrap resamples of
data in parallel, takes the median of the per-resample lambda
estimates, and refits the final model at that lambda on the full data.
More expensive than lbc_train_bagging (each resample itself
searches a lambda grid via lbc_train_ms) but more robust to
poor starting values within each resample.
Bootstrap samples use the caller's current random-number state. Call
set.seed() before this function when reproducible resamples are
required.
lbc_train_all( formula, weight_column_name, data, init = NULL, svy_lambda_vector = seq(0, 2, length = 100), num_cores = 1, cores = 5 )lbc_train_all( formula, weight_column_name, data, init = NULL, svy_lambda_vector = seq(0, 2, length = 100), num_cores = 1, cores = 5 )
formula |
a formula of the form |
weight_column_name |
the name of the column in |
data |
dataframe containing the dataset to train on. |
init |
initial estimates for the coefficients. If |
svy_lambda_vector |
values of lambda used to build the grid of
starting vectors that |
num_cores |
the number of cores used when searching |
cores |
number of cores to parallelize the bootstrap resamples over. |
a list of length 3: the final refit model (as returned by
lbc_train_ms), the list of per-resample fits (NA
for any resample where lbc_train_ms errored), and the
count of resamples that errored.
This is reliant on the following work:
Microsoft Corporation, Weston, S. (2020). foreach: Provides Foreach Looping Construct. R package version 1.5.1.
Microsoft Corporation, Weston, S. (2020). doParallel: Foreach Parallel Adaptor for the 'parallel' Package. R package version 1.0.16.
lbc_maxlik
Fits lbc_maxlik on each of 100 bootstrap resamples of
data in parallel, takes the median of the per-resample lambda
estimates, and refits the final model at that lambda on the full data.
Bootstrap samples use the caller's current random-number state. Call
set.seed() before this function when reproducible resamples are
required.
lbc_train_bagging( formula, weight_column_name, data, init = NULL, svy_lambda_vector = seq(0, 2, length = 4), init_lambda_vector = seq(0, 2, length = 100), num_cores = 1, cores = 10 )lbc_train_bagging( formula, weight_column_name, data, init = NULL, svy_lambda_vector = seq(0, 2, length = 4), init_lambda_vector = seq(0, 2, length = 100), num_cores = 1, cores = 10 )
formula |
a formula of the form |
weight_column_name |
the name of the column in |
data |
dataframe containing the dataset to train on. |
init |
initial estimates for the coefficients. If |
svy_lambda_vector |
values of lambda used in training the |
init_lambda_vector |
values of lambda used, as a fallback, to find the
best-log-likelihood starting point if the direct maximization does not
converge to a lambda in |
num_cores |
the number of cores used when searching |
cores |
number of cores to parallelize the bootstrap resamples over. |
a list of length 3: the final refit model (as returned by
lbc_maxlik/its fallback), the list of per-resample fits
(NA for any resample where lbc_maxlik errored), and
the count of resamples that errored.
This is reliant on the following work:
Microsoft Corporation, Weston, S. (2020). foreach: Provides Foreach Looping Construct. R package version 1.5.1.
Microsoft Corporation, Weston, S. (2020). doParallel: Foreach Parallel Adaptor for the 'parallel' Package. R package version 1.0.16.
Trains the given formula using a logistic Box-Cox model by running
maxLik::maxLik from a grid of svyglm-based starting vectors
(one per entry of svy_lambda_vector) and keeping the fit with the
highest achieved log-likelihood. More expensive than
lbc_maxlik but more robust to a poor single starting point.
lbc_train_ms( formula, weight_column_name, data, init = NULL, svy_lambda_vector = seq(0, 2, length = 100), num_cores = 1 )lbc_train_ms( formula, weight_column_name, data, init = NULL, svy_lambda_vector = seq(0, 2, length = 100), num_cores = 1 )
formula |
a formula of the form |
weight_column_name |
the name of the column in |
data |
dataframe containing the dataset to train on. |
init |
initial estimates for the coefficients. If |
svy_lambda_vector |
values of lambda used to build the grid of
starting vectors that |
num_cores |
the number of cores used when searching |
the refit svyglm model at the selected lambda, with
$estimate (named Beta_0, Beta_1, Lambda, then
covariates) and a convergence flag in $error; error = 1
means no continuous BFGS run converged and the exact profile grid was
used as a fallback.
This is reliant on the following work:
Henningsen, A., Toomet, O. (2011). maxLik: A package for maximum likelihood estimation in R. Computational Statistics, 26(3), 443-458.
Trains the given formula using a logistic Box-Cox model whose lambda is
chosen, instead of by likelihood maximization, as the value in
lambda_vector with lowest mean k-fold cross-validated
devr (deviance residual prediction error). The model is then
refit on the full data at the selected lambda.
lboxcox_cv.fit( mydata, ixx, iyy, formula, weight_column_name = NULL, lambda_vector = seq(0, 2, length = 100), k )lboxcox_cv.fit( mydata, ixx, iyy, formula, weight_column_name = NULL, lambda_vector = seq(0, 2, length = 100), k )
mydata |
data frame containing the dataset to train on. |
ixx |
the (untransformed) primary predictor vector, e.g. |
iyy |
the binary outcome vector, e.g. |
formula |
a formula of the form |
weight_column_name |
the name of the column in |
lambda_vector |
grid of Box-Cox lambda values to select from. |
k |
number of cross-validation folds. |
the refit svyglm model at the selected lambda, with
$estimate (named Beta_0, Beta_1, Lambda, then
covariates) attached.
lboxcox_cv.fit modelPredict from a lboxcox_cv.fit model
lboxcox_cv.predict(myCVfit, newdata, formula)lboxcox_cv.predict(myCVfit, newdata, formula)
myCVfit |
a model fit by |
newdata |
data frame of new observations to predict on. |
formula |
the same formula used to fit |
a numeric vector of predicted probabilities.
Averages predicted probabilities across the (up to 100) per-resample fits
returned as the second element of lbc_train_bagging or
lbc_train_all, skipping resamples that failed to converge.
lboxcox_maxLik_el.predict(myMaxLik_elfit, newdata, formula)lboxcox_maxLik_el.predict(myMaxLik_elfit, newdata, formula)
myMaxLik_elfit |
the length-3 list returned by
|
newdata |
data frame of new observations to predict on. |
formula |
the same formula used to fit |
a numeric vector of predicted probabilities, averaged over the converged bootstrap resamples.
Produces predicted probabilities for newdata from a model fit by
lbc_maxlik, lbc_train_ms, or the first
($estimate-bearing) element returned by lbc_train_bagging
or lbc_train_all.
lboxcox_maxLik.predict(myMaxLikfit, newdata, formula)lboxcox_maxLik.predict(myMaxLikfit, newdata, formula)
myMaxLikfit |
a fitted model with a named |
newdata |
data frame of new observations to predict on. |
formula |
the same formula used to fit |
a numeric vector of predicted probabilities.
Calculates a number that represents the overall gradient measurement
between the (log-scale) predictor and the log-odds of the outcome,
evaluated at the survey-weighted mean of log(x), along with a
Wald-type 95% confidence interval derived from the model's Hessian.
median_effect(formula, weight_column_name, data, trained_model)median_effect(formula, weight_column_name, data, trained_model)
formula |
the formula used to train the logistic Box-Cox model. |
weight_column_name |
the name of the column in |
data |
dataframe containing the dataset the model was trained on. |
trained_model |
the already-trained model, e.g. the output of
|
a named numeric vector with elements median effect,
lower 95% ci, and upper 95% ci.