Skip to content
Snippets Groups Projects
Commit 2ea2fa1f authored by Thomas  OBADIA's avatar Thomas OBADIA :speech_balloon:
Browse files

Change name of INVENTORY_05 script and clean content to make it call the...

Change name of INVENTORY_05 script and clean content to make it call the required functions and generate the expected output
parent 7ed519f8
No related branches found
No related tags found
1 merge request!1Implement sampling from population inventory
...@@ -101,3 +101,13 @@ MAP_TYPE <- "hybrid" ...@@ -101,3 +101,13 @@ MAP_TYPE <- "hybrid"
## Layout of panels for plotting GPS maps ## Layout of panels for plotting GPS maps
MAP_PLOT_PANEL_LAYOUT_NROW <- 3 MAP_PLOT_PANEL_LAYOUT_NROW <- 3
MAP_PLOT_PANEL_LAYOUT_NCOL <- 3 MAP_PLOT_PANEL_LAYOUT_NCOL <- 3
######################################################################
### SEED VALUE
######################################################################
## A global seed value to be used by set.seed() calls
SEED <- 12345
## 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)
## INVENTORY_05_select_list_function.R ## INVENTORY_05_select_list_function.R
## Date : 2024/02/02 ## Date : 2024/02/02
## Author : Eliharintsoa Rajaoranimirana ## Author : Eliharintsoa Rajaoranimirana, Thomas Obadia
## ##
## This script provides functions to sample from the population from ## This script provides functions to sample from the population from
## the inventory project and generate a list of participants of ## the inventory project and generate a list of participants of
...@@ -11,6 +11,67 @@ ...@@ -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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment