Survival analysis in R using factor variables When using survival analysis in R I found that I cannot code the event variable "death" as a factor variable as it flips the Kaplan Meier curve, and I have to use is as a numeric variable. I'm not sure why this happens.
In light of this -- is it safe to ordinarily code other explanatory categorical variables as factor variables for the survival analysis, or can that lead to incorrect values in the hypothesis tests?
 A: The answer is given in the documentation for ?Surv.

The function tries to distinguish between the use of 0/1 and 1/2 coding for censored data via the condition if (max(status)==2). If 1/2 coding is used and all the subjects are censored, it will guess wrong. In any questionable case it is safer to use logical coding, e.g., Surv(time, status==3) would indicate that '3' is the code for an event. For multi-state survival the status variable will be a factor, whose first level is assumed to correspond to censoring.

By using a factor variable as a censoring indicator you are telling Surv() that you want a multi-state model. It interprets the first level as censoring and the second level as entering state 2, like you might expect. But for multi-state models the Kaplan-Meier curve plots the total group membership over time, rather than the proportion surviving up to that point. For more details you can read the competing risks vignette.  https://cran.r-project.org/web/packages/survival/vignettes/compete.pdf
For explanatory variables this difference does not happen. The usual care needs to be taken to ensure that you understand which level is being taken as the reference level, but it is safe to use factor variables as explanatory variables.
