diff --git a/01_INVENTORY/INVENTORY_00_R_environment.R b/01_INVENTORY/INVENTORY_00_R_environment.R index aa7f8ab362bd18070021eab12a5dd37ebb9a708c..58a7fdb06ec1143980415c2271b5e0143ec37976 100644 --- a/01_INVENTORY/INVENTORY_00_R_environment.R +++ b/01_INVENTORY/INVENTORY_00_R_environment.R @@ -101,3 +101,13 @@ MAP_TYPE <- "hybrid" ## Layout of panels for plotting GPS maps MAP_PLOT_PANEL_LAYOUT_NROW <- 3 MAP_PLOT_PANEL_LAYOUT_NCOL <- 3 + + + + + +###################################################################### +### SEED VALUE +###################################################################### +## A global seed value to be used by set.seed() calls +SEED <- 12345 diff --git a/01_INVENTORY/INVENTORY_02_list_all_inventory_participants.R b/01_INVENTORY/INVENTORY_02_list_all_inventory_participants.R deleted file mode 100644 index e5faf98dc5de82d5f82630ed56222a59acc4fe18..0000000000000000000000000000000000000000 --- a/01_INVENTORY/INVENTORY_02_list_all_inventory_participants.R +++ /dev/null @@ -1,120 +0,0 @@ -## INVENTORY_02_list_all_inventory_participants.R -## Date : 2023/10/31 -## Author : Thomas Obadia -## -## This script starts by fetching another one to dump the database -## for the PvSTATEM Inventory project (hosted into REDCap). -## It subsequently processes the inventory database to generate a list -## of all individuals that were listed, and saves that list into -## an output CSV file. -###################################################################### - - - - - -###################################################################### -### SOURCE THE DATABASE -###################################################################### -if (!exists("DATA_EXTRACT_IS_RECENT") || DATA_EXTRACT_IS_RECENT) { - source("./01_INVENTORY/INVENTORY_01_dump_REDCap_database.R") -} - -## Update DATA_EXTRACT_IS_RECENT_FLAG for future calls -DATA_EXTRACT_IS_RECENT <- as.logical(as.numeric(difftime(time1 = Sys.Date(), - time2 = DATA_EXTRACT_TS, - units = "days")) <= DATA_EXTRACT_EXPIRY_TIME_D) - - - - - -###################################################################### -### DERIVE LIST OF ALL INDIVIDUALS -###################################################################### -inventory_list_p <- dat_inventory_raw %>% - select( - # REDCap internal record_id - record_id, - - # Items used to derive the house-and-household ID - country, clusterid, hid, hh_n, - tidyselect::matches("^hh\\d{2}_subjid\\d{2}_(id|sex|agey)$")) %>% - - # Prevent mis-interpretation of *_sex columns because 'F' can be FALSE in some context - mutate(across(ends_with("_sex"), function(x) {plyr::mapvalues(x, - from = c(FALSE), - to = c("F"), - warn_missing = FALSE)})) %>% - - # Go from wide format to long to list household members by row, - # then propagate age and sex to dedicated columns, to end up with - # a single row per individual - pivot_longer(cols = starts_with("hh0"), - names_to = c("pivot_hh_n", "pivot_hh_subjid", "pivot_variable"), - names_pattern = c("^(hh\\d{2})_(subjid\\d{2})_(id|sex|agey)$"), - values_transform = as.character) %>% - pivot_wider(names_from = "pivot_variable", - values_from = "value") %>% - mutate(agey = as.numeric(agey)) %>% - extract(pivot_hh_n, - into = c("nested_hhid"), - regex = "^hh(\\d{2})$") %>% - extract(pivot_hh_subjid, - into = c("nested_subjid"), - regex = "^subjid(\\d{2})$") %>% - - # Add age as a categorical variable - mutate(agey_cat = case_when(country == "M" & agey < 1 ~ "y00_01", - country == "M" & agey >= 1 & agey <= 5 ~ "y01_05", - country == "M" & agey > 5 & agey <= 13 ~ "y05_13", - country == "M" & agey > 13 & agey <= 24 ~ "y13_24", - country == "M" & agey > 24 ~ "y24_inf")) %>% - - # Discard rows for whom no data was filled, i.e. empty placeholders in REDCap - filter(!is.na(sex) & !is.na(agey)) %>% - - # Retain only columns of interest for derived data (list of everyone in inventory) - select( - # REDCap internal record_id - record_id, - - # Items used to derive the house-and-household ID - country, clusterid, hid, nested_hhid, nested_subjid, - - # Actual demographics - sex, agey, agey_cat) - - - - - -###################################################################### -### WRITE LIST TO OUTPUT -###################################################################### -## Name of output file -INVENTORY_OUT_02_FILENAME <- paste0("INVENTORY_OUT_02_list_individuals", - "_country-", - paste(unique(inventory_list_p$country), collapse = "-"), - "_timestamp-", - strftime(Sys.time(), format = "%Y%m%d_%H%M%S"), - ".csv") - -## Write to output file -write.table(inventory_list_p, - file = paste0("./01_INVENTORY/outputs/", - INVENTORY_OUT_02_FILENAME), - sep = ",", - dec = ".", - quote = TRUE, - col.names = TRUE, - row.names = FALSE) - - - - - -###################################################################### -### CLEANUP -###################################################################### -rm(INVENTORY_OUT_02_FILENAME) diff --git a/01_INVENTORY/INVENTORY_05_generate_list_of_participants_for_observational_study.R b/01_INVENTORY/INVENTORY_05_generate_list_of_participants_for_observational_study.R index dae565a5c2ad7008bec1679d40d65e792fe78447..3ec471852394810dfd0df468aee4642a82d779c4 100644 --- a/01_INVENTORY/INVENTORY_05_generate_list_of_participants_for_observational_study.R +++ b/01_INVENTORY/INVENTORY_05_generate_list_of_participants_for_observational_study.R @@ -1,6 +1,6 @@ ## INVENTORY_05_select_list_function.R ## Date : 2024/02/02 -## Author : Eliharintsoa Rajaoranimirana +## Author : Eliharintsoa Rajaoranimirana, Thomas Obadia ## ## This script provides functions to sample from the population from ## the inventory project and generate a list of participants of @@ -11,6 +11,67 @@ -myfunction(32, 20) +###################################################################### +### SOURCE THE DATABASE +###################################################################### +if (!exists("DATA_EXTRACT_IS_RECENT") || DATA_EXTRACT_IS_RECENT) { + source("./01_INVENTORY/INVENTORY_01_dump_REDCap_database.R") +} + +## Update DATA_EXTRACT_IS_RECENT_FLAG for future calls +DATA_EXTRACT_IS_RECENT <- as.logical(as.numeric(difftime(time1 = Sys.Date(), + time2 = DATA_EXTRACT_TS, + units = "days")) <= DATA_EXTRACT_EXPIRY_TIME_D) + + + + + +###################################################################### +### REPLICABILITY +###################################################################### +set.seed(seed = SEED) + + -save_multiple_cluster(c(3,4), c(10,5)) + + +###################################################################### +### GENERATE SAMPLING LIST +###################################################################### +observational_list_p <- get_target_list_for_observational_study(x = inventory_list_p, + n_target = 270) + + + + + +###################################################################### +### WRITE LIST TO OUTPUT +###################################################################### +## Name of output file +INVENTORY_OUT_05_FILENAME <- paste0("INVENTORY_OUT_05_list_participants_for_observational_study", + "_country-", + paste(unique(observational_list_p$country), collapse = "-"), + "_timestamp-", + strftime(Sys.time(), format = "%Y%m%d_%H%M%S"), + ".csv") + +## Write to output file +write.table(observational_list_p, + file = paste0("./01_INVENTORY/outputs/", + INVENTORY_OUT_05_FILENAME), + sep = ",", + dec = ".", + quote = TRUE, + col.names = TRUE, + row.names = FALSE) + + + + + +###################################################################### +### CLEANUP +###################################################################### +rm(INVENTORY_OUT_05_FILENAME)