generated from opensafely/research-template
/
03-createnattrends_codes.R
162 lines (128 loc) · 6.58 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
# ##==============================================================================
# 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"))
}
## 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)
## 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)
}
## 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(month=query_dates[1])
for (datenow in tail(query_dates,-1)){
df_input_now <- read_csv(
here::here("output","measures",paste0("input_measures_bycode_", datenow, ".csv")))
df_input_now <- df_input_now %>% mutate(month=datenow)
df_input <- df_input %>% bind_rows(df_input_now)
}
df_input <- as.data.frame(df_input)
df_input <- df_input %>% rename(snomedc_gp_consult_count=gp_consult_count)
rm(df_input_now)
myprefix="snomed"
df_summary <- df_input %>%
group_by(month) %>%
summarise_at(vars(starts_with(myprefix)),~sum(.,na.rm=T))
df_summary_pop <- df_input %>% group_by(month) %>% summarise(OC_population=n_distinct(patient_id))
df_summary <- left_join(df_summary,df_summary_pop,id="month")
rm(df_summary_pop)
## 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))
tbx_practice_flags_ <- pivot_longer(df_practice_flags,cols=starts_with(myprefix),
names_to="code",
values_to="had_instance")
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)))
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)))
tbx_practice_flags_$`Instance presence` <- factor(tbx_practice_flags_$`Instance presence`)
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()
ggsave(paste0(here::here("output","plots"),"/sc03_fig03_pracnatcoverage.svg"),width = 30, height = 20, dpi=300,units ="cm")
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()
ggsave(paste0(here::here("output","plots"),"/sc03_fig04_pracbyregcoverage.svg"),width = 30, height = 30, dpi=300,units ="cm")
rm(df_input)
rm(df_practice_flags)
rm(tbx_practice_flags_)
rm(tbx_practice_flags_reg)
## National trends
df_summary_long <- df_summary %>% pivot_longer(cols=starts_with(myprefix),
names_to="Code",
values_to="Count")
df_summary_long$Count <- redactor(df_summary_long$Count,threshold =6,e_overwrite=NA_integer_)
write.csv(df_summary_long,paste0(here::here("output","tables"),"/sc03_tb01_nattrends.csv"))
# 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/OC_population*1000)
ggplot(data=df_summary_long,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))
ggsave(paste0(here::here("output","plots"),"/sc03_fig01_nattrends.svg"),width = 30, height = 30, dpi=300,units ="cm")
# Disclosiveness: plot of national monthly tally of clinical code occurrence, not deemed disclosive.
ggplot(data=df_summary_long %>% filter(Code%!in% c("snomedc_gp_consult_count")),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))
ggsave(paste0(here::here("output","plots"),"/sc03_fig02_nattrends.svg"),width = 40, height = 20, dpi=300,units ="cm")
# Disclosiveness: plot of national monthly tally of clinical code occurrence, not deemed disclosive.
## close log connection
sink()