-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmprsk_hd.R
198 lines (162 loc) · 5.87 KB
/
cmprsk_hd.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
library(boot)
library(skimr)
library(table1)
library(survival)
library(dplyr)
library(cmprsk)
library(randomForestSRC)
library(riskRegression)
library(prodlim)
library(pec)
library(party)
library(BART)
library(feather)
library(rsample)
#Import dataset
data(hd)
cmpdata <- hd
skim(cmpdata)
recoded <- cmpdata
##0 = Alive, 1 = Relapse, 2= Death
table(recoded$status)
recoded$sex <- as.factor(recoded$sex)
recoded$trtgiven <- as.factor(recoded$trtgiven)
recoded$medwidsi <- as.factor(recoded$medwidsi)
recoded$extranod <- as.factor(recoded$extranod)
recoded$clinstg <- as.factor(recoded$clinstg)
#table 1
cmpdata.labeled <- recoded
cmpdata.labeled$status <-
factor(recoded$status,
levels=c(0,1,2),
labels=c("Alive",
"Relapse", # Reference
"Death"))
table1(~ age + sex + trtgiven + medwidsi + extranod + clinstg + time
| status, data=cmpdata.labeled)
recoded <- recoded %>% mutate_at(c("age"),
~(scales::rescale(.) %>% as.vector))
write_feather(recoded, "hd.feather")
#Cox PH (Cause-specific Hazards model)
print("COX PROPORTIONAL HAZARDS")
cmpdatacen <- recoded %>% #censoring competing risks (Death)
mutate(status = replace(status, status != 1, 0))
# table(cmpdatacen$status)
# table(recoded$status)
#fit model
coxmodel<- coxph(Surv(time, status) ~ age + sex + trtgiven + medwidsi +
clinstg,
data = cmpdatacen, x = TRUE)
cox.zph(coxmodel)
# modelsum <- summary(coxmodel)
#C-index
c<-(cindex(coxmodel, formula = Surv(time, status) ~
age + sex + trtgiven + medwidsi +
clinstg,
data=cmpdatacen, splitMethod="boot632", B=1000,verbose = FALSE, keep.index = TRUE,
keep.matrix = TRUE, keep.pvalues = TRUE))
print(c)
cvector<-unlist(c$BootstrapCrossValCindexMat)
print(sd(cvector))
brier<-pec(coxmodel, data = cmpdatacen, splitMethod="boot632", cause = 1, B=1000,
verbose = FALSE)
print(brier)
ls<-brier$Boot632Err[2]
print(sd(unlist(ls), na.rm = TRUE))
print(quantile(unlist(ls), c(.025, .975), na.rm = TRUE))
print("END OF COXPH")
cat("\n\n\n\n") #for creating new lines in printed output
#Fine & Gray model (Subdistribution model)
print("FINE AND GRAY MODEL")
#visualise CIF
CIF <- cuminc(ftime = cmpdata.labeled$time, # failure time variable (years)
fstatus = cmpdata.labeled$status, # variable with distinct codes for different causes of failure
# group = cmpdata.labeled$ulcer, # estimates will calculated within groups
## strata = , # Tests will be stratified on this variable.
rho = 0, # Power of the weight function used in the tests.
cencode = 1, # value of fstatus variable which indicates the failure time is censored.
## subset = ,
## na.action = na.omit
)
plot(CIF, color=1:6)
title("HD")
#c-index must standardize td-c-index date across all methods
fgr <- FGR(Hist(time,status)~age + sex + trtgiven + medwidsi + clinstg,
data=recoded, cause = 1) ##Fine-Gray
c2<-(cindex(fgr, data = recoded, splitMethod="boot632", B=1000,
verbose = FALSE, keep.index = TRUE,
keep.matrix = TRUE, keep.pvalues = TRUE))
print(c2)
cvector2<-unlist(c2$BootstrapCrossValCindexMat)
print(sd(cvector2))
brier<-pec(fgr, data = recoded, splitMethod="boot632", cause = 1, B=1000,
verbose = FALSE)
print(brier)
ls<-brier$Boot632Err[2]
print(sd(unlist(ls), na.rm = TRUE))
print(quantile(unlist(ls), c(.025, .975), na.rm = TRUE))
print("END OF FINE & GRAY")
cat("\n\n\n\n") #for creating new lines in printed output
##Random Survival Forest (Ishwaran, 2013)
## Analysis 1
## modified Gray's weighted log-rank splitting
## (equivalent to cause=c(1,1) and splitrule="logrankCR")
print("RANDOM SURVIVAL FOREST")
fitform <- Surv(time, status) ~ age + sex + trtgiven + medwidsi + clinstg
o1 <- rfsrc(fitform, recoded, ntree = 1000)
##C-index
c3<-(cindex(o1, fitform, data=recoded, splitMethod="boot632",
cause = 1,
B=1000, verbose = FALSE, keep.index = TRUE,
keep.matrix = TRUE, keep.pvalues = TRUE))
print(c3)
cvector3<-unlist(c3$BootstrapCrossValCindexMat)
print(sd(cvector3))
brier<-pec(o1, data = recoded, splitMethod="boot632", cause = 1, B=1000,
verbose = FALSE)
print(brier)
ls<-brier$Boot632Err[2]
print(sd(unlist(ls), na.rm = TRUE))
print(quantile(unlist(ls), c(.025, .975), na.rm = TRUE))
print("END OF RANDOM SURVIVAL FOREST")
cat("\n\n\n\n")
#####BART (Bayesian additive regression trees)
print("BAYESIAN ADDITIVE REGRESSION TREES")
set.seed(99)
split <- initial_split(recoded, prop = .7)
train <- training(split)
times <- pmax(1, ceiling(train$time)) ## years
delta <- train$status
test <- testing(split)
train <- data.matrix(train[,-c(7,8)])
test <- data.matrix(test[,-c(7,8)])
pre <- crisk.pre.bart(x.train=train, times=times, delta=delta, x.test=test)
post <- crisk.bart(x.train=train, times=times, delta=delta, x.test=test)
brier.score(bartpred$prob.test, post)
bartpred$tx.test
Cindex=function(risk, times, delta=NULL)
{
N=length(risk)
if(N!=length(times))
stop('risk and times must be the same length')
if(length(delta)==0) delta=rep(1, N)
else if(N!=length(delta))
stop('risk and delta must be the same length')
l=0
k=0
for(i in 1:N) {
h=which((times[i]==times & delta[i]>delta) |
(times[i]<times & delta[i]>0))
if(length(h)>0) {
l=l+sum(risk[i]<risk[h])
k=k+length(h)
}
}
return(l/k)
}
ttrain <- post$tx.train
times<-ttrain[,1]
print("Bootstrap c-index")
print(boot(data=c(post$prob.train.mean, times), statistic=Cindex, R=1000))
print("END OF BART")
cat("\n\n\n\n")