Skip to contents

๐Ÿ“ฆ Load RHealth

Before starting, make sure you have loaded the RHealth package:

devtools::load_all()

๐Ÿ“ Step 1: Initialize MIMIC-IV Dataset

You need to specify the root directory where your MIMIC-IV CSV files are stored:

data_dir <- "F:/R-project/mimic-iv-ehr/physionet.org/files/mimiciv/3.1"

ds <- MIMIC4EHRDataset$new(
  root = data_dir,
  tables = c("patients", "admissions", "diagnoses_icd", "procedures_icd", "prescriptions"),
  dataset_name = "mimic4_ehr",
  dev = TRUE
)

ds$stats()

๐Ÿง  Step 2: Set Prediction Task

Here we choose 30-day readmission as the prediction target:

sd <- ds$set_task(task = Readmission30DaysMIMIC4$new())

This will process the raw tables and generate a task-specific sample dataset.

๐Ÿงช Step 3: Split Dataset

Split the data by patient into train/val/test (80%/10%/10%):

splits <- split_by_patient(sd, c(0.8, 0.1, 0.1))

train_dl <- get_dataloader(splits[[1]], batch_size = 32, shuffle = TRUE)
val_dl <- get_dataloader(splits[[2]], batch_size = 32)
test_dl <- get_dataloader(splits[[3]], batch_size = 32)

๐Ÿ”ง Step 4: Build Model

We use a GRU-based recurrent neural network model:

model <- RNN$new(
  dataset = sd,
  embedding_dim = 128,
  rnn_type = "GRU",
  num_layers = 1
)

๐Ÿ‹๏ธโ€โ™‚๏ธ Step 5: Train Model

Create a Trainer object and start training:

trainer <- Trainer$new(model = model)

trainer$train(
  train_dataloader = train_dl,
  val_dataloader = val_dl,
  epochs = 10,
  optimizer_params = list(lr = 1e-3),
  monitor = "roc_auc"
)

๐Ÿ“ˆ Step 6: Evaluate Model

Finally, evaluate on the test set:

result <- trainer$evaluate(test_dl)
print(result)

โœ… Done!

You have successfully completed a full deep learning pipeline for EHR prediction using the RHealth package.