-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDescriptiveGraphs.Rmd
379 lines (283 loc) · 11.5 KB
/
DescriptiveGraphs.Rmd
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
---
title: "Descriptive Graphs"
author: "Anna"
date: "10/16/2020"
output:
html_document:
keep_md: true
---
```{r setup, include=FALSE}
library(ggplot2)
library(knitr)
library(fst)
library(data.table)
library(tidyverse)
library(ggthemes)
data.table::setDTthreads(10) #this is so that don't use all the processors
xaxisyear <- c(2004, 2008, 2012, 2016, 2020)
```
# load the data
```{r echo=FALSE, out.width = '100%'}
data_dir_CPRD <-
function(x = character(0))
paste0("/mnt/", Sys.info()[["user"]],
"/UoL/CPRD2019mm/Data May 2020/", x)
combi_mm <- read_fst(data_dir_CPRD("combi_mm_detailed.fst"), as.data.table = T)
combi_mm[, `:=` (
imd = factor(imd),
bmm = factor(bmm),
cmm = factor(cmm))]
```
# Descriptive
```{r}
require(scales)
ggplot(combi_mm[, .N , keyby = c("year", "gender") ],
aes(x =year, y = N, fill = gender)) +
geom_bar(stat= "identity", position=position_stack()) +
scale_y_continuous(labels = comma)
#ggsave("prelim_graphs/descr_num.png")
ggplot(combi_mm[, .N , keyby = c("year", "gender", "agegrp10_simple") ],
aes(x =year, y = N, fill = agegrp10_simple)) +
geom_bar(stat= "identity", position=position_fill()) +
facet_grid(cols = vars(gender)) +
ylab("proportion")
#ggsave("prelim_graphs/descr_sex_age.png")
ggplot(combi_mm[imd != "", .N , keyby = c("year", "imd") ],
aes(x =year, y = N, fill = imd)) +
geom_bar(stat= "identity", position=position_fill()) +
ylab("proportion")
ggplot(combi_mm[imd != "", .N , keyby = c("year", "imd", "gender") ],
aes(x =year, y = N, fill = imd)) +
geom_bar(stat= "identity", position=position_fill()) +
facet_grid(cols = vars(gender)) +
ylab("proportion")
#ggsave("prelim_graphs/descr_sex_imd.png")
ggplot(combi_mm[imd != "", .N , keyby = c("year", "imd", "agegrp10_simple") ],
aes(x =year, y = N, fill = agegrp10_simple)) +
geom_bar(stat= "identity", position=position_fill()) +
facet_grid(cols = vars(imd)) +
ylab("proportion") +
theme_few() +
theme(axis.text.x = element_text(size = 6, angle = 90, hjust = 1)) +
labs(fill = "Age group")
ggplot(combi_mm[imd != "", .N , keyby = c("year", "imd", "agegrp10_simple") ],
aes(x =year, y = N, fill = imd)) +
geom_bar(stat= "identity", position=position_fill()) +
facet_grid(cols = vars(agegrp10_simple)) +
ylab("proportion") +
theme_few() +
theme(axis.text.x = element_text(size = 6, angle = 90, hjust = 1)) +
labs(fill = "Age group")
#ggsave("prelim_graphs/descr_imd_age.png")
```
# Indeterminate Sex
```{r}
ggplot(combi_mm[gender == "Indeterminate",
.N ,
keyby = c("year", "agegrp10_simple")],
aes(x = year, y = N, fill = agegrp10_simple)) +
geom_bar(stat = "identity", position = position_stack()) +
scale_y_continuous(name = "Number individuals registered",
limits = c(0, 200))
#ggsave("prelim_graphs/descr_sexI_age.png")
ggplot(combi_mm[gender == "Indeterminate" & imd != "",
.N ,
keyby = c("year", "imd") ],
aes(x =year, y = N, fill = imd)) +
geom_bar(stat= "identity", position=position_fill()) +
ylab("proportion") #+
#scale_y_continuous(name="Number individuals registered", limits = c(0, 200))
#ggsave("prelim_graphs/descr_sexI_imd.png")
```
# IMD Missing
```{r}
ggplot(combi_mm[gender != "Indeterminate" &
imd == "", .N , keyby = c("year", "gender")],
aes(x = year, y = N, fill = gender)) +
geom_bar(stat = "identity", position = position_stack()) +
scale_y_continuous(name = "Number individuals registered", limits = c(0, 750))
#ggsave("prelim_graphs/noimd_sex.png")
ggplot(combi_mm[imd == "", .N , keyby = c("year", "agegrp10_simple") ],
aes(x =year, y = N, fill = agegrp10_simple)) +
geom_bar(stat= "identity", position=position_fill()) +
ylab("proportion") #+
#scale_y_continuous(name="Number individuals registered", limits = c(0, 200))
#ggsave("prelim_graphs/noimd_age.png")
```
#Looking at nmbers by sex
```{r}
combi_mm[, table(year, gender)]
combi_mm[, as.list(round(table(gender)/.N*100, 2)), keyby = .(year)]
combi_mm[year %in% c(2004, 2019),
table(year, gender)]
combi_mm[year %in% c(2004, 2019),
as.list(round(table(gender)/.N*100, 2)), keyby = .(year)]
```
#Looking at nmbers in different agegroups
```{r}
combi_mm[year == 2004, summary(age)]
combi_mm[year == 2019, summary(age)]
combi_mm[, table(year, agegrp10_simple)]
combi_mm[, as.list(round(table(agegrp10_simple)/.N*100, 2)), keyby = .(year)]
combi_mm[year %in% c(2004, 2019),
table(year, agegrp10_simple)]
combi_mm[year %in% c(2004, 2019),
as.list(round(table(agegrp10_simple)/.N*100, 2)), keyby = .(year)]
ggplot(combi_mm[imd != "", .N , keyby = c("year", "imd", "agegrp10_simple") ],
aes(x =year, y = N, fill = agegrp10_simple)) +
geom_bar(stat= "identity", position=position_stack()) +
facet_grid(cols = vars(imd)) +
ylab("proportion") +
theme(axis.text.x = element_text(size = 6, angle = 90, hjust = 1))
ggplot(combi_mm[imd != "", .N , keyby = c("year", "imd", "agegrp10_simple") ],
aes(x =year, y = N, fill = agegrp10_simple)) +
geom_bar(stat= "identity", position=position_stack()) +
facet_grid(cols = vars(imd)) +
ylab("proportion") +
theme_few() +
theme(axis.text.x = element_text(size = 6, angle = 90, hjust = 1))
ggplot(combi_mm[imd != "", .N , keyby = c("year", "imd", "agegrp10_simple") ],
aes(x =year, y = N, fill = imd)) +
geom_bar(stat= "identity", position=position_stack()) +
facet_grid(cols = vars(agegrp10_simple)) +
ylab("proportion") +
theme_few() +
theme(axis.text.x = element_text(size = 6, angle = 90, hjust = 1))
ggplot(combi_mm[agegrp10_simple == "18-29" &
imd != "", .N , keyby = c("year", "imd")],
aes(x = year, y = N, fill = imd)) +
geom_bar(stat = "identity", position = position_stack()) +
ylab("proportion") +
theme_few() +
theme(axis.text.x = element_text(
size = 6,
angle = 90,
hjust = 1
))
```
#Looking at nmbers by imd
```{r}
combi_mm[, table(year, imd)]
combi_mm[, as.list(round(table(imd)/.N*100, 2)), keyby = .(year)]
combi_mm[year %in% c(2004, 2019),
table(year, imd)]
combi_mm[year %in% c(2004, 2019),
as.list(round(table(imd)/.N*100, 2)), keyby = .(year)]
```
#Looking at nmbers by region
```{r}
combi_mm[, table(year, region)]
combi_mm[, as.list(round(table(region)/.N*100, 2)), keyby = .(year)]
combi_mm[year %in% c(2004, 2019),
table(year, region)]
combi_mm[year %in% c(2004, 2019),
as.list(round(table(region)/.N*100, 2)), keyby = .(year)]
```
#Looking at bmm & cmm totals by year
```{r}
combi_mm[, .(N=sum(bmm != 0), pc = sum(bmm != 0)/.N), keyby = year]
combi_mm[, .(N=sum(cmm != 0), pc = sum(cmm != 0)/.N), keyby = year]
```
# looking at time in study
```{r}
study_pats <- read_fst(data_dir_CPRD("study_pats.fst"), as.data.table = TRUE)
study_pats[, enterstudy := as.IDate(ifelse(
reg1yr < 2004-01-01, #setting start date to 1Jan2004 as earliest
as.IDate((paste0("01012004")), format = "%d%m%Y"),
reg1yr ), format = "Y%/m%/d%")]
study_pats[, enterstudy := as.IDate(ifelse(
reg1yr < 2004-01-01, #setting start date to 1Jan2004 as earliest
as.IDate((paste0("01012004")), format = "%d%m%Y"),
reg1yr ), format = "Y%/m%/d%")]
study_pats[censordate - enterstudy >=0,
summary((censordate - enterstudy)/365.24)]
```
# Looking at start vs end status - need to add back in censordate
```{r}
setkey(combi_mm, patid, year)
options(digits = 3)
first_year <- combi_mm[combi_mm[, .I[1], keyby = c("patid")]$V1]
combi_mm[study_pats, on = 'patid', `:=`
(censordate = i.censordate, censorreason = i.censorreason)]
last_year <-
combi_mm[year(censordate) == year | #want the final year of reg
(year == "2019" & year(censordate) == 2020),]
first_year[last_year, on = 'patid', `:=` (end_bmm = i.bmm, end_cmm = i.cmm)]
first_year[, .(
h_h = sum(end_bmm == 0) / sum(bmm != 2) * 100,
#Start healthy (includes incidentbmm in 1st year) stay healthy
h_bmm = sum(end_bmm != 0 &
bmm != 2 & end_cmm == 0) / sum(bmm != 2) * 100,
#healthy --> bmm
h_cmm = sum(end_cmm != 0 &
bmm != 2) / sum(bmm != 2) * 100,
#healthy --> cmm
bmm_cmm = sum(end_cmm != 0 &
bmm == 2 & cmm == 0) / sum(bmm == 2) * 100
)] #bmm --> cmm
#Doing in 50+ only for comparison with Strauss paper
combi_mm_50plus <- combi_mm[age>49,]
first_year_50plus <- combi_mm_50plus[combi_mm_50plus[, .I[1],
keyby = c("patid")]$V1]
first_year_50plus[last_year, on = 'patid', `:=`
(end_bmm = i.bmm, end_cmm = i.cmm)]
first_year_50plus[, #Start healthy (incl incidentbmm in 1st year) stay healthy:
.(h_h = sum(end_bmm == 0)/sum(bmm != 2)*100,
h_bmm = sum(end_bmm != 0 & bmm != 2 & end_cmm == 0)/
sum(bmm != 2)*100, #healthy --> bmm
h_cmm = sum(end_cmm != 0 & bmm != 2)/
sum(bmm != 2)*100, #healthy --> cmm
bmm_cmm = sum(end_cmm != 0 & bmm == 2 & cmm == 0 )/
sum(bmm == 2)*100)] #bmm --> cmm
```
# Mean/sd/median/iqr # of conditions in those with basic/complex multimorbidity
```{r}
options(digits = 2)
combi_mm[bmm != 0, .(mean = mean(n_cond),
sd = sd(n_cond),
lower = quantile(n_cond, .25, na.rm=TRUE),
middle = quantile(n_cond, .50, na.rm=TRUE),
upper = quantile(n_cond, .75, na.rm=TRUE)), keyby = year]
combi_mm[cmm != 0, .(mean = mean(n_cond),
sd = sd(n_cond),
lower = quantile(n_cond, .25, na.rm=TRUE),
middle = quantile(n_cond, .50, na.rm=TRUE),
upper = quantile(n_cond, .75, na.rm=TRUE)), keyby = year]
```
# Overall event numbers for indeterminate gender
```{r}
#Prevalence
first_year[, sum(end_bmm != 0), by = gender] #bmm
first_year[, sum(end_cmm != 0), by = gender] #cmm
#Incidence
first_year[, sum(end_bmm != 0 & cmm !=2), by = gender] #bmm
first_year[, sum(end_cmm != 0 & cmm !=2), by = gender] #cmm
#CF
first_year[,sum(end_bmm != 0 & censorreason ==1), by = gender] #bmm
first_year[,sum(end_cmm != 0 & censorreason ==1), by = gender] #cmm
```
# looking at censordates
```{r}
patient <- read_fst(data_dir_CPRD("patient.fst"),
columns = c("patid", "pracid", "dob", "reg1yr"),
as.data.table = TRUE)
practice <- read_fst(data_dir_CPRD("practice.fst"),
columns = c("pracid", "lcd"),
as.data.table = TRUE)
last_year[patient, on = 'patid', `:=`
(dob = i.dob, pracid = i.pracid, reg1yr = i.reg1yr)]
last_year[practice, on = 'pracid', lcd := i.lcd]
last_year[, enterstudy := reg1yr]
last_year[reg1yr < "2004-01-01", enterstudy := as.IDate("2004-01-01")]
last_year[enterstudy - dob < 6574, enterstudy := dob + 6574]
last_year[, daysinstudy := censordate - enterstudy]
#Censored for reason other than death #458,635
summary(last_year[censorreason == 0 & censordate < lcd &
censordate < "2019-12-31" ])
#stayed until the end of the study/lcd of practice #462,413
summary(last_year[censorreason == 0 &
(censordate == lcd |censordate >= "2019-12-31" ) ] )
#died #70,195
summary(last_year[censorreason==1])
```
People who are censored (didnt die) are younger and have fewer conditions, more likely to have IMD as NA or be more deprived and more likely to be female.