From 9fcf3c4b18e87081e205297f47138ac71b3ba7d3 Mon Sep 17 00:00:00 2001 From: Linda Nab Date: Sun, 12 Nov 2023 14:18:10 +0000 Subject: [PATCH] describe trt outc same day --- .../describe_trt_outcm_same_day.R | 71 +++++++++++++++++++ project.yaml | 8 +++ 2 files changed, 79 insertions(+) create mode 100644 analysis/data_properties/describe_trt_outcm_same_day.R diff --git a/analysis/data_properties/describe_trt_outcm_same_day.R b/analysis/data_properties/describe_trt_outcm_same_day.R new file mode 100644 index 0000000..1c5541b --- /dev/null +++ b/analysis/data_properties/describe_trt_outcm_same_day.R @@ -0,0 +1,71 @@ +################################################################################ +# +# Number of people treated and experiencing an outcome on same day +# +# This script can be run via an action in project.yaml using one argument +# - 'period' /in {ba1, ba2} --> period +# +# Depending on 'period' the output of this script is: +# empty file ./output/data_properties/trt_outcm_same_day.csv +################################################################################ + +################################################################################ +# 0.0 Import libraries + functions +################################################################################ +library(readr) +library(dplyr) +library(fs) +library(here) +library(purrr) + +################################################################################ +# 0.1 Create directories for output +################################################################################ +output_dir <- here("output", "data_properties") +fs::dir_create(output_dir) + +################################################################################ +# 0.2 Import command-line arguments +################################################################################ + +################################################################################ +# 0.3 Import data +################################################################################ +file_names <- paste0(c("", "ba2_"), "data_processed.rds") +data <- + map(.x = fs::path(here("output", "data"), file_names), + .f = ~ read_rds(.x)) +names(data) <- c("ba1", "ba2") + +################################################################################ +# Number of pt treated + outcome on same day +################################################################################ +out <- + imap(.x = data, + .f = ~ + .x %>% + filter(status_primary %in% c("covid_hosp_death", "noncovid_death", "dereg") & + treatment == "Treated" & + treatment_date == min_date_all) %>% + group_by(status_primary, .drop = "FALSE") %>% + summarise(n = n()) %>% + filter(status_primary != "none") %>% + mutate(period = .y, .before = status_primary) + ) %>% bind_rows() +# Set rounding and redaction thresholds +rounding_threshold = 6 +redaction_threshold = 8 +out_red <- + out %>% + mutate(n = if_else(n > 0 & n <= redaction_threshold, + "[REDACTED]", + plyr::round_any(n, rounding_threshold) %>% as.character() + ) + ) +################################################################################ +# Save output +################################################################################ +write_csv(out, + path(output_dir, "trt_outcm_same_day.csv")) +write_csv(out_red, + path(output_dir, "trt_outcm_same_day_red.csv")) diff --git a/project.yaml b/project.yaml index 417cffb..32f79b0 100644 --- a/project.yaml +++ b/project.yaml @@ -229,6 +229,14 @@ actions: moderately_sensitive: csv: output/data_properties/ba2_sense_checks.txt + describe_trt_outcm_same_day: + run: r:latest analysis/data_properties/describe_trt_outcm_same_day.R + needs: [data_process, data_process_ba2] + outputs: + moderately_sensitive: + csv1: output/data_properties/trt_outcm_same_day.csv + csv2: output/data_properties/trt_outcm_same_day_red.csv + ## # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # # # # # ## 1 MAIN ANALYSIS - COX