-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmethod_pseudoobs_SL.R
More file actions
185 lines (143 loc) · 6.46 KB
/
method_pseudoobs_SL.R
File metadata and controls
185 lines (143 loc) · 6.46 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#################################################################################
#################################################################################
#Super learner for time-to-event outcomes tutorial
#
#Method: pseudo observations super learner
#Sachs et al 2019
#
#Implemented using superLearner package,
#and making use of Michael Sach's code from https://github.com/sachsmc/pseupersims
#################################################################################
#################################################################################
source("packages.R")
source("rotterdam_setup.R")
source("functions.R")
source("functions_pseudoobs.R")
source("censoring_weights_KM.R")#Kaplan-Meier estimates of censoring weights
# source("censoring_weights_discretetime_SL.R")#uncomment to use SL estimates of censoring weights instead
#extra packages
library(pseudo)
library(kernlab)
#---------------------------------
#---------------------------------
#calculate pseudo observations at times 1:10
#this uses the add_pseudo_obs which is modified form the code of Sachs.
#Create a new version of the data which contains pseudo observations at times 1:10 and a time variable (ptime),
#---------------------------------
#---------------------------------
add_pseudo_obs <- function(data, tme = 1:10) {
psuo <- pseudoci(data$time, event = data$status, tmax = tme)
data <- do.call(rbind, lapply(1:length(tme),
function(x) cbind(data,
pseudo = psuo$pseudo$cause1[, x],
ptime = tme[x])))
data
}
#dta_train_p is stacked across times 1:10
dta_train_p<-add_pseudo_obs(data=dta_train,tme=1:10)
#---------------------------------
#---------------------------------
#Superlearner using pseudo observations at 10 time horizons
#---------------------------------
#---------------------------------
#add time of interest to the test data
dta_test$ptime<-10
#set up learners
tune = list(ntrees = c(200),
max_depth = 2,
shrinkage = c(0.01, 0.1, .2))
learners = create.Learner("SL.xgboost", tune = tune, detailed_names = T, name_prefix = "xgb")
SL.library <- c("SL.glm", "SL.gam", "SL.ksvm", "SL.ranger",
"SL.rpart", "SL.glmnet","SL.polymars", learners$names)
#standardise continuous variables,
#as this seems to make quite a big difference to avoiding pseudo observatiosn outside the range form 0 to 1
mean.age<-mean(dta_train$age)
sd.age<-sd(dta_train$age)
mean.nodes<-mean(dta_train$nodes)
sd.nodes<-sd(dta_train$nodes)
mean.pgr<-mean(dta_train$pgr)
sd.pgr<-sd(dta_train$pgr)
mean.er<-mean(dta_train$er)
sd.er<-sd(dta_train$er)
dta_train_p$age_std<-(dta_train_p$age-mean.age)/sd.age
dta_train_p$nodes_std<-(dta_train_p$nodes-mean.nodes)/sd.nodes
dta_train_p$pgr_std<-(dta_train_p$pgr-mean.pgr)/sd.pgr
dta_train_p$er_std<-(dta_train_p$er-mean.er)/sd.er
dta_test$age_std<-(dta_test$age-mean.age)/sd.age
dta_test$nodes_std<-(dta_test$nodes-mean.nodes)/sd.nodes
dta_test$pgr_std<-(dta_test$pgr-mean.pgr)/sd.pgr
dta_test$er_std<-(dta_test$er-mean.er)/sd.er
set.seed(1)
sl<- SuperLearner(Y = dta_train_p$pseudo,
X = dta_train_p[,c("ptime","year1","year2","age_std","meno","size1","size2","grade","nodes_std","pgr_std",
"er_std","hormon","chemo")],
newX=dta_test[,c("ptime","year1","year2","age_std","meno","size1","size2","grade","nodes_std","pgr_std",
"er_std","hormon","chemo")],
SL.library = SL.library,
id = dta_train_p$id,
verbose = TRUE,
method = "method.pseudoAUC",
control = list(timedex = dta_train_p$ptime == 10))
#---------------------------------
#---------------------------------
#obtain predictions
#---------------------------------
#---------------------------------
risk.pred<-sl$SL.predict
#truncate to range 0 to 1
risk.pred<-ifelse(risk.pred<0,0,risk.pred)
risk.pred<-ifelse(risk.pred>1,1,risk.pred)
#---------------------------------
#---------------------------------
#Calibration plots
#---------------------------------
#---------------------------------
#---
#calibration plot - using our function
cut_points=c(0,quantile(risk.pred,probs=seq(0.1,0.9,0.1)),1)
risk_group=cut(risk.pred,breaks=cut_points,include.lowest = T,labels = F)
calib_risk_group<-sapply(FUN=function(x){mean(risk.pred[risk_group==x])},1:10)
km.grp=survfit(Surv(time,status)~strata(risk_group),data=dta_test)
risk_obs_grp<-rep(NA,10)
for(k in 1:10){
group.exists<-paste0("strata(risk_group)=risk_group=",k)%in%summary(km.grp,cens=T)$strata
if(group.exists){
step.grp=stepfun(km.grp$time[summary(km.grp,cens=T)$strata==paste0("strata(risk_group)=risk_group=",k)],
c(1,km.grp$surv[summary(km.grp,cens=T)$strata==paste0("strata(risk_group)=risk_group=",k)]))
risk_obs_grp[k]<-1-step.grp(10)
}
}
plot(calib_risk_group,risk_obs_grp,type="both",xlab="Predicted risk",ylab="Estimated actual risk",xlim=c(0,1),ylim=c(0,1))
abline(0,1)
#---------------------------------
#---------------------------------
#Brier score and Scaled Brier (IPA)
#---------------------------------
#---------------------------------
#---
#Brier score and IPA - using our function
Brier(time=dta_test$time, status=dta_test$status, risk=risk.pred,
seq.time=10, weights=dta_test$cens.wt)
ipa(time=dta_test$time, status=dta_test$status, risk=risk.pred,
seq.time=10, weights=dta_test$cens.wt)
#---------------------------------
#---------------------------------
#C-index and AUC
#---------------------------------
#---------------------------------
#---
#C-index - using our function
c_index_ties(time=dta_test$time,status=dta_test$status, risk=risk.pred, tau=10, weightmatrix = wt_matrix_eventsonly)
#c-index - using concordance
concordance(Surv(dta_test$time, dta_test$status) ~ risk.pred,
newdata=dta_test,
reverse = TRUE,
timewt = "n/G2")$concordance
#---
#C/D AUCt - using our function
max.event.time<-max(dta_test$time[dta_test$status==1])
wCD_AUCt(time=dta_test$time,status=dta_test$status, risk=risk.pred, seq.time =max.event.time, weightmatrix = wt_matrix_eventsonly)
#---
#AUC - using timeROC
timeROC( T = dta_test$time,delta = dta_test$status,marker = risk.pred,
cause = 1,weighting = "marginal",times = 10,iid = FALSE)