Cox partial likelihood over three event times with a binary covariate

At each event time the term is the risk score of the person who had the event divided by the summed risk scores of everyone still at risk just before that time. Multiplying the terms gives the partial likelihood, in which the baseline hazard has cancelled, so beta can be estimated without it. With x coded 1 for treatment and 0 for the comparator, a treated person scores r = exp(beta), the form of HE-FM-HR-002, and a comparator person scores 1, so each risk set reduces to two counts. Three event times are written out, and more event times add further terms. Only the order of events matters, not the gaps between them. The maximising value of beta comes from HE-CF-CPH-001, and HE-FM-HR-002 then gives the hazard ratio.

Signature

r = exp(beta); p_1 = r^x_1 / (n0_1 + n1_1 * r); p_2 = r^x_2 / (n0_2 + n1_2 * r); p_3 = r^x_3 / (n0_3 + n1_3 * r); L = p_1 * p_2 * p_3; logL = log(L)
Inputs
InputsDefinitionUnit
betaValue of the coefficient of the binary covariate at which the partial likelihood is evaluatedlog hazard ratio, no unit
x_11 if the person with the first event was treated, 0 if comparatorindicator
n0_1Number with x = 0 at risk just before the first event time, including the person with the event if comparatorpeople
n1_1Number with x = 1 at risk just before the first event time, including the person with the event if treatedpeople
x_21 if the person with the second event was treated, 0 if comparatorindicator
n0_2Number with x = 0 at risk just before the second event timepeople
n1_2Number with x = 1 at risk just before the second event timepeople
x_31 if the person with the third event was treated, 0 if comparatorindicator
n0_3Number with x = 0 at risk just before the third event timepeople
n1_3Number with x = 1 at risk just before the third event timepeople
Output
rExponential of beta, the risk score of a person with x = 1; a person with x = 0 scores 1ratio, no unit
p_1Risk score of the person with the first event over the summed risk scores of the risk setprobability
p_2Risk score of the person with the second event over the summed risk scores of the risk setprobability
p_3Risk score of the person with the third event over the summed risk scores of the risk setprobability
LProduct of the terms at all event timesnone
logLNatural log of L, the quantity statistical software reportsnone

Function

Cox proportional hazards model function for hazards, partial likelihood and baseline curves

Maps right-censored survival records with covariates to the coefficients of the Cox model, their standard errors and the estimated baseline curve. HE-FM-CPH-001 gives the hazard and survival of one covariate profile from a baseline and a linear predictor, HE-FM-CPH-002 the partial likelihood over the risk sets, HE-FM-CPH-003 one step of the Breslow baseline cumulative hazard, HE-FM-CPH-004 the Breslow and Efron terms for two tied deaths, and HE-FM-CPH-005 a hazard ratio that changes with log time. HE-CF-CPH-001 maximises the partial likelihood by Newton-Raphson. HE-FM-HR-002 turns a coefficient into a hazard ratio, HE-FM-HR-006 gives its confidence interval, HE-FN-CUMH-001 links a cumulative hazard to survival and HE-FM-HR-003 is the power rule for a survival curve, and none of these is restated here as a formula of its own. Notation follows the Cox Proportional Hazards article.

