
Fast Weibull AFT Regression (R Wrapper: Rcpp Backend or survival)
Source:R/helper_glm_fit.R
fast_weibull_regression.RdFits the Weibull accelerated failure time model documented in full at
fast_weibull_regression_general_cpp (\(\log T_i = \eta_i +
\sigma W_i\), \(\eta_i = x_i^\top\beta\), right-censoring only), via either
that C++ backend (use_rcpp = TRUE, the default) or
survreg with dist = "weibull"
(use_rcpp = FALSE) as a fallback/cross-check implementation.
Usage
fast_weibull_regression(
y,
dead,
X,
use_rcpp = TRUE,
estimate_only = FALSE,
optimization_alg = "lbfgs",
warm_start_params = NULL,
warm_start_fisher_info = NULL
)Arguments
- y
Observed survival/censoring times (must be positive).
- dead
Event indicator: 1 for an exactly observed event, 0 for right-censored (survival known only to exceed
y[i]).- X
A numeric matrix of predictor variables. It is assumed that an intercept column (e.g., a column of ones) is already included in
Xif desired.- use_rcpp
Logical. If
TRUE(default), use the optimized Rcpp implementation. IfFALSE, usesurvreg.- estimate_only
Logical. If
TRUE, skip variance-covariance matrix calculation for speed. Only has an effect whenuse_rcpp = TRUE.- optimization_alg
Optimization algorithm:
"lbfgs"(default) or"newton_raphson". Only has an effect whenuse_rcpp = TRUE.- warm_start_params
Optional starting values for \([\beta, \log\sigma]\). Only has an effect when
use_rcpp = TRUE.- warm_start_fisher_info
Optional initial curvature (Fisher/observed information) matrix. Only has an effect when
use_rcpp = TRUE.
Value
A list containing the following components:
- coefficients
A numeric vector of the estimated Weibull regression coefficients \(\hat\beta\), including the intercept.
- log_sigma
The logarithm of the fitted scale parameter \(\hat\sigma\) of the Weibull AFT distribution.
- vcov
The variance-covariance matrix of the estimated coefficients, or
NULLifestimate_only = TRUE(Rcpp path only).- neg_log_lik
(Rcpp path only) The negative log-likelihood at the fitted parameters.
- fisher_information
(Rcpp path only) The observed information matrix, or
NULLifestimate_only = TRUE.- std_errs
(survival path only) The coefficient standard errors,
sqrt(diag(vcov)).
Details
When use_rcpp = TRUE, an intercept column is prepended to
X automatically if not already present (detected as a first column
of all 1s), fitting always starts from a zero cold start
(smart_cold_start = FALSE is hardcoded, regardless of whether
warm_start_params is supplied), and a non-converged C++ fit is
escalated to an R-level stop() rather than returned silently.
When use_rcpp = FALSE, any existing intercept column is stripped and
survreg is left to add its own; remaining covariate columns are first
passed through drop_linearly_dependent_cols to remove
collinear columns before fitting (silently — no error or warning is raised
for dropped columns). estimate_only, optimization_alg,
warm_start_params, and warm_start_fisher_info have no
effect on this path — survreg always computes the full
variance-covariance matrix, and std_errs (from sqrt(diag(vcov)))
is included only in this path's return value, not the Rcpp path's. Both
non-finite coefficients and (unless estimate_only = TRUE, which is
ignored on this path regardless) non-finite variance-covariance entries from
survreg are escalated to an R-level stop().
See also
fast_weibull_regression_general_cpp for the full model documentation
and Rcpp backend contract.
Examples
X = matrix(rnorm(500), 100, 5)
y = runif(100)
dead = rbinom(100, 1, 0.5)
fast_weibull_regression(y, dead, X)
#> $coefficients
#> (Intercept)
#> -0.14810658 -0.04468756 0.09869144 -0.05585793 0.10141319 -0.09649075
#>
#> $log_sigma
#> [1] -0.5447962
#>
#> $vcov
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 7.576255e-03 -1.978455e-05 1.320297e-03 -0.0002325685 0.0014257867
#> [2,] -1.978455e-05 6.210289e-03 9.740123e-04 -0.0009268948 -0.0003709875
#> [3,] 1.320297e-03 9.740123e-04 6.389962e-03 0.0003669197 0.0003361983
#> [4,] -2.325685e-04 -9.268948e-04 3.669197e-04 0.0058996395 0.0003002120
#> [5,] 1.425787e-03 -3.709875e-04 3.361983e-04 0.0003002120 0.0048310895
#> [6,] -1.253042e-03 8.488492e-04 6.479947e-05 -0.0008576936 -0.0009073874
#> [7,] 2.575350e-03 3.713415e-04 1.705837e-03 -0.0004980350 0.0008897537
#> [,6] [,7]
#> [1,] -1.253042e-03 0.0025753498
#> [2,] 8.488492e-04 0.0003713415
#> [3,] 6.479947e-05 0.0017058369
#> [4,] -8.576936e-04 -0.0004980350
#> [5,] -9.073874e-04 0.0008897537
#> [6,] 6.605157e-03 -0.0010919624
#> [7,] -1.091962e-03 0.0136335395
#>
#> $neg_log_lik
#> [1] 40.00174
#>
#> $fisher_information
#> [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,] 154.598913 2.264016 -25.511687 11.294919 -36.243305 22.218541
#> [2,] 2.264016 172.344807 -27.841232 25.652056 10.190204 -17.168283
#> [3,] -25.511687 -27.841232 171.924848 -18.580543 -3.914897 -8.718571
#> [4,] 11.294919 25.652056 -18.580543 179.558185 -8.540907 22.562462
#> [5,] -36.243305 10.190204 -3.914897 -8.540907 224.279720 20.522038
#> [6,] 22.218541 -17.168283 -8.718571 22.562462 20.522038 165.320420
#> [7,] -21.515581 -2.741407 -17.055472 8.416322 -6.246711 10.087495
#> [,7]
#> [1,] -21.515581
#> [2,] -2.741407
#> [3,] -17.055472
#> [4,] 8.416322
#> [5,] -6.246711
#> [6,] 10.087495
#> [7,] 81.144505
#>