therneau / survival

Survival package for R
381 stars 104 forks source link

Error from concordancefit with strata term when nvar > 1 #249

Open sritchie73 opened 5 months ago

sritchie73 commented 5 months ago

The concordance function currently cannot be used to compare models when also using a strata term.

Reproducible example using the mgus2 data based on the example code for the concordance function comparing multiple models:

library(survival)

# Filter mgus2 to complete data for simplicity
tdat <- mgus2[complete.cases(mgus2),]

# Fit two sex stratified models for comparison:
fit4 <- coxph(Surv(ptime, pstat) ~ strata(sex) + age + mspike, tdat)
fit5 <- coxph(Surv(ptime, pstat) ~ strata(sex) + age + hgb + creat, tdat)

# Extract linear predictors
p4 <- -predict(fit4) # invert; high risk scores predict shorter survival, equivalent to reverse=TRUE
p5 <- -predict(fit5)

# Compare condorance
cfit <- concordance(Surv(ptime, pstat) ~ strata(sex) + p4 + p5, tdat)

This gives the following error message:

Error in dimnames(rval$count) <- list(Xname, cname) : 
  length of 'dimnames' [1] not equal to array extent

Stepping through the concordance function, we can trace this error to the assembling of results in the concordancefit function on line 374 in the concordance.R file: https://github.com/therneau/survival/blob/master/R/concordance.R#L374

Here are the working states of rval and list(Xname, cname) right before the error:

Browse[2]> rval
$concordance
       p4        p5      <NA>      <NA> 
0.5146919 0.5000000 0.5256784 0.5000000 

$count
      [,1]  [,2] [,3] [,4]    [,5]
[1,] 12913 11986 6649 7610      36
[2,]     6     6    0    0 2834173
[3,] 12490 10959 6362 7931      38
[4,]     6     6    0    0 2875235

$n
[1] 1338
Browse[2]> list(Xname, cname)
[[1]]
[1] "p4" "p5"

[[2]]
[1] "concordant" "discordant" "tied.x"     "tied.y"     "tied.xy"   
therneau commented 4 months ago

I would do this in a much simpler way, i.e., concordance(fit4, fit5)
I had not considered the setup you used, and will have to think about this a bit.