Description Usage Arguments Details Value Side Effects Case weights Special terms Convergence Ties Penalized regression References See Also Examples
Fits a Cox proportional hazards regression model. Time dependent variables, time dependent strata, multiple events per subject, and other extensions are incorporated using the counting process formulation of Andersen and Gill.
1 2 3 4 5 6 
formula 
a formula object, with the response on the left of a 
data 
a data.frame in which to interpret the variables named in
the 
weights 
vector of case weights, see the note below. For a thorough discussion of these see the book by Therneau and Grambsch. 
subset 
expression indicating which subset of the rows of data should be used in the fit. All observations are included by default. 
na.action 
a missingdata filter function. This is applied to the model.frame
after any
subset argument has been used. Default is 
init 
vector of initial values of the iteration. Default initial value is zero for all variables. 
control 
Object of class 
ties 
a character string specifying the method for tie handling. If there are no tied death times all the methods are equivalent. Nearly all Cox regression programs use the Breslow method by default, but not this one. The Efron approximation is used as the default here, it is more accurate when dealing with tied death times, and is as efficient computationally. The “exact partial likelihood” is equivalent to a conditional logistic model, and is appropriate when the times are a small set of discrete values. See further below. 
singular.ok 
logical value indicating how to handle collinearity in the model matrix.
If 
robust 
should a robust variance be computed.
The default is TRUE if: there is a 
id 
optional variable name that identifies subjects. Only
necessary when a subject can have multiple rows in the data, and
there is more than one event type. This variable will normally be
found in 
cluster 
optional variable which clusters the observations, for
the purposes of a robust variance. If present, it implies

