generated from opensafely/research-template
/
03-createnattrends_codes.R
286 lines (237 loc) · 12.6 KB
/
03-createnattrends_codes.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# ##==============================================================================
# Analysis filename: 03-createnattrends_codes
# Project: Pilot on online consultation
# Author: MF
# Date: 01/02/2020 (save as from vidclinic one)
# Version: R
# Description: Produce tally on instances of relevant codes over time (national trend)
# Output to csv files
# Datasets used: input.csv
# Datasets created: None
# Other output: tables: 'tb*.csv'
# Log file: log-03-createnattrends.txt
#
## ==============================================================================
## open log connection to file
sink(here::here("logs", "log-03-createnattrends.txt"))
# create directory for saving plots, if not existent
if (!dir.exists(here::here("output", "plots"))){
dir.create(here::here("output", "plots"))
}
# create directory for saving plots, if not existent
if (!dir.exists(here::here("output", "tables"))){
dir.create(here::here("output", "tables"))
}
print("directories cleared")
## library
library(tidyverse)
library(here)
library(svglite)
`%!in%` = Negate(`%in%`)
query_dates=seq(as.Date("2019-01-01"),length=24,by="months")
query_dates <- paste0(query_dates)
print("Libraries loaded. Query dates established.")
## Redactor code (W.Hulme)
redactor <- function(n, threshold,e_overwrite=NA_integer_){
# given a vector of frequencies, this returns a boolean vector that is TRUE if
# a) the frequency is <= the redaction threshold and
# b) if the sum of redacted frequencies in a) is still <= the threshold, then the
# next largest frequency is also redacted
n <- as.integer(n)
leq_threshold <- dplyr::between(n, 1, threshold)
n_sum <- sum(n)
# redact if n is less than or equal to redaction threshold
redact <- leq_threshold
# also redact next smallest n if sum of redacted n is still less than or equal to threshold
if((sum(n*leq_threshold) <= threshold) & any(leq_threshold)){
redact[which.min(dplyr::if_else(leq_threshold, n_sum+1L, n))] = TRUE
}
n_redacted <- if_else(redact, e_overwrite, n)
}
print("Redactor defined")
## import and pre-process cohort data
df_input <- read_csv(
here::here("output","measures",paste0("input_measures_bycode_", query_dates[1], ".csv")))
df_input <- df_input %>% mutate(population=1) %>% group_by(region,stp,practice) %>% summarise_all(~sum(.,na.rm=T)) %>% ungroup()
df_input <- df_input %>% mutate(month=query_dates[1]) %>% select(-patient_id)
print(here::here("output","measures",paste0("input_measures_bycode_", query_dates[1], ".csv")))
for (datenow in tail(query_dates,-1)){
df_input_now <- read_csv(
here::here("output","measures",paste0("input_measures_bycode_", datenow, ".csv")))
print(here::here("output","measures",paste0("input_measures_bycode_", datenow, ".csv")))
print("loaded")
df_input_now <- df_input_now %>% mutate(population=1) %>% group_by(region,stp,practice) %>% summarise_all(~sum(.,na.rm=T)) %>% ungroup()
print("aggregated")
df_input_now <- df_input_now %>% mutate(month=datenow) %>% select(-patient_id)
df_input_now <- df_input_now %>% mutate(month=datenow)
print("mutated")
df_input <- df_input %>% bind_rows(df_input_now)
print("bound to master")
}
df_input <- as.data.frame(df_input)
#df_input <- df_input %>% rename(snomedc_gp_consult_count=gp_consult_count)
print(df_input %>% select_if(is.numeric) %>% summarise_all(~sum(.,na.rm=T)))
print(summary(df_input))
print(head(df_input,0))
rm(df_input_now)
df_summary <- df_input %>%
group_by(month) %>%
summarise_at(vars(starts_with("snomed"),starts_with("OC"),population,gp_consult_count),~sum(.,na.rm=T))
print("summary created -a")
#df_summary_pop <- df_input %>% group_by(month) %>% summarise(OC_population=n_distinct(patient_id))
#print("summary created -b")
#df_summary <- left_join(df_summary,df_summary_pop,id="month")
#print("summary created -c")
#rm(df_summary_pop)
#### SNOMED - practice coverage ###
myprefix="snomed"
## Calculations for practice coverage
df_practice_flags <- df_input %>% group_by(region,stp,practice) %>% summarise_at(vars(starts_with(myprefix)),~ifelse(sum(.,na.rm=T)>0,1,0))
print("practice calc 1")
tbx_practice_flags_ <- pivot_longer(df_practice_flags,cols=starts_with(myprefix),
names_to="code",
values_to="had_instance")
print("practice calc 2")
tbx_practice_flags_reg <- tbx_practice_flags_ %>%
group_by(region,code) %>%
summarise(Present=sum(had_instance),Absent=n()-Present) %>%
pivot_longer(c("Present","Absent"),names_to="Instance presence",values_to="no_practices") %>%
mutate(code=substr(code,nchar(myprefix)+2,nchar(code)))
print("practice calc 3")
tbx_practice_flags_ <- tbx_practice_flags_ %>%
group_by(code) %>%
summarise(Present=sum(had_instance),Absent=n()-Present) %>%
pivot_longer(c("Present","Absent"),names_to="Instance presence",values_to="no_practices") %>%
mutate(code=substr(code,nchar(myprefix)+2,nchar(code)))
print("practice calc 4")
tbx_practice_flags_$`Instance presence` <- factor(tbx_practice_flags_$`Instance presence`)
print("practice calc 5")
ggplot(tbx_practice_flags_, aes(fill=`Instance presence`,x=code, y=no_practices,label=no_practices)) +
geom_bar( stat="identity")+
geom_text(aes(vjust=0),position = position_stack(vjust = 0.2))+
theme(axis.text.x = element_text(angle = -90),text = element_text(size=15))+
labs(title="Portion of practices with code recorded",y="Count of practices",x="Code")+
coord_flip()
print("practice fig03 created")
ggsave(paste0(here::here("output","plots"),"/sc03_fig03_pracnatcoverage.svg"),width = 30, height = 20, dpi=300,units ="cm")
print("practice fig03 saved")
ggplot(tbx_practice_flags_reg, aes(fill=`Instance presence`,x=code, y=no_practices,label=no_practices)) +
geom_bar( stat="identity")+
geom_text(aes(hjust=-1),position = position_stack(vjust = 0.0))+
theme(axis.text.x = element_text(angle = -90),text = element_text(size=15))+
labs(title="Portion of practices with code recorded",y="Count of practices",x="Code")+facet_wrap(~region)+
coord_flip()
print("practice fig04 created")
ggsave(paste0(here::here("output","plots"),"/sc03_fig04_pracbyregcoverage.svg"),width = 30, height = 30, dpi=300,units ="cm")
print("practice fig04 saved")
#rm(df_input)
rm(df_practice_flags)
rm(tbx_practice_flags_)
rm(tbx_practice_flags_reg)
#### CTV3 - practice coverage ###
myprefix="OC"
## Calculations for practice coverage
df_practice_flags <- df_input %>% group_by(region,stp,practice) %>% summarise_at(vars(starts_with(myprefix)),~ifelse(sum(.,na.rm=T)>0,1,0))
print("practice calc 1")
tbx_practice_flags_ <- pivot_longer(df_practice_flags,cols=starts_with(myprefix),
names_to="code",
values_to="had_instance")
print("practice calc 2")
tbx_practice_flags_reg <- tbx_practice_flags_ %>%
group_by(region,code) %>%
summarise(Present=sum(had_instance),Absent=n()-Present) %>%
pivot_longer(c("Present","Absent"),names_to="Instance presence",values_to="no_practices") %>%
mutate(code=substr(code,nchar(myprefix)+2,nchar(code)))
print("practice calc 3")
tbx_practice_flags_ <- tbx_practice_flags_ %>%
group_by(code) %>%
summarise(Present=sum(had_instance),Absent=n()-Present) %>%
pivot_longer(c("Present","Absent"),names_to="Instance presence",values_to="no_practices") %>%
mutate(code=substr(code,nchar(myprefix)+2,nchar(code)))
print("practice calc 4")
tbx_practice_flags_$`Instance presence` <- factor(tbx_practice_flags_$`Instance presence`)
print("practice calc 5")
ggplot(tbx_practice_flags_, aes(fill=`Instance presence`,x=code, y=no_practices,label=no_practices)) +
geom_bar( stat="identity")+
geom_text(aes(vjust=0),position = position_stack(vjust = 0.2))+
theme(axis.text.x = element_text(angle = -90),text = element_text(size=15))+
labs(title="Portion of practices with code recorded",y="Count of practices",x="Code")+
coord_flip()
print("practice fig05 created")
ggsave(paste0(here::here("output","plots"),"/sc03_fig05_ctv3pracnatcoverage.svg"),width = 30, height = 20, dpi=300,units ="cm")
print("practice fig05 saved")
ggplot(tbx_practice_flags_reg, aes(fill=`Instance presence`,x=code, y=no_practices,label=no_practices)) +
geom_bar( stat="identity")+
geom_text(aes(hjust=-1),position = position_stack(vjust = 0.0))+
theme(axis.text.x = element_text(angle = -90),text = element_text(size=15))+
labs(title="Portion of practices with code recorded",y="Count of practices",x="Code")+facet_wrap(~region)+
coord_flip()
print("practice fig06 created")
ggsave(paste0(here::here("output","plots"),"/sc03_fig06_ctv3pracbyregcoverage.svg"),width = 30, height = 30, dpi=300,units ="cm")
print("practice fig06 saved")
#### National trends - snomed ####
myprefix="snomed"
df_summary_long <- df_summary %>% pivot_longer(cols=c("gp_consult_count",union(starts_with("snomed"),starts_with("oc"))),
names_to="Code",
values_to="Count")
print("national calc 1")
df_summary_long$Count <- redactor(df_summary_long$Count,threshold =6,e_overwrite=NA_integer_)
print("national calc2")
write.csv(df_summary_long,paste0(here::here("output","tables"),"/sc03_tb01_nattrends.csv"))
print("national calc saved")
# Disclosiveness: national monthly tally of clinical code occurrence, not deemed disclosive.
df_summary_long$month <- as.Date(df_summary_long$month)
df_summary_long <- df_summary_long %>% mutate(rate_per_1000=Count/population*1000)
ggplot(data=df_summary_long %>% filter(Code=="gp_consult_count"|substr(Code,1,nchar(myprefix))==myprefix),aes(x=month,y=rate_per_1000,group=Code)) +
geom_bar(stat="identity",fill="#56B4E9") +
facet_wrap(~Code,nrow=4,scales="free_y") +
labs(y="Instance rate (per 1,000)")+
scale_x_date(date_breaks = "2 months",expand=c(0,0)) +
theme(axis.text.x = element_text(angle = -90,vjust = 0))
print("national fig01 created")
ggsave(paste0(here::here("output","plots"),"/sc03_fig01_nattrends.svg"),width = 30, height = 30, dpi=300,units ="cm")
print("national fig01 saved")
# Disclosiveness: plot of national monthly tally of clinical code occurrence, not deemed disclosive.
ggplot(data=df_summary_long %>% filter(substr(Code,1,nchar(myprefix))==myprefix),aes(x=month,y=rate_per_1000,color=Code)) +
geom_line()+
scale_x_date(date_breaks = "2 months",expand=c(0,0))+
labs(y="Instance rate (per 1,000)")+
theme(axis.text.x = element_text(angle = -90,vjust = 0))
print("national fig02 created")
ggsave(paste0(here::here("output","plots"),"/sc03_fig02_nattrends.svg"),width = 40, height = 20, dpi=300,units ="cm")
print("national fig02 saved")
# Disclosiveness: plot of national monthly tally of clinical code occurrence, not deemed disclosive.
#### National trends - ctv3 ####
myprefix="OC"
df_summary_long <- df_summary %>% pivot_longer(cols=c("gp_consult_count",union(starts_with("snomed"),starts_with("oc"))),
names_to="Code",
values_to="Count")
print("national calc 1")
df_summary_long$Count <- redactor(df_summary_long$Count,threshold =6,e_overwrite=NA_integer_)
print("national calc2")
write.csv(df_summary_long,paste0(here::here("output","tables"),"/sc03_tb01_nattrends.csv"))
print("national calc saved")
# Disclosiveness: national monthly tally of clinical code occurrence, not deemed disclosive.
df_summary_long$month <- as.Date(df_summary_long$month)
df_summary_long <- df_summary_long %>% mutate(rate_per_1000=Count/population*1000)
ggplot(data=df_summary_long %>% filter(Code=="gp_consult_count"|substr(Code,1,nchar(myprefix))==myprefix),aes(x=month,y=rate_per_1000,group=Code)) +
geom_bar(stat="identity",fill="#56B4E9") +
facet_wrap(~Code,nrow=4,scales="free_y") +
labs(y="Instance rate (per 1,000)")+
scale_x_date(date_breaks = "2 months",expand=c(0,0)) +
theme(axis.text.x = element_text(angle = -90,vjust = 0))
print("national fig07 created")
ggsave(paste0(here::here("output","plots"),"/sc03_fig07_ctv3nattrends.svg"),width = 30, height = 30, dpi=300,units ="cm")
print("national fig07 saved")
# Disclosiveness: plot of national monthly tally of clinical code occurrence, not deemed disclosive.
ggplot(data=df_summary_long %>% filter(substr(Code,1,nchar(myprefix))==myprefix),aes(x=month,y=rate_per_1000,color=Code)) +
geom_line()+
scale_x_date(date_breaks = "2 months",expand=c(0,0))+
labs(y="Instance rate (per 1,000)")+
theme(axis.text.x = element_text(angle = -90,vjust = 0))
print("national fig08 created")
ggsave(paste0(here::here("output","plots"),"/sc03_fig08_ctv3nattrends.svg"),width = 40, height = 20, dpi=300,units ="cm")
print("national fig08 saved")
# Disclosiveness: plot of national monthly tally of clinical code occurrence, not deemed disclosive.
## close log connection
sink()