-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscore.R
106 lines (93 loc) · 3.25 KB
/
score.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
suppressMessages(library("survival"))
suppressMessages(library("timeROC"))
suppressMessages(library("Bolstad2"))
suppressMessages(library("ROCR"))
# question 1A
# riskScoreGlobal is the global risk score
# riskScore12, riskScore18, riskScore24 are the risk scores at 12, 18 and 24 months
# time is called LKADT_P in the CoreTable meaning the last known follow up time in days
# death is last known follow up status (F=survival, T=death)
# all input parameters are vectors
# returned value is a vector containing:
# * concordance index
# * AUC of ROC at 12, 18, and 24 months
# * integrated AUC (integrated over all time points)
## from Justin: https://mail.google.com/mail/u/1/#inbox/14c8f82a26d23b80
score_q1a<-function(time, death, riskScoreGlobal, riskScore12, riskScore18, riskScore24)
{
if (missing(riskScore12)) {
riskScore12 <- riskScoreGlobal
}
if (missing(riskScore18)) {
riskScore18 <- riskScoreGlobal
}
if (missing(riskScore24)) {
riskScore24 <- riskScoreGlobal
}
auc12 <- timeROC(T=time,
delta=death,
marker=riskScore12,
cause=1,
weighting="marginal",
times=12 * 30.5,
iid=FALSE)$AUC[2]
auc18 <- timeROC(T=time,
delta=death,
marker=riskScore18,
cause=1,
weighting="marginal",
times=18 * 30.5,
iid=FALSE)$AUC[2]
auc24 <- timeROC(T=time,
delta=death,
marker=riskScore24,
cause=1,
weighting="marginal",
times=24 * 30.5,
iid=FALSE)$AUC[2]
# compute global concordance index
surv <- Surv(time,death)
cIndex <- survConcordance(surv ~ riskScoreGlobal)$concordance
# compute iAUC from 6 to 30 months
times <- seq(6,30,by=1) * 30.5
aucs <- timeROC(T=time,
delta=death,
marker=riskScoreGlobal,
cause=1,
weighting="marginal",
times=times,
iid=FALSE)$AUC
# Simpsons rules for integrating under curve
iAUC <- sintegral(times, aucs)$int / (max(times) - min(times))
return (list(cIndex=cIndex, auc12=auc12, auc18=auc18, auc24=auc24, iAUC=iAUC))
}
# question 1B
# predTime is the predicted exact survival time for all patients in days
# LKADT_P is last known follow up time in days
# DEATH is last known follow up status (F=survival, T=death)
# all input parameters are vectors
# returned value is RMSE
score_q1b<-function(predTime,LKADT_P,DEATH)
{
x=LKADT_P[DEATH==T]
y=predTime[DEATH==T]
sqrt(sum((x-y)^2)/length(x))
}
# question 2
# pred is the predicted risk with a higher value corresponding to a higher
# probility of discontinue due to AE before 3 months
# y is the censored true label of discontinue due to AE before 3 month
# y=1 if it happens, 0, otherwise
# both input parameters are vectors
# returned value is AUC of PR
score_q2<-function(pred,y)
{
## remove subjects whose discontinuation status is NA
pred <- pred[!is.na(y)]
y <- y[!is.na(y)]
prf=ROCR::performance(ROCR::prediction(pred,y),"prec","rec")
[email protected][[1]]
[email protected][[1]]
auc=sum((x[-1]-x[-length(x)])*y[-1])
auc
}