0

I want to plot how the estimated survival from a Cox model depends upon the value of a covariate of interest, while the rest of variables are fixed to their average values (if they are continuous variables) or lowest values for dummy. Following this example http://www.sthda.com/english/wiki/cox-proportional-hazards-model , I have construct a new data frame with three rows, one for each value of my variable of interest; and the other covariates are fixed. Among these covariates I have two factor vectors. I created the new dataset and later it is passed to survfit() via the newdata argument.

When I passed the data frame to survfit(), I obtain the following error message error in relevel.default(occupation) : 'relevel' only for factors. Where is the source of problem? If the source of problem is related to the factor vectors, how I can solve it? Below find an example of the code. Unfortunately, I cannot share the data or find a dataset that produces the same error message:

I have transformed the factor variables into integer vectors in the cox model and in the new dataset. it did not work. I have deleated all the factor variables and it works. I have tried to implement this strategy, but it did not work: Plotting predicted survival curves for continuous covariates in ggplot

fit <- coxph(Surv(entry, exit, event == 1)  ~ status_plot + 
exp_national +  relevel(occupation, 5) + age + gender + EDUCATION , data = data)

data_rank <- with(data, 
  data.frame(status_plot = c(1,2,3), # factor vector of interest
             exp_national=rep(mean(exp_national, na.rm = TRUE), 3),
              occupation = c(5,5,5), # factor with 6 categories, number 5 is the category of reference in the cox model
             age=rep(mean(age, na.rm = TRUE), 3),
             gender = c(1,1,1),
             EDUCATION=rep(mean(EDUCATION, na.rm = TRUE), 3) ))


surv.fin <- survfit(fit, newdata=data_rank) # this produces the error

1 Answers1

0

Looking at the code it appears you probably attempted to take the mean of a factor. So do post at least str(data) as an edit to the body of your question. You should also realize that you can give a single value to a column in a data.frame call and have it recycled to the correct length, you all the meanss could be entered as a single item rather thanrep`-ng.

IRTFM
  • 258,963
  • 21
  • 364
  • 487