-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcumulative_incidence.R
411 lines (365 loc) · 12 KB
/
cumulative_incidence.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
# cumulative_incidence.R
# Parametric estimation of cumulative incidence
# June 15, 2020
# Kevin Chen
# Plan ####
# 1. Use pooled logistic regression to crudely estimate the J = 2 subdistribution hazard functions (j = 1 for cancer incidence and j = 2 for natural cause mortality), given covariates W and exposure X
# h_j (t | W, X) = lim_{δ -> 0} 1/δ * Pr{ t < T < t + δ, J = j | T > T or (T < t and J ≠ j), W, X }
# ≈ 1/1 * Pr{ t < T < t + 1, J = j | T > T or (T < t and J ≠ j), W, X }
# 2. For each person-year, get p_j by evaluating the jth model at (X = x, W = w) where x is the counter-factual exposure level of interest, and w is the observed covariate combination
# 3. For each person-year, take the the product p_1 * p_2
# (Would this to be something like a model-based estimate of S(t)h(t) where h(t) is the hazard of cancer incidence weighted by that for natural cause mortality, marginal over covariates for a particular exposure level x? Since folks do not contribute person time after experiencing the event, can we take S(t) = 1 for at-risk time and S(t) = 0 for not at-risk time?)
# 4. Take the sum of products
# (Would this be a rough estimate of some weighted cumulative incidence ∫S(t)h(t)dt ?)
# 5. Take the ratio of (1) the cumulative incidence estimate under an exposure level of interest to (2) that for the reference level of exposure.
if (!grepl("gm", getwd(), ignore.case = T)) {
if ("here" %in% .packages()) {
detach("package:here", unload = T)
}
setwd('eisen/gm')
library(here)
}
# Table 1 helper function
source(here::here("reports/table1.R"))
# Get analytic data ####
if (!("exposure.lag" %in% ls())) {
exposure.lag <- 20
end.year <- 2015
# rm(cohort_analytic)
}
# Get cohort analytic ####
if (
!('cohort_analytic' %in% ls())
) {
outcome.type <- 'incidence'
source(here::here('../gm-wrangling/', '05-Get-Exposure-Outcome.R'))
cohort_analytic <- get.cohort_analytic(
outcome_type = outcome.type,
exposure.lag = exposure.lag,
deathage.max = NULL,
end.year = end.year,
hire.year.min = 1938,
use_seer = T
)
setorder(cohort_analytic, studyno, year)
cohort_analytic[, `:=`(yin.gm = date.to.gm(yin))]
# # Keep only people who appear in the exposure data
# cohort_analytic <-
# cohort_analytic[studyno %in% unique(exposure$studyno)]
# PICK YOUT ####
cohort_analytic[, jobloss.date := employment_end.date]
# Exposure after leaving work is 0
cohort_analytic[year > (year(jobloss.date) + exposure.lag), `:=`(
straight = 0,
soluble = 0,
synthetic = 0)]
# NA fill
cohort_analytic[year <= (year(jobloss.date) + exposure.lag), `:=`(
straight = zoo::na.locf(straight),
soluble = zoo::na.locf(soluble),
synthetic = zoo::na.locf(synthetic)
), by = .(studyno)]
cohort_analytic[, `:=`(
cum_straight = cumsum(straight),
cum_soluble = cumsum(soluble),
cum_synthetic = cumsum(synthetic)
), by = .(studyno)]
# Which columns ####
col.names <- c(
"studyno",
"age.year1",
"age.year2",
"year1",
"year2",
grep("canc\\_", names(cohort_analytic), value = T),
"straight",
"cum_straight",
"soluble",
"cum_soluble",
"synthetic",
"cum_synthetic",
"year",
"yin.gm",
"yin",
"yrin",
"yrin16",
"race",
"finrace",
"plant",
grep("ddiag", names(cohort_analytic), value = T),
"yod",
"yoc",
"yob",
"sex",
"dateout.date",
"employment_end.date",
"employment_end.date.legacy",
"yout",
"yout_recode",
"jobloss.date",
"All causes",
"Chronic obstructive pulmonary disease",
"All external causes",
"nohist", "wh", "immortal", "right.censored",
"possdiscr_new", "flag77", "oddend", "status15")
# Drop unnecessary data ####
cohort_analytic <- cohort_analytic[
wh == 1 & nohist == 0 & immortal == 0 & right.censored == 0,
col.names, with = F]
}
# Get no seer data ####
if (
!('cohort_noseer' %in% ls())
) {
cohort_noseer <- get.cohort_analytic(
outcome_type = outcome.type,
exposure.lag = exposure.lag,
deathage.max = NULL,
end.year = end.year,
hire.year.min = 1938,
use_seer = F
)
setorder(cohort_noseer, studyno, year)
cohort_noseer[, `:=`(yin.gm = date.to.gm(yin))]
# # Keep only people who appear in the exposure data
# cohort_noseer <-
# cohort_noseer[studyno %in% unique(exposure$studyno)]
# PICK YOUT ####
cohort_noseer[, jobloss.date := employment_end.date]
# Exposure after leaving work is 0
cohort_noseer[year > (year(jobloss.date) + exposure.lag), `:=`(
straight = 0,
soluble = 0,
synthetic = 0)]
# NA fill
cohort_noseer[year <= (year(jobloss.date) + exposure.lag), `:=`(
straight = zoo::na.locf(straight),
soluble = zoo::na.locf(soluble),
synthetic = zoo::na.locf(synthetic)
), by = .(studyno)]
cohort_noseer[, `:=`(
cum_straight = cumsum(straight),
cum_soluble = cumsum(soluble),
cum_synthetic = cumsum(synthetic)
), by = .(studyno)]
# Drop unnecessary data ####
cohort_noseer <- cohort_noseer[
wh == 1 & nohist == 0 & immortal == 0 & right.censored == 0,
col.names, with = F]
}
# Restrict cohort to those still alive at the start of 1973
cohort_cancer <- cohort_analytic[
plant != 3 &
sex == "M" & wh == 1 & nohist == 0 &
possdiscr_new != 3 &
flag77 == 0 & oddend == 0 &
(is.na(yod) | yod >= as.Date("1973-01-01")) &
(is.na(ddiag_first) | ddiag_first >= as.Date("1973-01-01")) &
yin < as.Date("1986-01-01") &
immortal == 0,]
# Get incidence key and other helpers
cohort2 <- as.data.frame(matrix(ncol = length(col.names)))
source(here::here("cancer incidence", "incidence.R"))
# Sort
setorder(cohort_cancer, studyno, year)
cohort_cancer[plant != 3,`:=`(
I = 1:.N,
N = .N
), by = .(studyno)]
# Check Ns
length(table(cohort_cancer$studyno))
nrow(cohort_cancer[year(ddiag_first) >= 1973 & canc_first == 1, ])
nrow(cohort_cancer[canc_first == 1])
nrow(cohort_cancer[canc_first == 1 & (ddiag_first <= ddiag_first | is.na(ddiag_first))])
# Get reshaped analytic dataset for cancer ####
if (!"first" %in% incidence.key$code) {
incidence.key <- rbindlist(list(
incidence.key,
data.table(
code = "first",
description = "All cancers",
var.name = "canc_first",
date.name = "ddiag_first")))
}
get.coxph(
cohort_name = "cohort_cancer",
outcomes = which(incidence.key$code == "first"),
start.year = 1973,
time_scale = "year", run_model = F)
cancer.dat <- as.data.frame(first.dat)
setDT(cancer.dat)
rm(first.dat)
# Make yoi with class numeric
cancer.dat[!is.na(yoi), `:=`(
yoi.gm = date.to.gm(yoi))]
# Fewer age categories
cancer.dat <- cancer.dat[Sex == "M"]
cancer.dat[,`:=`(Age = cut(age.year2/365.25,
c(-Inf, 60, 70, 80, Inf) - 1))]
# Check levels
cancer.dat$Race %>% levels
# Pretty Age cagetories
cancer.dat[, `:=`(Age = factor(
Age, levels = levels(Age), labels = {
# Get upper and lower bounds (delimited by comma)
lower <- as.numeric(substr(levels(Age), 2, unlist(gregexpr(",", levels(Age))) - 1))
upper <- as.numeric(substr(levels(Age), unlist(gregexpr(",", levels(Age))) + 1,
nchar(levels(Age)) - 1))
# Make new levels by pasting bounds
new.levels <- paste(lower + 1, "to", upper)
# Correct endpoints
new.levels[!is.finite(upper)] <- paste(lower[!is.finite(upper)] + 1, "and older")
new.levels[!is.finite(lower)] <- paste(upper[!is.finite(lower)], "and younger")
new.levels
}))]
cancer.dat$Age %>% table
# Overall incidence
overall.tab <- cancer.dat[Plant != 3, .(
N = length(table(studyno)),
py = sum(year2 - year1),
cases = sum(status),
rate = sum(status)/(sum(year2 - year1)/365.25),
risk = sum(status)/length(table(studyno))
), by = .(year)]
# Get CIs
overall.tab[,`:=`(
rate.upper = exp(log(rate) + qnorm(0.975) * sqrt(rate + (1 - rate)/py)),
rate.lower = exp(log(rate) - qnorm(0.975) * sqrt(rate + (1 - rate)/py)),
risk.upper = risk + qnorm(0.975) * sqrt(risk + (1 - risk)/N),
risk.lower = risk - qnorm(0.975) * sqrt(risk + (1 - risk)/N)
)]
# Age-specific stuff
age_specific.tab <- cancer.dat[Plant != 3, .(
N = length(table(studyno)),
py = sum(year2 - year1),
cases = sum(status),
rate = sum(status)/(sum(year2 - year1)/365.25),
risk = sum(status)/length(table(studyno))
), by = .(year, Age)]
# Get CIs
age_specific.tab[,`:=`(
rate.upper = exp(log(rate) + qnorm(0.975) * sqrt(rate + (1 - rate)/py)),
rate.lower = exp(log(rate) - qnorm(0.975) * sqrt(rate + (1 - rate)/py)),
risk.upper = risk + qnorm(0.975) * sqrt(risk + (1 - risk)/N),
risk.lower = risk - qnorm(0.975) * sqrt(risk + (1 - risk)/N)
)]
# Make plots ####
overall.ggplot <- ggplot() +
geom_line(data = overall.tab, aes(
x = year,
y = rate * 100000), size = 0.5) +
labs(x = "Calendar year",
y = "Incidence (per 100,000 p$\\cdot$years)") +
geom_rug(data = cancer.dat[status == 1], aes(
x = yoi.gm
), size = 0.01) +
mytheme
by_age.ggplot <- ggplot() +
geom_line(data = age_specific.tab, aes(
x = year,
y = rate * 100000,
color = Age), size = 0.5) +
# geom_smooth(se = T, span = 0.6, size = 0.2, alpha = 0.2) +
# facet_wrap(. ~ Age, ncol = 2) +
labs(x = "Calendar year",
y = "Incidence (per 100,000 p$\\cdot$years)") +
geom_rug(data = cancer.dat[status == 1], aes(
x = yoi.gm
), size = 0.01) +
mytheme + theme(
legend.position = "bottom",
legend.margin = margin()
) + guides(color = guide_legend(nrow = 2))
# # Render
# tikz(here::here(paste0("/Users/kevinchen/eisen/GM/left truncation/resources", "FU from ", min(cancer.dat$year)), "all-cancers_all-ages.tex"),
# standAlone = T, height = 2, width = 3)
print(overall.ggplot)
# dev.off()
#
# tikz(here::here(paste0("/Users/kevinchen/eisen/GM/left truncation/resources", "FU from ", min(cancer.dat$year)), "all-cancers_by-age.tex"),
# standAlone = T, height = 2.5, width = 3)
print(by_age.ggplot)
# dev.off()
#
# # Compile
# lualatex(directory = here::here(paste0("/Users/kevinchen/eisen/GM/left truncation/resources", "FU from ", min(cancer.dat$year))))
# Add rows after death ####
# Make index
setorder(cancer.dat, studyno, year)
cancer.dat$I <- NA; cancer.dat$N <- NA
cancer.dat[is.na(yoi), `:=`(
I = 1:.N,
N = .N
), by = .(studyno)]
# Make rows to add, carrying forward last covariate combination
to_add <- merge(
# Rows to add
cancer.dat[I == N, .(
year,
studyno)][year != end.year, .(
year = (year + 1):2015
), by = .(studyno)],
# Covariates in last row
cancer.dat[I == N, -"year"],
on = "studyno",
all.x = T)
# Add rows
cancer.dat <- rbindlist(list(
cancer.dat,
to_add
), use.names = T, idcol = "subdistribution")
setorder(cancer.dat, studyno, year)
cancer.dat[, subdistribution := (0:1)[as.numeric(factor(subdistribution, levels = 1:2))]]
# Check levels
cancer.dat[,`:=`(
Race = relevel(factor(Race), "White"),
Plant = relevel(factor(Plant), "1"),
Age = cut(age.year2/365,
unique(c(min(age.year2/365),
quantile(cancer.dat[status == 1, age.year2/365],
c(0.25, 0.5, 0.75)),
max(age.year2/365))),
include.lowest = T)
)]
# # Descriptive table
# source(here::here("reports", "table1.R"))
# popchar1.tab1 <- get.tab1(
# df = cancer.dat[subdistribution == 0], exposure_lag = 20, incidence = T)
# saveRDS(popchar1.tab1,
# file = here::here(
# 'left truncation/resources/cif', "popchar1.tab1.rds"))
#
# Run PLR ####
library(mgcv)
names(cancer.dat) <- gsub(" ", "_", names(cancer.dat))
# tmp.plr <- mgcv::gam(status ~
# Cumulative_straight +
# Cumulative_soluble_5 +
# Cumulative_synthetic +
# + Plant + Race + Year_of_hire +
# Age +
# s(year, bs = "ps") +
# s(yin.gm, bs = "ps"),
# data = cancer.dat)
#
# # Save PLR
# saveRDS(tmp.plr,
# file = paste0(
# to_drive_D(here::here('left truncation/resources/')),
# "cancer.plr.rds"
# ))
readRDS(paste0(
to_drive_D(here::here('left truncation/resources/')),
"cancer.plr.rds"))
# Get coefficient table
get.coef(outcomes = which(incidence.key$code == "first"),
cohort_name = "cancer.dat",
analytic.name = "cancer.dat",
new_dat = F,
mod.directory = to_drive_D(here::here('left truncation/resources/')),
mod.name = "cancer.plr.rds",
directory = here::here('reports/left truncationweighted cumulative incidence/resources/'),
file.prefix = "cancer.plr")
# Get survival estimates ####
survival_by_year <- box_read(679752455177)
setDT(survival_by_year)