-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecentPredection_CV.R
181 lines (161 loc) · 6.38 KB
/
RecentPredection_CV.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
#prediction using cross validation
df=read.csv("df_2018-5.csv",sep= ",",header=T,stringsAsFactors=FALSE)
df=df[,2:ncol(df)]
names(df)=c("Clade","numberTipsClade","numberTipsTrimmed","sackin","colless","Variance","I2","B1","B2",
"avgLadder","ILnumber","pitchforks","maxHeight","MaxWidth","DelW","Stairs1","Stairs2","Cherries",
"BS","descinm","getstattest","skewness","kurtosis","MeanPairwiseDist","MaxPairwiseDist", "diameter",
"WienerIndex", "betweenness", "closeness", "eigenvector","MeadianEp","MaxEp","MeanEp",
"numberTipsTrimmed_3.4","Labels")
load("~/flutree2018-5.Rdata")
tree=flutree
allHeights=node.depth.edgelength(tree)
allD=allDescendants(tree)
ind=which(df$Labels <= 1.1)
length(ind)
df$Labels[ind]=0
df$Labels[-ind]=1
round(length(which(df$Labels==1))/nrow(df),1)
#find the clades which their root is in the last 3.4 years of the tree and their labels are 0
#we are not sure about the labels of these clades since they do not have enough time to growth
res=numeric()
for(i in 1:nrow(df)){
if(df$Labels[i]==0){
print(i)
root=df$Clade[i]
if( max(allHeights)-allHeights[root]<=3.4){res=c(res,i)}
}
}
length(res)
RecentData=df[res,]
DD=RecentData[,c(1,2,3,34,35)]
df=df[-res,]
df=df[,3:ncol(df)]
df=df[,-32]
kernel="linear"
r=numeric()
set.seed(123)
data=Preparedf(df)
folds <- cut(seq(1,nrow(data)),breaks=10,labels=FALSE)
result_i=numeric()
labels=numeric()
for(j in 1:10){
print(j)
testIndexes <- which(folds==j,arr.ind=TRUE)
test <- data[testIndexes,2:ncol(data) ]
train <- data[-testIndexes,2:ncol(data) ]
gamma=0.03125
cost=64
svm.fit = svm(data = train, Labels ~ .,
kernel =kernel, degree = 3, gamma = gamma,
coef0 = 0, cost =cost, nu = 0.5, class.weigth=c("0"=0.5,"1"=0.5))
svm.prob <- predict(svm.fit, newdata = test)
agreement <- svm.prob == test$Labels
acc=length(which(svm.prob == test$Labels))/length(test$Labels)
svmmodel.predict<-predict(svm.fit, newdata = test,decision.values=TRUE)
svmmodel.probs<-attr(svmmodel.predict,"decision.values")
svmmodel.class<-predict(svm.fit,test,type="class")
svmmodel.labels<-test$Labels
#roc analysis for test data
svmmodel.prediction<-prediction(svmmodel.probs,svmmodel.labels)
svmmodel.performance<-performance(svmmodel.prediction,"tpr","fpr")
svmmodel.auc<-performance(svmmodel.prediction,"auc")@y.values[[1]]
svmmodel.auc
res=c(acc,svmmodel.auc)
result_i=rbind(result_i,res)
labels=c(labels,svm.prob )
#prediction on recent clades
testData=RecentData[,3:ncol(RecentData)]
testData=testData[,-32]
testData=scaleData(testData)
svm.prob <- predict(svm.fit, newdata = testData)
DD=cbind(DD,svm.prob)
}
write.csv(DD,"RecentPredictionCV")
#==================================================================================================
####################################predicton on clades after 2016#################################
df=read.csv("df_2018-5.csv",sep= ",",header=T,stringsAsFactors=FALSE)
df=df[,2:ncol(df)]
names(df)=c("Clade","numberTipsClade","numberTipsTrimmed","sackin","colless","Variance","I2","B1","B2",
"avgLadder","ILnumber","pitchforks","maxHeight","MaxWidth","DelW","Stairs1","Stairs2","Cherries",
"BS","descinm","getstattest","skewness","kurtosis","MeanPairwiseDist","MaxPairwiseDist", "diameter",
"WienerIndex", "betweenness", "closeness", "eigenvector","MeadianEp","MaxEp","MeanEp",
"numberTipsTrimmed_3.4","Labels")
ind=which(df$Labels <= 1.1)
length(ind)
df$Labels[ind]=0
df$Labels[-ind]=1
round(length(which(df$Labels==1))/nrow(df),1)
#find the clades which their tips are after 2016
res_2016=numeric()
myclades=getClades2(flutree, MinTotalSize = 8, MinTrimSize = 8, TimeFrame = 1.4)
Final_trimmedClades=myclades$trimclades[myclades$rejected==0]
Final_trimmedClades_root=as.numeric(names(which(myclades$rejected==0)))
Auxdata=read.csv("/Users/maryam/Desktop/Research/ImperialCollege/Prediction/MyTree/NewEperiments/Trees/RNAP2018.csv",sep= ",",header=T,stringsAsFactors=FALSE)
Auxdata=Auxdata[,2:4]
allHeights=node.depth.edgelength(flutree); max(allHeights)
hdata=data.frame(tiplab=flutree$tip.label, height=allHeights[1:length(flutree$tip.label)])
res_2016=numeric()
for(i in 1:length(df$Clade)){
print(i)
tr1=extract.clade(flutree,df$Clade[i])
tr=drop.tip(tr1,setdiff(tr1$tip.label,hdata$tiplab[Final_trimmedClades[[i]]]), trim.internal = TRUE)
ind=match(tr$tip.label,Auxdata[,1])
date=as.Date(Auxdata[ind,3])
if(min(date)>"2016-01-01"){res_2016=c(res_2016,i)}
}
RecentData=df[res_2016,]
DD=RecentData[,c(1,2,3,34,35)]
#find the clades which their root is in the last 3.4 years of the tree and their labels are 0
#we are not sure about the labels of these clades since they do not have enough time to growth
res=numeric()
for(i in 1:nrow(df)){
if(df$Labels[i]==0){
print(i)
root=df$Clade[i]
if( max(allHeights)-allHeights[root]<=3.4){res=c(res,i)}
}
}
length(res)
df=df[-res,]
df=df[,3:ncol(df)]
df=df[,-32]
kernel="linear"
r=numeric()
set.seed(123)
data=Preparedf(df)
folds <- cut(seq(1,nrow(data)),breaks=10,labels=FALSE)
result_i=numeric()
labels=numeric()
for(j in 1:10){
print(j)
testIndexes <- which(folds==j,arr.ind=TRUE)
test <- data[testIndexes,2:ncol(data) ]
train <- data[-testIndexes,2:ncol(data) ]
gamma=0.03125
cost=64
svm.fit = svm(data = train, Labels ~ .,
kernel =kernel, degree = 3, gamma = gamma,
coef0 = 0, cost =cost, nu = 0.5, class.weigth=c("0"=0.5,"1"=0.5))
svm.prob <- predict(svm.fit, newdata = test)
agreement <- svm.prob == test$Labels
acc=length(which(svm.prob == test$Labels))/length(test$Labels)
svmmodel.predict<-predict(svm.fit, newdata = test,decision.values=TRUE)
svmmodel.probs<-attr(svmmodel.predict,"decision.values")
svmmodel.class<-predict(svm.fit,test,type="class")
svmmodel.labels<-test$Labels
#roc analysis for test data
svmmodel.prediction<-prediction(svmmodel.probs,svmmodel.labels)
svmmodel.performance<-performance(svmmodel.prediction,"tpr","fpr")
svmmodel.auc<-performance(svmmodel.prediction,"auc")@y.values[[1]]
svmmodel.auc
res=c(acc,svmmodel.auc)
result_i=rbind(result_i,res)
labels=c(labels,svm.prob )
#prediction on recent clades
testData=RecentData[,3:ncol(RecentData)]
testData=testData[,-32]
testData=scaleData(testData)
svm.prob <- predict(svm.fit, newdata = testData)
DD=cbind(DD,svm.prob)
}
write.csv(DD,"2016PredictionCV")