-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.R
286 lines (268 loc) · 13.4 KB
/
Utilities.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
## Functions
area <- function(age,ldlc) sum(diff(age)*(ldlc[-length(ldlc)] + ldlc[-1])*0.5)
## load data files from SPSS
loadSPSS <- function(name,path) {
data.table::as.data.table(foreign::read.spss(
paste(path,name,sep=""),
trim_values=TRUE,
stringsAsFactors=FALSE,
add.undeclared.levels="no"))
}
## get CIF from gformula objective
gformCIF <- function(allCauseMortality,CVD,ASCVD,intDescript=NULL) {
## Combined the cumulative risk
datRes <- rbind(
allCauseMortality$result[,`:=`(Outcome="All-cause mortality")],
CVD$result[,`:=`(Outcome="Cardiovascular disease")],
ASCVD$result[,`:=`(Outcome="Atherosclerostic cardiovascular disease")])
if ('Risk lower 95% CI' %in% names(datRes)) {
datResNew <- datRes[
,`:=`('Cumulative risk'=get('g-form risk'),
'Lower 95% CI'=get('Risk lower 95% CI'),
'Upper 95% CI'=get('Risk upper 95% CI'))]
# datResNew <- melt(datRes,
# id.vars=c("k","Outcome","Interv."),
# measure.vars=c("NP Risk","g-form risk","Risk lower 95% CI","Risk upper 95% CI"),
# variable.name="Method",
# value.name="Cumulative risk")
# datResNew <- datResNew[
# ,`:=`(Model=ifelse(Method=="NP Risk",
# "Non-parametric estimate",
# ifelse(Method=="g-form risk",
# "Parametric g-formula estimate",
# ifelse(Method=="Risk lower 95% CI",
# "Lower 95% CI","Upper 95%"))))]
datResNew$Intervention <- with(
datResNew,
factor(`Interv.`,
labels = c("Natural course",intDescript),
levels = unique(`Interv.`)))
datRes0 <- datResNew[k==0][,`:=`(`Cumulative risk`=0,t0=0,
`Lower 95% CI`=0,
`Upper 95% CI`=0)]
datRes1 <- datResNew[,`:=`(t0=k+1)]
} else {
datResNew <- melt(datRes,
id.vars=c("k","Outcome"),
measure.vars=c("NP Risk","g-form risk"),
variable.name="Method",
value.name="Cumulative risk")
datResNew <- datResNew[,`:=`(Model=ifelse(Method=="NP Risk",
"Non-parametric estimate",
"Parametric g-formula estimate"))]
datRes0 <- datResNew[k==0][,`:=`(`Cumulative risk`=0,t0=0)]
datRes1 <- datResNew[,`:=`(t0=k+1)]
}
datRes <- rbind(datRes0,datRes1)
datRes <- datRes[order(Outcome,t0)]
return(datRes)
}
getTableForest <- function(datCIF) {
datRes <- datCIF[,.(`g-form risk` = last(`g-form risk`),
`Risk lower 95% CI` = last(`Risk lower 95% CI`),
`Risk upper 95% CI` = last(`Risk upper 95% CI`),
`Risk difference` = last(`Risk difference`),
`RD lower 95% CI` = last(`RD lower 95% CI`),
`RD upper 95% CI` = last(`RD upper 95% CI`),
`Risk ratio` = last(`Risk ratio`),
`RR lower 95% CI` = last(`RR lower 95% CI`),
`RR upper 95% CI` = last(`RR upper 95% CI`),
`% Intervened On` = last(`% Intervened On`),
`Aver % Intervened On` = last(`Aver % Intervened On`),
RMST=29-area(age=k,ldlc=`g-form risk`)),
by=.(Outcome,Intervention)]
datRes <- datRes[
,`:=`(Risk=paste(format95CIOneline(`g-form risk`,
`Risk lower 95% CI`,
`Risk upper 95% CI`)),
RiskDiff=paste(format95CIOneline(`Risk difference`,
`RD lower 95% CI`,
`RD upper 95% CI`)),
RiskRatio=paste(format95CIOneline(`Risk ratio`,
`RR lower 95% CI`,
`RR upper 95% CI`)),
RMET=sprintf("%.2f",RMST),
cumIntervened=sprintf("%.2f",`% Intervened On`),
avgIntervened=sprintf("%.2f",`Aver % Intervened On`),
NNT=ceiling(first(RMST)/(RMST-first(RMST))))
,by=.(Outcome)]
datRes$Outcome <- with(
datRes,ifelse(Outcome=="Cardiovascular disease","CVD",
ifelse(Outcome=="All-cause mortality","All-cause mortality",
"ASCVD")))
datResTable <- dcast(
datRes,
Intervention ~ Outcome,
value.var = c("Risk","RiskDiff",
"Risk difference","RD lower 95% CI","RD upper 95% CI",
"RiskRatio","RMET",
"cumIntervened","avgIntervened","NNT"))
datResTable$idx <- with(
datResTable,ifelse(grepl("Natural",Intervention),1,
ifelse(grepl("Dynamic",Intervention) & grepl("CSC",Intervention), 2,
ifelse(grepl("Feasible",Intervention) & grepl("CSC",Intervention),3,
ifelse(grepl("Dynamic",Intervention) & grepl("AHA",Intervention),4,
ifelse(grepl("Feasible",Intervention) & grepl("AHA",Intervention),5,6))))))
datResTable <- datResTable[order(idx)]
names <- names(datResTable)
vnames <- c("Intervention",
names[grepl("_CVD",names)],
names[grepl("All-cause",names)],
names[grepl("ASCVD",names)])
return(datResTable[,vnames,with=FALSE])
}
getTable <- function(datCIF) {
datRes <- datCIF[,.(`g-form risk` = last(`g-form risk`),
`Risk lower 95% CI` = last(`Risk lower 95% CI`),
`Risk upper 95% CI` = last(`Risk upper 95% CI`),
`Risk difference` = last(`Risk difference`),
`RD lower 95% CI` = last(`RD lower 95% CI`),
`RD upper 95% CI` = last(`RD upper 95% CI`),
`Risk ratio` = last(`Risk ratio`),
`RR lower 95% CI` = last(`RR lower 95% CI`),
`RR upper 95% CI` = last(`RR upper 95% CI`),
`% Intervened On` = last(`% Intervened On`),
`Aver % Intervened On` = last(`Aver % Intervened On`),
RMST=29-area(age=k,ldlc=`g-form risk`)),
by=.(Outcome,Intervention)]
datRes <- datRes[
,`:=`(Risk=paste(format95CI(`g-form risk`,
`Risk lower 95% CI`,
`Risk upper 95% CI`)),
RiskDiff=paste(format95CI(`Risk difference`,
`RD lower 95% CI`,
`RD upper 95% CI`)),
RiskRatio=paste(format95CI(`Risk ratio`,
`RR lower 95% CI`,
`RR upper 95% CI`)),
RMET=sprintf("%.2f",RMST),
cumIntervened=sprintf("%.2f",`% Intervened On`),
avgIntervened=sprintf("%.2f",`Aver % Intervened On`),
NNT=ceiling(first(RMST)/(RMST-first(RMST))))
,by=.(Outcome)]
datRes$Outcome <- with(
datRes,ifelse(Outcome=="Cardiovascular disease","CVD",
ifelse(Outcome=="All-cause mortality","All-cause mortality",
"ASCVD")))
datResTable <- dcast(
datRes,
Intervention ~ Outcome,
value.var = c("Risk","RiskDiff","RiskRatio","RMET","cumIntervened","avgIntervened","NNT"))
datResTable$idx <- with(
datResTable,ifelse(grepl("Natural",Intervention),1,
ifelse(grepl("Dynamic",Intervention) & grepl("CSC",Intervention), 2,
ifelse(grepl("Feasible",Intervention) & grepl("CSC",Intervention),3,
ifelse(grepl("Dynamic",Intervention) & grepl("AHA",Intervention),4,
ifelse(grepl("Feasible",Intervention) & grepl("AHA",Intervention),5,6))))))
datResTable <- datResTable[order(idx)]
names <- names(datResTable)
vnames <- c("Intervention",
names[grepl("_CVD",names)],
names[grepl("All-cause",names)],
names[grepl("ASCVD",names)])
return(datResTable[,vnames,with=FALSE])
}
# datCIF$Intervention <- with(datCIF, gsub(" (2020 CSC)","",as.character(Intervention)))
pltCIF <- function(datCIF) {
outList <- unique(datCIF$Outcome)
if('Risk lower 95% CI' %in% names(datCIF)) {
pCIF <- setNames(lapply(outList, function(i) {
ggplot(
data=datCIF[Outcome %in% i],
aes(x=t0,y=`Cumulative risk`,color=Intervention,fill=Intervention)) +
geom_smooth(se=FALSE,linewidth=0.5) +
geom_ribbon(aes(ymin=`Lower 95% CI`,ymax=`Upper 95% CI`,
color=Intervention,fill=Intervention),
alpha=0.3, linetype=0) +
theme_classic() +
theme(axis.line.x=element_line(colour="black",linewidth=0.75),
axis.line.y=element_line(colour="black",linewidth=0.75),
panel.grid.major.y = element_line(colour="gray90"),
legend.position=c(0.15,0.865),
legend.title=element_blank()) +
scale_color_jama() + scale_fill_jama() +
scale_x_continuous(name="Follow-up time (years)",
breaks=seq(0,30,5),
limits=c(0,30)) +
scale_y_continuous(name="Cumulative risk",limits=c(0,0.31)) +
labs(title=i)
}),outList)
} else {
pCIF <- setNames(lapply(outList, function(i) {
ggplot(
data=datCIF[Outcome %in% i],
aes(x=t0,y=`Cumulative risk`,color=Model)) +
geom_smooth(se=FALSE,linewidth=0.5) +
theme_classic() +
theme(axis.line.x=element_line(colour="black",linewidth=0.75),
axis.line.y=element_line(colour="black",linewidth=0.75),
panel.grid.major.y = element_line(colour="gray90"),
legend.position=c(0.15,0.865),
legend.title=element_blank()) +
scale_color_jama() +
scale_x_continuous(name="Follow-up time (years)",
breaks=seq(0,30,5),
limits=c(0,30)) +
scale_y_continuous(name="Cumulative risk",limits=c(0,0.30)) +
labs(title=i)
}),outList)
}
return(pCIF)
}
## save results from excel
saveExcel <- function(x,name,path) {
xlsx::write.xlsx(x,file=paste(path,"/",name,".xlsx",sep=""))
}
saveCSV <- function(x,name,path) {
write.csv(x,file=paste(path,"/",name,".csv",sep=""))
}
## plot cumulative risk
## save figure to pdf file
pltSave <- function(list.plot,name,path,width=10,height=8) {
ggsave(filename=paste(path,"/",name,".pdf",sep=""),
plot=list.plot,width=width,height=height,units="in")
}
## Point pgform-estimated risk
gformNNT <- function(gformula_survival.fit,int_descript=int_descript) {
datRes <- as.data.table(gformula_survival.fit$result)
datRes$Intervention <- with(datRes,
factor(Interv.,
levels=sort(unique(Interv.)),
labels=c("Natural course",int_descript)))
datRes$Intervention <- relevel(datRes$Intervention,ref=1)
datResNNT <- datRes[,.(risk=last(`g-form risk`),
riskRatio=last(`Risk ratio`),
riskDiff=last(`Risk difference`),
riskNNT=1/last(`Risk difference`),
RMST=29-area(age=k,ldlc=`g-form risk`),
NPrisk=last(`NP Risk`),
t0=last(k)+1)
,by=.(Intervention)]
datResNNT[,`:=`(RMSTRatio=RMST/(first(RMST)),
RMSTDiff=RMST-first(RMST),
RMSTNNT=first(RMST)/(RMST-first(RMST)))]
return(list(datRes=datRes,datResNNT=datResNNT))
}
## format p-value
formatP <- function(x) ifelse(x < 0.001, sprintf("%.3f",x),"<.001")
## construct 95% confidence interval
format95CI <- function(x,lci,uci) {
paste(sprintf("%.2f",x), "\n (",
sprintf("%.2f",lci), " to ",
sprintf("%.2f",uci), ")", sep="")
}
format95CIOneline <- function(x,lci,uci) {
paste(sprintf("%.2f",x), " (",
sprintf("%.2f",lci), " to ",
sprintf("%.2f",uci), ")", sep="")
}
format95CINNT <- function(x,lci,uci) {
paste(ceiling(x), " \n (",
ceiling(lci), " to ",
ceiling(uci),")", sep="")
}
## rename the intervention label
splitCom <- function(x) {
a <- unlist(strsplit(as.character(x)," "))
return(paste(a[2:length(a)],collapse=" ",sep=""))
}