| Title: | Multistate Model Bias-Corrected Robust Variance |
|---|---|
| Description: | Computes robust and bias-corrected sandwich variance estimators for multi-state Cox models with clustered time-to-event data. Also provides Wald tests for heterogeneity, generalized least-squares linear trends, and order-restricted trends among transition-specific coefficients. The methodology extends the marginal Cox model bias-correction framework of Wang et al. (2023) <doi:10.1002/bimj.202200113> to the multi-state setting. |
| Authors: | Can Meng [aut, cre], Denise Esserman [aut], Fan Li [aut], Erich Greene [aut] |
| Maintainer: | Can Meng <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.0.0 |
| Built: | 2026-07-22 08:05:57 UTC |
| Source: | https://github.com/cran/mmbcv |
Performs a non-directional omnibus Wald test of whether a selected collection of correlated coefficient parameters is equal. The null hypothesis states that all selected parameters are equal; the unrestricted alternative states that at least one parameter differs.
The test can be applied to any collection of coefficient estimates for which a full variance-covariance matrix is available. No natural ordering or numeric scores are required.
heterogeneity_test(beta, vcov, index = NULL)heterogeneity_test(beta, vcov, index = NULL)
beta |
A finite numeric vector containing the coefficient estimates.
At least two coefficients must be selected for testing. If |
vcov |
A square numeric variance-covariance matrix corresponding to
|
index |
Optional integer, logical, or character vector selecting the
coefficients to test. If |
Let denote the vector of selected
coefficient estimates, and let
denote their variance-covariance matrix. The adjacent
contrast matrix defines
The null and alternative hypotheses are
which is equivalent to
against
The Wald statistic is
Under the null hypothesis, has an asymptotic chi-square
distribution with degrees of freedom.
This is a non-directional omnibus test. Although its p-value is calculated
from the upper tail of the chi-square distribution, it is not a
directional one-sided test. The quadratic statistic is unchanged when
is replaced by
, so departures from equality in every
direction are included in the alternative.
When two coefficients are selected, the test is equivalent to a
two-sided Wald Z test because . When more than two coefficients
are selected, it is more precisely described as a non-directional
omnibus test. Directional linear or monotone alternatives should be
evaluated using linear_trend_test() or order_restricted_test(),
respectively.
An object of class "htest" containing:
The omnibus Wald statistic .
The chi-square degrees of freedom, equal to .
The upper-tail chi-square p-value.
A character string identifying the statistical test.
A character string describing the unrestricted alternative hypothesis.
The selected coefficient estimates
.
The variance-covariance matrix corresponding to the selected coefficients.
The resolved positions of the selected coefficients in the original coefficient vector.
The estimated adjacent contrasts
.
The adjacent contrast matrix .
The variance-covariance matrix of the adjacent
contrasts, .
The "htest" class provides the standard printed format for hypothesis
tests in R. Additional returned components can be accessed using $.
Wald, A. (1943). Tests of statistical hypotheses concerning several parameters when the number of observations is large. Transactions of the American Mathematical Society, 54, 426–482.
Lehmann, E. L., and Romano, J. P. (2022). Testing Statistical Hypotheses, 4th ed. Springer, Cham.
Cox, D. R., and Hinkley, D. V. (1974). Theoretical Statistics. Chapman and Hall, London.
linear_trend_test(), order_restricted_test()
beta <- c(-0.3, -0.2, -0.1, 0) V <- diag(0.01, 4) heterogeneity_test(beta, V)beta <- c(-0.3, -0.2, -0.1, 0) V <- diag(0.01, 4) heterogeneity_test(beta, V)
Tests for a linear relationship between a selected sequence of correlated coefficient parameters and their assigned numeric scores. Although intended primarily for transition-specific treatment effects, the procedure can be applied to any ordered coefficient sequence with a full variance-covariance matrix.
linear_trend_test( beta, vcov, index = NULL, scores = NULL, alternative = c("greater", "less", "two.sided") )linear_trend_test( beta, vcov, index = NULL, scores = NULL, alternative = c("greater", "less", "two.sided") )
beta |
A finite numeric vector of coefficient estimates. If |
vcov |
A square numeric variance-covariance matrix corresponding to
|
index |
An optional integer, logical, or character vector selecting the
coefficients to include in the trend test. If |
scores |
A finite numeric vector assigning an ordered position
|
alternative |
A character string specifying the alternative hypothesis:
|
Suppose the selected coefficient parameters are treated as an ordered sequence
,
with estimated variance-covariance matrix . The test represents
the coefficients using the linear trend model
where is an intercept, is the trend slope, and
is the strictly increasing numeric score assigned to ordered-position .
The scores define the ordering and relative spacing used in the linear
trend model. They may represent ordered categories, measurement occasions,
dose levels, transition positions, or any other meaningful ordered scale.
Let
be the design matrix. The generalized least-squares estimator of
is
Its estimated variance-covariance matrix is
The test statistic for the trend slope is
Under the boundary point of the null parameter space ,
has an asymptotic standard-normal distribution.
The hypotheses depend on alternative:
"greater" against . The p-value is
.
"less" against . The p-value is
.
"two.sided" against . The p-value is
.
Here, denotes the standard-normal cumulative distribution
function.
The direction of the trend is defined relative to increasing score values. A positive slope indicates that the coefficients tend to increase as the assigned scores increase, whereas a negative slope indicates that they tend to decrease. The appropriate alternative should be selected according to the coefficient scale and the scientific question.
The spacing of the assigned scores affects the fitted trend. With the default
scores, represents the estimated change in the
coefficient per one unit increase in ordered position. With user-specified
scores, it represents the estimated change per one score unit. Replacing the
scores by a positive affine transformation changes the numerical scale of the
slope but not its Wald statistic or p-value. Changing the relative spacing of
the scores can change the fitted trend and the test result.
This test assesses whether the selected coefficients have a nonzero linear
trend with respect to their assigned scores. A significant result indicates
a nonzero generalized least-squares slope; it does not establish that a
linear model provides an exact or adequate description of the coefficient
pattern. The observed coefficient estimates are not required to be monotone.
Use heterogeneity_test() to test for any departure from equality without
imposing a trend shape. Use order_restricted_test() to test a monotone
increasing or decreasing alternative without assuming linearity or
equal-sized changes between adjacent coefficients.
An object of class "htest" containing:
The standard-normal Wald statistic .
The p-value corresponding to the selected alternative hypothesis.
The null value of the trend slope, equal to zero.
The selected alternative: "greater", "less", or
"two.sided".
A character string identifying the generalized least-squares linear-trend test.
The generalized least-squares estimate
of the trend slope.
The estimated standard error of
.
The selected coefficient estimates.
The variance-covariance matrix corresponding to the selected coefficients.
The resolved positions of the selected coefficients in the original coefficient vector.
The resolved ordered-position scores used as predictor values in the linear trend model fitted by generalized least squares.
The generalized least-squares estimate
of the intercept.
The estimated
variance-covariance matrix of the intercept and trend-slope estimates.
The "htest" class provides the standard printed format for hypothesis
tests in R. Additional returned components can be accessed using $, for
example, result$slope, result$stderr, and result$scores.
Cochran, W. G. (1954). Some methods for strengthening the common chi-squared tests. Biometrics, 10, 417–451.
Armitage, P. (1955). Tests for linear trends in proportions and frequencies. Biometrics, 11, 375–386.
Aitken, A. C. (1935). On least squares and linear combinations of observations. Proceedings of the Royal Society of Edinburgh, 55, 42–48.
Seber, G. A. F., and Lee, A. J. (2003). Linear Regression Analysis, 2nd ed. John Wiley & Sons, Hoboken, NJ.
Kariya, T., and Kurata, H. (2004). Generalized Least Squares. John Wiley & Sons.
heterogeneity_test, order_restricted_test
beta <- c( beta1 = -0.40, beta2 = -0.30, beta3 = -0.20, beta4 = -0.10, beta5 = 0.00 ) V <- diag(0.01, 5) dimnames(V) <- list(names(beta), names(beta)) # Increasing linear trend with equally spaced transitions result <- linear_trend_test( beta, V, alternative = "greater" ) result$slope result$stderr # Preserve the original spacing after omitting beta2 keep <- c(1, 3, 4, 5) linear_trend_test( beta, V, index = keep, scores = 0:4, alternative = "greater" ) # Unequally spaced transition scores linear_trend_test( beta, V, scores = c(0, 1, 3, 6, 10), alternative = "two.sided" )beta <- c( beta1 = -0.40, beta2 = -0.30, beta3 = -0.20, beta4 = -0.10, beta5 = 0.00 ) V <- diag(0.01, 5) dimnames(V) <- list(names(beta), names(beta)) # Increasing linear trend with equally spaced transitions result <- linear_trend_test( beta, V, alternative = "greater" ) result$slope result$stderr # Preserve the original spacing after omitting beta2 keep <- c(1, 3, 4, 5) linear_trend_test( beta, V, index = keep, scores = 0:4, alternative = "greater" ) # Unequally spaced transition scores linear_trend_test( beta, V, scores = c(0, 1, 3, 6, 10), alternative = "two.sided" )
Computes several sandwich variance estimators (robust, MR, MD, FG, KC, MBN and combinations) for multistate Cox models using cluster-level score aggregation, with Breslow or Efron ties.
MMBCV( fit, data, StartTime, StopTime, ClusterID, SubjectID, Event, tie = c("breslow", "efron"), details = FALSE, detail_names = NULL, kc_tol = 1e-10 )MMBCV( fit, data, StartTime, StopTime, ClusterID, SubjectID, Event, tie = c("breslow", "efron"), details = FALSE, detail_names = NULL, kc_tol = 1e-10 )
fit |
A fitted multistate Cox model object with components |
data |
A data.frame in counting-process form that matches the fit. |
StartTime, StopTime
|
Unquoted column names for start/stop times. |
ClusterID |
Unquoted column name for the cluster identifier. |
SubjectID |
Unquoted column name for the subject identifier. |
Event |
Unquoted column name for the event/state indicator used to define transition-specific events. |
tie |
Tie-handling method: |
details |
Logical; if TRUE, returns additional intermediate quantities. |
detail_names |
Optional character vector specifying which extra components
of the full sandwich object to append when |
kc_tol |
Numeric tolerance used in the KC positive-semidefinite adjustment. |
A list containing variance-covariance matrices:
Standard cluster-robust sandwich variance estimator.
Martingale-residual (MR) bias-corrected variance estimator.
Mancl-DeRouen (MD) bias-corrected variance estimator.
Hybrid Mancl-DeRouen plus martingale-residual (MDMR) bias-corrected variance estimator.
Fay-Graubard (FG) bias-corrected variance estimator.
Hybrid Fay-Graubard plus martingale-residual (FGMR) bias-corrected variance estimator.
Kauermann-Carroll (KC) bias-corrected variance estimator.
Hybrid Kauermann-Carroll plus martingale-residual (KCMR) bias-corrected variance estimator.
Morel-Bokossa-Neerchal (MBN) bias-corrected variance estimator.
Hybrid Morel-Bokossa-Neerchal plus martingale-residual (MBNMR) bias-corrected variance estimator.
If details = TRUE, additional intermediate matrices may also be returned.
Wang, X., Turner, E. L., and Li, F. (2023). Improving sandwich variance estimation for marginal Cox analysis of cluster randomized trials. Biometrical Journal, 65, 2200113.
Schaubel, D. E. (2005). Adjusting analytic inference for small samples in recurrent event regression. Statistics in Medicine, 24(19), 3037–3051.
Kauermann, G., and Carroll, R. J. (2001). A note on the efficiency of sandwich covariance matrix estimation. Journal of the American Statistical Association, 96(456), 1387–1396.
Fay, M. P., and Graubard, B. I. (2001). Small-sample adjustments for Wald-type tests using sandwich estimators. Biometrics, 57(4), 1198–1206.
Mancl, L. A., and DeRouen, T. A. (2001). A covariance estimator for GEE with improved small-sample properties. Biometrics, 57(1), 126–134.
Morel, J. G., Bokossa, M., and Neerchal, N. K. (2003). Small sample correction for the variance of GEE estimators. Biometrical Journal, 45(4), 395–409.
Therneau, T., Crowson, C., and Atkinson, E. (2026).
Multi-state models and competing risks.
Vignette for the survival package.
See vignette("compete", package = "survival") or
https://CRAN.R-project.org/package=survival.
data("msdat3") # Example requires the suggested package 'survival' if (requireNamespace("survival", quietly = TRUE)) { fit <- survival::coxph( list( survival::Surv(Tstart, Tstop, event) ~ 1, state("(s0)"):state("S1") + state("S1"):state("S2") + state("S2"):state("S3") ~ Z + X, state("(s0)"):state("D") + state("S1"):state("D") + state("S2"):state("D") + state("S3"):state("D") ~ (Z + X)/common ), data = msdat3, id = id, ties = "breslow", timefix = FALSE ) MMBCV( fit = fit, data = msdat3, StartTime = Tstart, StopTime = Tstop, ClusterID = clus_id, SubjectID = id, Event = event, tie = "breslow", details = FALSE) }data("msdat3") # Example requires the suggested package 'survival' if (requireNamespace("survival", quietly = TRUE)) { fit <- survival::coxph( list( survival::Surv(Tstart, Tstop, event) ~ 1, state("(s0)"):state("S1") + state("S1"):state("S2") + state("S2"):state("S3") ~ Z + X, state("(s0)"):state("D") + state("S1"):state("D") + state("S2"):state("D") + state("S3"):state("D") ~ (Z + X)/common ), data = msdat3, id = id, ties = "breslow", timefix = FALSE ) MMBCV( fit = fit, data = msdat3, StartTime = Tstart, StopTime = Tstop, ClusterID = clus_id, SubjectID = id, Event = event, tie = "breslow", details = FALSE) }
A simulated clustered, subject-level multistate dataset in long (counting-process) format. Treatment is assigned at the cluster level and baseline covariates are available at both the cluster and individual levels. Each subject may contribute multiple rows, corresponding to transitions between states over time.
data(msdat3)data(msdat3)
A data frame with 3,004 rows and 9 variables:
Subject identifier. There are 1,500 unique subjects.
Cluster-level treatment assignment indicator. Z = 1 indicates the
cluster is assigned to treatment; Z = 0 indicates control.
Individual-level baseline covariate generated from a standard normal distribution.
Cluster identifier. There are 30 clusters.
Starting state for the current observation interval (e.g., (s0)).
Ending state for the current observation interval (e.g., S1, S2,
S3, D, or censor).
Start time of the interval.
Stop time of the interval.
End state at Tstop. Values include censor, S1, S2,
S3, and D, where D is the absorbing state.
The multistate process uses the following state labels:
(s0): initial (starting) state.
S1: first intermediate transition state.
S2: second intermediate transition state.
S3: third intermediate transition state.
D: absorbing (terminal) state.
Each row represents a time interval [Tstart, Tstop] during which the subject is
at risk for the transition indicated by from and to. The variable
event records the state at which the interval ends. event = "censor"
indicates the observation is right-censored at Tstop (i.e., no transition is
observed at the end of the interval).
The distribution of event in this dataset is:
censor: 1050
S1: 837
S2: 450
S3: 217
D: 450
Simulated data bundled with the package for examples and testing.
data("msdat3") nrow(msdat3) length(unique(msdat3$id)) length(unique(msdat3$clus_id)) table(msdat3$event)data("msdat3") nrow(msdat3) length(unique(msdat3$id)) length(unique(msdat3$clus_id)) table(msdat3$event)
Tests whether a selected sequence of correlated coefficient parameters is constant against the alternative that the parameters follow a monotone increasing or decreasing order. The procedure uses a covariance-weighted projection onto an order-restricted cone and a chi-bar-square reference distribution.
Although the function is designed primarily for comparing transition-specific treatment effects, it can be applied to any ordered vector of coefficient estimates for which a full variance-covariance matrix is available.
order_restricted_test( beta, vcov, index = NULL, alternative = c("increasing", "decreasing"), weight_method = c("auto", "simulation"), nsim = 100000L, seed = NULL, weights = NULL, tol = 1e-10 )order_restricted_test( beta, vcov, index = NULL, alternative = c("increasing", "decreasing"), weight_method = c("auto", "simulation"), nsim = 100000L, seed = NULL, weights = NULL, tol = 1e-10 )
beta |
A finite numeric vector containing the coefficient estimates.
At least two coefficients must be selected for testing. The positions
of the coefficients, or the order specified through |
vcov |
A square numeric variance-covariance matrix corresponding to
|
index |
An optional integer, logical, or character vector selecting
the coefficients to be tested. If |
alternative |
The direction of the order-restricted alternative.
|
weight_method |
The method used to calculate the chi-bar-square mixture weights. Available choices are:
This argument is ignored when |
nsim |
A positive integer giving the number of simulation draws used
to estimate the chi-bar-square weights. It is used only when weights are
calculated by simulation. Larger values reduce Monte Carlo error but
increase computation time. The default is |
seed |
An optional non-negative integer used as the random-number seed when the chi-bar-square weights are estimated by simulation. Supplying a seed makes the estimated weights and p-value reproducible. It has no effect when analytic or user-supplied weights are used. |
weights |
An optional numeric vector of precomputed chi-bar-square
weights. If |
tol |
A positive finite numeric tolerance used in covariance-matrix
checks, covariance-weighted projection, determination of active cone
faces, and validation of supplied weights. The default is |
Suppose coefficient parameters are selected and collected in
The null hypothesis states that all selected parameters are equal:
For an increasing alternative, the alternative hypothesis is
with at least one strict inequality. For a decreasing alternative, all
inequalities are reversed. Thus, the test is directional. It differs from
heterogeneity_test(), whose alternative includes departures from equality
in any direction.
Let denote the directional adjacent contrast matrix. For an
increasing alternative,
For a decreasing alternative, the sign of is reversed, so that
positive contrasts always represent movement in the direction specified
by alternative. If is the variance-covariance matrix of
, then
is the variance-covariance matrix of the directional adjacent contrasts. The hypotheses can therefore be written as
The observed contrast vector is projected onto the nonnegative orthant
using the metric induced by :
The order-restricted statistic is
The statistic measures the improvement in covariance-weighted fit obtained by moving from the equality-constrained null point to the closest point in the monotone alternative cone. It is non-negative. If the observed contrast vector already lies in the alternative cone, its projection is unchanged and the statistic can equal the ordinary omnibus Wald statistic. The two procedures nevertheless have different reference distributions and generally different p-values.
Under the null hypothesis, the statistic has an asymptotic chi-bar-square distribution:
where the non-negative weights sum to one and depend on the
covariance structure and geometry of the order cone. The p-value is
obtained by combining the upper-tail probabilities of the chi-square
components using these weights. If the statistic is numerically zero, the
p-value is set to one.
With one or two order constraints, corresponding to two or three selected
coefficients, analytic weights are available. For larger problems,
weight_method = "auto" estimates the weights by simulation. The current
simulation algorithm supports at most 12 order constraints; for a larger
problem, precomputed weights must be supplied through weights.
Only the ordering of the selected coefficients is relevant. Numeric
distances between transitions or other ordered categories are not required.
For example, index = c(1, 3, 4, 5) tests the order of
. Unlike
linear_trend_test(), no transition scores are needed because the
order-restricted test does not assume a constant linear change per unit of
transition score.
The function reports a statistic and p-value but does not require a
significance level. A user-selected level can be applied by
comparing result$p.value < alpha.
An object of class "htest" containing:
The non-negative order-restricted test statistic, named "Tord".
The upper-tail p-value calculated from the chi-bar-square reference distribution.
A character string identifying the testing procedure and its chi-bar-square reference distribution.
A character string describing the increasing or decreasing monotone alternative hypothesis.
The selected coefficient estimates in the order used by the test.
The variance-covariance matrix of the selected coefficients.
The resolved positions of the selected coefficients in the original
beta vector.
The estimated directional adjacent contrasts. For an increasing
alternative, these are later-minus-earlier differences
. For a decreasing
alternative, their signs are reversed.
The covariance-weighted projection of differences onto the
nonnegative orthant representing the monotone alternative.
The directional adjacent contrast matrix used to calculate
differences.
The variance-covariance matrix
of the directional adjacent contrasts.
The chi-bar-square mixture weights corresponding to degrees of freedom
.
The degrees of freedom associated with the chi-square mixture components.
The method actually used to obtain the weights: analytic calculation, projection simulation, or user-supplied weights.
The number of simulation draws actually used. This is zero when analytic or user-supplied weights are used.
The estimated Monte Carlo standard error of each simulated chi-bar-square weight. These values are zero for analytic or user-supplied weights.
The estimated Monte Carlo standard error of the p-value arising from simulation of the chi-bar-square weights. It is zero for analytic or user-supplied weights.
A character string identifying the numerical method used for the covariance-weighted cone projection.
The "htest" class provides R's standard printed format for hypothesis
tests. Additional components not shown in the printed output can be
accessed with $.
Robertson, T., Wright, F. T., and Dykstra, R. L. (1988). Order Restricted Statistical Inference. John Wiley & Sons, New York.
Kudo, A. (1963). A multivariate analogue of the one-sided test. Biometrika, 50, 403–418.
Shapiro, A. (1985). Asymptotic distribution of test statistics in the analysis of moment structures under inequality constraints. Biometrika, 72, 133–144.
Silvapulle, M. J., and Sen, P. K. (2005). Constrained Statistical Inference: Inequality, Order, and Shape Restrictions. John Wiley & Sons, Hoboken, NJ.
heterogeneity_test(), linear_trend_test()
beta <- c(beta1 = -0.30, beta2 = -0.20, beta3 = -0.10, beta4 = 0) V <- diag(0.01, 4) result <- order_restricted_test( beta, V, alternative = "increasing", nsim = 2000, seed = 47 ) result$contrast_matrix result$weightsbeta <- c(beta1 = -0.30, beta2 = -0.20, beta3 = -0.10, beta4 = 0) V <- diag(0.01, 4) result <- order_restricted_test( beta, V, alternative = "increasing", nsim = 2000, seed = 47 ) result$contrast_matrix result$weights
Subset a dataset by transition using a row map
subset_by_transition(dataA, dataB, transition)subset_by_transition(dataA, dataB, transition)
dataA |
Full dataset to subset. |
dataB |
Two-column object: column 1 = row indices in |
transition |
Character vector of transition names used as output list names. |
A named list of data frames, one per transition.