-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangesInAdmissionsBaselineToTDC
174 lines (151 loc) · 8.86 KB
/
ChangesInAdmissionsBaselineToTDC
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
# Creates code to compare admissions rate between baseline and time-dependent classification
# Do not count HF diagnosis where diagnosis occured in death.
library(RODBC)
library(tidyverse)
library(ggpubr)
library(reprex)
library(broom)
library(ggsci)
library(grid)
library(reshape2)
library(scales)
library(grid)
library(gridExtra)
library(ggtext)
library(patchwork)
#global functions
my.format = function(x=0, ndp)format(round(x,ndp), ns=ndp, just="none", scientific = F)
bigNumbers <- function(x=0, ndp=0)format(as.numeric(my.format(x, ndp)), big.mark=",")
my.contSummary = function(x, ndp)(format(paste0(my.format(median(x), ndp),' (',my.format(quantile(x, 0.25), ndp)," - ",
my.format(quantile(x, 0.75), ndp), ")")))
my.factSummary <- function(x, tot, ndp)format(paste0(ifelse((tot < 6 & tot != 0), "<6", bigNumbers(tot)), " (",my.format(as.numeric(x), ndp),"%)"))
my.irSummary <- function(x, tot, ndp)format(paste0(ifelse((tot < 6.0 & tot != 0), "<6", bigNumbers(tot)), " (",ifelse(my.format(as.numeric(x), ndp) < 0.01 & tot != 0, '<0.01', my.format(as.numeric(x), ndp)),")"))
my.tableValues <- function(x, ndp=0)format(ifelse((x < 6.0 & x != 0), "<6", bigNumbers(x)))
#global variables
## personTimeScale (by x person-years)
personTimeScale = 1
## number of decimal places used in table
tableNdps = 0
## number of decimal places used to report Hb
hbNdps = 1
## number of decimal places used to report person-time
personTimeNdps = 2
# Baseline classification data
conn <- odbcDriverConnect("Driver=SQL Server; Server=#####; Database=#####")
included <- sqlQuery(conn, 'select * from #####' )
odbcCloseAll()
included$group <- factor(included$inclusionStatusSplit,
levels = c("-1" ,"0", "1", "2", "3"),
labels = c( "Neither (18-59)", "Neither (60+)", "LD Only", "HF Only", "Both: LD + HF"))
#Time-dependent covariate classification data
conn <- odbcDriverConnect("Driver=SQL Server; Server=####; Database=####")
data_raw_tdc <- sqlQuery(conn, 'select * from ####' )
odbcCloseAll()
tdc <- data_raw_tdc
tdc$updatedGroupSplit <- factor(tdc$updatedGroupSplit,
levels = c( "-1", "0", "1", "2", "3"),
labels = c("Neither (18-59)", "Neither (60+)", "LD Only", "HF Only", "Both: LD + HF"))
getBaselineByGroup <- function(included, tdc, cohortName, cgc){
blPt <- sum(included$numDaysFree)/365.25
blAdmits <- included %>% select(numHospAdmit, numHfHospAdmit, numCvOtherHospAdmit, numRespHospAdmit, numInfectHospAdmit, numNeoplasmHospAdmit,
numRenalHospAdmit, numEyeHospAdmit, numGiHospAdmit,
numInjuryHospAdmit, numMentNeuroHospAdmit,
numMskHospAdmit, numOtherHospAdmit)
blAdmitsPt <- unlist(lapply(blAdmits ,function(x) if(is.numeric(x)) round((sum(replace_na(x, 0), na.rm = T)/blPt),2)))
tdcPt <- sum(tdc$numDaysFree)/365.25
tdcAdmits <- tdc %>% select(numHospAdmit, numHfHospAdmit, numCvOtherHospAdmit, numRespHospAdmit, numInfectHospAdmit, numNeoplasmHospAdmit,
numRenalHospAdmit, numEyeHospAdmit, numGiHospAdmit,
numInjuryHospAdmit, numMentNeuroHospAdmit,
numMskHospAdmit, numOtherHospAdmit)
tdcAdmitsPt <- unlist(lapply(tdcAdmits ,function(x) if(is.numeric(x)) round((sum(replace_na(x, 0), na.rm = T)/tdcPt),2)))
admits <- rbind(1:12,blAdmitsPt[2:13], tdcAdmitsPt[2:13])
admits.df <- as.data.frame(admits)
colnames(admits.df) <- c("HF", "CV Other", "Chronic respiratory diseases", "Infection", "Neoplasm",
"Renal", "Eye & Adnexa", "Digestive disease",
"Injury", "Mental health & neurological disorders",
"Musculoskeletal", "Other")
(changesInAdmits <- admits.df %>% slice(c(2,3)) %>% t()%>% as.data.frame %>%
add_rownames()%>% arrange(V1) %>% mutate(rowname= factor(rowname,
levels =c("Other", "Musculoskeletal", "Mental health & neurological disorders", "Injury", 'Digestive disease',
"Eye & Adnexa", "Renal", "Neoplasm", "Infection", "Chronic respiratory diseases",
"CV Other", "HF"))) %>%
ggplot(aes(x=rowname, y=V1)) +
geom_segment(aes(x=rowname, xend=rowname, y=V1, yend=V2), color='grey')+
geom_point(aes(y=V1), size=5, color=cgc, alpha=0.1)+
geom_point(shape = 1, size = 5, color = cgc)+
geom_point(aes(y=V2), size=5, color=cgc, alpha = 0.6) +
coord_flip()+
theme_classic()+
theme(
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
axis.text = element_text(size=16)
)+
ylim(0,0.2)+
ylab('Number of admissions \n per person-year at risk')+
xlab(''))
svg(paste0(cohortName, "_changesInMorb.svg"))
print(changesInAdmits)
dev.off()
}
getBaselineByGroupNUllHf <- function(included, tdc, cohortName, cgc){
blPt <- sum(included$numDaysFree)/365.25
blAdmits <- included %>% select(numHospAdmit, numHfHospAdmit, numCvOtherHospAdmit, numRespHospAdmit, numInfectHospAdmit, numNeoplasmHospAdmit,
numRenalHospAdmit, numEyeHospAdmit, numGiHospAdmit,
numInjuryHospAdmit, numMentNeuroHospAdmit,
numMskHospAdmit, numOtherHospAdmit)
blAdmitsPt <- unlist(lapply(blAdmits ,function(x) if(is.numeric(x)) round((sum(replace_na(x, 0), na.rm = T)/blPt),2)))
tdcPt <- sum(tdc$numDaysFree)/365.25
tdcAdmits <- tdc %>% select(numHospAdmit, numHfHospAdmit, numCvOtherHospAdmit, numRespHospAdmit, numInfectHospAdmit, numNeoplasmHospAdmit,
numRenalHospAdmit, numEyeHospAdmit, numGiHospAdmit,
numInjuryHospAdmit, numMentNeuroHospAdmit,
numMskHospAdmit, numOtherHospAdmit)
tdcAdmitsPt <- unlist(lapply(tdcAdmits ,function(x) if(is.numeric(x)) round((sum(replace_na(x, 0), na.rm = T)/tdcPt),2)))
admits <- rbind(1:12,blAdmitsPt[2:13], tdcAdmitsPt[2:13])
admits.df <- as.data.frame(admits)
colnames(admits.df) <- c("HF", "CV Other", "Chronic respiratory diseases", "Infection", "Neoplasm",
"Renal", "Eye & Adnexa", "Digestive disease",
"Injury", "Mental health & neurological disorders",
"Musculoskeletal", "Other")
changesInAdmits <- admits.df %>% slice(c(2,3)) %>% t()%>% as.data.frame %>%
add_rownames()%>% arrange(V1) %>% mutate(rowname= factor(rowname,
levels =c("Other", "Musculoskeletal", "Mental health & neurological disorders", "Injury", 'Digestive disease',
"Eye & Adnexa", "Renal", "Neoplasm", "Infection", "Chronic respiratory diseases",
"CV Other", "HF")))
hide <- changesInAdmits[which(changesInAdmits$rowname == "HF"),]
(p <- ggplot(changesInAdmits, aes(x=rowname, y=V1)) +
geom_segment(aes(x=rowname, xend=rowname, y=V1, yend=V2), color='grey')+
{if(cohortName != "group4" & cohortName != "group5")annotate("segment", x=hide$rowname, xend=hide$rowname, y=hide$V1, yend=hide$V2, color = "white", size = 1.01)} +
geom_point(aes(y=V1), size=5, color=cgc, alpha=0.1)+
geom_point(shape = 1, size = 5, color = cgc)+
geom_point(aes(y=V2), size=5, color=cgc, alpha = 0.6)+
{if(cohortName != "group4" & cohortName != "group5")annotate(geom = "point", x=hide$rowname, y=hide$V2, size=6, color="white")} +
geom_point(aes(y=V1), size=5, color=cgc, alpha=0.1)+
geom_point(shape = 1, size = 5, color = cgc)+
coord_flip()+
theme_classic()+
theme(
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_blank(),
axis.text = element_text(size=16)
)+
ylim(0,0.2)+
ylab('Number of admissions \n per person-year at risk')+
xlab(''))
svg(paste0(groupName, "_changesInMorbNaHf.svg"))
print(p)
dev.off()
}
labels = c( "Neither (18-59)", "Neither (\U2265 60 yrs)", "LD Only", "HF Only", "Both: LD + HF")
colors = c("#ADB6B6FF", "#FDAF91FF", "#00468BFF", "#ED0000FF", "#925E9FFF")
count = 1
for(label in labels){
print(label)
bl <- included %>% filter(group == label)
tdcG <- tdc %>% filter(updatedGroupSplit == label)
print(bl %>% nrow())
print(tdcG %>% nrow())
getBaselineByGroup(bl,tdcG, paste0("group", count), colors[count])
getBaselineByGroupNUllHf(bl,tdcG, paste0("group", count), colors[count])
count = count + 1
}