Open JuliaTutzauer opened 6 years ago
Hi,
The error you report comes from the Surv function, which is part of the survival package, not survminer.
Did you check the class of your time object or run a summary?
Thank you for your reply. The time object is numeric.
Is there a risk that the definition of the event is the problem?
You may have a problem of non-standard evaluation. Can you try
surv2 <- function(data, timevar, eventvar, grpvar){
str <- sprintf(
'survfit(formula = Surv(%s,%s) ~ %s, data = data)',
timevar,
eventvar,
grpvar)
fit<- eval(parse(text = str))
ggsurvplot(fit, data = data)
}
surv2(dataj, "time_BC1_to_met", "metastasis","tam_bc1")`
This should work. A few, notes: 1) I changed time
to tte
and data
to dat
to prevent conflicts with the time
and data
functions (more a preference than a necessity. 2) Constructing a Surv
requires the use of with
so that Surv
knows which object the columns come from. i.e. try
data(lung)
Surv(time,status)
with(Surv(time,status), data=lung)
Though this did not solve the error, so I took another route by creating a string on the fly and coercing it to a formula.
surv2 <- function(dat, tte, event, grpvar){
# SurvObj <- Surv(time, event)
# fit<- do.call(survfit(SurvObj ~ grpvar, data = data))
fit <- survfit(as.formula(paste('Surv(',tte, ",",event,') ~', grpvar)), data = dat)
ggsurvplot(fit, data = dat)
}
I am not an expert in parametrizing against the formula interface, but this seems a bit more natural when using a formula interface. HTH.
I'm trying to code a function with ggsurvplot that returns a Kaplan Meier curve. I've previously done this with the Kaplan Meier function from the survival package without any problem, but I can't make it work with ggsurvplot.
Expected behavior
I want the function to return a Kaplan Meier curve showing the time to metastasis for different groups of breast cancer patients.
The time variable I use is numeric (values are given in years from first diagnosis to metastasis).
Actual behavior
I get the message "Error in Surv(time, event) : Time variable is not numeric".
I have also tried to use as.numeric(time) but then I get the message "Error in Surv(as.numeric(time), event) : Invalid status value, must be logical or numeric In addition: Warning message: In Surv(as.numeric(time), event) : NAs introduced by coercion"
Steps to reproduce the problem
surv2(dataj, "time_BC1_to_met", "metastasis","tam_bc1")`
session_info()