-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree_predict_Cindex_nhpp_2.R
84 lines (59 loc) · 2.38 KB
/
tree_predict_Cindex_nhpp_2.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
# ---------------------------------------------------------------
# ---------------------------------------------------------------
# Sub-routine for RF-R.R
# Description: compute the OOB prediction C-index
# Date: 04/20/2018
# ---------------------------------------------------------------
# Author:
# Xiao Liu, Dept of Industrial Engineering
# 4174 Bell Engineering Center
# University of Arkansas
# e-mail: xl027@uark.edu
# ---------------------------------------------------------------
# ---------------------------------------------------------------
tree_predict_C = function(i, X.train, X.pred, data.pred, tree.trained, data.train, Z.pred, Z.train, tree.mle){
# X.pred: input X
# tree.trained: trained tree stucture
n.terminal = length(tree.trained)
output = sapply(1:n.terminal, tree_predict_sub, X.pred.0=X.pred[i,], tree.trained.0=tree.trained )
terminal.case = which(output==1)[1]
terminal.select = tree.trained[[terminal.case]]
terminal.struct = terminal.select[[1]]
terminal.data.id = terminal.select[[2]]
# actual
n.failure = length(which(data.pred[i,]>0))-1
t.length = data.pred[i,n.failure+1]
rate = n.failure/t.length
# pred:
#nhpp.t = nhpp.beta(Data = data.train[terminal.data.id, ], Z = Z.train[terminal.data.id], X=X.train[terminal.data.id, ])
nhpp.t = tree.mle[,terminal.case]
#x.mat = t( matrix( rep(as.numeric(X.pred[i,]), nrow(Z.pred[[i]])), ncol=nrow(Z.pred[[i]])) )
#tmp = cbind(array(1, dim=c(nrow(Z.pred[[i]]),1)), x.mat, Z.pred[[i]])
tmp = cbind(array(1, dim=c(nrow(Z.pred[[i]]),1)), Z.pred[[i]])
rate.pred = mean( exp(tmp %*% nhpp.t), na.rm=TRUE)
# oob error
error = c(rate.pred, rate)
return(error)
}
tree_predict_sub = function(i, X.pred.0, tree.trained.0 ){
tree = data.frame( matrix(tree.trained.0[[i]][[1]][-1,1:3],ncol=3) )
tmp = 0
for (j in 1:nrow(tree)){
if (tree[j,3]==1){
tmp = tmp + as.numeric( (X.pred.0[,tree[j,1]] - tree[j,2])<=0 )
}else{
tmp = tmp + as.numeric( (X.pred.0[,tree[j,1]] - tree[j,2])>0 )
}
}
output = 0
if (tmp == nrow(tree)){
output = 1
}
return(output)
}
tree_predict_C_index = function(i, Pairs, Data.input){
temp = Data.input[Pairs[i,],]
judge = (temp[2,2] - temp[2,1] ) * (temp[2,1] - temp[1,1] )
Concordance = as.numeric((judge>=0))
return(Concordance)
}