generated from opensafely/research-template
/
Table_postopcovid_crude.R
35 lines (24 loc) · 1.86 KB
/
Table_postopcovid_crude.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
library(foreach)
library(data.table)
ncores <- parallel::detectCores(logical = T) - 1
data.table::setDTthreads(ncores)
source(here::here("analysis","Utils.R"))
###########################################################
dt.tv <- data.table::setDT(feather::read_feather(here::here("output","cohort_long.feather")))
procedures <- c('Abdominal','Cardiac','Obstetrics','Orthopaedic','Thoracic', 'Vascular')
data.table::setkey(dt.tv,patient_id,tstart,tstop)
covariates <- c(procedures,'age.cat','sex','bmi.cat','imd5','wave',
'vaccination.status.factor','region','Current.Cancer','Emergency','Charl12','recentCOVID','previousCOVID')
data.table::setkey(dt.tv,patient_id,tstart,tstop)
summary(dt.tv)
#Could be removed after data.manage rerun
dt.tv[, region:= as.factor(region)]
#bin.cov <- c(procedures,'sex','Current.Cancer','Emergency','recentCOVID','previousCOVID')
#dt.tv[,(bin.cov) := lapply(.SD, function(x) data.table::fifelse(is.na(x),F,x)), .SDcols = c(bin.cov)]
crude.covid.cov <- data.table::rbindlist(lapply(1:length(covariates), function(i) cbind(rep(covariates[i],length(levels(dt.tv[(postop.covid.cohort),
as.factor(get(covariates[i]))]))),
levels(dt.tv[(postop.covid.cohort),as.factor(get(covariates[i]))]),
cuminc.km(covariates[i], niter = 2)[,2:5])))
names(crude.covid.cov) <- c("Characteristic","Level","Number at risk",
"Number of events","30 day Cumulative Risk adjusted for censoring","30 day Cumulative Risk adjusted for death and emergency readmission")
save(crude.covid.cov, file = here::here("output","postopcovid_crude.RData"))