Error in R Function

I am trying to create ggsurvplot objects using a list of exposure variables using functions
My code is working for the first part to create KM models (survfit() objects)

library(survival)

anxiety_df <- data.frame(
  id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
  time_months = c(0, 2, 25, 15, 38, 4, 6, 12, 22, 9),
  outcome = c(0, 0, 1, 0, 0, 1, 0, 1, 0, 1),
  anxiety = c(0, 1, 1, 0, 1, 1, 0, 0, 0, 1)
)

adhd_df <- data.frame(
  id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
  time_months = c(0, 2, 25, 15, 38, 4, 6, 12, 22, 9),
  outcome = c(0, 0, 1, 0, 0, 1, 0, 1, 0, 1),
  adhd= c(0, 1, 1, 0, 1, 1, 0, 0, 0, 1)
)
exposures <- c("anxiety", "adhd", "ocd", "ptsd")


create_model <- function(cox_df, exposure_var) {

  km.model <- survfit(Surv(time_months, outcome) ~ get(exposure_var), cox_df)

return(km.model)
}

model1 <- create_model(anxiety_df, exposures[1])
model2 <- create_model(adhd_df, exposures[2])

But it gives an error when I add the ggsurvplot code:

library(survival)
library(survminer)
exposures <- c("anxiety", "adhd", "ocd", "ptsd")


create_model <- function(cox_df, exposure_var) {

  km.model <- survfit(Surv(time_months, outcome) ~ get(exposure_var), cox_df)
  
  plot<-ggsurvplot(km.model, cox_df, fun="event")

return(plot)
}

model1 <- create_model(anxiety_df, exposures[1])
model2 <- create_model(adhd_df, exposures[2])

Error Message: Error in FUN(X[[i]], ...) : object 'exposure_var' not found

Difficult to help troubleshoot without something to use in place of these data frames.

Sorry about that. This is what the 2 data frames looks like (just swap the exposure variables). One data frame should be enough to solve the problem.

id <- c(0,1, 2,3,4,5,6,7,8,9)
time_months<- c(0, 2,25,15,38,4,6,12,22,9)
outcome<- c(0, 0, 1, 0, 0, 1, 0, 1, 0, 1) 
anxiety<c(0,1,1,0,1,1,0,0,0,1)

df <- cbind(id, time_months, outcome, anxiety)

Hope this helps

library(survival)
anxiety_df <- data.frame(
  id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
  time_months = c(0, 2, 25, 15, 38, 4, 6, 12, 22, 9),
  outcome = c(0, 0, 1, 0, 0, 1, 0, 1, 0, 1),
  anxiety = c(0, 1, 1, 0, 1, 1, 0, 0, 0, 1)
)

exposures <- c("anxiety", "adhd", "ocd", "ptsd")

create_model <- function(cox_df, exposure_var) {
  km.model <- survfit(Surv(time_months, outcome) ~ get(exposure_var), cox_df)
  return(km.model)
}

model1 <- create_model(anxiety_df, exposures[1])
model1
#> Call: survfit(formula = Surv(time_months, outcome) ~ get(exposure_var), 
#>     data = cox_df)
#> 
#>                     n events median 0.95LCL 0.95UCL
#> get(exposure_var)=0 5      1     NA      12      NA
#> get(exposure_var)=1 5      3     17       4      NA

Created on 2023-10-17 with reprex v2.0.2

Exactly. Part one seems to work. But if you look at the second part of my problem, when I add

plot<-ggsurvplot(km.model, cox_df, fun="event")

it does not work. I am guessing because the km.model's predictor (x) variable is read as "get(exposure_var)" instead of "anxiety" or "adhd"

Sorry, I'm afraid I don't understand well enough what you're trying to accomplish here.

I'm trying to make plots from the survfit objects for different variables.

library(survival)
library(survminer)
anxiety_df <- data.frame(
  id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
  time_months = c(0, 2, 25, 15, 38, 4, 6, 12, 22, 9),
  outcome = c(0, 0, 1, 0, 0, 1, 0, 1, 0, 1),
  anxiety = c(0, 1, 1, 0, 1, 1, 0, 0, 0, 1)
)

adhd_df <- data.frame(
  id = c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
  time_months = c(0, 2, 25, 15, 38, 4, 6, 12, 22, 9),
  outcome = c(0, 0, 1, 0, 0, 1, 0, 1, 0, 1),
  adhd= c(0, 1, 1, 0, 1, 1, 0, 0, 0, 1)
)
exposures <- c("anxiety", "adhd", "ocd", "ptsd")


create_model <- function(cox_df, exposure_var) {
  mysurvfit <- paste0("survfit(Surv(time_months, outcome) ~ ",exposure_var,", cox_df)")
  print(mysurvfit)
  km.model <- eval(parse(text=mysurvfit))
  print(km.model)
  plot<-ggsurvplot(km.model, cox_df, fun="event")
  return(list(model=km.model,
              plot=plot))
}

(model1 <- create_model(anxiety_df, exposures[1]))
(model2 <- create_model(adhd_df, exposures[2]))
1 Like

That worked. Thank you!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.