coxph_pairs: Pairwise 'coxph' comparisons

coxph_pairsR Documentation

Pairwise coxph comparisons

Description

Evaluate pairwise group differences in hazard ratios with coxph. Note this function is intended to be used for uni-variable models with a factor-like predictor.

Usage

coxph_pairs(
  object,
  data = NULL,
  level = 0.95,
  ...,
  relevel = FALSE,
  method = p.adjust.methods,
  digits = getOption("digits")
)

Arguments

object

an object of class survdiff, survfit, or coxph

alternatively, a formula in which case data must be provided

data

(optional) a data frame in which to interpret the variables named in object if given as a formula

level

the confidence level for intervals; see confint

...

additional arguments passed to survdiff such as na.action or rho to control the test

relevel

logical; if TRUE, the reference group is flipped

method

p-value correction method (default is 'holm'; see p.adjust

digits

integer indicating the number of decimal places to be used

Value

A list with three elements:

n

the number of subjects in each pair of groups

n_model

the number of subjects in each pair of models, i.e., excluding subjects with missing data

hr

the hazard ratio between pairs of groups

lci

the lower level confidence intervals for each hazard ratio

uci

the upper level confidence intervals for each hazard ratio

p.value

Wald p-value for each hazard ratio. The lower and upper triangles of the matrix are uncorrected and adjusted, respectively, for multiple comparisons using method (the default is the Holm correction, see p.adjust)

See Also

survdiff_pairs

Examples

library('survival')
sdif <- survdiff(Surv(time, status) ~ sex, data = lung)
sfit <- survfit(Surv(time, status) ~ sex, data = lung)
cxph <- coxph(Surv(time, status) ~ sex, data = lung)

stopifnot(
  identical(coxph_pairs(sdif), coxph_pairs(sfit)),
  identical(coxph_pairs(sdif), coxph_pairs(cxph))
)

## numeric and integer variables will be treated as factor-like
sfit <- survfit(Surv(time, status) ~ extent, data = colon)
kmplot(sfit, add = TRUE)
coxph_pairs(sfit)$n

leg <- function(ii, jj, ...) {
  hr <- coxph_pairs(sfit, ...)
  sapply(seq_along(ii), function(i) {
    j <- ii[i]
    i <- jj[i]
    sprintf(
      '%s vs %s: n=%s, HR: %.2f (%.2f - %.2f), %s',
      i, j, hr$n[i, j], hr$hr[i, j], hr$lci[i, j], hr$uci[i, j],
      pvalr(hr$p.value[i, j], show.p = TRUE)
    )
  })
}
legend(
  'bottomleft', leg(c('1', '1', '1'), c('2', '3', '4')),
  lty = 1, col = 2:4, bty = 'n'
)


## compare
f <- function(x) exp(coef(x))
f(coxph(Surv(time, status) ~ extent, data = colon[colon$extent %in% c(1, 2), ]))
f(coxph(Surv(time, status) ~ extent, data = colon[colon$extent %in% c(1, 3), ]))
f(coxph(Surv(time, status) ~ extent, data = colon[colon$extent %in% c(1, 4), ]))
## etc ...


## rawr >= 1.0.0 allows multiple variables
sfit <- survfit(Surv(time, status) ~ sex + extent, data = colon)
coxph_pairs(sfit, method = 'BH')


## also allows for a formula to be passed (data arg required)
coxph_pairs(Surv(time, status) ~ sex + extent, data = colon)


## strata will be ignored
sdif <- survdiff(Surv(time, status) ~ sex + strata(inst), data = lung)
coxph_pairs(sdif)


raredd/rawr documentation built on March 4, 2024, 1:36 a.m.