Categories &

Functions List

Function Reference: coxphfit

statistics: b = coxphfit (X, T)
statistics: b = coxphfit (X, T, name, value, …)
statistics: [b, logl] = coxphfit (…)
statistics: [b, logl, H] = coxphfit (…)
statistics: [b, logl, H, stats] = coxphfit (…)

Fit a Cox proportional hazards regression model.

b = coxphfit (X, T) returns the p-by-1 vector of coefficients b of the Cox model

$$ h(x_i, t) = h_0(t)\exp\left(\sum_{j=1}^{p} x_{ij} b_j\right) $$

fitted to the n-by-p matrix of predictors X and the n-by-1 vector of event times T. T may instead be an n-by-2 matrix whose rows give a (start, stop] interval of exposure, the counting process form, in which an observation joins the risk set only after its start time. h_0(t) is the baseline hazard, which is left unspecified: the coefficients are estimated by maximizing the Cox partial likelihood, which does not involve it.

X must not contain a column of ones. The model has no constant term, since any constant is absorbed into the baseline hazard. A constant column is detected, reported by a warning, and given a zero coefficient.

Rows of X, T or "Frequency" holding NaN are removed before fitting.

[b, logl, H, stats] = coxphfit (…) additionally returns the maximized partial log-likelihood logl, the estimated baseline cumulative hazard H, and a structure stats of coefficient statistics and residuals.

H is a two-column matrix whose first column holds the distinct event times and whose second holds the estimated cumulative hazard at those times, evaluated at the predictor values given by "Baseline". Its first row is the first event time with a cumulative hazard of zero; an observation censored before any event contributes no row. In a stratified model H gains a third column carrying the stratum, the blocks appear in ascending stratum order, and each block leads with its own zero row at its own first event time. A stratum holding no event contributes a single row of NaN with its label.

The following name/value pairs are accepted:

NameValue
"Baseline"The X values at which the baseline hazard is computed, either a scalar or a 1-by-p vector. The default is the mean of X weighted by "Frequency" and taken within each stratum, so the hazard is that of an average observation of its stratum; pass 0 for a hazard relative to the origin. A value given explicitly is used for every stratum. The coefficients do not depend on this choice, only H does.
"Censoring"A logical or 0/1 vector of length n, where 1 marks an observation right-censored at its recorded time. The default is a vector of zeros, so every observation is a recorded event.
"Frequency"A vector of length n of non-negative values giving the number of observations each row represents, or a weight. The default is a vector of ones.
"Ties"The method of handling tied event times, either "breslow" (default) or "efron".
"B0"The starting value of the iteration, a vector of length p. The default is 0.01 ./ std (X).
"Options"A structure of iteration settings, as built by statset ("coxphfit"). The fields used are "MaxIter", "TolX" and "Display".
"Strata"A vector of length n of stratum labels. Each stratum carries its own baseline hazard and its own risk sets, while the coefficients are shared across all of them. A predictor that does not vary within any stratum cannot be estimated from a stratified fit; it is reported by a warning and held at zero.

Source Code: coxphfit

The fields of stats are:

FieldContents
"covb"The estimated covariance matrix of b.
"beta"The coefficients, as returned in b.
"se"The standard errors of the coefficients.
"z"The z statistics, b over its standard error.
"p"The two-sided p-values of the z statistics.
"csres"The Cox-Snell residuals.
"devres"The deviance residuals.
"martres"The martingale residuals.
"schres"The Schoenfeld residuals, NaN for a censored observation. The mean an event is measured against follows "Ties": under "efron" a tied death is measured against the mean over the sub-risk sets that approximation splits the tie into, so that every tied death at one time shares one mean and the residual does not depend on the order the tie was recorded in. Without a tie the two methods agree.
"sschres"The scaled Schoenfeld residuals.
"scores"The score residuals.
"sscores"The scaled score residuals.
"LikelihoodRatioTestP"The p-value of the likelihood ratio test against the model with no predictors.

Source Code: coxphfit

Two documented deviations, both where R2024a disagrees with itself. The martingale residual is defined as the event indicator minus the cumulative hazard the observation actually experienced, so "csres" and "martres" must sum to that indicator. They do here, always.

Under "efron" ties MATLAB’s do not: its "martres" comes from a cumulative hazard agreeing neither with its own "csres" nor with the H it returns, and the two sum to 1.0437 and -0.0414 where they must give 1 and 0.

In the counting process form MATLAB’s "martres" correctly subtracts the hazard accrued before the observation entered, but its "csres" does not, so the two disagree by exactly that amount for any row whose start time follows an event. Here both account for it, so "csres" differs from MATLAB’s by \Lambda(start) \exp (x'b) and the identity is preserved.

The score residuals inherit the first of those two deviations, being an integral against the martingale residual: under "efron" ties they differ from MATLAB’s, whose own do not sum to the score at the maximum, while these sum to zero under both tie methods, weighted by "Frequency" where one is given.

Every other output agrees with R2024a to machine precision, across censoring, weights, both tie methods, stratification, and the counting process form.

See also: statset, ecdf, fitlm

Source Code: coxphfit

Fit a Cox model to right-censored survival data

 T = [4; 6; 8; 11; 13; 16; 18; 21; 25; 30];
 X = [2 0; 5 1; 3 0; 8 1; 4 0; 7 1; 6 0; 9 1; 5 0; 10 1];
 censored = [0; 0; 1; 0; 0; 1; 0; 0; 1; 0];
 [b, logl] = coxphfit (X, T, 'Censoring', censored)
b =

  -1.0423
   3.3745

logl = -7.6974

Compare the two methods of handling tied event times

 T = [4; 4; 6; 6; 8; 8; 11; 11; 13; 13];
 X = [2 0; 5 1; 3 0; 8 1; 4 0; 7 1; 6 0; 9 1; 5 0; 10 1];
 breslow = coxphfit (X, T, 'Ties', 'breslow');
 efron = coxphfit (X, T, 'Ties', 'efron');
 [breslow, efron]
ans =

  -1.0232  -1.1267
   3.9741   4.3931