Skip to content

Instantly share code, notes, and snippets.

# Read Dialysis dataset from SurvSet.data
from SurvSet.data import SurvLoader
loader = SurvLoader()
df, ref = loader.load_dataset(ds_name='Dialysis').values()
df.to_csv('Dialysis.csv', index=False)
@cindyangelira
cindyangelira / survival_library.R
Created January 7, 2023 23:59
survival_dependencies
required_packages <- c("tidyverse", "survivalmodels", "mlr3benchmark",
"mlr3pipelines", "mlr3proba", "mlr3tuning", "mlr3extralearners")
install.packages(required_packages)
set.seed(1)
df <- read_csv("Dialysis.csv")
head(df)
@cindyangelira
cindyangelira / surv_preprocessing.R
Created January 9, 2023 10:47
survival data preprocessing
# Pre processing----
df <- df %>% select(-6)
colnames(df)[c(4,5,6)] <- c("age", "begin", "disease")
# Encoding the categorical feature----
library(caret)
dmy <- dummyVars(" ~ .", data = df)
df_clean <- data.frame(predict(dmy, newdata = df)) %>% select(-c(1,6))
head(df_clean)
task <- TaskSurv$new("patient_id", backend = df_clean, time = "time", event = "event", type = "right")
task
@cindyangelira
cindyangelira / surv_learners.R
Last active January 9, 2023 11:57
survival learners R
# Create learners----
learners <- lrns(
paste0("surv.", c("coxtime","deephit", "deepsurv", "loghaz", "pchazard")),
frac = 0.3, activation = "relu",
dropout = 0.1,
early_stopping = TRUE,
epochs = 10,
batch_size = 32L
)
learners <- c(learners, lrns(c("surv.kaplan", "surv.coxph")))
# Benchamrk----
resample <- rsmp("cv", folds = 5)
models <- benchmark_grid(task, learners, resample)
bnchmrk <- benchmark(models)
# Aggregate result
measures <- msrs(c("surv.cindex", "surv.graf"))
bnchmrk$aggregate(measures)
bm <- fortify(bnchmrk)
aggr <- bnchmrk$aggregate(measures)
@cindyangelira
cindyangelira / surv_pred.R
Created January 9, 2023 12:27
survival_prediction
coxtime = aggr$resample_result[[1]]
coxtime$prediction(predict_sets = "test")
# Plot all survival curves----
plot(coxtime$prediction(predict_sets = "test")$distr, ind = 1, fun = "survival")
# Plot first two survival curves----
plot(coxtime$prediction(predict_sets = "test")$distr[1], "survival", main = "First 2 Survival Curves")
lines(coxtime$prediction(predict_sets = "test")$distr[2], "survival", col = 2)
@cindyangelira
cindyangelira / setup_pyenv_R.R
Created January 9, 2023 13:39
Set up miniconda env in R
# Set up python env in R----
library(survivalmodels)
install_pycox(pip = TRUE, install_torch = TRUE)
install_keras(pip = TRUE, install_tensorflow = TRUE)