3
votes

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.

1
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. - BenBarnes
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 - DeltaKappa

1 Answers

0
votes

The proximate reason for failure was correctly described in the comments , but there is an incredibly simple fix that was not offered. As you pointed out the strata function is not exposed but the use of the triple-colon operator (:::) lets you get around that aspect:

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))
test.surv_fit_1
#--------
Call: survfit(formula = coxph(test.Surv ~ strata(levo), data = acbi30))

        n events median 0.95LCL 0.95UCL
levo=0 25     13     24      19      NA
levo=1  5      2     NA      13      NA

test.surv_fit_2
#-------------
Call: survfit(formula = coxph(test.Surv ~ survival:::strata(levo), 
    data = acbi30))

      n events median 0.95LCL 0.95UCL
[1,] 30     15     24      19      NA

Whether this was the correct solution in the context of using the sampling package, I'm not able to render an opinion, but it would definitely circumvent the masking of the survival:::strata function.