2

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Algot
  • 31
  • 2
  • Does this answer your question? https://stackoverflow.com/questions/41794649/can-mice-handle-crr-fine-gray-model – IRTFM Jul 07 '22 at 01:28
  • Unfortunalty no. Thanks for help though! Managed to solve it on my own according to next comment :) – Algot Jul 09 '22 at 12:03

1 Answers1

1

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))
Algot
  • 31
  • 2