I am trying to make a KM curve function, however, I am not sure where I am going wrong.
library(tidyverse)library(ggplot)library(survival)library(survminer)km_curve <- function(df, tm, ev, predictor) { surv_object <- Surv(time = df$tm, event = df$ev) fit1 <- survfit(surv_object ~ predictor, data = df) ggsurvplot(fit1, data = df, pval = TRUE, xlab = "Months") }
I also tried this
km_curve <- function(df, tm, ev, predictor) { surv_object <- Surv(time = df[,tm], event = df[,ev]) fit1 <- survfit(surv_object ~ predictor, data = df) ggsurvplot(fit1, data = df, pval = TRUE, xlab = "Months") } km_curve(df = first_primaries, tm = pfs_time_months, ev = pfs_fail.x, predictor = risk)
I have also tried
km_curve <- function(df, tm, ev, predictor) { tim <- df %>% select(tm) even <- df %>% select(ev) surv_object <- Surv(time = tim, event = even) fit1 <- survfit(surv_object ~ predictor, data = df) ggsurvplot(fit1, data = df, pval = TRUE, xlab = "Months") } km_curve(df = first_primaries, tm = pfs_time_months, ev = pfs_fail.x, predictor = risk)
It keeps giving me error messages that pf_time_months not found. Even though it is a column in my data frame I specify.