generated from opensafely/research-template
/
process_ts_overall.R
96 lines (79 loc) · 3.92 KB
/
process_ts_overall.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
######################################################
# This script:
# - imports measures data for overall prescribing
# - cleans up data
# - applies rounding and redaction
# - saves processed dataset(s)
#######################################################
# For running locally only #
#setwd("C:/Users/aschaffer/OneDrive - Nexus365/Documents/GitHub/opioids-covid-research")
#getwd()
# Import libraries #
library('tidyverse')
library('lubridate')
library('arrow')
library('here')
library('reshape2')
library('dplyr')
library('fs')
## Custom functions
source(here("analysis", "lib", "custom_functions.R"))
# Create directory
dir_create(here::here("output", "timeseries"), showWarnings = FALSE, recurse = TRUE)
dir_create(here::here("output", "measures"), showWarnings = FALSE, recurse = TRUE)
###############################
# Clean up measures datasets #
###############################
# Overall counts
overall <- read_csv(here::here("output", "measures", "measures_overall.csv")) %>%
filter(str_detect(measure, "_nocancer", negate = TRUE)) %>%
mutate(month = as.Date(interval_start, format="%Y-%m-%d"),
period = ifelse(month < as.Date("2020-03-01"), "Pre-COVID",
ifelse(month >= as.Date("2021-04-01"), "Recovery", "Lockdown"))) %>%
dplyr::select(!c(interval_start, interval_end, ratio)) %>%
pivot_wider(names_from = measure, values_from = c(numerator, denominator)) %>%
rename(opioid_any = numerator_opioid_any,
hi_opioid_any = numerator_hi_opioid_any,
opioid_new = numerator_opioid_new,
pop_total = denominator_opioid_any,
pop_naive = denominator_opioid_new) %>%
dplyr::select(!c(denominator_hi_opioid_any))
write.csv(overall, file = here::here("output", "timeseries", "ts_overall.csv"),
row.names = FALSE)
# Overall counts - without cancer
overall_noca <- read_csv(here::here("output", "measures", "measures_overall.csv")) %>%
filter(str_detect(measure, "_nocancer", negate = FALSE)) %>%
mutate(month = as.Date(interval_start, format="%Y-%m-%d"),
period = ifelse(month < as.Date("2020-03-01"), "Pre-COVID",
ifelse(month >= as.Date("2021-04-01"), "Recovery", "Lockdown")),
measure = gsub("_nocancer", "", measure)) %>%
dplyr::select(!c(interval_start, interval_end, ratio)) %>%
pivot_wider(names_from = measure, values_from = c(numerator, denominator)) %>%
rename(opioid_any = numerator_opioid_any,
hi_opioid_any = numerator_hi_opioid_any,
opioid_new = numerator_opioid_new,
pop_total = denominator_opioid_any,
pop_naive = denominator_opioid_new) %>%
dplyr::select(!c(denominator_hi_opioid_any))
write.csv(overall_noca, file = here::here("output", "timeseries", "ts_overall_nocancer.csv"),
row.names = FALSE)
###########################
# Rounding and redaction #
###########################
overall_round <- read_csv(here::here("output", "timeseries", "ts_overall.csv")) %>%
mutate(opioid_any_round = rounding(opioid_any),
hi_opioid_any_round = rounding(hi_opioid_any),
opioid_new_round = rounding(opioid_new),
pop_total_round = rounding(pop_total),
pop_naive_round = rounding(pop_naive)) %>%
dplyr::select(!c(opioid_any, hi_opioid_any, opioid_new, pop_total, pop_naive))
write.csv(overall_round, here::here("output", "timeseries", "ts_overall_rounded.csv"),
row.names = FALSE)
overall_nocancer_round <- read_csv(here::here("output", "timeseries", "ts_overall_nocancer.csv")) %>%
mutate(opioid_any_round = rounding(opioid_any),
hi_opioid_any_round = rounding(hi_opioid_any),
opioid_new_round = rounding(opioid_new),
pop_total_round = rounding(pop_total),
pop_naive_round = rounding(pop_naive)) %>%
dplyr::select(!c(opioid_any, hi_opioid_any, opioid_new, pop_total, pop_naive))
write.csv(overall_nocancer_round, here::here("output", "timeseries", "ts_overall_nocancer_rounded.csv"), row.names = FALSE)