cant get crr() Fine-Gray model to work on imputed data using mice() in R (Cov1/model.matrix-problem?)

Viewed 71

Have hade major issues doing a fine-gray Crr() analysis with imputed data (mids-type data, imputed using mice-packge). Problem seem to be the Cov1-command, as i cannot get it to draw data from the mids-data. I have tried for a couple of hours searching for a solution, including using different types of packages and methods without success. Help would be most appreciated!

There is an old example with a different set of problem here where there was a vcov-related problem. As the package is now updated this is no longer the problem. Ill use the same code for exemplary purposes.

library(survival)
library(mice)
library(cmprsk)

test1 <- as.data.frame(list(time=c(4,3,1,1,2,2,3,5,2,4,5,1, 4,3,1,1,2,2,3,5,2,4,5,1), 
                            status=c(1,1,1,0,2,2,0,0,1,1,2,0, 1,1,1,0,2,2,0,0,1,1,2,0),
                            x=c(0,2,1,1,NA,NA,0,1,1,2,0,1, 0,2,1,1,NA,NA,0,1,1,2,0,1),
                            sex=c(0,0,0,NA,1,1,1,1,NA,1,0,0, 0,0,0,NA,1,1,1,1,NA,1,0,0)))

dat <- mice(test1,m=10, seed=1982)

#Cox regression: cause 1

models.cox1 <- with(dat,coxph(Surv(time, status==1) ~ x +sex ))                 

summary(pool(models.cox1))

#Cox regression: cause 1 or 2

models.cox <- with(dat,coxph(Surv(time, status==1 | status==2) ~ x +sex ))                 
models.cox
summary(pool(models.cox))


#### crr()

#Fine-Gray model

models.FG<- with(dat,crr(ftime=time, fstatus=status,  cov1=test1[,c( "x","sex")], failcode=1, cencode=0, variance=TRUE))                 

summary(pool(models.FG))

#8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values

#model draws from orignial dataset, thus missing values, changing to mids-dataset

models.FG<- with(dat,crr(ftime=time, fstatus=status,  cov1=dat[,c( "x","sex")], failcode=1, cencode=0, variance=TRUE))  

#Error in dat[, c("x", "sex")] : incorrect number of dimensions

#problem persists after changing to specific directory

models.FG<- with(dat,crr(ftime=time, fstatus=status,  cov1=dat$imp[,c( "x","sex")], failcode=1, cencode=0, variance=TRUE))  

#Error in dat$imp[, c("x", "sex")] : incorrect number of dimensions

# coding my own model.matrix

previous_na_action <- options('na.action')
options(na.action='na.pass')

cov1 <- model.matrix( ~ factor(x) 
                      + factor(sex),
                      data = test1)[, -1]

options(na.action=previous_na_action$na.action)

models.FG<- with(dat,crr(ftime=time, fstatus=status,  cov1=cov1, failcode=1, cencode=0, variance=TRUE))  

#8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values

# Same problem, i the same dimentional problems persist i u try to use "data=dat$imp"

# use listed imputed data as source?

longdat <- complete(dat, action='long', inc=TRUE)

previous_na_action <- options('na.action')
options(na.action='na.pass')

cov1 <- model.matrix( ~ factor(x) 
                      + factor(sex),
                      data = longdat)[, -1]

options(na.action=previous_na_action$na.action)

models.FG<- with(dat,crr(ftime=time, fstatus=status,  cov1=cov1, failcode=1, cencode=0, variance=TRUE))  

#8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values
8 cases omitted due to missing values

# still same problem
 
models.FG

Any ideas how i cant get the With() to index the coviariates correctly? Any other package who could handle CRR-analysis on mids-object? as theoretically i suppose i could convert the mids to usual data -> do analysis separately -> converge the datasets i tried to do that with as.mira-command but could not get that to work either.

Help would be enormously appreciated! As i am fairly new to R and stackoverflow I hope this example was clear enough.

2 Answers

Managed to solve it myself, posting if someone else run into same problem. Might have to be validated by a more senior statistician/R-user though.

### Proof of concept:example data from https://stackoverflow.com/questions/41794649/can-mice-handle-crr-fine-gray-model ###

