There are a couple of things that need to be done to get this to work.
Your initial data and imputation.
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, print=FALSE)
There is no vcov
method for crr
models which mice
requires, however,
we can access the covariance matrix using the model$var
returned value.
So write own vcov
method to extract, and also need a coef
method.
vcov.crr <- function(object, ...) object$var # or getS3method('vcov','coxph')
coef.crr <- function(object, ...) object$coef
There is also an error in how the model is passed to with.mids
: your code has cov1=test1[,c( "x","sex")]
, but really you want cov1
to use the imputed data. I am not sure how to correctly write this as an expression due to the cov1
requiring a matrix with relevant variables, but you can easily hard code a function.
# This function comes from mice:::with.mids
Andreus_with <-
function (data, ...) {
call <- match.call()
if (!is.mids(data))
stop("The data must have class mids")
analyses <- as.list(1:data$m)
for (i in 1:data$m) {
data.i <- complete(data, i)
analyses[[i]] <- crr(ftime=data.i[,'time'], fstatus=data.i[,'status'],
cov1=data.i[,c( "x","sex")],
failcode=1, cencode=0, variance=TRUE)
}
object <- list(call = call, call1 = data$call, nmis = data$nmis,
analyses = analyses)
oldClass(object) <- c("mira", "matrix")
return(object)
}
EDIT:
The mice
internals have changed since this answer; it now uses the broom
package to extract elements from the fitted crr
model. So tidy
and glance
methods for crr
models are required:
tidy.crr <- function(x, ...) {
co = coef(x)
data.frame(term = names(co),
estimate = unname(co),
std.error=sqrt(diag(x$var)),
stringsAsFactors = FALSE)
}
glance.crr <- function(x, ...){ }
The above code then allows the data to be pooled.
models.FG <- Andreus_with(dat)
summary(pool(models.FG))
Note that this gives warnings over df.residual
not being defined, and so large samples are assumed. I'm not familiar with crr
so a more sensible value can perhaps be extracted -- this would then be added to the tidy
method. (mice version ‘3.6.0’)
vcov
method forcrr
models whichmice
needs. Check by looking at one model.(m = models.FG$analyses[[1]]) ; vcov(m)
. But we can access this withmodels.FG$analyses[[1]]$var
. Check standard errors against returned values form
, against thissqrt(diag(models.FG$analyses[[1]]$var))
. So maybe write ownvcov
method (also needcoef
method):vcov.crr <- function(object, ...) object$var ; coef.crr <- function(object, ...) object$coef
. Then run againsummary(pool(models.FG))
(i have no idea if it is statistically correct to pool values for this model type) – Danumaintainer("cmprsk")
– Milquetoastcoef.crr=function(object,...) object$coef
vcov.crr=getS3method('vcov','coxph')
I think then vcov(crrobject) should work – Gas.mira()
list topool()
then... I am trying to do it but I'm doing something wrong... Withas.mira(fitlist)
I can not do a proper structure listlist()
. – Gcoef
andvcov
methods? When i did this in the comment above I was able to usepool
without any further tweaking. Can you edit your question to show why you are having to useas.mira
(psfitlist
is not in your question) – Danucoef.crr=function(object,...) object$coef
vcov.crr=getS3method('vcov','coxph')
#Fine-Gray modelmodels.FG<- with(dat,crr(ftime=time, fstatus=status, cov1=test1[,c( "x","sex")], failcode=1, cencode=0, variance=TRUE))
8 cases omitted due to missing values #summary(pool(models.FG))coef.crr.x<-coef.crr(models.FG)
vcov.crr.x<-vcov.crr(models.FG)
summary(pool(vcov.crr.x))
– Gwith()
it was warning about: 8 cases omitted due to missing values in eachm=
... So to be sure about the results I was thinking to set one by one model in eachm=
and useas.mira()
to thenpool()
them – Gsummary()
models.FG
, it shows me the same results in echm=
, likewith()
didn't use different models from different imputations... I saw it when comparing results from STATA.12 – Gwith
, likely because you are usingcov1=test1
in the formula which has missing data. – Danuvcov.crr <- function(object, ...) object$var
orvcov.crr=getS3method('vcov','coxph')
give me same SE, is that right? no differences in methods... if it is like this, when the results are iqual in Satat.12 in coef but not in vcov... So, caveat... – G