generated from opensafely/research-template
/
ITS_var_overall_monthly_without_A.R
133 lines (109 loc) · 4.84 KB
/
ITS_var_overall_monthly_without_A.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
#### this scirpt transfers the data to a prepared verson for ITS model
### load library ###
library("tidyverse")
library('dplyr')#conflict with plyr; load after plyr
library('lubridate')
### import data ###
rm(list=ls())
setwd(here::here("output", "measures"))
df2 <- readRDS('ab_type_2019.rds')
df3 <- readRDS('ab_type_2020.rds')
df4 <- readRDS('ab_type_2021.rds')
df5 <- readRDS('ab_type_2022.rds')
df2 <- bind_rows(df2)
df3 <- bind_rows(df3)
df4 <- bind_rows(df4)
DF <- rbind(df2,df3,df4,df5)
rm(df2,df3,df4,df5)
# recode
DF$infection=recode(DF$infection,
asthma ="Asthma",
cold="Cold",
cough="Cough",
copd="COPD",
pneumonia="Pneumonia",
renal="Renal",
sepsis="Sepsis",
throat="Sore throat",
uti = "UTI",
lrti = "LRTI",
urti = "URTI",
sinusits = "Sinusitis",
otmedia = "Otitis media",
ot_externa = "Otitis externa")
DF$infection[DF$infection == ""] <- NA
DF$infection <- ifelse(is.na(DF$infection),"uncoded",DF$infection)
DF$type[DF$type == ""] <- NA
### prepare var ###
broadtype <- c("Ampicillin","Co-amoxiclav","Moxifloxacin","Cefaclor","Cefadroxil",
"Cefuroxime", "Cefalexin","Cefazolin","Cefixime","Cefotaxime","Cefoxitin","Cefradine",
"Cefpirome","Ceftazidime","Ceftriaxone", "Cefprozil","Ciprofloxacin","Co-fluampicil",
"Doripenem","Ertapenem", "Cilastatin","Cefamandole","Levofloxacin" ,
"Meropenem" ,"Nalidixic acid","Norfloxacin", "Ofloxacin","Cefpodoxime","Cefepime")
start_covid = as.Date("2020-04-01")
covid_adjustment_period_from = as.Date("2020-01-01")
### Interrupted time-series analysis ###
df <- DF %>% filter (!is.na(DF$type))
### Transfer df into numOutcome / numEligible version
df$cal_year <- year(df$Date)
df$cal_mon <- month(df$Date)
df$time <- as.numeric(df$cal_mon+(df$cal_year-2019)*12)
df.broad <- df %>% filter(type %in% broadtype )
df.broad_total <- df.broad %>% group_by(time) %>% summarise(
numOutcome = n(),
)
df.all <- df %>% group_by(time) %>% summarise(
numEligible = n(),
)
df.model <- merge(df.broad_total,df.all,by="time")
df.model <- df.model %>% mutate(mon = case_when(time>=1 & time<=12 ~ time,
time>=13 & time<=24 ~ time-12,
time>=24 & time<=36 ~ time-24,
time>36 ~ time-36)) %>%
mutate(year = case_when(time>=1 & time<=12 ~ 2019,
time>=13 & time<=24 ~ 2020,
time>=24 & time<=36 ~ 2021,
time>36 ~ 2022)) %>%
mutate(day = 1)
df.model$monPlot <- as.Date(with(df.model,paste(year,mon,day,sep="-")),"%Y-%m-%d")
df.model <- df.model %>%
mutate(pre_covid = ifelse(monPlot < covid_adjustment_period_from , 1, 0),
during_covid = ifelse(monPlot >= start_covid , 1, 0)) %>%
mutate(covid = ifelse(pre_covid == 1 , 0,
ifelse (during_covid == 1, 1,
NA)))
write_csv(df.model, here::here("output", "df_noA_mon_model_overall.csv"))
rm(df,df.all,df.broad,df.broad_total,df.model)
### Overall but only included age >= 18
df <- DF %>% filter (age>=18)
df <- df %>% filter (!is.na(df$type))
### Transfer df into numOutcome / numEligible version
df$cal_year <- year(df$Date)
df$cal_mon <- month(df$Date)
df$time <- as.numeric(df$cal_mon+(df$cal_year-2019)*12)
df.broad <- df %>% filter(type %in% broadtype )
df.broad_total <- df.broad %>% group_by(time) %>% summarise(
numOutcome = n(),
)
df.all <- df %>% group_by(time) %>% summarise(
numEligible = n(),
)
df.model <- merge(df.broad_total,df.all,by="time")
df.model <- df.model %>% mutate(mon = case_when(time>=1 & time<=12 ~ time,
time>=13 & time<=24 ~ time-12,
time>=24 & time<=36 ~ time-24,
time>36 ~ time-36)) %>%
mutate(year = case_when(time>=1 & time<=12 ~ 2019,
time>=13 & time<=24 ~ 2020,
time>=24 & time<=36 ~ 2021,
time>36 ~ 2022)) %>%
mutate(day = 1)
df.model$monPlot <- as.Date(with(df.model,paste(year,mon,day,sep="-")),"%Y-%m-%d")
df.model <- df.model %>%
mutate(pre_covid = ifelse(monPlot < covid_adjustment_period_from , 1, 0),
during_covid = ifelse(monPlot >= start_covid , 1, 0)) %>%
mutate(covid = ifelse(pre_covid == 1 , 0,
ifelse (during_covid == 1, 1,
NA)))
write_csv(df.model, here::here("output", "df_noA_mon_model_overall_18.csv"))
rm(df,df.all,df.broad,df.broad_total,df.model)