forked from annalhead/CPRD_multimorbidity_trends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RelativeRisks.R
431 lines (362 loc) · 13.8 KB
/
RelativeRisks.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#Relative risks
library(data.table)
library(fst)
library(glm2) # better algo to fit glm, same syntax
library(sandwich) # robust CI
library(MASS)
library(gamlss)
# In the data, the relevant variables are:
# - bmm/cmm: 0 = no, 1 = incident, 2 = prevalent (in that year)
# - totaldays: total days registered in the year
# - bmm_days/cmm_days: days in the year with bmm/cmm
data_dir_CPRD <-
function(x = character(0))
paste0("/mnt/", Sys.info()[["user"]],
"/UoL/CPRD2019mm/Data May 2020/", x)
data_dir_lookup <-
function(x = character(0))
paste0("/mnt/", Sys.info()[["user"]],
"/UoL/CPRD2019mm/Dictionaries etc/", x)
mydt <- read_fst(data_dir_CPRD("combi_mm_detailed.fst"), as.data.table = TRUE)
names(mydt)
mydt <- mydt[imd != "" & gender != "Indeterminate" & region != ""]
# get robust CI using sandwich (just testing, do not use for publication)
# Note that while family = quasibinomial leads to a robust estimate of the
#variance (αp^(1−p^)), it is not the same robust estimator as the sandwich
#estimator (in which for each observation, the variance is estimated as
#(yi−y^i)2 rather than by some assumed functional form)
robustCI <- function(model, type = "HC0", digits = 3) {
cov_m1 <- sandwich::vcovHC(model, type = type)
std_err <- sqrt(diag(cov_m1))
q_val <- qnorm(0.975)
hlp <- coef(model)/std_err
r_est <- cbind(
Estimate = coef(model)
, "Robust SE" = std_err
, z = (hlp)
, "Pr(>|z|) " = 2 * pnorm(abs(hlp), lower.tail = FALSE)
, LL = coef(model) - q_val * std_err
, UL = coef(model) + q_val * std_err
, RR = exp(coef(model))
, LRR = exp(coef(model) - q_val * std_err)
, URR = exp(coef(model) + q_val * std_err)
)
round(r_est, digits = digits)
}
# Incident bmm ----
tt <- mydt[bmm != "2" & imd != "" & totaldays > 0]
tt[, bmm := as.integer(as.character(bmm))]
tt[, hist(bmm/totaldays)]
tt[, mean(bmm/totaldays), keyby = .(year, agegrp10_simple)]
tt[, mean(bmm/totaldays), keyby = year][, plot(year, V1)]
tt[, mean(bmm/totaldays), keyby = age][, plot(age, V1)]
start_p <- sum(tt$bmm)/sum(tt$totaldays)
bmm_inc <-
glm2(
cbind(bmm, totaldays - bmm_days) ~ year + gender + region +
imd + agegrp10_simple,
family = binomial(link = "log"),
data = tt,
start = c(log(start_p), rep(0, 22)), control = glm.control(trace = TRUE)
)
summary(bmm_inc)
bmm_inc_rr_Wald <- format(round(
cbind(exp(coef(bmm_inc)), exp(confint.default(bmm_inc))), 3),
scientific = FALSE) # Wald confidence interval:
#https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
bmm_inc_rr_robust <- robustCI(bmm_inc)
bmm_inc_rr_robust # use this one LRR & URR are lower and upper 95% CI for RR
stt <- na.omit(tt[, .(bmm = sum(bmm),
totaldays = sum(totaldays - bmm_days + bmm),
bmm_days = sum(bmm_days)),
by = .(year, gender, region, imd, agegrp10_simple)])
stt[, hist(bmm)]
bmm_inc2 <-
glm2(
cbind(bmm, totaldays - bmm) ~
year + gender + region + imd + agegrp10_simple,
family = binomial(link = "log"),
data = stt,
start = c(log(start_p), rep(0, 22)),
control = glm.control(trace = TRUE)
)
View(cbind(bmm_inc_rr_robust[, 7:9],
robustCI(bmm_inc2)[, 7:9],
round(cbind(exp(coef(bmm_inc2)),
exp(confint(bmm_inc2))), 3)))
# profile confidence intervals are very similar to robust ones
# Given the later method is much faster + as accurate will use it for all models
bmm_inc_rr2 <- round(cbind(exp(coef(bmm_inc2)), exp(confint(bmm_inc2))), 3)
bmm_inc3 <-
glm.nb(
bmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
data = stt
)
bmm_inc_rr3 <- round(cbind(exp(coef(bmm_inc3)), exp(confint(bmm_inc3))), 3)
View(cbind(bmm_inc_rr2, bmm_inc_rr3, robustCI(bmm_inc3)[, 7:9]))
bmm_inc4 <-
glm2(
bmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
family = poisson(link = "log"),
data = stt
)
bmm_inc_rr4 <- round(cbind(exp(coef(bmm_inc4)), exp(confint(bmm_inc4))), 3)
View(cbind(bmm_inc_rr2, bmm_inc_rr4, robustCI(bmm_inc4)[, 7:9]))
bmm_inc5 <-
glm2(
bmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
family = quasipoisson(link = "log"),
data = stt
)
bmm_inc_rr5 <- round(cbind(exp(coef(bmm_inc5)), exp(confint(bmm_inc5))), 3)
View(cbind(bmm_inc_rr2, bmm_inc_rr5, robustCI(bmm_inc5)[, 7:9]))
#Comparing the nb & the qp
View(cbind(bmm_inc_rr3, bmm_inc_rr5, robustCI(bmm_inc5)[, 7:9]))
plot(bmm_inc_rr3)
plot(bmm_inc_rr5)
#Comparing the nb & the qp
AER::dispersiontest(bmm_inc4, trafo = 1) # qp a > 0, p < 0.05 hence
#overdispersion
AER::dispersiontest(bmm_inc4, trafo = 2) # nb a > 0, p < 0.05 hence
#overdispersion but (less)
# Hence, an important diagnostic is to plot (Yi - μi)^2 against μi.
# Function for variance-to-mean plot
# The graph plots the mean versus the variance and overlays the curves
# corresponding to the over-dispersed quasi-Poisson model, where the
#variance is Ïμ,
# and the negative binomial model, where the variance is μ(1+μÏ2).
var2mean <- function(nb_model, qp_model, subtitle) {
xb <- log(nb_model$fitted.values) # same as predict(nb_model).
#At linear predictor scale
g <- cut(xb, breaks = quantile(xb, seq(0, 100, 5) / 100))
m <- tapply(nb_model$y, g, mean)
v <- tapply(nb_model$y, g, var)
size <- tapply(nb_model$y, g, length)
range01 <- function(x, ...){(x - min(x, ...)) / (max(x, ...) - min(x, ...))}
mat <- matrix(1:2, 2)
layout(mat, widths = c(3.5, 3.5), heights = c(8,6))
plot(m,
v,
cex = range01(size),
xlab = "Mean",
ylab = "Variance",
main = "Mean-Variance Relationship")
mtext(subtitle, padj = -0.5)
x <- seq(min(m), max(m), 0.02)
phi <- summary(qp_model)$dispersion
lines(x, x * phi, lty = "dashed")
lines(x, x * (1 + x / nb_model$theta))
legend(
"topleft",
lty = c("dashed", "solid"),
legend = c("Q. Poisson", "Neg. Binom."),
inset = 0.05
)
# weights plot
w_nb <- tapply(nb_model$weights, g, mean)
w_qp <- tapply(qp_model$weights, g, mean)
plot(m, w_nb, type = "l",
ylim = c(0, max(c(w_nb, w_qp))),
xlab = "Mean",
ylab = "weights",
main = "Estimated weights as a function of the mean"
)
lines(m, w_qp, lty = "dashed")
}
var2mean(bmm_inc3, bmm_inc5, "BMM incidence")
# Prevalent bmm ----
tt <- mydt[imd != "" & totaldays > 0]
tt[, bmm := as.integer(as.character(bmm))]
tt[bmm == 0L, bmm2 := 0L]
tt[bmm == 1L, bmm2 := bmm_days]
tt[bmm == 2L, bmm2 := totaldays]
tt[, hist(bmm2/totaldays)]
tt[, mean(bmm2/totaldays), keyby = .(year, agegrp10_simple)]
tt[, mean(bmm2/totaldays), keyby = year][, plot(year, V1)]
tt[, mean(bmm2/totaldays), keyby = age][, plot(age, V1)]
stt <- tt[, .(bmm = sum(bmm2), totaldays = sum(totaldays),
bmm_days = sum(bmm_days)),
by = .(year, gender, region, imd, agegrp10_simple)]
stt[, bmm_mean := mean(bmm, na.rm = T),
keyby = .(year, gender, region, imd, agegrp10_simple)]
stt[, bmm_var := var(bmm, na.rm = T, use = "pairwise.complete.obs"),
keyby = .(year, gender, region, imd, agegrp10_simple)]
stt[, var(bmm), keyby = .(year, gender, imd, agegrp10_simple)]
tmptab <- stt[, .(bmm_mean = mean(bmm), bmm_var = var(bmm)),
keyby = .(year, gender, imd, agegrp10_simple)]
bmm_prv <-
glm2(
bmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
family = quasipoisson(link = "log"),
data = stt
)
bmm_prv_rr <- round(cbind(exp(coef(bmm_prv)), exp(confint(bmm_prv))), 3)
View(cbind(bmm_prv_rr, robustCI(bmm_prv)[, 7:9]))
dispersiontest(bmm_prvpois, trafo = 1)
bmm_prvpois <-
glm2(
bmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
family = poisson(link = "log"),
data = stt
)
#comparing with binomial
bmm_prv_nb <-
glm.nb(
bmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
data = stt
)
bmm_prvnb_rr <- round(cbind(exp(coef(bmm_prv_nb)),
exp(confint(bmm_prv_nb))), 3)
View(cbind(bmm_prv_rr, bmm_prvnb_rr, robustCI(bmm_prv_nb)[, 7:9]))
plot(bmm_prv)
plot(bmm_prv_nb)
var2mean(bmm_prv_nb, bmm_prv, "BMM prevalence")
# Case fatality bmm --------------------
# assuming censorreason = 1 means death
tt <- mydt[imd != "" & totaldays > 0 & bmm != "0"]
tt[, bmm := as.integer(as.character(bmm))]
tt[, censorreason := as.integer(as.character(censorreason))]
stt <- na.omit(tt[, .(censorreason = sum(censorreason),
totaldays = sum(totaldays),
bmm_days = sum(bmm_days)),
by = .(year, gender, region, imd, agegrp10_simple)])
stt[, hist(censorreason)]
bmm_cf <-
glm2(
censorreason ~ offset(log(bmm_days)) +
year + gender + region + imd + agegrp10_simple,
family = quasipoisson(link = "log"),
data = stt
)
bmm_cf_rr <- round(cbind(exp(coef(bmm_cf)), exp(confint(bmm_cf))), 3)
View(cbind(bmm_cf_rr, robustCI(bmm_cf)[, 7:9]))
d <- fitDist(stt$censorreason, type = "counts",
try.gamlss = TRUE, trace = TRUE)
d
bmm_cf2 <-
gamlss(
censorreason ~ offset(log(bmm_days)) +
year + gender + region + imd + agegrp10_simple,
family = ZANBI(),
data = stt
)
bmm_cf_rr2 <- round(cbind(exp(coef(bmm_cf2)), exp(confint(bmm_cf2))), 3)
bmm_cf_rr2
#comparing with binomial
bmm_cf_nb <-
glm.nb(
censorreason ~ offset(log(bmm_days)) +
year + gender + region + imd + agegrp10_simple,
data = stt
)
bmm_cfnb_rr <- round(cbind(exp(coef(bmm_cf_nb)), exp(confint(bmm_cf_nb))), 3)
View(cbind(bmm_cf_rr, bmm_cfnb_rr, robustCI(bmm_cf_nb)[, 7:9]))
plot(bmm_cf)
plot(bmm_cf_nb)
AIC(bmm_cf_nb, bmm_cf2)
var2mean(bmm_cf_nb, bmm_cf, "BMM case-fatality")
# Incident cmm ----------------------------
tt <- mydt[cmm != "2" & imd != "" & totaldays > 0]
tt[, cmm := as.integer(as.character(cmm))]
original <- copy(tt)
tt[, hist(cmm/totaldays)]
tt[, mean(cmm/totaldays), keyby = .(year, agegrp10_simple)][, summary(V1)]
tt[, mean(cmm/totaldays), keyby = year][, plot(year, V1)]
tt[, mean(cmm/totaldays), keyby = age][, plot(age, V1)]
stt <- tt[, .(cmm = sum(cmm), totaldays = sum(totaldays - cmm_days + cmm),
cmm_days = sum(cmm_days)),
by = .(year, gender, region, imd, agegrp10_simple)]
cmm_inc <-
glm2(
cmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
family = quasipoisson(link = "log"),
data = stt
)
cmm_inc_rr <- round(cbind(exp(coef(cmm_inc)), exp(confint(cmm_inc))), 3)
View(cbind(cmm_inc_rr, robustCI(cmm_inc)[, 7:9]))
#comparing with nb
cmm_inc_nb <-
glm.nb(
cmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
data = stt
)
cmm_inc_nb_rr <- round(cbind(exp(coef(cmm_inc_nb)),
exp(confint(cmm_inc_nb))), 3)
View(cbind(cmm_inc_rr, cmm_inc_nb_rr, robustCI(cmm_inc_nb)[, 7:9]))
plot(cmm_inc)
plot(cmm_inc_nb)
var2mean(cmm_inc_nb, cmm_inc, "CMM incidence")
# Prevalent cmm ----------------------------
tt <- mydt[imd != "" & totaldays > 0]
tt[, cmm := as.integer(as.character(cmm))]
tt[cmm == 0L, cmm2 := 0L]
tt[cmm == 1L, cmm2 := cmm_days]
tt[cmm == 2L, cmm2 := totaldays]
tt[, hist(cmm2/totaldays)]
tt[, mean(cmm2/totaldays), keyby = .(year, agegrp10_simple)]
tt[, mean(cmm2/totaldays), keyby = year][, plot(year, V1)]
tt[, mean(cmm2/totaldays), keyby = age][, plot(age, V1)]
stt <- tt[, .(cmm = sum(cmm2), totaldays = sum(totaldays),
cmm_days = sum(cmm_days)),
by = .(year, gender, region, imd, agegrp10_simple)]
cmm_prv <-
glm2(
cmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
family = quasipoisson(link = "log"),
data = stt
)
cmm_prv_rr <- round(cbind(exp(coef(cmm_prv)), exp(confint(cmm_prv))), 3)
View(cbind(cmm_prv_rr, robustCI(cmm_prv)[, 7:9]))
#comparing with nb
cmm_prv_nb <-
glm.nb(
cmm ~ offset(log(totaldays)) +
year + gender + region + imd + agegrp10_simple,
data = stt
)
cmm_prv_nb_rr <- round(cbind(exp(coef(cmm_prv_nb)),
exp(confint(cmm_prv_nb))), 3)
View(cbind(cmm_prv_rr, cmm_prv_nb_rr, robustCI(cmm_prv_nb)[, 7:9]))
plot(cmm_prv)
plot(cmm_prv_nb)
var2mean(cmm_prv_nb, cmm_prv, "CMM prevalence")
# Case fatality cmm --------------------
# assuming censorreason = 1 means death
tt <- mydt[imd != "" & totaldays > 0 & cmm != "0"]
tt[, cmm := as.integer(as.character(cmm))]
tt[, censorreason := as.integer(as.character(censorreason))]
stt <- na.omit(tt[, .(censorreason = sum(censorreason),
totaldays = sum(totaldays),
cmm_days = sum(cmm_days)),
by = .(year, gender, region, imd, agegrp10_simple)])
stt[, hist(censorreason)]
cmm_cf <-
glm2(
censorreason ~ offset(log(cmm_days)) +
year + gender + region + imd + agegrp10_simple,
family = quasipoisson(link = "log"),
data = stt
)
cmm_cf_rr <- round(cbind(exp(coef(cmm_cf)), exp(confint(cmm_cf))), 3)
View(cbind(cmm_cf_rr, robustCI(cmm_cf)[, 7:9]))
#comparing with binomial
cmm_cf_nb <-
glm.nb(
censorreason ~ offset(log(cmm_days)) +
year + gender + region + imd + agegrp10_simple,
data = stt
)
cmm_cfnb_rr <- round(cbind(exp(coef(cmm_cf_nb)), exp(confint(cmm_cf_nb))), 3)
View(cbind(cmm_cf_rr, cmm_cfnb_rr, robustCI(cmm_cf_nb)[, 7:9]))
plot(cmm_cf)
plot(cmm_cf_nb)
var2mean(cmm_cf_nb, cmm_cf, "CMM case-fatality")