generated from opensafely/research-template
/
time_series.R
194 lines (154 loc) · 6.41 KB
/
time_series.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
######################################################
# This script:
# - Creates four datasets :
# 1. Prevalence of opioid prescribing in full population;
# 2. Prevalence of opioid prescribing in people without cancer;
# 3. Incidence of opioid prescribing in full population;
# 4. Incidence of opioid prescribing in people without cancer.
# - each dataset contains monthly time series of both
# any and high dose opioid prescribing,
# broken down by various characteristics
#######################################################
# 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')
library('ggplot2')
library('RColorBrewer')
## Create directories
dir_create(here::here("output", "time series"), showWarnings = FALSE, recurse = TRUE)
dir_create(here::here("output", "for release"), showWarnings = FALSE, recurse = TRUE)
# Read in data
prev_ts <- read_csv(here::here("output", "joined", "final_ts_prev.csv"),
col_types = cols(
group = col_character(),
label = col_character(),
sex = col_character(),
date = col_date(format = "%Y-%m-%d"))) %>%
select(c("cancer", "group", "label", "sex", "date", "population", "opioid_any",
"hi_opioid_any"))
new_ts <- read_csv(here::here("output", "joined", "final_ts_new.csv"),
col_types = cols(
group = col_character(),
label = col_character(),
sex = col_character(),
date = col_date(format = "%Y-%m-%d"))) %>%
select(c("cancer", "group", "label", "sex", "date", "opioid_naive",
"opioid_new" ))
###################################
# Prevalence
###################################
## Create dataset for opioid prescribing in
## full population (combine cancer/no cancer)
prev_full <- prev_ts %>%
group_by(date, group, label, sex) %>%
summarise(opioid_any = sum(opioid_any), hi_opioid_any = sum(hi_opioid_any),
population = sum(population)) %>%
mutate(
hi_opioid_any = ifelse(group %in% c("Ethnicity", "SCD"), NA, hi_opioid_any),
# Suppression and rounding
opioid_any = case_when(opioid_any > 5 ~ opioid_any),
opioid_any = round(opioid_any / 7) * 7,
hi_opioid_any = case_when(hi_opioid_any > 5 ~ hi_opioid_any),
hi_opioid_any = round(hi_opioid_any / 7) * 7,
population = case_when(population > 5 ~ population),
population = round(population / 7) * 7,
# calculating rates
prev_rate = opioid_any / population * 1000,
prev_hi_rate = hi_opioid_any / population * 1000
)
## Create dataset for any opioid prescribing in people without cancer only
prev_nocancer <- prev_ts %>%
subset(cancer == 0) %>%
mutate(
hi_opioid_any = ifelse(group %in% c("Ethnicity", "SCD"), NA, hi_opioid_any),
# Suppression and rounding
opioid_any = case_when(opioid_any > 5 ~ opioid_any),
opioid_any = round(opioid_any / 7) * 7,
hi_opioid_any = case_when(hi_opioid_any > 5 ~ hi_opioid_any),
hi_opioid_any = round(hi_opioid_any / 7) * 7,
population = case_when(population > 5 ~ population),
population = round(population / 7) * 7,
# calculating rates
prev_rate = opioid_any / population * 1000,
prev_hi_rate = hi_opioid_any / population * 1000
) %>%
select(!c("cancer"))
print(dim(prev_full))
print(dim(prev_nocancer))
###################################
# Incidence
###################################
## Create dataset for new opioid prescribing in
## full population (combine cancer/no cancer)
new_full <- new_ts %>%
group_by(date, group, label, sex) %>%
summarise(
opioid_new = sum(opioid_new),
#hi_opioid_new = sum(hi_opioid_new),
opioid_naive = sum(opioid_naive),
#hi_opioid_naive = sum(hi_opioid_naive)
) %>%
mutate(
# Suppression and rounding
opioid_new = case_when(opioid_new > 5 ~ opioid_new),
opioid_new = round(opioid_new / 7) * 7,
#hi_opioid_new = case_when(hi_opioid_new > 5 ~ hi_opioid_new),
# hi_opioid_new = round(hi_opioid_new / 7) * 7,
opioid_naive = case_when(opioid_naive > 5 ~ opioid_naive),
opioid_naive = round(opioid_naive / 7) * 7,
#hi_opioid_naive = case_when(hi_opioid_naive > 5 ~ hi_opioid_naive),
# hi_opioid_naive = round(hi_opioid_naive / 7) * 7,
# calculating rates
new_rate = opioid_new / opioid_naive * 1000,
#new_hi_rate = hi_opioid_new / hi_opioid_naive * 1000
)
## Create dataset for new opioid prescribing in people without cancer only
new_nocancer <- new_ts %>%
subset(cancer == 0) %>%
mutate(
# Suppression and rounding
opioid_new = case_when(opioid_new > 5 ~ opioid_new),
opioid_new = round(opioid_new / 7) * 7,
#hi_opioid_new = case_when(hi_opioid_new > 5 ~ hi_opioid_new),
# hi_opioid_new = round(hi_opioid_new / 7) * 7,
opioid_naive = case_when(opioid_naive > 5 ~ opioid_naive),
opioid_naive = round(opioid_naive / 7) * 7,
#hi_opioid_naive = case_when(hi_opioid_naive > 5 ~ hi_opioid_naive),
# hi_opioid_naive = round(hi_opioid_naive / 7) * 7,
# calculating rates
new_rate = opioid_new / opioid_naive * 1000,
#new_hi_rate = hi_opioid_new / hi_opioid_naive * 1000
) %>%
select(!c("cancer"))
print(dim(new_full))
print(dim(new_nocancer))
###############################
## Sort and save as .csv
###############################
prev_full <- prev_full %>%
arrange(group, label, sex, date)
write.csv(prev_full, file = here::here("output", "for release", "ts_prev_full.csv"),
row.names = FALSE)
prev_nocancer <- prev_nocancer %>%
arrange(group, label, sex, date) %>%
subset(!(group %in% c("SCD")))
write.csv(prev_nocancer, file = here::here("output", "for release", "ts_prev_nocancer.csv"),
row.names = FALSE)
new_full <- new_full %>%
arrange(group, label, sex, date) %>%
subset(!(group %in% c("Ethnicity", "SCD")))
write.csv(new_full, file = here::here("output", "for release", "ts_new_full.csv"),
row.names = FALSE)
new_nocancer <- new_nocancer %>%
arrange(group, label, sex, date) %>%
subset(!(group %in% c("Ethnicity", "SCD")))
write.csv(new_nocancer, file = here::here("output", "for release", "ts_new_nocancer.csv"),
row.names = FALSE)