Switch to unified view

a b/Linear-Lasso-Classifiers.R
1
2
rm(list = ls())
3
gc()
4
5
library("glmnet")
6
library("pROC")
7
library("readxl")
8
library("nnet")
9
10
11
workbook<-"Feature matrix extracted by BigbiGAN from yout training dataset."
12
Sta = read_excel(workbook,1)
13
14
workbook1<-"Feature matrix extracted by BigbiGAN from yout test dataset."
15
Sta1 = read_excel(workbook1,1)
16
17
workbook2<-"Feature matrix extracted by BigbiGAN from yout validation dataset."
18
Sta2 = read_excel(workbook2,1)
19
20
workbook3<-"Feature matrix extracted by BigbiGAN from yout external validation dataset."
21
Sta3 = read_excel(workbook3,1)
22
23
Train<-data.frame(Sta)
24
Tval<-data.frame(Sta1)
25
Test<-data.frame(Sta2)
26
ExterV<-data.frame(Sta3)
27
28
29
FeaNumTrain<-"Dimension of semantic features extracted by BigBiGAN"
30
FeaNumTest<-"Dimension of semantic features extracted by BigBiGAN"
31
FeaNumTval<-"Dimension of semantic features extracted by BigBiGAN"
32
FeaNumExtV<-"Dimension of semantic features extracted by BigBiGAN"
33
34
35
36
TrainFea<-data.frame(Train)
37
TvalFea<-data.frame(Tval)
38
TestFea<-data.frame(Test)
39
ExterVFea<-data.frame(ExterV)
40
41
42
43
Trainy<-Train[(ncol(Train)-FeaNumTrain):(ncol(Train))] ##Features are stored at the end of the matrix.
44
Trainy<-data.frame(Trainy)
45
46
##Linear Classifier
47
fm <- lm("COVID"~., data=Trainy)
48
summary(fm)
49
50
lmpred_Val<-predict(fm,Tval)
51
Roc_Val<-roc(Tval$"COVID",lmpred_Val)
52
auc(Roc_Val)
53
plot(Roc_Val, print.auc=TRUE, auc.polygon=TRUE, grid=c(0.1, 0.2),
54
     grid.col=c("green", "red"), max.auc.polygon=TRUE,
55
     auc.polygon.col="skyblue", print.thres=TRUE)
56
57
lmpred_Test<-predict(fm,Test)
58
Roc_Test<-roc(Test$"COVID",lmpred_Test)
59
auc(Roc_Test)
60
plot(Roc_Test, print.auc=TRUE, auc.polygon=TRUE, grid=c(0.1, 0.2),
61
     grid.col=c("green", "red"), max.auc.polygon=TRUE,
62
     auc.polygon.col="skyblue", print.thres=TRUE)
63
64
lmpred_Ext<-predict(fm,ExterVFea)
65
Roc_Ext<-roc(ExterVFea$"COVID",lmpred_Ext)
66
auc(Roc_Ext)
67
plot(Roc_Ext, print.auc=TRUE, auc.polygon=TRUE, grid=c(0.1, 0.2),
68
     grid.col=c("green", "red"), max.auc.polygon=TRUE,
69
     auc.polygon.col="skyblue", print.thres=TRUE)
70
71
72
##Lasso Classifier
73
y<- Train$COVID
74
TrainFea_Lasso <- TrainFea
75
names(TrainFea_Lasso) <- NULL
76
TrainFea_Lasso<-data.matrix(TrainFea_Lasso)
77
fit<-glmnet(TrainFea_Lasso,y,alpha=1,family='binomial')
78
plot(fit, xvar = "lambda", label = TRUE)
79
cv.fit<-cv.glmnet(TrainFea_Lasso,y,family="binomial")
80
81
lmpred_Train <-predict(fit,type="response",newx = TrainFea_Lasso,s=cv.fit$lambda.1se)
82
Roc_Train<-roc(Train$COVID,lmpred_Train)
83
auc(Roc_Train)
84
85
TvalFea_Lasso <- TvalFea
86
names(TvalFea_Lasso) <- NULL
87
TvalFea_Lasso<-data.matrix(TvalFea_Lasso)
88
lmpred_Tval <-predict(fit,type="response",newx = TvalFea_Lasso,s=cv.fit$lambda.1se)
89
Roc_Tval<-roc(Tval$COVID,lmpred_Tval)
90
auc(Roc_Tval)
91
92
TestFea_Lasso <- TestFea
93
names(TestFea_Lasso) <- NULL
94
TestFea_Lasso<-data.matrix(TestFea_Lasso)
95
lmpred_Test <-predict(fit,type="response",newx = TestFea_Lasso,s=cv.fit$lambda.1se)
96
Roc_Test<-roc(Test$COVID,lmpred_Test)
97
auc(Roc_Test)
98
99
ExterFea_Lasso <- ExterVFea
100
names(ExterFea_Lasso) <- NULL
101
ExterFea_Lasso<-data.matrix(ExterFea_Lasso)
102
lmpred_Ext <-predict(fit,type="response",newx = ExterFea_Lasso,s=cv.fit$lambda.min)
103
Roc_Ext<-roc(ExterV$COVID,lmpred_Ext)
104
auc(Roc_Ext)
105