Skip to main content

CoxFit

Struct CoxFit 

Source
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:

  • stratified fits a separate baseline per stratum with shared coefficients — the standard remedy when the proportional-hazards assumption holds only within subgroups.
  • counting_process accepts (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

Source

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
Source

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.

Source

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.

Source

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.

Source

pub fn n_observations(&self) -> usize

Number of observations (episodes, for counting-process data).

Source

pub fn n_parameters(&self) -> usize

Number of covariates.

Source

pub fn n_events(&self) -> usize

Number of observed events.

Source

pub fn n_strata(&self) -> usize

Number of strata (1 for an unstratified fit).

Source

pub fn ties(&self) -> Ties

Tie-handling rule used.

Source

pub fn iterations(&self) -> usize

Newton iterations taken to converge.

Source

pub fn design_matrix(&self) -> ArrayView2<'_, f64>

The covariate matrix as fitted.

Source

pub fn time(&self) -> ArrayView1<'_, f64>

Event/censoring (stop) times.

Source

pub fn event(&self) -> ArrayView1<'_, f64>

Event indicators.

Source

pub fn coefficients(&self) -> ArrayView1<'_, f64>

Estimated coefficients — log hazard ratios, aligned to the covariate columns.

Source

pub fn hazard_ratios(&self) -> Array1<f64>

Hazard ratios exp(βⱼ).

Source

pub fn covariance(&self) -> ArrayView2<'_, f64>

Coefficient covariance (inverse observed information at the MLE).

Source

pub fn log_partial_likelihood(&self) -> f64

Maximized log partial likelihood.

Source

pub fn linear_predictors(&self) -> Array1<f64>

Linear predictors (risk scores) ηᵢ = xᵢᵀβ.

Source

pub fn coefficient_standard_errors(&self) -> Array1<f64>

Coefficient standard errors √diag(cov).

Source

pub fn z_values(&self) -> Array1<f64>

Wald z-statistics βⱼ / seⱼ.

Source

pub fn p_values(&self) -> Array1<f64>

Two-sided Wald p-values from the standard normal.

Source

pub fn aic(&self) -> f64

AIC for the Cox model, −2·ℓ_partial + 2p.

Source

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.

Source

pub fn baseline_cumulative_hazard(&self) -> Vec<(f64, f64)>

The Breslow baseline cumulative hazard for the first stratum — the common unstratified case.

Source

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§

Source§

impl Clone for CoxFit

Source§

fn clone(&self) -> CoxFit

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CoxFit

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V