istate 
optional variable giving the current state at the start
each interval. This variable will normally be found in 
statedata 
optional data set used to describe multistate models. 
model 
logical value: if 
x 
logical value: if 
y 
logical value: if 
tt 
optional list of timetransform functions. 
method 
alternate name for the 
nocenter 
columns of the X matrix whose values lie strictly within this set are not recentered 
... 
Other arguments will be passed to 
The proportional hazards model is usually expressed in terms of a single survival time value for each person, with possible censoring. Andersen and Gill reformulated the same problem as a counting process; as time marches onward we observe the events for a subject, rather like watching a Geiger counter. The data for a subject is presented as multiple rows or "observations", each of which applies to an interval of observation (start, stop].
The routine internally scales and centers data to avoid overflow in
the argument to the exponential function. These actions do not change
the result, but lead to more numerical stability.
Any column of the X matrix whose values lie within nocenter
list
are not recentered. The practical consequence of the default is to not
recenter dummy variables corresponding to factors.
However, arguments to offset are not scaled since there are situations
where a large offset value is a purposefully used.
In general, however, users should not avoid very large numeric values
for an offset due to possible loss of precision in the estimates.
an object of class coxph
representing the fit.
See coxph.object
for details.
Depending on the call, the predict
, residuals
,
and survfit
routines may
need to reconstruct the x matrix created by coxph
.
It is possible for this to fail, as in the example below in
which the predict function is unable to find tform
.
1 2 3 
In such a case add the model=TRUE
option to the
coxph
call to obviate the
need for reconstruction, at the expense of a larger fit
object.
Case weights are treated as replication weights, i.e., a case weight of 2 is equivalent to having 2 copies of that subject's observation. When computers were much smaller grouping like subjects together was a common trick to used to conserve memory. Setting all weights to 2 for instance will give the same coefficient estimate but halve the variance. When the Efron approximation for ties (default) is employed replication of the data will not give exactly the same coefficients as the weights option, and in this case the weighted fit is arguably the correct one.
When the model includes a cluster
term or the robust=TRUE
option the computed variance treats any weights as sampling weights;
setting all weights to 2 will in this case give the same variance as weights of 1.
There are three special terms that may be used in the model equation.
A strata
term identifies a stratified Cox model; separate baseline
hazard functions are fit for each strata.
The cluster
term is used to compute a robust variance for the model.
The term + cluster(id)
where each value of id
is unique is
equivalent to
specifying the robust=TRUE
argument.
If the id
variable is not
unique, it is assumed that it identifies clusters of correlated
observations.
The robust estimate arises from many different arguments and thus has
had many labels. It is variously known as the
Huber sandwich estimator, White's estimate (linear models/econometrics),
the HorvitzThompson estimate (survey sampling), the working
independence variance (generalized estimating equations), the
infinitesimal jackknife, and the Wei, Lin, Weissfeld (WLW) estimate.
A timetransform term allows variables to vary dynamically in time. In
this case the tt
argument will be a function or a list of
functions (if there are more than one tt() term in the model) giving the
appropriate transform. See the examples below.
One user mistake that has recently arisen is to slavishly follow the
advice of some coding guides and prepend survival::
onto
everthing, including the special terms, e.g.,
survival::coxph(survival:Surv(time, status) ~ age +
survival::cluster(inst), data=lung)
First, this is unnecessary: arguments within the coxph
call will
be evaluated within the survival namespace, so another package's Surv or
cluster function would not be noticed.
(Full qualification of the coxph call itself may be protective, however.)
Second, and more importantly, the call just above will not give the
correct answer. The specials are recognized by their name, and
survival::cluster
is not the same as cluster
; the above model would
treat inst
as an ordinary variable.
A similar issue arises from using stats::offset
as a term, in
either survival or glm models.
In certain data cases the actual MLE estimate of a coefficient is infinity, e.g., a dichotomous variable where one of the groups has no events. When this happens the associated coefficient grows at a steady pace and a race condition will exist in the fitting routine: either the log likelihood converges, the information matrix becomes effectively singular, an argument to exp becomes too large for the computer hardware, or the maximum number of interactions is exceeded. (Most often number 1 is the first to occur.) The routine attempts to detect when this has happened, not always successfully. The primary consequence for the user is that the Wald statistic = coefficient/se(coefficient) is not valid in this case and should be ignored; the likelihood ratio and score tests remain valid however.
There are three possible choices for handling tied event times. The Breslow approximation is the easiest to program and hence became the first option coded for almost all computer routines. It then ended up as the default option when other options were added in order to "maintain backwards compatability". The Efron option is more accurate if there are a large number of ties, and it is the default option here. In practice the number of ties is usually small, in which case all the methods are statistically indistinguishable.
Using the "exact partial likelihood" approach the Cox partial likelihood
is equivalent to that for matched logistic regression. (The
clogit
function uses the coxph
code to do the fit.)
It is technically appropriate when the time scale is discrete and has
only a few unique values, and some packages refer to this as the
"discrete" option. There is also an "exact marginal likelihood" due to
Prentice which is not implemented here.
The calculation of the exact partial likelihood is numerically intense. Say for instance 180 subjects are at risk on day 7 of which 15 had an event; then the code needs to compute sums over all 180choose15 > 10^43 different possible subsets of size 15. There is an efficient recursive algorithm for this task, but even with this the computation can be insufferably long. With (start, stop) data it is much worse since the recursion needs to start anew for each unique start time.
A separate issue is that of artificial ties due to floatingpoint
imprecision. See the vignette on this topic for a full explanation or
the timefix
option in coxph.control
.
Users may need to add timefix=FALSE
for simulated data sets.
coxph
can maximise a penalised partial likelihood with
arbitrary userdefined penalty. Supplied penalty functions include
ridge regression (ridge), smoothing splines
(pspline), and frailty models (frailty).
Andersen, P. and Gill, R. (1982). Cox's regression model for counting processes, a large sample study. Annals of Statistics 10, 11001120.
Therneau, T., Grambsch, P., Modeling Survival Data: Extending the Cox Model. SpringerVerlag, 2000.
coxph.object
, coxph.control
,
cluster
, strata
, Surv
,
survfit
, pspline
,
ridge
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34  # Create the simplest test data set
test1 < list(time=c(4,3,1,1,2,2,3),
status=c(1,1,1,0,1,1,0),
x=c(0,2,1,1,1,0,0),
sex=c(0,0,0,0,1,1,1))
# Fit a stratified model
coxph(Surv(time, status) ~ x + strata(sex), test1)
# Create a simple data set for a timedependent model
test2 < list(start=c(1,2,5,2,1,7,3,4,8,8),
stop=c(2,3,6,7,8,9,9,9,14,17),
event=c(1,1,1,1,1,1,1,0,0,0),
x=c(1,0,0,1,0,1,1,1,0,0))
summary(coxph(Surv(start, stop, event) ~ x, test2))
#
# Create a simple data set for a timedependent model
#
test2 < list(start=c(1, 2, 5, 2, 1, 7, 3, 4, 8, 8),
stop =c(2, 3, 6, 7, 8, 9, 9, 9,14,17),
event=c(1, 1, 1, 1, 1, 1, 1, 0, 0, 0),
x =c(1, 0, 0, 1, 0, 1, 1, 1, 0, 0) )
summary( coxph( Surv(start, stop, event) ~ x, test2))
# Fit a stratified model, clustered on patients
bladder1 < bladder[bladder$enum < 5, ]
coxph(Surv(stop, event) ~ (rx + size + number) * strata(enum),
cluster = id, bladder1)
# Fit a time transform model using current age
coxph(Surv(time, status) ~ ph.ecog + tt(age), data=lung,
tt=function(x,t,...) pspline(x + t/365.25))