Computational function

  • Computational function: Newton-Raphson maximum partial likelihood estimate of a Cox coefficient with its standard error

    Maximises the Cox partial likelihood of HE-FM-CPH-002 by Newton-Raphson, returning the coefficient and its standard error from patient records. The inputs differ from the formula's counts, since the code takes whole vectors of times, event indicators and covariate values and builds each risk set itself. At each death time the score adds the covariate of the person who died less the risk-weighted mean covariate of the risk set, and the information adds the risk-weighted variance of the covariate. Each step moves beta by U / I. Tied deaths share one risk set here, which is the Breslow approximation. The calculator line is one step for a binary covariate and three deaths, using the 0 and 1 simplification of the variance. The standard error is the inverse root of the information, the usual maximum likelihood result, and it agrees with coxph on both examples below. HE-FM-HR-002 turns the estimate into a hazard ratio and HE-FM-HR-006 gives its interval.

    Inputs and outputs: beta is the current value of the coefficient, 0 to start, on the log hazard ratio scale. x_1, x_2 and x_3 are the covariates of the people with each death, 0 or 1. n0_1, n0_2 and n0_3 count comparator patients at risk at each death, and n1_1, n1_2 and n1_3 count treated patients. The chain returns r, the risk score of a treated person, xbar_1, xbar_2 and xbar_3, the risk-weighted mean covariates of the risk sets, U, the slope of the log partial likelihood at beta, I, minus its second derivative at beta, beta_next, the coefficient after one step, SE_beta, the standard error at the current beta, and HR, the hazard ratio at beta_next.

    Assumption: As in HE-AS-CPH-003 to HE-AS-CPH-005, except that the R, Python and Excel versions take one covariate of any scale and do not use the 0 and 1 simplification. The code treats tied deaths by the Breslow rule, so it matches coxph(..., ties = "breslow") and matches the default Efron fit only without ties. Both functions subtract the mean of x first, which leaves beta and its standard error unchanged and stops a covariate on a large scale overflowing the exponential. On edge inputs they return no estimate, with the note "no information" when there are no events or x is constant, the note "no finite estimate" when one arm has no events or the coefficient runs away so that its product with the largest centred x passes 30 in absolute value, and the note "not converged" after 25 steps.

    Worked example (Therneau test data 1, first step): Six subjects with deaths at times 1, 6, 6 and 9. The death at time 9 is alone in its risk set and adds 0 to both U and I, so three terms suffice, and the result matches the vignette's U(0) = 1, H(0) = 0.625 and first iteration of 8/5. beta = 0; x_1 = 1; n0_1 = 3; n1_1 = 3; x_2 = 1; n0_2 = 3; n1_2 = 1; x_3 = 0; n0_3 = 3; n1_3 = 1; r = 1; xbar_1 = 0.5; xbar_2 = 0.25; xbar_3 = 0.25; U = 1; I = 0.625; beta_next = 1.6; SE_beta = 1.2649; HR = 4.953

    Worked example (Therneau test data 1, at the solution): At the vignette's solution 1.475285 the score is zero, so beta does not move, I matches the vignette's H of 0.6341681 and SE_beta matches the coxph standard error of 1.255734. beta = 1.475285; x_1 = 1; n0_1 = 3; n1_1 = 3; x_2 = 1; n0_2 = 3; n1_2 = 1; x_3 = 0; n0_3 = 3; n1_3 = 1; r = 4.3723; xbar_1 = 0.8139; xbar_2 = 0.5931; xbar_3 = 0.5931; U = 0; I = 0.6342; beta_next = 1.4753; SE_beta = 1.2557; HR = 4.3723

    Worked example (article's four patients, first step): The slope is the article's 1-2r/(1+r)-2r/(2r+1) at r = 1. Repeating the step reaches -0.9406, the article's estimate, with a standard error of 1.2403 (coxph gives 1.240258). HE-FM-HR-006 with z = 1.96 then gives an interval of about 0.03 to 4.44 around 0.39, far too wide for inference, as the article warns for three events. beta = 0; x_1 = 0; n0_1 = 2; n1_1 = 2; x_2 = 1; n0_2 = 1; n1_2 = 2; x_3 = 0; n0_3 = 1; n1_3 = 1; r = 1; xbar_1 = 0.5; xbar_2 = 0.6667; xbar_3 = 0.5; U = -0.6667; I = 0.7222; beta_next = -0.9231; SE_beta = 1.1767; HR = 0.3973

    Excel: With one row per patient in TimeCol, StatusCol and CovCol and the current coefficient in BetaNow, the row formula =SUMPRODUCT((TimeCol>=A2)*CovCol*EXP(BetaNow*CovCol))/SUMPRODUCT((TimeCol>=A2)*EXP(BetaNow*CovCol)) fills MeanCol, where A2 is that row's time, and the same formula with CovCol^2 in place of CovCol fills SecondCol. Then =SUMPRODUCT(StatusCol*(CovCol-MeanCol)) goes in ScoreU, =SUMPRODUCT(StatusCol*(SecondCol-MeanCol^2)) in InfoI, =BetaNow+ScoreU/InfoI in BetaNext and =1/SQRT(InfoI) in StdErrBeta. Paste BetaNext into BetaNow as a value and repeat until it stops changing. With no events or a constant covariate InfoI is 0 and BetaNext returns #DIV/0!. A runaway coefficient on a covariate of large scale can push EXP to #NUM!, which is less likely once the mean is subtracted from CovCol.

    R: cox_nr <- function(time, status, x, beta = 0, tol = 1e-8, maxit = 25) { ev <- which(status == 1); if (length(ev) == 0 || length(unique(x)) < 2) return(list(beta = NA, se = NA, iter = 0, note = "no information")); x <- x-mean(x); v <- mean(x^2); si <- function(b) { U <- 0; I <- 0; for (i in ev) { w <- exp(b*x)*(time >= time[i]); m <- sum(w*x)/sum(w); U <- U+x[i]-m; I <- I+sum(w*x^2)/sum(w)-m^2 }; c(U, I) }; for (k in 1:maxit) { if (abs(beta)*max(abs(x)) > 30) return(list(beta = NA, se = NA, iter = k, note = "no finite estimate")); s <- si(beta); if (s[2] < 1e-8*v) return(list(beta = NA, se = NA, iter = k, note = "no finite estimate")); st <- s[1]/s[2]; beta <- beta+st; if (abs(st) < tol) return(list(beta = beta, se = 1/sqrt(si(beta)[2]), iter = k, note = "converged")) }; list(beta = NA, se = NA, iter = maxit, note = "not converged") } cox_nr(c(2, 3, 5, 7), c(1, 1, 1, 0), c(0, 1, 0, 1)) returns beta -0.9406 and se 1.2403. coxph(Surv(time, status) ~ x, ties = "breslow") from the survival package gives the same values.

    Python: def cox_si(time, status, x, b): ev = [i for i, s in enumerate(status) if s == 1]; rows = [[math.exp(b*c)*(t >= time[i]) for c, t in zip(x, time)] for i in ev]; m = [sum(a*c for a, c in zip(w, x))/sum(w) for w in rows]; return sum(x[i]-q for i, q in zip(ev, m)), sum(sum(a*c*c for a, c in zip(w, x))/sum(w)-q*q for w, q in zip(rows, m)) then def cox_nr(time, status, x, beta=0.0, tol=1e-8, maxit=25): z = [c-sum(x)/len(x) for c in x]; return dict(beta=None, se=None, note="no information") if (1 not in status or len(set(x)) < 2) else (dict(beta=None, se=None, note="not converged") if maxit == 0 else (dict(beta=None, se=None, note="no finite estimate") if abs(beta)*max(map(abs, z)) > 30 else (lambda U, I: dict(beta=None, se=None, note="no finite estimate") if I < 1e-8*sum(c*c for c in z)/len(z) else (dict(beta=beta+U/I, se=cox_si(time, status, z, beta+U/I)[1]**-0.5, note="converged") if abs(U/I) < tol else cox_nr(time, status, z, beta+U/I, tol, maxit-1)))(*cox_si(time, status, z, beta)))) Needs import math. The call cox_nr([2, 3, 5, 7], [1, 1, 1, 0], [0, 1, 0, 1]) returns the same values as the R function, and cox_nr([1, 1, 6, 6, 8, 9], [1, 0, 1, 1, 0, 1], [1, 1, 1, 0, 0, 0]) returns beta 1.4753 and se 1.2557.

    Test (first step on Therneau test data 1): With those six records entered and BetaNow = 0, BetaNext equals the vignette's 8/5. Expected result TRUE. FALSE shows a wrong risk set, as when subject 2, censored at time 1, is dropped, which gives a first step of 1.7886, or the step is doubled or halved. The Excel check is =ABS(BetaNext-8/5)<0.001.

    Test (article's closed form): After convergence on the article's four patients the exponential of BetaNow equals the root (sqrt(17)-1)/8 of 4r^2+r-1 = 0, obtained without iteration. Expected result TRUE. FALSE shows the iteration stopped early, as after one step, when it is 0.3973, or a wrong sign in the step. The Excel check is =ABS(EXP(BetaNow)-(SQRT(17)-1)/8)<0.001.

    Common error (0 and 1 variance shortcut on another coding): The form xbar*(1-xbar) holds only for a 0 and 1 covariate, as Therneau's note says. Coding the article's treatment as 0 and 2 with the shortcut gives I = -0.4444 at beta = 0 where the correct value is 2.8889, so the step goes the wrong way.

    Source: Therneau TM. Software validation. Vignette for the R survival package. Updated December 2022. Section 2 Basic formulas, equations 2, 3 and 5 (weighted mean, score, variance, and the sum of H(t) as the second derivative); section 3.1 (U, H, the 0 and 1 simplification, Newton-Raphson increments and the first iteration of 8/5); section 3.3 (the case when the Cox MLE is infinite). Therneau TM. survival: Survival Analysis. R package version 3.8-12, reference manual. 9 September 2026. Entry coxph.

    r = exp(beta); xbar_1 = n1_1 * r / (n0_1 + n1_1 * r); xbar_2 = n1_2 * r / (n0_2 + n1_2 * r); xbar_3 = n1_3 * r / (n0_3 + n1_3 * r); U = (x_1 - xbar_1) + (x_2 - xbar_2) + (x_3 - xbar_3); I = xbar_1 * (1 - xbar_1) + xbar_2 * (1 - xbar_2) + xbar_3 * (1 - xbar_3); beta_next = beta + U / I; SE_beta = 1 / sqrt(I); HR = exp(beta_next)

Try this function

Implementations

  • Excel

    Excel Cox partial likelihood over three event times

    With LogHazardRatio, the event covariates EventCovFirst, EventCovSecond and EventCovThird, and the risk-set counts ComparatorAtRiskFirst and so on, the formulas go in RiskRatio, TermFirst, TermSecond, TermThird, PartialLik and LogPartialLik.

    =EXP(LogHazardRatio); =RiskRatio^EventCovFirst/(ComparatorAtRiskFirst+TreatedAtRiskFirst*RiskRatio); =RiskRatio^EventCovSecond/(ComparatorAtRiskSecond+TreatedAtRiskSecond*RiskRatio); =RiskRatio^EventCovThird/(ComparatorAtRiskThird+TreatedAtRiskThird*RiskRatio); =TermFirst*TermSecond*TermThird; =LN(PartialLik)

Assumptions

  • One event at each event time in the Cox partial likelihood

    Each term has a single person with the event. Tied deaths need a rule, and HE-FM-CPH-004 gives the Breslow and Efron terms. Without ties the methods are equivalent, as the survival package manual states.

  • Censored people stay in the Cox risk set until they leave observation

    A censored person counts in every risk set up to the censoring time, and a person censored at an event time counts as at risk at that time, as in Therneau's test data 1. This is valid only when censoring is unrelated to the event process given the covariates.

  • Binary covariate coded 0 and 1 in the two-count Cox risk set

    The form n0 + n1 r holds only for a 0 and 1 covariate. For continuous or several covariates each person keeps an individual score, and the code in HE-CF-CPH-001 sums those for one covariate of any scale.

Worked examples

  • Article's four patients with no treatment effect

    The article's illustrative trial, with deaths at 2, 3 and 5 months and one treated patient censored at 7 months. With beta = 0 every person scores 1, so the terms are 1/4, 1/3 and 1/2 and the value is 1/24, as in the article. The log value is the first loglik the survival package reports for these data.

    beta = 0; x_1 = 0; n0_1 = 2; n1_1 = 2; x_2 = 1; n0_2 = 1; n1_2 = 2; x_3 = 0; n0_3 = 1; n1_3 = 1; r = 1; p_1 = 0.25; p_2 = 0.3333; p_3 = 0.5; L = 0.0417; logL = -3.1781
  • Article's four patients at the maximum partial likelihood estimate

    The same patients at the estimate from the article, where r = (sqrt(17)-1)/8, about 0.390. The value rises above the no-effect value of 0.0417, and the log value rounds the second loglik that the survival package reports for these data.

    beta = -0.9406; x_1 = 0; n0_1 = 2; n1_1 = 2; x_2 = 1; n0_2 = 1; n1_2 = 2; x_3 = 0; n0_3 = 1; n1_3 = 1; r = 0.3904; p_1 = 0.3596; p_2 = 0.2192; p_3 = 0.7192; L = 0.0567; logL = -2.87
  • Article's four patients at a harmful coefficient of 0.5

    The same patients at an illustrative positive coefficient. The value falls below the no-effect value, because the first and last deaths were comparator patients, which fits poorly with a treatment that raises the hazard.

    beta = 0.5; x_1 = 0; n0_1 = 2; n1_1 = 2; x_2 = 1; n0_2 = 1; n1_2 = 2; x_3 = 0; n0_3 = 1; n1_3 = 1; r = 1.6487; p_1 = 0.1888; p_2 = 0.3837; p_3 = 0.3775; L = 0.0273; logL = -3.5993

Common errors

  • Removing censored patients from the Cox risk sets

    Dropping patient 4 of the article's example, censored at 7 months, leaves risk sets 2 + r, 1 + r and 1. The estimate becomes r = sqrt(2), a hazard ratio of 1.41 against 0.39, so the treatment appears harmful rather than beneficial (derived and checked with the survival package).

  • Reporting a Cox coefficient when one group has no events

    If every event falls in one arm and someone from the other arm is at risk at an event time, the partial likelihood keeps rising as beta moves further in one direction, and the estimate is infinite. With three comparator deaths and one treated patient censored last, L tends to 1/6 as beta falls, and coxph stopped at about -20.85 with a standard error near 24,896 and a warning (survival 3.8.6). The survival package manual says the Wald statistic should then be ignored, and that likelihood ratio and score tests remain valid.

Sources

  • Cox 1972 conditional likelihood for the regression coefficients

    Cox DR. Regression models and life-tables. Journal of the Royal Statistical Society Series B (Methodological). 1972;34(2):187-202. The summary states that a conditional likelihood is obtained, leading to inferences about the unknown regression coefficients.

    View source →

  • Therneau's log partial likelihood term at each death time

    Therneau TM. Software validation. Vignette for the R survival package. Updated December 2022. Section 2 Basic formulas, equation 1, gives the contribution at time t as the sum of dNi(t) log(ri) less the log of the sum of Yi(t) wi ri. Section 3.1 describes each term as the log of the ratio of the score for the subject who had an event over the sum of scores (its wording goes on "for those who did not", while equation 1 and the printed terms include the person who had the event, as these records do), with r = exp(beta) for x = 1 and exp(0) = 1 for x = 0 in section 3.

    View source →

  • survival package manual on tied death times and infinite coefficients

    Therneau TM. survival: Survival Analysis. R package version 3.8-12, reference manual. 9 September 2026. Entry coxph. Under Arguments, ties says that if there are no tied death times all the methods are equivalent. Section Convergence says that in certain data cases the actual MLE estimate of a coefficient is infinity, for example a dichotomous variable where one of the groups has no events; the Wald statistic is not valid in this case and should be ignored, while the likelihood ratio and score tests remain valid.

    View source →

Canonical Identity

Stable URI · Machine-readable · Resolvable · CC BY 4.0

This formula is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).

Author: Dr Darrin Baines · Publisher: Intelligent Parameters Ltd · Identifier: https://healtheconomics.wiki/formula/cox-partial-likelihood-over-three-event-times-with-a-binary-covariate