therneau / survival

Survival package for R
394 stars 106 forks source link

stratified predictors in coxph models not recognized by emmeans #248

Closed mdjpe closed 1 week ago

mdjpe commented 7 months ago

Survival models with a stratified factor appear to have stopped cooperating with emmeans.

For example, the following model, which was fully functional on R for Windows last year (and was used in my published work), currently produces errors when run in R for Ubuntu (I don't currently have R on a Windows machine, so can't check that):

cphfr <- coxph(Surv(tstart, tstop, death) ~ strata(Region) * Patties + Fume, newBHPsurv, cluster=ColonyGroup)

emm <- emmeans(cphfr, ~ Patties | Region) seem <- summary(pairs(emm, reverse = T), by = NULL, type="response", adjust = "none", infer = c(T, T)) names(seem)[1] <- "Patties effect" pander(seem)

Error in emmeans(cphfr, ~Patties | Region) : No variable named Region in the reference grid In addition: Warning message: In model.matrix.default(trms, m, contrasts.arg = object$contrasts) : variable 'strata(Region)' is absent, its contrast will be ignored

The above definitely worked in:

R version 4.2.2 (2022-10-31 ucrt)

Platform: x86_64-w64-mingw32/x64 (64-bit)

Running under: Windows 10 x64 (build 19044)

survival_3.4-0 emmeans_1.8.2

And is not working in:

R version 4.3.3 (2024-02-29) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 22.04.3 LTS [1] survival_3.5-7 emmeans_1.10.0

therneau commented 7 months ago

Why are you asking me about a failure in another package? Ask the author of emmeans.

mdjpe commented 7 months ago

I have opened an issue in emmeans and rvlenth has provided some initial responses. It seems there was a change in July 2023 (in response to a previous bug report) that might have been in error. However, it has not been resolved at first glance. If there is any chance that the two of you could talk about this, it would be appreciated (at least certainly by me). I'm pretty sure that the previous output was basically correct; there should be emmean available whenever the model provides an estimate for the factor (ie not for the stratified factor itself, but for the other predictors that interact with it, at each level of the stratifed factor).