Call:
coxph(formula = Surv(time, status) ~ x + strata(sex), data = test1)
coef exp(coef) se(coef) z p
x 0.8023 2.2307 0.8224 0.976 0.329
Likelihood ratio test=1.09 on 1 df, p=0.2971
n= 7, number of events= 5
Call:
coxph(formula = Surv(start, stop, event) ~ x, data = test2)
n= 10, number of events= 7
coef exp(coef) se(coef) z Pr(>z)
x 0.02111 0.97912 0.79518 0.027 0.979
exp(coef) exp(coef) lower .95 upper .95
x 0.9791 1.021 0.2061 4.653
Concordance= 0.526 (se = 0.129 )
Likelihood ratio test= 0 on 1 df, p=1
Wald test = 0 on 1 df, p=1
Score (logrank) test = 0 on 1 df, p=1
Call:
coxph(formula = Surv(start, stop, event) ~ x, data = test2)
n= 10, number of events= 7
coef exp(coef) se(coef) z Pr(>z)
x 0.02111 0.97912 0.79518 0.027 0.979
exp(coef) exp(coef) lower .95 upper .95
x 0.9791 1.021 0.2061 4.653
Concordance= 0.526 (se = 0.129 )
Likelihood ratio test= 0 on 1 df, p=1
Wald test = 0 on 1 df, p=1
Score (logrank) test = 0 on 1 df, p=1
Call:
coxph(formula = Surv(stop, event) ~ (rx + size + number) * strata(enum),
data = bladder1, cluster = id)
coef exp(coef) se(coef) robust se z p
rx 0.52598 0.59097 0.31583 0.31524 1.669 0.09521
size 0.06961 1.07209 0.10156 0.08863 0.785 0.43220
number 0.23818 1.26894 0.07588 0.07459 3.193 0.00141
rx:strata(enum)enum=2 0.10633 0.89913 0.50424 0.33396 0.318 0.75019
rx:strata(enum)enum=3 0.17251 0.84155 0.55780 0.39868 0.433 0.66523
rx:strata(enum)enum=4 0.10945 0.89632 0.65730 0.50636 0.216 0.82886
size:strata(enum)enum=2 0.14737 0.86298 0.16803 0.11409 1.292 0.19646
size:strata(enum)enum=3 0.28345 0.75318 0.20894 0.15220 1.862 0.06255
size:strata(enum)enum=4 0.27607 0.75876 0.25222 0.18904 1.460 0.14418
number:strata(enum)enum=2 0.10125 0.90370 0.11904 0.11759 0.861 0.38920
number:strata(enum)enum=3 0.06467 0.93738 0.12925 0.12035 0.537 0.59101
number:strata(enum)enum=4 0.09429 1.09888 0.14594 0.11973 0.788 0.43097
Likelihood ratio test=30.09 on 12 df, p=0.002708
n= 340, number of events= 112
Call:
coxph(formula = Surv(time, status) ~ ph.ecog + tt(age), data = lung,
tt = function(x, t, ...) pspline(x + t/365.25))
coef se(coef) se2 Chisq DF p
ph.ecog 0.4528 0.1178 0.1174 14.7704 1.00 0.00012
tt(age), linear 0.0112 0.0093 0.0093 1.4414 1.00 0.22991
tt(age), nonlin 2.6992 3.08 0.45431
Iterations: 4 outer, 10 NewtonRaphson
Theta= 0.796
Degrees of freedom for terms= 1.0 4.1
Likelihood ratio test=22.5 on 5.07 df, p=5e04
n= 227, number of events= 164
(1 observation deleted due to missingness)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.