-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.R
More file actions
264 lines (201 loc) · 8.11 KB
/
Script.R
File metadata and controls
264 lines (201 loc) · 8.11 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
library(pROC)
library("e1071")
library(class)
library(tree)
library(MASS)
library(leaps)
### Exploratory analysis of the data set
dataset=read.table(file = 'dataR2.csv',header = T, sep=",")
attach(dataset)
summary(dataset)
plot(dataset)
#Correlation
cor(dataset[,c(0:9)])
#Redefine the variable to predict
YNClassification <- factor(Classification, levels=c(1,2), labels=c("Healthy control","Patient" ))
sum(YNClassification=="Patient")
sum(YNClassification=="Healthy control")
### Logistic regression
log.fit=glm(YNClassification~Age+BMI+Glucose+Insulin+HOMA+Leptin+Adiponectin+Resistin+MCP.1,data=dataset,family = binomial)
summary(log.fit)
log.probs=predict(log.fit,type="response")
log.probs[1:10] # To visualize the first 10 values.
contrasts(YNClassification)
log.pred=rep("Healthy control",nrow(dataset))
log.pred[log.probs>0.5]="Patient"
#Confusion matrix
table(log.pred,YNClassification)
#Accuracy
mean(log.pred==YNClassification)
#Optimisation
logOpt.fit=glm(YNClassification~Age+BMI+Glucose+Insulin+Leptin+Resistin+MCP.1,data=dataset,family = binomial)
logOpt.probs=predict(logOpt.fit,type="response")
logOpt.pred=rep("Healthy control",nrow(dataset))
logOpt.pred[logOpt.probs>0.5]="Patient"
#Confusion matrix
table(logOpt.pred,YNClassification)
#Accuracy
mean(logOpt.pred==YNClassification)
# Using a train and a test set
## 75% of the sample size
smp_size <- floor(0.75 * nrow(dataset))
set.seed(123)
train_ind <- sample(seq_len(nrow(dataset)), size = smp_size)
train <- dataset[train_ind, ]
train_bool = rep(FALSE,nrow(dataset))
train_bool[train_ind]=TRUE
test <- dataset[-train_ind, ]
log2.fit=glm(YNClassification ~ Age+BMI+Glucose+Insulin+HOMA+Leptin+Adiponectin+Resistin+MCP.1,data=dataset,family = binomial,subset=train_bool)
summary(log2.fit)
log2.probs=predict(log2.fit,newdata = test,type="response",probability = TRUE)
log2.pred=rep("Healthy control",nrow(test))
log2.pred[log2.probs>0.5]="Patient"
test$YNClassification <- factor(test$Classification, levels=c(1,2), labels=c("Healthy control","Patient" ))
#Confusion matrix
table(log2.pred,test$YNClassification)
#Accuracy
mean(log2.pred==test$YNClassification)
#mean(log2.pred!=test$YNClassification)
ROC.log=roc(YNClassification,log.probs,levels=c("Healthy control","Patient"), thresholds=seq(0.1,1,0.1))
plot.roc(ROC.log,print.auc =T,xlab="Specificity",col="red",axes=T)
# Model selection for logistic regression :
# Use model selection methods to select a pertinent
# subset of features for the logistic regression classifier.
# Draw the Error - rate versus flexibility curve in order
# to choose the best level of flexibility
### KNN
#TODO: standarize data ?
train.X= as.matrix(train[,c(0:9)])
test.X= as.matrix(test[,c(0:9)])
train.Y=YNClassification[train_bool]
test.Y=YNClassification[!train_bool]
set.seed(1)
test.error=c()
NB_MOTIFS = 87
sequence = seq(1,NB_MOTIFS,NB_MOTIFS/6) #seq(40,50,1)
for(i in sequence)
{
set.seed(1)
knn.pred=knn(train.X,test.X,train.Y,k=i)
test.error=c(test.error,mean(knn.pred!=test.Y))
}
plot(sequence,test.error,xlab="Nombre de motifs")
KOpt = 41#2
knn.pred=knn(train.X,test.X,train.Y,k=KOpt)
#Confusion matrix
table(knn.pred,YNClassification[!train_bool])
#Accuracy
mean(knn.pred==test.Y)
test.error=mean(knn.pred!=test.Y)
test.error
### LDA
lda.fit=lda(YNClassification~ Age+BMI+Glucose+Insulin+HOMA+Leptin+Adiponectin+Resistin+MCP.1,data=dataset,subset = train_bool)
lda.pred=predict(lda.fit,newdata = test,type="response")
par(mfrow=c(1,2))
TPR = c()
TNR = c()
accList = c()
alphaList = seq(0.1,1,0.01)
for(alpha in alphaList){
#crée un vecteur qui contient la valeur prédite pour une aleur donnée (initialisé à R)
lda.pred.class = rep("Patient",nrow(test))
#Modifie le seuil de probabilité pour prédire G
#Change si la probabilité d'obtenir G est >alpha (O.5 par défaut)
lda.pred.class[lda.pred$posterior[,1]>alpha]="Healthy control"
#sensitivity ou TPR (True positive Rate)
sensitivity = sum(lda.pred.class=="Healthy control" & test$YNClassification=="Healthy control")/sum(test$YNClassification=="Healthy control")
TPR = c(TPR,sensitivity)
#1-specificity ou TNR (True negative Rate)
specificity = sum(lda.pred.class=="Patient" & test$YNClassification=="Patient")/sum(test$YNClassification=="Patient")
TNR = c(TNR,specificity)
accList = c(accList,mean(lda.pred.class==test$YNClassification))
}
plot(TNR,TPR,type="b",ylim=c(0,1),xlim = c(1,0))
plot(alphaList,accList,type="b")
#ROC curve
lda.pred.class = rep("Patient",nrow(test))
ROC.lda=roc(test$YNClassification,lda.pred$posterior[,1],levels=c("Healthy control","Patient"), thresholds=seq(0.1,1,0.1))
plot.roc(ROC.lda,print.auc =T,xlab="Specificity",col="red",axes=T)
lda.pred.class = rep("Patient",nrow(test))
lda.pred.class[lda.pred$posterior[,1]>0.4]="Healthy control"
#Confusion matrix
table(lda.pred.class,test$YNClassification)
#Accuracy
mean(lda.pred.class==test.Y)
### QDA
qda.fit=qda(YNClassification~ Age+BMI+Glucose+Insulin+HOMA+Leptin+Adiponectin+Resistin+MCP.1,subset = train_bool)
qda.pred=predict(qda.fit,newdata = test,type="response")
par(mfrow=c(1,2))
TPR = c()
TNR = c()
accList = c()
alphaList = seq(0.1,1,0.01)
for(alpha in alphaList){
#crée un vecteur qui contient la valeur prédite pour une aleur donnée (initialisé à R)
qda.pred.class = rep("Patient",nrow(test))
#Modifie le seuil de probabilité pour prédire G
#Change si la probabilité d'obtenir G est >alpha (O.5 par défaut)
qda.pred.class[qda.pred$posterior[,1]>alpha]="Healthy control"
#sensitivity ou TPR (True positive Rate)
sensitivity = sum(qda.pred.class=="x²Healthy control" & test$YNClassification=="Healthy control")/sum(test$YNClassification=="Healthy control")
TPR = c(TPR,sensitivity)
#1-specificity ou TNR (True negative Rate)
specificity = sum(qda.pred.class=="Patient" & test$YNClassification=="Patient")/sum(test$YNClassification=="Patient")
TNR = c(TNR,specificity)
accList = c(accList,mean(qda.pred.class==test$YNClassification))
}
#plot(TNR,TPR,type="b",ylim=c(0,1),xlim = c(1,0))
plot(alphaList,accList,type="b")
qda.pred.class = rep("Patient",nrow(test))
qda.pred.class[qda.pred$posterior[,1]>0.9]="Healthy control"
#Confusion matrix
table(qda.pred.class,test$YNClassification)
#Accuracy
mean(qda.pred.class==test$YNClassification)
#ROC curve
ROC.qda=roc(YNClassification,qda.pred$posterior[,1],levels=c("Healthy control","Patient"), thresholds=seq(0.1,1,0.1))
plot.roc(ROC.qda,print.auc =T,xlab="Specificity",col="red",axes=T)
### Decision trees
tree.dataset=tree(YNClassification ~ Age+BMI+Glucose+Insulin+HOMA+Leptin+Adiponectin+Resistin+MCP.1,dataset ,subset=train_ind)
summary(tree.dataset)
plot(tree.dataset)
text(tree.dataset,pretty=0)
dataset.test=dataset[-train_ind,]
dataset.train=dataset[train_ind,]
YNClassification.test=YNClassification[-train_ind]
tree.pred=predict(tree.dataset, dataset.test,type="class")
table(tree.pred,YNClassification.test)
##Cross validation
cv.dataset=cv.tree(tree.dataset, FUN=prune.misclass, K=5)
plot(cv.dataset$size ,cv.dataset$dev ,type='b')
prune.dataset=prune.misclass(tree.dataset, best=3)
summary(prune.dataset)
plot(prune.dataset)
text(prune.dataset,pretty=0)
tree.pred=predict(prune.dataset,dataset.test, type="class")
table(tree.pred,YNClassification.test)
plot(tree.pred)
#Confusion matrix
#Accuracy
mean(tree.dataset[["y"]]==test$YNClassification)
### Support Vector Machine
set.seed(1)
#Feature Selection
regfit.full = regsubsets(YNClassification~.,dataset[,c(0:9)],nvmax=20,method="exhaustive")
summary(regfit.full)
reg.summary = summary(regfit.full)
reg.summary$adjr2
par(mfrow=c(2,2))
plot(reg.summary$rss,xlab="Number of Variables",ylab="RSS",type="l")
plot(reg.summary$adjr2,xlab="Number of Variables",ylab="Adjusted RSq",type="l")
p=which.max(reg.summary$adjr2)
paste("Adjusted RSq Best Number of Variable",p)
points(p,reg.summary$adjr2[p], col="red",cex=2,pch=20)
plot(reg.summary$cp,xlab="Number of Variables",ylab="Cp",type='l')
p=which.min(reg.summary$cp)
paste("Cp Best Number of Variable",p)
points(p,reg.summary$cp[p],col="red",cex=2,pch=20)
p=which.min(reg.summary$bic)
paste("Cp Number of Variable",p)
plot(reg.summary$bic,xlab="Number of Variables",ylab="BIC",type='l')
points(p,reg.summary$bic[p],col="red",cex=2,pch=20)