This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
required_packages <- c("tidyverse", "survivalmodels", "mlr3benchmark", | |
"mlr3pipelines", "mlr3proba", "mlr3tuning", "mlr3extralearners") | |
install.packages(required_packages) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set.seed(1) | |
df <- read_csv("Dialysis.csv") | |
head(df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
task <- TaskSurv$new("patient_id", backend = df_clean, time = "time", event = "event", type = "right") | |
task |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
coxtime = aggr$resample_result[[1]] | |
coxtime$prediction(predict_sets = "test") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set up python env in R---- | |
library(survivalmodels) | |
install_pycox(pip = TRUE, install_torch = TRUE) | |
install_keras(pip = TRUE, install_tensorflow = TRUE) |
OlderNewer