0

I am using the msfit function in mstate package to predict/fit the cumulative transition hazards for a new data. Then I want to manually update the cumulative transition hazards stored in the msfit object with some new info, but I got stuck here. I am using list type operations for faster computation. Here is a sample code,

library(mstate)
tmat <- trans.illdeath() # transition matrix for illness-death multistate model    
tg <- data.frame(illt=c(1,1,6,6,8,9),ills=c(1,0,1,1,0,1),
             dt=c(5,1,9,7,8,12),ds=c(1,1,1,1,1,1),
             x1=c(1,1,1,0,0,0),x2=c(6:1)) # creating a data for fitting

# converting data to long format using msprep
tglong <- msprep(time=c(NA,"illt","dt"),status=c(NA,"ills","ds"),
             data=tg,keep=c("x1","x2"),trans=tmat)
tglong <- expand.covs(tglong,c("x1","x2")) # expanding the covariate data
cx <- coxph(Surv(Tstart,Tstop,status)~x1.1+x2.2+strata(trans),
        data=tglong,method="breslow") # fitting coxph models
# Now preparing a newdata for fitting/ predicting 
newdata <- data.frame(id=c(rep(1,3),rep(2,3)),trans=rep(1:3,2),x1.1=rep(0,6),x2.2=c(rep(5,3),rep(4,3)),strata=rep(1:3,2))
# using msfit as list
fit_list<-lapply(1:max(newdata$id), function(x) return(msfit(cx, newdata = newdata[newdata$id==x,], trans = tmat)))

# storing the transition hazards into a new object
a<-lapply(1:length(fit_list), function(i) fit_list[[i]]$Haz)
# manually updating one of the transition hazards
a<-lapply(1:length(a), function(i) within(a[[i]], Haz <- ifelse(trans==3, Haz*2, Haz)))

Now I want to only update the Haz data frame stored in each of the fit_list objects with the new list object a, and keep the rest as is like a msfit object. Here is what I did,

 fit_list<-lapply(1:length(a), function(i) {
 fit_list[[i]]$Haz<-a[[i]]
 })

This does not keep the original msfit object, rather returns a list of Haz. But it would work if there is no list, that is, if the newdata contains only a single id (i.e. for data.frame type object). I have huge data set in the newdata using list operation is much faster. Can anyone help me on this?

ssaha
  • 459
  • 2
  • 10

1 Answers1

1

This does not keep the original msfit object, rather returns a list of Haz.

Of course, because this is what you are doing when you code

fit_list <- lapply(1:length(a),
                 function(i)
                 {
                       fit_list[[i]]$Haz <- a[[i]] 
                 })

This returns fit_list[[i]]$Haz as a list and saves it into the variable fit_list. In other words, you loose what you have save in fit_list. Simply change the code to

fit_list <- lapply(1:length(a),
       function(i)
       {
            fit_list[[i]]$Haz <- a[[i]]
            return(fit_list[[i]])
       })

and it should work.

HTH!

MacOS
  • 1,149
  • 1
  • 7
  • 14