library(survival)
library(mice)
library(cmprsk)
test1 <- as.data.frame(list(time=c(4,3,1,1,2,2,3,5,2,4,5,1, 4,3,1,1,2,2,3,5,2,4,5,1), 
                            status=c(1,1,1,0,2,2,0,0,1,1,2,0, 1,1,1,0,2,2,0,0,1,1,2,0),
                            x=c(0,2,1,1,NA,NA,0,1,1,2,0,1, 0,2,1,1,NA,NA,0,1,1,2,0,1),
                            sex=c(0,0,0,NA,1,1,1,1,NA,1,0,0, 0,0,0,NA,1,1,1,1,NA,1,0,0)))


sapply(test1, class)
test1[sapply(test1, is.character)] <- lapply(test1[sapply(test1, is.character)], as.factor)

dat <- mice(test1,m=10, seed=1982)



#Fine-Gray model

## create loopable list with imputed data  ##

longdat <- complete(dat, action='long', inc=TRUE) #long format for creating a list containing both original data and imputed

longdatsplit <- split(longdat, longdat$.imp) # split into imputed n to make individual vectors 

longdatlist <- list()   

for(i in 1:length(longdatsplit)) {             ##  for making list to able loop
  longdatlist[[i]] <- longdatsplit[[i]]
}

longdatlist2 <- longdatlist[- 1] #first row are non-imputed data, thus containing one more level than imputed data. 


### analysis-loop using list ###

mod <- list(analyses=vector("list", dat$m))

for(i in 1:dat$m){
  
covariates <- model.matrix(~longdatlist2[[i]]$x + longdatlist2[[i]]$sex , data = longdatlist2[[i]])[,-1]
  
  
mod$analyses[[i]] <- crr(ftime=longdatlist2[[i]]$time, fstatus=longdatlist2[[i]]$status,  cov1=covariates, failcode=1, cencode=0, variance=TRUE)

}

### Create as.mira ###

obj <- as.mira(mod)   

obj <- list(call=mod$analyses[[1]]$call, call1=dat$call, nmis=dat$nmis, analyses=mod$analyses) ## all calls originates form correct source, same as with-command

oldClass(obj) <- "mira"

summary(obj)
pool(obj)

summary(pool(obj))

I had a similar issue regarding crr() working with mice() and found your solution helpful - so thank you.

However, I have encountered a issue and wondered if you faced the same issue initially or if you have a potential solution. Any feedback would be much appreciated.

data = MCmice_fg and there are 5 imputation models

   # step 1: create loopable list with imputed data #
long.MCmice_fg <- complete(MCmice_fg, action = "long", inc=TRUE)
long.MCmice_fg.split <- split(long.MCmice_fg, long.MCmice_fg$.imp)
long.MCmice_fg.list <- list()

for(i in 1:length(long.MCmice_fg.split)) {
  long.MCmice_fg.list[[i]] <- long.MCmice_fg.split[[i]]
}

long.MCmice_fg.list2 <- long.MCmice_fg.list[- 1]

#step 2: analysis-loop using list

mod <- list(analyses=vector("list", MCmice_fg$m))

for(i in 1:MCmice_fg$m){
  MCcovariates <- model.matrix(~long.MCmice_fg.list2[[i]]$Ageband + long.MCmice_fg.list2[[i]]$Sex + long.MCmice_fg.list2[[i]]$townsend + long.MCmice_fg.list2[[i]]$Diabetes + long.MCmice_fg.list2[[i]]$Depression + long.MCmice_fg.list2[[i]]$Obesity + long.MCmice_fg.list2[[i]]$HearingLoss + long.MCmice_fg.list2[[i]]$AlcoholMisuse + long.MCmice_fg.list2[[i]]$Smoker + long.MCmice_fg.list2[[i]]$TBI , data = long.MCmice_fg.list2[[i]])[,-1] 
mod$analyses[[i]] <- crr(ftime=long.MCmice_fg.list2[[i]]$PYAR, fstatus=long.MCmice_fg.list2[[i]]$event, failcode="Dementia", cencode="Censored", cov1=MCcovariates, variance = TRUE)
  }

After several hours of running, these warning messages appear:

1: In doTryCatch(return(expr), name, parentenv, handler) :
restarting interrupted promise evaluation 2: In identical(object, base::break) : restarting interrupted promise evaluation 3: In get(object, envir = currentEnv, inherits = TRUE) : restarting interrupted promise evaluation 4: In doTryCatch(return(expr), name, parentenv, handler) : restarting interrupted promise evaluation

I believe at the moment when I go onto the final step # Create as.mira # and pool the models, I am only using the final model (model 5) due to this warning.

Any feedback on how to address this issue would be very much appreciated.

Related