1 year ago

#267287

test-img

Miguel Angel Arnau

How to create a data.frame in format start-stop for a dependent time covariable who initial state can be 0 or 1

I´d like to get a data.frame in format start-stop for a time dependent covariate (trabajo) who initial state (trab0) can be 0 or 1, and change at a time (timetrab1) to trab1. In this way, a patient can be

  • trab0=0 and no change (trab1=0)
  • trab0=0 and change (trab1=1) at time timetrab1
  • trab0=1 and no change (trab1=1) or
  • trab0=1 and change (trab1=0) at time timetrab1 The data.frame to change is:
id<- as.numeric(1:32)
time <- c(48,85,908,443,144,85,  14, 132, 218, 804,14,695, 69,392,726,39,638,6,320,32,543, 114, 484,56,85,370,236,27,237,190,106,183)
status <- c(0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1)
trab0 <- c(1, 1, 1, 0, 1, 0, 0,0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1)
trab1<- c(0, NA, NA,  1, NA, NA, NA, NA, NA, NA, NA,  1, NA, NA, NA, NA, NA, NA, NA, NA, NA,  0, NA, NA, NA, NA, NA, NA, NA,  1, NA, NA)
timetrab1 <- c(22,NA,NA,106,NA,NA,NA,NA,NA,NA,NA,150,NA,NA,NA,NA,NA,NA,NA,NA,NA,93,NA,NA,NA,NA,NA,NA,NA,40,NA,NA)
df<- data.frame(id, time, status, trab0, trab1, timetrab1)

The reason is for doing a Cox extended model

Thanks.

I have get the same result with two ways to do the analysis:

library (dplyr)
library(survival)

df1 <- tmerge(df, df, id=id, death = event(time, status)) #set range
df1 <- tmerge(df1, df, id=id, trabajo = tdc(timetrab1, trab1))
df1$trabajo <- ifelse (is.na(df1$trabajo), df1$trab0, df1$trabajo)

fit<- coxph(Surv(tstart, tstop, death==0) ~ trabajo, data=df1, method = 'breslow')
summary(fit)

################ as in Therneau 3.4 (PBC data)

df$timetrab0<- rep(0,nrow(df))  # at tiempo 0, with nº rows (df)
temp1<- select(df, c('id','time','status'))
temp2<- select(df, c('id','timetrab0','trab0','timetrab1','trab1'))
temp3<- reshape(temp2, idvar = "id", varying = list(c('timetrab0','timetrab1'),c('trab0','trab1')),
               v.names = c('t','trabajo'), 
               direction = "long") ##from wide to long with reshape

tdc2 <- tmerge(temp1, temp1, id=id, death = event(time, status)) #set range tstart/tstop
tdc2 <- tmerge(tdc2, temp3, id=id, trabajo = tdc(t, trabajo))
tdc2   # data.frame in format start/stop, with 'trabajo' as covariate time dependent
fit <- coxph(Surv(tstart, tstop, death==0) ~ trabajo, tdc2, method='breslow')   # run extended Cox model 
summary(fit)

Both ways give me the same result:

enter image description here

But it's different of the output of SPSS:

TIME PROGRAM.
COMPUTE trabajo = (T_ < timetrab1) * trab0 + (T_ >= timetrab1) * trab1.
IF (MISSING (timetrab1)) trabajo = trab0.

COXREG    time /STATUS= status (0)
   /METHOD= ENTER trabajo
   /CRITERIA= PIN (.05) POUT (.10) ITERATE (20).

enter image description here

OR

TIME PROGRAM.
COMPUTE trabajo = (T_ <= timetrab1) * trab0 + (T_ > timetrab1) * trab1.
IF (MISSING (timetrab1)) trabajo = trab0.

COXREG    time /STATUS= status (0)
   /METHOD= ENTER trabajo
   /CRITERIA= PIN (.05) POUT (.10) ITERATE (20).

enter image description here

The question is,what is the correct equation in SPSS (T< OR T <=) and in any case why do the results between SPSS and R not match? Is it possible that the conversion to the start-stop format was not successful?

Thanks again

r

dataframe

dplyr

survival-analysis

cox-regression

0 Answers

Your Answer

Accepted video resources