[R] conflicting results for a time-varying coefficient in a Cox model

Ferenci Tamas t@m@@@|erenc| @end|ng |rom med@t@t@hu
Tue Aug 6 21:49:48 CEST 2019

Dear All,

I was thinking of two possible ways to plot a time-varying coefficient
in a Cox model.

One is simply to use survival::plot.cox.zph which directly produces a
beta(t) vs t diagram.

The other is to transform the dataset to counting process format and
manually include an interaction with time, expanded with spline (to be
similar to plot.cox.zph). Plotting the coefficient produces the needed
beta(t) vs t diagram.

I understand that they're slightly different approaches, so I don't
expect totally identical results, but nevertheless, they approximate
the very same thing, so I do expect that the results are more or less


library( survival )
library( splines )

data( veteran )

zp <- cox.zph( coxph(Surv(time, status) ~ trt + prior + karno,
                     data = veteran ), transform = "identity" )[ 3 ]

veteran3 <- survSplit( Surv(time, status) ~ trt + prior + karno,
                       data = veteran, cut = 1:max(veteran$time) )

fit <- coxph(Surv(tstart,time, status) ~ trt + prior + karno +
               karno:ns( time, df = 4 ), data = veteran3 )
cf <- coef( fit )
nsvet <- ns( veteran3$time, df = 4 )

plot( zp )
lines( 0:1000, ns( 0:1000, df = 4, knots = attr( nsvet, "knots" ),
                   Boundary.knots = attr( nsvet, "Boundary.knots" ) )%*%cf[
                     grep( "karno:ns", names( cf ) ) ] + cf["karno"],
       type = "l", col = "red" )

Where is the mistake? Something must be going on here, because the
plots are vastly different...

Thank you in advance,

More information about the R-help mailing list