Skip to contents

This vignette demonstrates a complex and realistic use case of Rydra for creating a model for selecting patients for a clinical trial.

1. The Patient Selection Model

This model calculates a score for a patient based on their demographics, lab results, and medical history. The score is used to determine if the patient is a good candidate for the clinical trial.

The model is defined in the clinical_trial_selection.yaml file:

model_name: "clinical_trial_selection"

constants:
  creatinine: 1.0

main_model:
  intercepts:
    baseline: 0

  coefficients:
    age_above_60: 0.1
    creatinine_high: 0.2
    comorbidity_score_high: 0.3
    previous_treatment_failed: 0.4

  transformations:
    - name: "age_category"
      formula: "ifelse(age > 60, 1, 0)"
    - name: "creatinine_category"
      formula: "ifelse(creatinine > 1.5, 1, 0)"

  factors:
    - name: "comorbidity_score"
      levels:
        - value: "low"
          coefficient: "intercepts.baseline"
        - value: "medium"
          coefficient: "intercepts.baseline"
        - value: "high"
          coefficient: "coefficients.comorbidity_score_high"
    - name: "previous_treatment"
      levels:
        - value: "naive"
          coefficient: "intercepts.baseline"
        - value: "failed"
          coefficient: "coefficients.previous_treatment_failed"
        - value: "success"
          coefficient: "intercepts.baseline"

  conditions:
    - name: "age_above_60_cond"
      condition: "age_category == 1"
      coefficient: "coefficients.age_above_60"
    - name: "creatinine_high_cond"
      condition: "creatinine_category == 1"
      coefficient: "coefficients.creatinine_high"

  output_transformation: "truncate_variable(result, 0, 1.0)"

2. Using the Patient Selection Model

We can use the rydra_calculate function to calculate the score for different patients.

Scenario 1: Ideal Candidate

library(Rydra)

input_data <- list(
  age = 55,
  creatinine = 1.2,
  comorbidity_score = "medium",
  previous_treatment = "naive"
)

result <- rydra_calculate(
  config_path = system.file("extdata", "clinical_trial_selection.yaml", package = "Rydra"),
  data = input_data,
  model_name = "main_model"
)

print(result)
[1] 0

Scenario 2: High-Risk Candidate

input_data <- list(
  age = 61,
  creatinine = 1.6,
  comorbidity_score = "high",
  previous_treatment = "naive"
)

result <- rydra_calculate(
  config_path = system.file("extdata", "clinical_trial_selection.yaml", package = "Rydra"),
  data = input_data,
  model_name = "main_model"
)

print(result)
[1] 0.6

How the calculation works

  1. Transformations
  • age_category = ifelse(age > 60, 1, 0)
  • creatinine_category = ifelse(creatinine > 1.5, 1, 0)
  1. Aggregate score (result before output transformation)
  • base_score = intercepts.baseline = 0
  • factor_coeffs_sum from levels:
    • comorbidity_score: low/medium → intercepts.baseline (0); high → coefficients.comorbidity_score_high (0.3)
    • previous_treatment: naive/success → intercepts.baseline (0); failed → coefficients.previous_treatment_failed (0.4)
  • conditional_coeffs_sum:
    • if age_category == 1 add coefficients.age_above_60 (0.1)
    • if creatinine_category == 1 add coefficients.creatinine_high (0.2)
  • total_score = base_score + factor_coeffs_sum + conditional_coeffs_sum
  1. Output transformation
  • final_result = truncate_variable(result, 0, 1.0)

Worked examples - Scenario 1 (age=55, creatinine=1.2, comorbidity_score=medium, previous_treatment=naive): - age_category = 0; creatinine_category = 0 - factor_coeffs_sum = 0 + 0 = 0; conditional_coeffs_sum = 0 + 0 = 0 - total_score = 0 → final_result = 0 - Scenario 2 (age=61, creatinine=1.6, comorbidity_score=high, previous_treatment=naive): - age_category = 1; creatinine_category = 1 - factor_coeffs_sum = 0.3 + 0 = 0.3; conditional_coeffs_sum = 0.1 + 0.2 = 0.3 - total_score = 0.6 → final_result = 0.6

3. Conclusion

This example demonstrates how Rydra can be used to create highly complex and realistic models. The ability to define transformations, factors, and conditions in a structured YAML file makes it possible to build and manage sophisticated calculation logic. The versionable nature of the configuration file is crucial in a clinical setting, where traceability and reproducibility are paramount.