-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsevereC19_3_model_over60.R
More file actions
285 lines (218 loc) · 8.9 KB
/
severeC19_3_model_over60.R
File metadata and controls
285 lines (218 loc) · 8.9 KB
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
rm(list = ls())
invisible(gc(full = T))
library(data.table)
library(dplyr)
library(lubridate)
library(stringr)
library(ggplot2)
library(scales)
library(survival)
library(broom)
library(tidyr)
library(rlang)
library(fst)
library(future.apply)
setwd("N:/output/scripts/acorbetta/")
na_to_0 = function(x){ifelse(is.na(x), 0,x)}
tresh22 = ymd("2022-06-01")
df = read_fst("N:/output/data/COV-CVD/working_severeC19_static_QCed.fst")
setDT(df)
df = df[age_2019 + 2 >= 60]
#### create tv datasets ####
setDTthreads(percent = 100) # let data.table use all CPU threads
options(future.globals.maxSize = 12 * 1024^3) # 6 GiB
plan(multisession, workers = 2) # choose number of workers
# --- source data as data.table ---
# df is your original data.frame
# keep only columns we need (if some are absent, intersect handles it)
need <- c("COD_SOGGETTO","AGE_CLA","SEX","MCS_CLA","URBAN_DOM","CONT", 'PRESYMP',
paste0("OUTCOME", 1:4),
paste0("DATA_END", 1:4), paste0("DATE_END_", 1:4),
paste0("DATA_VACCINO", 1:4), "begin")
DT <- df[, intersect(names(df), need), with = FALSE]
rm(df)
gc(full=T)
# If you have a constant study start (e.g. tresh22), set it here once.
DT[, begin := tresh22] # uncomment if you use a constant start
# --- helper: map end columns (supports DATA_END* or DATE_END*) ---
map_end <- function(i) {
# prefer DATA_ENDi; else DATE_ENDi; else DATE_END_i (with underscore) if present
cands <- c(sprintf("DATA_END_%d", i), sprintf("DATE_END_%d", i), sprintf("DATE_END_%d", i))
cands[cands %in% names(DT)][1]
}
END_COLS <- vapply(1:4, map_end, character(1))
if (any(is.na(END_COLS))) stop("Couldn't find all 4 end-date columns.")
# --- vaccination backfill / monotone cleanup (vectorised) ---
# rule: if dose k exists, ensure dose k-1 exists; if missing or later than k, copy from k
for (k in 2:4) {
v <- sprintf("DATA_VACCINO%d", k)
vp <- sprintf("DATA_VACCINO%d", k - 1)
if (!v %in% names(DT)) next
if (!vp %in% names(DT)) DT[, (vp) := as.IDate(NA)]
DT[!is.na(get(v)) & (is.na(get(vp)) | get(vp) > get(v)), (vp) := get(v)]
}
# ensure non-decreasing sequence across doses
for (k in 2:4) {
v <- sprintf("DATA_VACCINO%d", k)
vp <- sprintf("DATA_VACCINO%d", k - 1)
if (all(c(v, vp) %in% names(DT)))
DT[!is.na(get(v)) & !is.na(get(vp)) & get(v) < get(vp), (v) := get(vp)]
}
expand_one_outcome <- function(z, out_col, end_col, dose_cols, baseline_cols) {
# z is a one-row data.table
start <- z$begin
end <- z[[end_col]]
stat <- z[[out_col]]
# dose dates in canonical order (DATA_VACCINO1..4)
all_doses <- if (length(dose_cols)) as.Date(unlist(z[, ..dose_cols])) else as.Date(character())
n_pre <- sum(!is.na(all_doses) & all_doses <= start)
onstudy_doses <- sort(all_doses[!is.na(all_doses) & all_doses > start & all_doses <= end])
changes <- sort(unique(c(start, onstudy_doses, end)))
if (length(changes) < 2L) return(NULL)
tstart_date <- head(changes, -1L)
tstop_date <- tail(changes, -1L)
n_on_by_tstart <- findInterval(tstart_date, onstudy_doses)
exposure <- pmax(4L - (n_pre + n_on_by_tstart), 0L)
ans <- data.table(
COD_SOGGETTO = z$COD_SOGGETTO,
tstart_date = tstart_date,
tstop_date = tstop_date,
tstart = as.numeric(tstart_date - start),
tstop = as.numeric(tstop_date - start),
exposure = as.integer(exposure),
status = as.integer(tstop_date == end & isTRUE(stat == 1)),
begin = start,
end = end
)
if (length(baseline_cols))
ans[, (baseline_cols) := z[, ..baseline_cols][rep(1L, .N)]]
ans
}
# one-time setup (do this once, not inside build_one)
setDTthreads() # let data.table use all threads
setkeyv(DT, "COD_SOGGETTO") # fast grouping
dose_cols <- intersect(names(DT), paste0("DATA_VACCINO", 1:4))
baseline_cols <- intersect(names(DT), c("AGE_CLA","SEX","MCS_CLA","URBAN_DOM","CONT","PRESYMP"))
# faster build_one
build_one <- function(i) {
out_col <- sprintf("OUTCOME%d", i)
end_col <- END_COLS[i]
sdcols <- c("COD_SOGGETTO", "begin", out_col, end_col, dose_cols, baseline_cols)
res <- DT[
, expand_one_outcome(.SD, out_col, end_col, dose_cols, baseline_cols),
by = COD_SOGGETTO,
.SDcols = sdcols
]
res[, outcome_no := i]
setkey(res, NULL)
res[, COD_SOGGETTO := NULL]
setcolorder(res, c("COD_SOGGETTO","outcome_no"))
# Fast write (optional) — roughly 10–20× faster than RDS for big tables:
write_fst(res, sprintf("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome%d.fst", i), compress = 50)
# If you must stick to .rds, at least turn off compression to save CPU:
#saveRDS(res, sprintf("N:/output/data/COV-CVD/tmp_tv_dataset_outcome%d.rds", i), compress = FALSE)
res
}
gc(full=T)
# Parallel build (four independent jobs)
future_lapply(3:4, build_one)
#### clean up ####
rm(DT)
gc(full=T)
#### RUN parallel model ####
library(future.apply)
options(future.globals.maxSize = 8 * 1024^3) # 7 GiB
plan(multisession, workers = 4) # choose number of workers
fit_tv_model_basic = function(df_adj){
tv_model <- coxph(Surv(time = tstart, time2 = tstop, event = status) ~
VACCINE_DEFICIT + SEX + AGE_CLA + CONT + MCS_CLA + URBAN_DOM,
data = df_adj)
return(tv_model)
}
files = list(c("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome1.fst"),
c("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome2.fst"),
c("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome3.fst"),
c("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome4.fst"))
models <- future_lapply(files,function(out){
df_adj = read_fst(out) %>% setDT()
if (grepl("3|4", out)) {
df_adj = df_adj[PRESYMP == 0]
}
df_adj[, `:=`(
VACCINE_DEFICIT = as.factor(exposure),
AGE_CLA = relevel(factor(AGE_CLA), ref = "[60,65)"))
]
mod = fit_tv_model_basic(df_adj)
return(mod)
})
names(models) = paste0("OUTCOME",1:4)
plan(sequential)
gc(full=T)
saveRDS(models,"N:/output/data/COV-CVD/OVER_60_tv_models_outcome.rds", compress = F)
#### parallel more adj ####
df = read_fst("N:/output/data/COV-CVD/working_severeC19_static_QCed.fst")
setDT(df)
df = df[age_2019 + 2 >= 60]
gc(full=T)
library(future.apply)
options(future.globals.maxSize = 8* 1024^3) # 7 GiB
plan(multisession, workers = 4) # choose number of workers
fit_tv_model = function(df_adj){
tv_model <- coxph(Surv(time = tstart, time2 = tstop, event = status) ~
VACCINE_DEFICIT + SEX + AGE_CLA + CONT + MCS_CLA + URBAN_DOM +
HOSP + CHOSP + N_PCR_POS + N_PCR + LAST_PCR + ATS,
data = df_adj)
return(tv_model)
}
files_adj = list(c("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome1.fst"),
c("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome2.fst"),
c("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome3.fst"),
c("N:/output/data/COV-CVD/OVER60_tv_dataset_outcome4.fst"))
add_covs = c("COD_SOGGETTO","HOSP", "CHOSP", "N_PCR_POS", "N_PCR", "LAST_PCR", "ATS")
df_cov = df[,..add_covs]
(miss = apply(df_cov, 2, function(x){sum(is.na(x))}))
# 19480 missing CONT
# 40923 missing domicile
tmp = na.omit(df_cov,invert = T)
nrow(tmp) #40923
df_cov = df_cov[!COD_SOGGETTO %in% tmp$COD_SOGGETTO]
rm(tmp,df)
gc(full=T)
models <- future_lapply(files_adj,function(out){
df_adj = read_fst(out) %>% setDT()
if (grepl("3|4", out)) {
df_adj = df_adj[PRESYMP == 0]
}
df_adj[, `:=`(
VACCINE_DEFICIT = as.factor(exposure),
AGE_CLA = relevel(factor(AGE_CLA), ref = "[60,65)"))
]
df_adj = merge(df_adj, df_cov, by = "COD_SOGGETTO")
mod = fit_tv_model(df_adj)
return(mod)
})
names(models) = paste0("OUTCOME",1:4)
plan(sequential)
gc(full=T)
saveRDS(models, file = "N:/output/data/COV-CVD/OVER60_tv_models_outcome_add.rds",
compress =F)
#### EXTRA ####
#fit_tv = coxph(Surv(time = tstart, time2 = tstop, event = OUTCOME2) ~
# VACCINE_DEFICIT + SEX + AGE_CLA + CONT + MCS_CLA + URBAN_DOM +
# HOSP + CHOSP + N_PCR_POS + N_PCR + LAST_PCR + ATS,
# data = df_adj ,weights = weight, x =T)
#new = df_adj %>%
# filter(COD_SOGGETTO %in% SAMPLE_ALL)
#adjsurv <- adjustedsurv(data=new,
# variable="VACCINE_DEFICIT",
# ev_time="tstop",
# event="OUTCOME2",
# method="direct",
# outcome_model=fit_tv,
# conf_int=TRUE)
#
#p_adj = plot(adjsurv,conf_int=TRUE)
#ggsave("N:/output/plots/SEVERC19_ADJ_CURVES.png", plot = p_adj, width = 10, height = 10, dpi = 300)
test_ph_tv <- cox.zph(models$OUTCOME1)
test_ph_tv$table
plot(test_ph_tv, size = 0.1)