kassambara / survminer

Survival Analysis and Visualization
https://rpkgs.datanovia.com/survminer/
507 stars 162 forks source link

Plot results of multiple univariate coxph models at the same time? #459

Open MelisAnaturk opened 4 years ago

MelisAnaturk commented 4 years ago

Hi, I would like to plot the results of multiple univariate cox prop hazard models as an initial exploratory step in my analysis. Is it possible to adapt the ggforest function to take in data frames that contain the same information as tidy(model) and glance(model) but for several seperate models? My code so far:

# Create vector of dvs & iv
dvs_vec <- paste("Surv(time", ",", "status)")

# Create vector of candidate factors as ivs
ivs_vec <- c("sex", "rx", "adhere", "differ", "extent", "node4")
ivs_covs <- paste0(" ~ ", ivs_vec)

# run models
dvs_ivs_covs <- unlist(lapply(ivs_covs, function(x) paste0(dvs_vec, x)))
formulas <- lapply(dvs_ivs_covs, formula)
cox_results <- lapply(formulas, function(x) {coxph(x, data = colon)})

#tidy data
tidy_results      <- lapply(cox_results, broom::tidy, exponentiate = TRUE)
lstData           <- Map(as.data.frame, tidy_results)
tidy_results <-  rbindlist(lstData)

glance_results    <- lapply(cox_results, broom::glance)
lstData           <- Map(as.data.frame, glance_results)
glance_results     <- rbindlist(lstData)

# ggforest_test
ggforest(cox_results, data = colon)
#Error in ggforest(cox_results, data = colon) : 
#class(model) == "coxph" is not TRUE

Thank you!

KrisdeKlerk commented 4 years ago

Hi! I have the same problem; would like to use ggforest to plot several univariate coxph models. Has anyone found out yet how to do this? Thank you!

tristanhayes commented 3 years ago

Check out forestmodel. Here is some example code that will do exactly what you would like.

`univ_formulas <- sapply(vars_for_table, function(x) as.formula(paste('Surv(Survival_Months, Overall_Death)~', x)))

univ_models <- lapply(univ_formulas, function(x){coxph(x, data = data2)})

forest_model(model_list = univ_models,covariates = vars_for_table,merge_models =T)`