Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with ggadjustedcurves() : grouping factor has to be included in the coxph model #623

Open
AlexisZr opened this issue Mar 28, 2023 · 3 comments

Comments

@AlexisZr
Copy link

Expected behavior

As explicitly noted in the survminer cheatsheet:

is not necessary to include the grouping factor in the Cox model

Actual behavior

Despite following the exemplified information presented in the cheatsheet, the ggadjustedcurves() function exclusively generates a solitary survival curve without taking into account the provided grouping factor (which is not included in the cox model). To ascertain the veracity of the issue, I replicated the aforementioned example from the cheatsheet, which once more yielded solely one survival curve as opposed to the illustrated four.

Steps to reproduce the problem

lung$sex <- ifelse(lung$sex == 1, 
                   "Male", "Female")
fit <- coxph(Surv(time, status) ~ 
               sex + ph.ecog + age +
               strata(sex), 
             data = lung)

lung$age3 <- cut(lung$age, 
                 c(35,55,65,85))
ggadjustedcurves(fit, data=lung, 
                 variable="age3")
@markhwhiteii
Copy link

markhwhiteii commented Jun 18, 2023

+1 to this. The cheat sheet—and the README—both mention this is actually supposed to be done with the ggcoxadjustedcurves function, but that function is nowhere in the repository and thus not exported when downloading either from CRAN or GitHub.

EDIT: Ah, searching the NEWS, I see that it was deprecated, per #229. For what it's worth, when I run it on my data (unfortunately, I can't share the dataset), I get an error:

Error in if (xi > xj) 1L else -1L : missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In xtfrm.data.frame(x) : cannot xtfrm data frames
2: In Ops.factor(xi, xj) : ‘>’ not meaningful for factors

@markhwhiteii
Copy link

@AlexisZr I found a workaround where you use the predict method:

# fit model
fit <- coxph(Surv(time, status) ~ sex + ph.ecog + age, data = lung)

# make prediction data
ages <- c(35, 55, 65, 85)
times <- min(lung$time):max(lung$time)
fit_pred <- data.frame(
  age = rep(ages, each = length(times)), 
  time = rep(times, length(ages)),
  sex = "Female", # holding this
  ph.ecog = 1, # and this constant
  status = 0 # doesnt matter if 1 or 0, you get the same prediction
)

# predict survival
fit_pred$pred <- predict(fit, fit_pred, type = "survival")

# plot it
ggplot(fit_pred, aes(x = time, y = pred)) +
  geom_line(aes(color = factor(age), group = factor(age)))

@AlexisZr
Copy link
Author

@markhwhiteii Deeply appreciated the workaround. Nonetheless, I manage to tackle the problem a while ago using the adjustedCurves Package (I highly recommend this package, as it offers a more straightforward approach )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants