generated from opensafely/research-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Linda Nab
committed
Nov 12, 2023
1 parent
b4af6f3
commit 9fcf3c4
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters