
Fast Cox Proportional Hazards Regression (R Wrapper)
Source:R/helper_glm_fit.R
fast_coxph_regression.RdFits the Cox proportional-hazards partial-likelihood model documented in full at
build_cox_data_cache_cpp (model equation, Breslow tie-handling, and
input conventions). This R-level wrapper dispatches to either the package's own
native C++ implementation (fast_coxph_regression_cpp, the default
and recommended path) or, for cross-checking or when the Rcpp path is
unavailable, an elastic-net-with-zero-penalty Cox fit via glmnet
(use_rcpp = FALSE) — not survival::coxph, despite that
being the more commonly used reference implementation for Cox models in R.
Usage
fast_coxph_regression(
X,
y,
dead,
use_rcpp = TRUE,
estimate_only = FALSE,
optimization_alg = "lbfgs",
warm_start_beta = NULL,
warm_start_fisher_info = NULL,
smart_cold_start = TRUE
)Arguments
- X
A numeric matrix of predictor variables. It is assumed that an intercept term is handled implicitly by the Cox model and should not be included in
X.- y
A numeric vector representing the observed time (event time or censoring time).
- dead
A numeric vector (0 or 1) indicating event status (1 for event, 0 for censored).
- use_rcpp
Logical. If
TRUE(default), use the optimized Rcpp implementation (fast_coxph_regression_cpp). IfFALSE, use glmnet's Cox path at zero penalty (glmnet(..., family = "cox", lambda = 0)) instead.- estimate_only
Logical. If
TRUE, skip variance-covariance matrix calculation for speed. Only affects theuse_rcpp = TRUEpath; the glmnet fallback path does not compute a variance-covariance matrix at all (vcovis never populated whenuse_rcpp = FALSE, regardless ofestimate_only).- optimization_alg
Optimization algorithm:
"lbfgs"(default) or"newton_raphson". Only affects theuse_rcpp = TRUEpath; unused whenuse_rcpp = FALSE.- warm_start_beta
Optional starting values for coefficients. If provided,
smart_cold_startis ignored. Only affects theuse_rcpp = TRUEpath.- warm_start_fisher_info
Optional initial Fisher Information matrix. Only affects the
use_rcpp = TRUEpath.- smart_cold_start
Logical. If
TRUE(default), use an initial OLS-based guess when starting from scratch (a "cold start") with no prior knowledge. This is ignored ifwarm_start_betais provided. Only affects theuse_rcpp = TRUEpath.
Value
A list. When use_rcpp = TRUE (default), a list with components
- b, coefficients
A numeric vector of the estimated log-hazard-ratio coefficients \(\hat\beta\) (
bandcoefficientsare identical; both are populated for interface consistency with the package's otherfast_*wrappers).- vcov
The variance-covariance matrix of \(\hat\beta\), or
NULLwhenestimate_only = TRUE.- neg_log_lik
The negative Cox partial log-likelihood at the fitted coefficients.
- fisher_information
The Hessian of the negative partial log-likelihood at the fitted coefficients.
When use_rcpp = FALSE, only a single component,
b (the glmnet-fitted coefficient vector via coef(), in
glmnet's own sparse-matrix representation rather than a plain numeric
vector) — none of coefficients/vcov/neg_log_lik/
fisher_information are present on this path.
Details
Failure semantics. If the C++ fit (use_rcpp = TRUE) errors or
fails to report converged, this function stops with an error rather than
silently falling back to glmnet — the two code paths are alternative
caller choices, not an automatic fallback chain (contrast with, e.g.,
fast_beta_regression's automatic betareg fallback).
glmnet dependency. When use_rcpp = FALSE, this function requires the glmnet package,
which is listed in Suggests and is not installed automatically with EDI; it errors immediately if
glmnet is not installed.
See also
build_cox_data_cache_cpp for the full Cox partial-likelihood
model, Breslow tie-handling, and input conventions;
fast_coxph_regression_cpp for the native C++ backend this
wrapper calls by default.
Examples
X = matrix(rnorm(500), 100, 5)
y = runif(100)
dead = rbinom(100, 1, 0.5)
fast_coxph_regression(X, y, dead)
#> $b
#> [1] -0.07827465 0.14886280 -0.31237001 0.25505133 0.00424261
#>
#> $coefficients
#> [1] -0.07827465 0.14886280 -0.31237001 0.25505133 0.00424261
#>
#> $vcov
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 0.0354563763 0.0006143974 0.0014563149 -2.740894e-03 -3.463152e-03
#> [2,] 0.0006143974 0.0138571573 0.0027491371 2.235220e-03 -1.856858e-03
#> [3,] 0.0014563149 0.0027491371 0.0243000244 1.974958e-03 2.922704e-04
#> [4,] -0.0027408943 0.0022352204 0.0019749578 2.308017e-02 -7.908545e-05
#> [5,] -0.0034631523 -0.0018568584 0.0002922704 -7.908545e-05 2.279807e-02
#>
#> $neg_log_lik
#> [1] 146.1624
#>
#> $fisher_information
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 29.0156907 -0.9054055 -1.991329 3.7188333 4.3723282
#> [2,] -0.9054055 75.7035755 -8.036772 -6.7304730 6.1080551
#> [3,] -1.9913289 -8.0367720 42.450583 -3.0958074 -1.5120272
#> [4,] 3.7188333 -6.7304730 -3.095807 44.6863217 0.2114302
#> [5,] 4.3723282 6.1080551 -1.512027 0.2114302 45.0451425
#>