Repository navigation
Expand file tree
/
Copy pathsimulation2.R
More file actions
166 lines (153 loc) · 5.5 KB
/
Copy pathsimulation2.R
File metadata and controls
166 lines (153 loc) · 5.5 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
#generate train data
library(xgboost)
n <- 200; p <- 100
beta <- c(rep(1,10),rep(0,p-10))
x <- matrix(rnorm(n*p),n,p)
real.time <- -(log(runif(n)))/(10*exp(drop(x %*% beta)))
cens.time <- rexp(n,rate=1/10)
status <- ifelse(real.time <= cens.time,1,0)
obs.time <- ifelse(real.time <= cens.time,real.time,cens.time)
#convert to xgboost data structure
t=200
dtrain<-list(data=x[1:t,],label=obs.time[1:t])
attr(dtrain,'censor')<-status[1:t]
Dtrain<-xgb.DMatrix(dtrain$data,label=dtrain$label)
attr(Dtrain,"censor")<-attr(dtrain,"censor")
Dtrain2<-xgb.DMatrix(dtrain$data[,1:10],label=dtrain$label)
attr(Dtrain2,"censor")<-attr(dtrain,"censor")
#generate test data
n2 <- 200; p2 <- 100
beta2 <- c(rep(1,10),rep(0,p2-10))
x2 <- matrix(rnorm(n2*p2),n2,p2)
real.time2 <- -(log(runif(n2)))/(10*exp(drop(x2 %*% beta2)))
cens.time2 <- rexp(n2,rate=1/10)
status2 <- ifelse(real.time2 <= cens.time2,1,0)
obs.time2 <- ifelse(real.time2 <= cens.time2,real.time2,cens.time2)
#convert to xgboost data structure
t=200
dtest<-list(data=x2[1:t,],label=obs.time2[1:t])
attr(dtest,'censor')<-status2[1:t]
Dtest<-xgb.DMatrix(dtest$data,label=dtest$label)
attr(Dtest,"censor")<-attr(dtest,"censor")
Dtest2<-xgb.DMatrix(dtest$data[,1:10],label=dtest$label)
attr(Dtest2,"censor")<-attr(dtest,"censor")
library(xgboost)
#define objective function and evaluation function
mylossobj2<-function(preds, dtrain) {
labels <- getinfo(dtrain, "label") #labels<-dtrain$label
#print(labels)
censor<-attr(dtrain,"censor")
ord<-order(labels)
ran=rank(labels)
#print(ord)
#foo<<-censor
#compute the first gradient
d=censor[ord] #status
etas=preds[ord] #linear predictor
haz<-as.numeric(exp(etas)) #w[i]
#print(haz)
rsk<-rev(cumsum(rev(haz))) #W[i]
P<-outer (haz,rsk,'/')
P[upper.tri(P)] <- 0
grad<- -(d-P%*%d)
grad=grad[ran]
#derive hessian
# H1=outer(haz,rsk^2,'/')
# H1=t(t(H1)*rsk)
H1=P
H2=outer(haz^2,rsk^2,'/')
H=H1-H2
H[upper.tri(H)]=0
hess=H%*%d
hess=hess[ran]
# Return the result as a list
return(list(grad = grad, hess = hess))
}
evalerror2 <- function(preds, dtrain) {
labels <- getinfo(dtrain, "label") #labels<-dtrain$label
censor<-attr(dtrain,"censor") #not working!
#foo<<-censor
#compute the first gradient
ord<-order(labels)
d=censor[ord] #status
etas=preds[ord] #linear predictor
haz<-as.numeric(exp(etas)) #w[i]
rsk<-rev(cumsum(rev(haz)))
err <- -sum(d*(etas-log(rsk)))
return(list(metric = "deviance", value = err))
}
##parameter tuning
best_param = list()
best_seednumber = 1234
best_loss = Inf
best_loss_index = 0
for (iter in 1:5000) {
param <- list(objective = mylossobj2,
eval_metric = evalerror2,
#num_class = 12,
max_depth = sample(6:13, 1),
eta = runif(1, .01, .3),
gamma = runif(1, 0.0, 0.2),
subsample = runif(1, .6, .9),
colsample_bytree = runif(1, .5, 1),
min_child_weight = sample(1:40, 1),
max_delta_step = sample(1:10, 1),
colsample_bylevel=runif(1, .5, 1),
lambda=runif(1,0,2),
alpha=runif(1,0,2)
)
cv.nround = 500
cv.nfold = 5
seed.number = sample.int(10000, 1)[[1]]
set.seed(seed.number)
mdcv <- xgb.cv(data=Dtrain, params = param, nthread=6,
nfold=cv.nfold, nrounds=cv.nround,
verbose = F)
min_loss = min(mdcv$evaluation_log[,'test_deviance_mean'])
min_loss_index = which.min(as.numeric(unlist(mdcv$evaluation_log[,'test_deviance_mean'])))
if (min_loss < best_loss) {
best_loss = min_loss
best_loss_index = min_loss_index
best_seednumber = seed.number
best_param = param
}
print(iter)
}
nround = best_loss_index
set.seed(best_seednumber)
md <- xgboost(data=Dtrain, params=best_param, nrounds=nround, nthread=6)
#md2 <- xgboost(data=Dtrain2, params=best_param, nrounds=nround, nthread=6)
xgb.importance(model=md)
#predict(md,Dtrain)
#order(predict(md,Dtrain2))
#rev(order(obs.time))
#concordance index for test data
library(survival)
survConcordance(Surv(obs.time, status) ~predict(md,Dtrain))
survConcordance(Surv(obs.time2, status2) ~predict(md,Dtest))
#compare with gbm cox
library(gbm)
library(survival)
gbm1 <- gbm(Surv(obs.time,status)~ ., # formula
data=as.data.frame(x), # dataset
#weights=w,
#var.monotone=c(0,0,0), # -1: monotone decrease, +1: monotone increase, 0: no monotone restrictions
distribution="coxph",
n.trees=1000, # number of trees
shrinkage=0.001, # shrinkage or learning rate, 0.001 to 0.1 usually work
#interaction.depth=3, # 1: additive model, 2: two-way interactions, etc
bag.fraction = 0.5, # subsampling fraction, 0.5 is probably best
train.fraction = 0.8, # fraction of data for training, first train.fraction*N used for training
cv.folds = 5, # do 5-fold cross-validation
#n.minobsinnode = 10, # minimum total weight needed in each node
keep.data = TRUE,
verbose = TRUE) # print progress
summary(gbm1)
#aa=predict(gbm1,data=as.data.frame(x2))
survConcordance(Surv(obs.time, status) ~ predict(gbm1,data=as.data.frame(x2)))
#compare with coxboost
library(CoxBoost)
cbfit <- CoxBoost(time=obs.time,status=status,x=x)
summary(cbfit)
survConcordance(Surv(obs.time, status) ~ as.vector(predict(cbfit)))
survConcordance(Surv(obs.time2, status2) ~ as.vector(predict(cbfit,newdata=x2)))