pub struct CoxFit { /* private fields */ }Expand description
A fitted Cox proportional-hazards model and its diagnostics.
Models the hazard hᵢ(t) = h₀(t)·exp(xᵢᵀβ) and estimates β by maximizing
the partial likelihood — the baseline h₀(t) drops out, so no intercept
is used (a constant column would be unidentifiable and is rejected). Fit by
Newton–Raphson; at the maximum the score U(β) = 0 and the inverse observed
information gives the coefficient covariance behind the Wald z-statistics.
The coefficients are log hazard ratios: exp(βⱼ) is the multiplicative
effect of a one-unit increase in predictor j on the hazard. Tied event
times use Ties::Efron by default.
Two extensions share the same machinery:
stratifiedfits a separate baseline per stratum with shared coefficients — the standard remedy when the proportional-hazards assumption holds only within subgroups.counting_processaccepts(start, stop]intervals for time-varying covariates, where each row is one at-risk episode and a subject contributes several rows.
Implementations§
Source§impl CoxFit
impl CoxFit
Sourcepub fn new(
time: Array1<f64>,
event: Array1<f64>,
x: Array2<f64>,
) -> Result<Self>
pub fn new( time: Array1<f64>, event: Array1<f64>, x: Array2<f64>, ) -> Result<Self>
Fit a Cox model of survival (time, event) on covariates X with Efron
tie handling (default: up to 100 Newton iterations, tolerance 1e-9).
event[i] is 1.0 for an observed event and 0.0 for right-censoring.
Do not include an intercept column — the baseline hazard absorbs it.
§Errors
RegressionError::EmptyInput/RegressionError::ShapeMismatch.RegressionError::InvalidResponseif any time is non-positive, an event flag is not0/1, there are no events, orXhas a constant (intercept) column.RegressionError::RankDeficient/RegressionError::NotConverged.
Sourcepub fn with_options(
time: Array1<f64>,
event: Array1<f64>,
x: Array2<f64>,
ties: Ties,
max_iter: usize,
tol: f64,
) -> Result<Self>
pub fn with_options( time: Array1<f64>, event: Array1<f64>, x: Array2<f64>, ties: Ties, max_iter: usize, tol: f64, ) -> Result<Self>
Like CoxFit::new with an explicit tie-handling rule, iteration cap and
tolerance.
Sourcepub fn stratified(
time: Array1<f64>,
event: Array1<f64>,
x: Array2<f64>,
strata: &[usize],
ties: Ties,
) -> Result<Self>
pub fn stratified( time: Array1<f64>, event: Array1<f64>, x: Array2<f64>, strata: &[usize], ties: Ties, ) -> Result<Self>
Fit a stratified Cox model: a separate baseline hazard per stratum,
with the coefficients β shared across strata. The partial likelihood is
summed over strata, each contributing only its own risk sets.
strata[i] is the stratum label of observation i (arbitrary integers).
§Errors
As CoxFit::with_options, plus RegressionError::ShapeMismatch if
strata has the wrong length.
Sourcepub fn counting_process(
start: Array1<f64>,
stop: Array1<f64>,
event: Array1<f64>,
x: Array2<f64>,
ties: Ties,
) -> Result<Self>
pub fn counting_process( start: Array1<f64>, stop: Array1<f64>, event: Array1<f64>, x: Array2<f64>, ties: Ties, ) -> Result<Self>
Fit a Cox model on counting-process (start, stop] data, the format
for time-varying covariates: each row is one at-risk episode with
covariates constant over (startᵢ, stopᵢ], and a subject spanning changing
covariates appears as several consecutive rows. A row is in the risk set at
event time t when startᵢ < t ≤ stopᵢ.
§Errors
As CoxFit::with_options, plus RegressionError::InvalidResponse if
any interval has start ≥ stop.
Sourcepub fn n_observations(&self) -> usize
pub fn n_observations(&self) -> usize
Number of observations (episodes, for counting-process data).
Sourcepub fn n_parameters(&self) -> usize
pub fn n_parameters(&self) -> usize
Number of covariates.
Sourcepub fn iterations(&self) -> usize
pub fn iterations(&self) -> usize
Newton iterations taken to converge.
Sourcepub fn design_matrix(&self) -> ArrayView2<'_, f64>
pub fn design_matrix(&self) -> ArrayView2<'_, f64>
The covariate matrix as fitted.
Sourcepub fn time(&self) -> ArrayView1<'_, f64>
pub fn time(&self) -> ArrayView1<'_, f64>
Event/censoring (stop) times.
Sourcepub fn event(&self) -> ArrayView1<'_, f64>
pub fn event(&self) -> ArrayView1<'_, f64>
Event indicators.
Sourcepub fn coefficients(&self) -> ArrayView1<'_, f64>
pub fn coefficients(&self) -> ArrayView1<'_, f64>
Estimated coefficients — log hazard ratios, aligned to the covariate columns.
Sourcepub fn hazard_ratios(&self) -> Array1<f64>
pub fn hazard_ratios(&self) -> Array1<f64>
Hazard ratios exp(βⱼ).
Sourcepub fn covariance(&self) -> ArrayView2<'_, f64>
pub fn covariance(&self) -> ArrayView2<'_, f64>
Coefficient covariance (inverse observed information at the MLE).
Sourcepub fn log_partial_likelihood(&self) -> f64
pub fn log_partial_likelihood(&self) -> f64
Maximized log partial likelihood.
Sourcepub fn linear_predictors(&self) -> Array1<f64>
pub fn linear_predictors(&self) -> Array1<f64>
Linear predictors (risk scores) ηᵢ = xᵢᵀβ.
Sourcepub fn coefficient_standard_errors(&self) -> Array1<f64>
pub fn coefficient_standard_errors(&self) -> Array1<f64>
Coefficient standard errors √diag(cov).
Sourcepub fn baseline_cumulative_hazard_stratum(&self, s: usize) -> Vec<(f64, f64)>
pub fn baseline_cumulative_hazard_stratum(&self, s: usize) -> Vec<(f64, f64)>
The Breslow baseline cumulative hazard Ĥ₀(t) for stratum s as
(time, H₀) pairs at the distinct event times (a right-continuous step
function). For an unstratified fit use s = 0.
Sourcepub fn baseline_cumulative_hazard(&self) -> Vec<(f64, f64)>
pub fn baseline_cumulative_hazard(&self) -> Vec<(f64, f64)>
The Breslow baseline cumulative hazard for the first stratum — the common unstratified case.
Sourcepub fn concordance(&self) -> f64
pub fn concordance(&self) -> f64
Harrell’s concordance index (C-index): the fraction of comparable
subject pairs (within the same stratum) whose predicted risk ordering
agrees with their observed survival ordering. 0.5 is chance, 1.0
perfect; ties in risk count as half. NaN if there are no comparable
pairs.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for CoxFit
impl RefUnwindSafe for CoxFit
impl Send for CoxFit
impl Sync for CoxFit
impl Unpin for CoxFit
impl UnsafeUnpin for CoxFit
impl UnwindSafe for CoxFit
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.