Stratified cox regression in R using package namespace

Tag: r , cox-regression Author: zhaolei1015 Date: 2013-06-15

I'm doing stratified cox regression in R with the survival package:

cox <- coxph(response~strata(x), data=data)

This works fine, but when using the sampling package the strata function from the survival package is hidden and cannot be used directly.

However, calling it with the explicit namespace survival::strata does not create a stratified analysis:

library(survival)

# sample data
mort28day <- c(1,0,1,0,0,0,0,1,0,0,0,1,1,0,1,0,0,1,0,1,1,1,1,0,1,1,1,0,0,1)
daysurv <- c(4,29,24,29,29,29,29,19,29,29,29,3,9,29,15,29,29,11,29,5,13,20,22,29,16,21,9,29,29,15)
levo <- c(0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0)
acbi30 <- data.frame(mort28day, daysurv, levo)

# create survival object
test.Surv <- Surv(acbi30$daysurv, acbi30$mort28day)

test.surv_fit_1 <- survfit(coxph(test.Surv~strata(levo), data=acbi30))
test.surv_fit_2 <- survfit(coxph(test.Surv~survival::strata(levo), data=acbi30))

So,

test.surv_fit_1$strata
# levo=0 levo=1 
#    12      3 

test.surv_fit_2$strata
# NULL

Why is this the case and how can I access this function when it is hidden by sampling::strata (apart from detaching the sampling package ;-)

I'm using R version 2.15.2

Thanks, DeltaKappa


Update:

As a workaround to using survival::strata together with the sampling package I did

library(survival)
library(sampling, pos="package:base")

which loads the sampling package with a low search priority right above the base package. Then I can do

coxph(test.Surv~strata(levo), data=acbi30)

while calling sampling::strata explicitly.

coxph looks for certain functions, including strata, in the supplied formula to identify as "special" terms. It calls terms.formula (via terms) with special = c("strata", "cluster", "tt"), and it looks like this fails to identify survival::strata as a special term. That missing special term then propagates throughout the rest of the calculations and leaves you with no strata. terms.formula uses internal code, and I don't have the nerve (or perhaps the know-how) for source-surfing right now.
I guessed, it would be something in that direction, since assigning st <- strata(levo) and using it in the formula test.Surv~st also didn't work... thanks for claryfing @BenBarnes