From 802dc5c613797db6d672b1d6194f0d53aa6a4f1f Mon Sep 17 00:00:00 2001
From: Thomas  OBADIA <tobadia@ml24-0166.corp.pasteur.fr>
Date: Wed, 4 Dec 2024 15:18:32 +0100
Subject: [PATCH] Now that IPM data from the observational study are available,
 I need to split the curation rule (that fixes the agey calculation) by
 country, because only the data from Ethiopia suffered from that bug.

---
 .../OBSERVATIONAL_00_R_environment.R          |  5 +--
 .../OBSERVATIONAL_02_curate_REDCap_raw_data.R | 32 +++++++++++++------
 2 files changed, 25 insertions(+), 12 deletions(-)

diff --git a/02_OBSERVATIONAL/OBSERVATIONAL_00_R_environment.R b/02_OBSERVATIONAL/OBSERVATIONAL_00_R_environment.R
index ef3c615..d1987f2 100644
--- a/02_OBSERVATIONAL/OBSERVATIONAL_00_R_environment.R
+++ b/02_OBSERVATIONAL/OBSERVATIONAL_00_R_environment.R
@@ -44,7 +44,7 @@ dotenv::load_dot_env("./02_OBSERVATIONAL/.env")
 #    - TRUE : data will be dumped
 #    - FALSE: data will not be dumped
 DATA_SOURCE_REDCAP_IPP_OBS     <- FALSE
-DATA_SOURCE_REDCAP_IPM_OBS     <- FALSE
+DATA_SOURCE_REDCAP_IPM_OBS     <- TRUE
 DATA_SOURCE_REDCAP_AHRI_OBS    <- TRUE
 
 
@@ -89,4 +89,5 @@ SEED <- 12345
 ## the raw data dumped from REDCap or not.
 #    - TRUE : curation rule will be enforced
 #    - FALSE: curation rule will not be enforced (default)
-CURATE_DATA_RECALCULATE_AGEY <- TRUE
+CURATE_DATA_RECALCULATE_AGEY_ETHIOPIA   <- TRUE
+CURATE_DATA_RECALCULATE_AGEY_MADAGASCAR <- FALSE
diff --git a/02_OBSERVATIONAL/OBSERVATIONAL_02_curate_REDCap_raw_data.R b/02_OBSERVATIONAL/OBSERVATIONAL_02_curate_REDCap_raw_data.R
index 3c0c2a1..9be600b 100644
--- a/02_OBSERVATIONAL/OBSERVATIONAL_02_curate_REDCap_raw_data.R
+++ b/02_OBSERVATIONAL/OBSERVATIONAL_02_curate_REDCap_raw_data.R
@@ -32,16 +32,28 @@ source("./02_OBSERVATIONAL/OBSERVATIONAL_01_dump_REDCap_database.R")
 ## Start from the raw dataset generated in OBSERVATIONAL_01_dump_REDCap_database.R
 dat_observational_curated <- dat_observational_raw %>% 
   # Go through each curation flag
-  mutate(agey = case_when(CURATE_DATA_RECALCULATE_AGEY & (dob_yn %in% c(1, "Yes") & !is.na(dob)) ~ round(x      = as.numeric(difftime(time1 = visdat, 
-                                                                                                                                      time2 = dob, 
-                                                                                                                                      units = "days")) / 365.25, 
-                                                                                                         digits = 0), 
-                          CURATE_DATA_RECALCULATE_AGEY & (dob_yn %in% c(0, "No") | is.na(dob)) ~ round(x      = as.numeric(difftime(time1 = visdat, 
-                                                                                                                                    time2 = as.Date(x      = calc_dob_from_myob, 
-                                                                                                                                                    format = "%d-%m-%Y"), 
-                                                                                                                                    units = "days")) / 365.25, 
-                                                                                                       digits = 0), 
-                          .default = agey))
+  mutate(
+    # agey: distinct curation flages for Ethiopia & Madagascar
+    agey = case_when(CURATE_DATA_RECALCULATE_AGEY_ETHIOPIA & country %in% c("E", "Ethiopia") & (dob_yn %in% c(1, "Yes") & !is.na(dob)) ~ round(x      = as.numeric(difftime(time1 = visdat, 
+                                                                                                                                                                            time2 = dob, 
+                                                                                                                                                                            units = "days")) / 365.25, 
+                                                                                                                                               digits = 0), 
+                     CURATE_DATA_RECALCULATE_AGEY_ETHIOPIA & country %in% c("E", "Ethiopia") & (dob_yn %in% c(0, "No") | is.na(dob)) ~ round(x      = as.numeric(difftime(time1 = visdat, 
+                                                                                                                                                                          time2 = as.Date(x      = calc_dob_from_myob, 
+                                                                                                                                                                                          format = "%d-%m-%Y"), 
+                                                                                                                                                                          units = "days")) / 365.25, 
+                                                                                                                                             digits = 0), 
+                     
+                     CURATE_DATA_RECALCULATE_AGEY_MADAGASCAR & country %in% c("M", "Madagascar") & (dob_yn %in% c(1, "Yes") & !is.na(dob)) ~ round(x      = as.numeric(difftime(time1 = visdat, 
+                                                                                                                                                                                time2 = dob, 
+                                                                                                                                                                                units = "days")) / 365.25, 
+                                                                                                                                                   digits = 0), 
+                     CURATE_DATA_RECALCULATE_AGEY_MADAGASCAR & country %in% c("M", "Madagascar") & (dob_yn %in% c(0, "No") | is.na(dob)) ~ round(x      = as.numeric(difftime(time1 = visdat, 
+                                                                                                                                                                              time2 = as.Date(x      = calc_dob_from_myob, 
+                                                                                                                                                                                              format = "%d-%m-%Y"), 
+                                                                                                                                                                              units = "days")) / 365.25, 
+                                                                                                                                                 digits = 0), 
+                     .default = agey))
 
 
 
-- 
GitLab