| Title: | Bivariate Logistic Box-Cox Regression |
|---|---|
| Description: | Fits bivariate logistic Box-Cox regression models for binary outcomes and positive continuous predictors. Transformation parameters are selected by cross-validated grid search with adaptive refinement and thin-plate spline smoothing. The package also provides prediction, empirical and sampling-weighted median effects, simulation tools, and sampling-weighted model fitting. The methodology extends the logistic Box-Cox approach of Xing et al. (2021) <doi:10.1002/cjs.11587>. |
| Authors: | Shiyu Xu [aut], Xuekui Zhang [aut, cre] |
| Maintainer: | Xuekui Zhang <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.4 |
| Built: | 2026-09-26 09:58:12 UTC |
| Source: | https://github.com/cran/mvboxcox |
A sampling-weighted analytic dataset derived from the 2005-2006 and 2007-2008 cycles of the US National Health and Nutrition Examination Survey (NHANES). The data are used in the bivariate logistic Box-Cox application described by Xu and Zhang.
depressdepress
A data frame with 8,893 rows and 6 variables:
Integer indicator: 1 for a Patient Health Questionnaire-9 (PHQ-9) score of at least 10, and 0 otherwise.
Total blood mercury concentration in micrograms per liter (mcg/L).
Blood lead concentration in micrograms per deciliter (mcg/dL).
Age in years.
Integer indicator: 1 for male and 0 for female.
Four-year Day 1 dietary sampling weight, constructed for the combined NHANES cycles.
The analytic sample contains adults aged 20 years or older with a valid
Day 1 dietary sampling weight, complete PHQ-9 information, and nonmissing
positive blood mercury and blood lead measurements. The package's
survey-weighted implementation uses weight but does not incorporate
NHANES strata or primary sampling-unit identifiers, so analyses should be
interpreted as sampling-weighted rather than as complete design-based
survey analyses.
National Center for Health Statistics, National Health and Nutrition Examination Survey, 2005-2006 and 2007-2008 cycles.
Xu, S., & Zhang, X. Bivariate logistic Box-Cox regression for interpretable nonlinear exposure-response modeling. Manuscript.
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.
data(depress) str(depress) summary(depress)data(depress) str(depress) summary(depress)
if (li == 0) log(xi) else (xi**li - 1)/li
ld(xi, li)ld(xi, li)
xi |
positive numeric values to be Box-Cox transformed |
li |
i-th lambda value |
numeric vector of Box-Cox transformed values
Apply a set of lambda exponents to the positive predictor terms of formulaA
(via ld()) and combine with the untransformed terms of formulaB into a single
formula suitable for glm().
mvbc.fullformula(formulaA, formulaB, lambdas)mvbc.fullformula(formulaA, formulaB, lambdas)
formulaA |
a formula containing the binary outcome and the positive continuous predictors to be Box-Cox transformed |
formulaB |
an optional one-sided formula containing additional
covariates that remain untransformed, or |
lambdas |
vector of lambda exponents, one per term in |
a formula object with the positive predictor terms of formulaA replaced by
ld(term, lambda) and the terms of formulaB appended
Fits glm() to train at a given set of lambda exponents (weighted, if
survey = TRUE and weights is supplied) and, if test is supplied,
scores the fit on test and attaches sspr/ssdr (see mvbc.trainer.ssr()) to the
returned model object.
mvbc.glm.step( formulaA, formulaB = NULL, train, test = NULL, lambdas, weights = NULL, test_weights = NULL, survey = FALSE )mvbc.glm.step( formulaA, formulaB = NULL, train, test = NULL, lambdas, weights = NULL, test_weights = NULL, survey = FALSE )
formulaA |
a formula containing the binary outcome and the positive continuous predictors to be Box-Cox transformed |
formulaB |
an optional one-sided formula containing additional
covariates that remain untransformed, or |
train |
training data frame |
test |
optional test data frame to score the fit on |
lambdas |
vector of lambda exponents, one per term in |
weights |
optional vector of observation weights aligned with the rows of |
test_weights |
optional vector of observation weights aligned with the rows of |
survey |
logical; if |
the fitted glm object, with sspr and ssdr attached when test is supplied
Computes coefficient-like median effects for the positive predictors in a
model fitted by mvbc.train(). The calculation uses the empirical median,
or the sampling-weighted empirical median when weights is supplied, and
therefore does not require a log-normal distribution.
mvbc.median.effect(object, data, q = 1, weights = NULL, na.rm = TRUE)mvbc.median.effect(object, data, q = 1, weights = NULL, na.rm = TRUE)
object |
a fitted |
data |
data frame containing the positive predictors used in
|
q |
numeric reference-transformation value; either a scalar or one value per transformed predictor |
weights |
optional nonnegative sampling-weight vector aligned with the
rows of |
na.rm |
logical; if |
For predictor and reference transformation , the median
effect is
where is the marginal median of . The plug-in estimator
replaces , , and with their fitted or
empirical counterparts. If is log-normal with location parameter
, then and the expression reduces to
.
With weights, medians are calculated as weighted empirical 0.5 quantiles:
values are sorted ascending and the median is the value at which the
weighted empirical CDF first reaches 0.5 (equivalent to a one-stage survey
design with ids = ~1). The returned effects are on the chosen
reference-transformation scale. Comparisons between predictors remain
dependent on their measurement units.
A data frame with one row per transformed predictor and columns
predictor, beta, lambda, q, median, and median_effect.
fit <- structure( list( formulaA = y ~ x1 + x2, lambda.fits = data.frame(x1 = 0.5, x2 = 1), beta.fits = data.frame( foldid = 0L, intercept = -1, x1 = 2, x2 = -0.5 ) ), class = "mvbc.train.model" ) dat <- data.frame( y = c(0, 0, 1, 1, 1), x1 = c(1, 2, 3, 7, 12), x2 = c(2, 3, 4, 5, 8) ) mvbc.median.effect(fit, dat, q = 1)fit <- structure( list( formulaA = y ~ x1 + x2, lambda.fits = data.frame(x1 = 0.5, x2 = 1), beta.fits = data.frame( foldid = 0L, intercept = -1, x1 = 2, x2 = -0.5 ) ), class = "mvbc.train.model" ) dat <- data.frame( y = c(0, 0, 1, 1, 1), x1 = c(1, 2, 3, 7, 12), x2 = c(2, 3, 4, 5, 8) ) mvbc.median.effect(fit, dat, q = 1)
The Multivariate Boxcox Optimizer accepts an instance of an
mvbc.trainer.model and uses Thin Plate Splines (TPS) to search for more
optimal lambda estimates.
mvbc.optimizer( model, data, tpsargs = list(scale.type = "unscaled"), optimargs = list(method = "L-BFGS-B", lower = 0, upper = 2), weights = NULL, survey = FALSE )mvbc.optimizer( model, data, tpsargs = list(scale.type = "unscaled"), optimargs = list(method = "L-BFGS-B", lower = 0, upper = 2), weights = NULL, survey = FALSE )
model |
an instance of an |
data |
a data frame containing the variables in the model |
tpsargs |
list of optional arguments to pass to |
optimargs |
list of optional arguments to pass to |
weights |
optional vector of observation weights (e.g. survey weights) aligned with
the rows of |
survey |
logical; if |
The mvbc.optimizer() function performs the following:
Starting with an instance of an mvbc.trainer.model repeat the following steps
for each minima value in the instance.
Generate a Thin Plate Spline (TPS) surface using the lambda grid as the independent x variables in the TPS, and the some-of-square residual minima as the dependent y variable.
Use the general purpose optim() function to choose updated estimates of lambda to
minimize each of the some-of-square residuals.
The updated lambda estimates are used to fit another set of beta values and compute
some-of-square residuals as is done in the original mvbc.trainer().
Append the updated lambda and beta estimates and residuals to the existing training results. The updated estimates may or may not have improved residuals.
mvbc.optimizer() returns an updated instance of the class mvbc.trainer.model
as defined in the original mvbc.trainer().
vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) summary(sim) # ignore "glm.fit: fitted probabilities numerically 0 or 1 occurred" suppressWarnings({ trained = mvbc.trainer( Ybin ~ mercury + lead, ~ age, sim$data, griddomain = seq(0, 2, length.out = 4), K = 3 ) }) trained = mvbc.optimizer(trained, sim$data) summary(trained)vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) summary(sim) # ignore "glm.fit: fitted probabilities numerically 0 or 1 occurred" suppressWarnings({ trained = mvbc.trainer( Ybin ~ mercury + lead, ~ age, sim$data, griddomain = seq(0, 2, length.out = 4), K = 3 ) }) trained = mvbc.optimizer(trained, sim$data) summary(trained)
mvbc.train.model
Predict on test data using a model fitted by mvbc.train(),
i.e. one whose lambda.fits and beta.fits have already been resolved to a single
chosen lambda tuple (unlike mvbc.trainer.predict.p_hat(), which predicts from a
specific tupleid inside an mvbc.trainer.model that may hold many candidate tuples).
mvbc.predict(trainModel, data)mvbc.predict(trainModel, data)
trainModel |
instance of the class |
data |
a data frame containing the test data |
Given the definition of and
as shown mvbc.simulator(),
mvbc.predict():
Builds the full formula using the chosen in trainModel$lambda.fits.
Builds a model matrix from data using that formula.
Uses the full-data estimate in trainModel$beta.fits
to compute and
.
Returns the estimated probabilities.
mvbc.predict() returns a vector of estimated probabilities, one per row of data.
vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") trainset = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) testset = mvbc.simulator(simModel = trainset) suppressWarnings({ trained = mvbc.train( Ybin ~ mercury + lead, ~ age, trainset$data, griddomain = seq(0, 2, length.out = 4), K = 3 ) }) p_hat = mvbc.predict(trained, testset$data) head(p_hat)vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") trainset = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) testset = mvbc.simulator(simModel = trainset) suppressWarnings({ trained = mvbc.train( Ybin ~ mercury + lead, ~ age, trainset$data, griddomain = seq(0, 2, length.out = 4), K = 3 ) }) p_hat = mvbc.predict(trained, testset$data) head(p_hat)
The Multivariate Boxcox Simulator mvbc.simulator generates binomial samples from
a logistic model built from both Boxcox transformed log normal random variables and normal random variables.
The simulator is given a set of model parameters, and returns a class object with the parameters,
the random variables, the logistic output and the binomial samples .
The simulator includes controlled seeding to allow for generation of the same sequence of simulated data each time.
mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, n, seed, simModel = NULL)mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, n, seed, simModel = NULL)
vLambda |
vector of Boxcox exponents |
vBeta |
vector of the logistic model coefficients |
vMean |
vector of the means of the random variables |
vSd |
vector of the standard deviations of the random variables |
vNames |
vector of names for the random variables |
n |
integer number of samples to generate |
seed |
integer value of initial random seed |
simModel |
instance of the class |
The basic Multivariate Boxcox logistic model is expressed as:
where the probability of a positive event is estimated as:
and the Boxcox transformation is:
The contributing variables are:
are the log normal random variables where
are the normal random variables where
is the number of log normal random variables
is the number of normal random variables
is a vector of the logistic model coefficients
is a vector of the means of the random variables
is a vector of the standard deviations of the random variables
is a vector of Boxcox exponents for the log normal random variables
The simulator will generate samples for and and then compute the vector .
The samples are generated using the R functions and respectively.
The vector is then used to generate binomial samples using the R function:
An initial first usage call to mvbc.simulator() will set the given seed using set.seed().
Subsequent calls using the object will increment the seed first, and then call set.seed().
mvbc.simulator() returns an object of class mvbc.simulator.model which contains the following components:
vLambda |
vector of Boxcox exponents |
vBeta |
vector of the logistic model coefficients |
vMean |
vector of the means of the random variables |
vSd |
vector of the standard deviations of the random variables |
vNames |
vector of names for the random variables |
n |
integer number of samples to generate |
c |
integer number of log normal, Boxcox transformed variables |
k |
integer number of normal variables |
seed |
integer value of the last used random seed |
ssr |
sum or square residuals with self |
cm |
caret confusion matrix result with self |
data |
matrix with |
vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 1000, 1) summary(sim)vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 1000, 1) summary(sim)
The Multivariate Boxcox Tester provides a means to predict binary outcomes for a test data set, given a trained model from the Multivariate Boxcox Trainer Core. The Tester can also run multiple train and test cycles using the Multivariate Boxcox Simulator to generate matching training data and test data. A variety of evaluation metrics are available including the Pearson and Deviance residuals and the F1-score and related metrics.
mvbc.tester( formulaA, formulaB = NULL, simModel, nTrain, nTest, griddomain = seq(0, 2, l = 5), K = 5, depth = 2, cores = 1, minima = c("ssdr"), tpsargs = list(scale.type = "unscaled"), optimargs = list(method = "L-BFGS-B", lower = 0, upper = 2) )mvbc.tester( formulaA, formulaB = NULL, simModel, nTrain, nTest, griddomain = seq(0, 2, l = 5), K = 5, depth = 2, cores = 1, minima = c("ssdr"), tpsargs = list(scale.type = "unscaled"), optimargs = list(method = "L-BFGS-B", lower = 0, upper = 2) )
formulaA |
a formula containing the binary outcome and the positive continuous predictors to be Box-Cox transformed |
formulaB |
an optional one-sided formula containing additional covariates that remain untransformed |
simModel |
instance of the class |
nTrain |
number of training sets to generate |
nTest |
number of test sets to generate |
griddomain |
vector or list of vectors defining the domain of candidate lambda values. If it is a list, then must contain |
K |
number of folds for cross validation |
depth |
allows for recursion to finer grained lambda grids. A value of |
cores |
the number of cores for parallel processing |
minima |
which minima to use for deeper grid and spline in the trainer, defaults to |
tpsargs |
list of optional arguments to pass to |
optimargs |
list of optional arguments to pass to |
The mvbc.tester() function performs the following:
Using simModel as a basis, call mvbc.simulator() to generate nTrain training data
sets and nTest test data sets. Relies on the embedded seed to generate repeatable results.
For each of the nTrain training data sets, fit an mvbc.trainer.model mvbc.trainer()
and then optimize the fit using mvbc.optimizer().
For the given minima:
For each nTest test data set, estimate using mvbc.trainer.predict.p_hat()
then estimate . Compute sum of square Pearson and Deviance residuals,
and a confusion matrix report cm. Save results in an instance of a mvbc.tester.result
Return an instance of mvbc.tester.result.
When estimating for use in the confusion matrix, the trainModel$prior will be used to define the cut-off for .
mvbc.tester() returns an object of class mvbc.tester.result which contains the following components:
nTrain |
number of training sets to generate |
nTest |
number of test sets to generate |
griddomain |
list of |
formulaA |
a formula containing the binary outcome and positive continuous predictors to be Box-Cox transformed. |
formulaB |
an optional one-sided formula containing additional covariates that remain untransformed. |
simModel |
instance of last returned |
K |
number of folds for cross validation |
depth |
allows for recursion to finer grained lambda grids. A value of |
minima |
name of minima selector(s) were used for finer grained grid and Thin Plate Spline optimization |
lambda.fits |
data frame containing estimated lambdas, residuals, F1 scores. Full metrics are in the |
beta.fits |
data frame containing estimated betas. |
cms |
list of caret confusion matrix results, corresponding to each row in |
vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.45,1.50) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 300, 1) # one training sample and one test sample # ignore "glm.fit: fitted probabilities numerically 0 or 1 occurred" suppressWarnings({ test.result = mvbc.tester( Ybin ~ mercury + lead, ~ age, sim, nTrain = 1, nTest = 1, griddomain = seq(0, 2, length.out = 3), K = 2, depth = 1 ) }) summary(test.result) maxF1 = which.max(test.result$lambda.fits$F1) test.result$cms[maxF1]vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.45,1.50) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 300, 1) # one training sample and one test sample # ignore "glm.fit: fitted probabilities numerically 0 or 1 occurred" suppressWarnings({ test.result = mvbc.tester( Ybin ~ mercury + lead, ~ age, sim, nTrain = 1, nTest = 1, griddomain = seq(0, 2, length.out = 3), K = 2, depth = 1 ) }) summary(test.result) maxF1 = which.max(test.result$lambda.fits$F1) test.result$cms[maxF1]
Full pipeline: runs mvbc.trainer() to grid-search candidate
lambdas, then mvbc.optimizer() to refine the selected minima with a Thin
Plate Spline, refits the regression coefficients on the full data set at
the selected lambda, and returns a fitted mvbc.train.model with
lambda.fits and beta.fits populated at the chosen minima.
mvbc.train( formulaA, formulaB = NULL, data, griddomain = seq(0, 2, l = 10), K = 5, depth = 1, cores = 1, minima = c("ssdr"), tpsargs = list(scale.type = "unscaled"), optimargs = list(method = "L-BFGS-B", lower = 0, upper = 2), seed = 111, weights = NULL, survey = FALSE )mvbc.train( formulaA, formulaB = NULL, data, griddomain = seq(0, 2, l = 10), K = 5, depth = 1, cores = 1, minima = c("ssdr"), tpsargs = list(scale.type = "unscaled"), optimargs = list(method = "L-BFGS-B", lower = 0, upper = 2), seed = 111, weights = NULL, survey = FALSE )
formulaA |
a formula containing the binary outcome and the positive continuous predictors to be Box-Cox transformed |
formulaB |
an optional one-sided formula containing additional covariates that remain untransformed |
data |
a data frame containing the variables in the model |
griddomain |
vector or list of vectors defining the domain of candidate lambda values. If it is a list, then must contain $c$ elements, one vector for each lambda in the model. |
K |
number of folds for cross validation |
depth |
allows for recursion to finer grained lambda grids. A value of |
cores |
the number of cores for parallel processing |
minima |
a single minima name (e.g. |
tpsargs |
list of optional arguments to pass to |
optimargs |
list of optional arguments to pass to |
seed |
random seed used to assign cross-validation folds |
weights |
optional vector of observation weights (e.g. survey weights), passed through
to |
survey |
logical; if |
mvbc.train() returns an object of class mvbc.train.model, which extends
mvbc.trainer.model (see mvbc.trainer()) with lambda.fits and beta.fits set to the
estimates at the chosen minima. beta.fits contains the single
full-data refit, identified by foldid = 0.
vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) # ignore "glm.fit: fitted probabilities numerically 0 or 1 occurred" suppressWarnings({ trained = mvbc.train( Ybin ~ mercury + lead, ~ age, sim$data, griddomain = seq(0, 2, length.out = 4), K = 3, depth = 1 ) }) summary(trained)vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) # ignore "glm.fit: fitted probabilities numerically 0 or 1 occurred" suppressWarnings({ trained = mvbc.train( Ybin ~ mercury + lead, ~ age, sim$data, griddomain = seq(0, 2, length.out = 4), K = 3, depth = 1 ) }) summary(trained)
The Multivariate Box-Cox Trainer Core mvbc.trainer accepts
positive continuous predictors to be Box-Cox transformed,
optional untransformed covariates , and a binary outcome
. Using K-fold cross-validation, the trainer estimates
logistic models for
consisting of the estimated Box-Cox exponents
and sets of estimated coefficients .
mvbc.trainer( formulaA, formulaB = NULL, data, griddomain = seq(0, 2, l = 10), K = 5, depth = 1, cores = 1, minima = c("ssdr"), seed = 111, weights = NULL, survey = FALSE )mvbc.trainer( formulaA, formulaB = NULL, data, griddomain = seq(0, 2, l = 10), K = 5, depth = 1, cores = 1, minima = c("ssdr"), seed = 111, weights = NULL, survey = FALSE )
formulaA |
a formula containing the binary outcome and the positive continuous predictors to be Box-Cox transformed |
formulaB |
an optional one-sided formula containing additional covariates that remain untransformed |
data |
a data frame containing the variables in the model |
griddomain |
vector or list of vectors defining the domain of candidate lambda values. If it is a list, then must contain $c$ elements, one vector for each lambda in the model. |
K |
number of folds for cross validation| |
depth |
allows for recursion to finer grained lambda grids. A value of |
cores |
the number of cores for parallel processing |
minima |
which minima or minimas to use for deeper grid and spline, defaults to |
seed |
random seed used to assign cross-validation folds |
weights |
optional vector of observation weights (e.g. survey weights) aligned with
the rows of |
survey |
logical; if |
Recall from mvbc.simulator() that the basic Multivariate Boxcox logistic model is expressed as:
where:
is the number of positive predictors to be transformed
is the number of additional untransformed covariates
is the Box-Cox transformation
is the estimated probability of a positive event
The mvbc.trainer() algorithm is summarized here:
Creates a Lambda grid matrix from the griddomain. The grid matrix contains all the possible candidate tuples of
Lambda, with additional columns sspr and ssdr to contain sum-of-squares of Pearson and Deviance residuals.
For example, if each lambda domain contains 10 values, then there would be tuples.
Split the data into K-folds (reuse folds if available in object$folds).
Create a gridfits matrix where each row will store tupleid, foldid, and the estimates from a glm() model.
For each fold, create training-set and test-set...
For each lambda tupleid in grid...
Fit to the training-set using glm().
Use the glm model to estimate probabilities from the test-set.
Use and from the test-set to compute Pearson and Deviance residuals and accumulate in grid.
Save the glm model coefficients as the estimates in the gridfits matrix.
Find lambda tupleids for the minimum sspr and ssdr and save in minimas list.
While depth > 1 then:
Decrement depth.
Compute a finer griddomain centered on chosen minima. Repeat steps 3 to 5 and merge the the grid, gridfits, and minimas results.
mvbc.trainer() returns an object of class mvbc.trainer.model which contains the following components:
prior |
estimated prior probability |
grid |
|
griddomain |
list of |
gridfits |
|
minimas |
list of minima selectors of best lambdas of the form |
minima |
name of minima selector(s) were used for finer grained grid and Thin Plate Spline optimization |
folds |
vector of |
formulaA |
a formula containing the binary outcome and positive continuous predictors to be Box-Cox transformed. |
formulaB |
an optional one-sided formula containing additional covariates that remain untransformed. |
K |
number of folds for cross validation |
depth |
allows for recursion to finer grained lambda grids. A value of |
vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) summary(sim) # ignore "glm.fit: fitted probabilities numerically 0 or 1 occurred" suppressWarnings({ trained = mvbc.trainer( Ybin ~ mercury + lead, ~ age, sim$data, griddomain = seq(0, 2, length.out = 4), K = 3 ) }) summary(trained)vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") sim = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) summary(sim) # ignore "glm.fit: fitted probabilities numerically 0 or 1 occurred" suppressWarnings({ trained = mvbc.trainer( Ybin ~ mercury + lead, ~ age, sim$data, griddomain = seq(0, 2, length.out = 4), K = 3 ) }) summary(trained)
mvbc.trainer.model
Workhorse to fit instance of mvbc.trainer.model
mvbc.trainer.fit( trainModel, data, gridid.start = 1, weights = NULL, survey = FALSE )mvbc.trainer.fit( trainModel, data, gridid.start = 1, weights = NULL, survey = FALSE )
trainModel |
instance of the class |
data |
a data frame containing the variables in the model |
gridid.start |
tuple grid id to begin fitting |
weights |
optional vector of observation weights (e.g. survey weights) aligned with
the rows of |
survey |
logical; if |
An updated object of class mvbc.trainer.model. Its grid component
contains the accumulated cross-validation residual criteria for each lambda
tuple, and gridfits contains the corresponding fold-specific regression
coefficients. All other training metadata in trainModel is retained.
Predict on test data using training model
mvbc.trainer.predict.p_hat(trainModel, data, tupleid)mvbc.trainer.predict.p_hat(trainModel, data, tupleid)
trainModel |
instance of the class |
data |
a data frame containing the test data |
tupleid |
which grid tuple from the |
Given the definition of and
as shown mvbc.simulator(),
the mvbc.trainer.predict.p_hat() function performs the following:
Look up the estimated values associated with the given tupleid.
Look up the sets of estimated values associated with the given tupleid.
For each of the sets of values, compute an estimated value of
, and from the test data
Compute the mean of the estimates of as
and return this vector of mean values.
mvbc.trainer.predict.p_hat() returns vector of estimates.
vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") trainset = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) testset = mvbc.simulator(simModel = trainset) # train and predict one pair suppressWarnings({ trained = mvbc.trainer( Ybin ~ mercury + lead, ~ age, trainset$data, griddomain = seq(0, 2, length.out = 4), K = 3 ) }) p_hat = mvbc.trainer.predict.p_hat(trained, testset$data, trained$minimas[["ssdr"]]) head(p_hat)vBeta = c(-2.2,-0.4,-0.2,-0.005) vLambda = c(0.5,1.5) vMean = c(-0.08, -0.01, 50) vSd = c(0.932543, 0.8, 18.11569) vNames = c("mercury", "lead", "age") trainset = mvbc.simulator(vLambda, vBeta, vMean, vSd, vNames, 500, 1) testset = mvbc.simulator(simModel = trainset) # train and predict one pair suppressWarnings({ trained = mvbc.trainer( Ybin ~ mercury + lead, ~ age, trainset$data, griddomain = seq(0, 2, length.out = 4), K = 3 ) }) p_hat = mvbc.trainer.predict.p_hat(trained, testset$data, trained$minimas[["ssdr"]]) head(p_hat)
Predict outcomes from training model
mvbc.trainer.predict.Ybin_hat(prior, p_hat)mvbc.trainer.predict.Ybin_hat(prior, p_hat)
prior |
estimated prior probability |
p_hat |
estimated |
integer vector of predicted binary outcomes (0/1)
Return sum-of-squares of Pearson and Deviance Residuals
mvbc.trainer.ssr(Ybin, p_hat, weights = NULL)mvbc.trainer.ssr(Ybin, p_hat, weights = NULL)
Ybin |
actual class values |
p_hat |
estimated |
weights |
optional vector of observation weights (e.g. survey weights); defaults to
equal weights of 1 when |
named numeric vector c(sspr, ssdr) with the sum-of-squares of the
Pearson and Deviance residuals
mvbc.sim.result
Plot instance of mvbc.sim.result
## S3 method for class 'mvbc.sim.result' plot(x, ...)## S3 method for class 'mvbc.sim.result' plot(x, ...)
x |
instance of the class |
... |
pass thru to boxplot |
No return value, called for side effects
mvbc.tester.result
Plot instance of mvbc.tester.result
## S3 method for class 'mvbc.tester.result' plot(x, ...)## S3 method for class 'mvbc.tester.result' plot(x, ...)
x |
instance of the class |
... |
pass thru to boxplot |
No return value, called for side effects
mvbc.sim.result
Summarize instance of mvbc.sim.result
## S3 method for class 'mvbc.sim.result' summary(object, ...)## S3 method for class 'mvbc.sim.result' summary(object, ...)
object |
instance of the class |
... |
pass thru nowhere |
No return value, called for side effects
mvbc.simulator.model
Summarize instance of mvbc.simulator.model
## S3 method for class 'mvbc.simulator.model' summary(object, ...)## S3 method for class 'mvbc.simulator.model' summary(object, ...)
object |
instance of |
... |
pass thru to nowhere |
No return value, called for side effects
mvbc.tester.result
Summarize instance of mvbc.tester.result
## S3 method for class 'mvbc.tester.result' summary(object, ...)## S3 method for class 'mvbc.tester.result' summary(object, ...)
object |
instance of the class |
... |
pass thru nowhere |
No return value, called for side effects
mvbc.trainer.model
Summarize instance of mvbc.trainer.model
## S3 method for class 'mvbc.trainer.model' summary(object, ...)## S3 method for class 'mvbc.trainer.model' summary(object, ...)
object |
instance of the class |
... |
pass thru to nowhere |
No return value, called for side effects