forked from Shicheng-Guo/GscRbasement
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMLG.R
54 lines (49 loc) · 1.88 KB
/
MLG.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
library("randomForest")
library("arm")
library("plyr")
library("PredictABEL")
library("neuralnet")
input<-newinput
set.seed(49)
cv.error <- NULL
k <- 10
pbar <- create_progress_bar('text')
pbar$init(k)
rlt1<-c()
rlt2<-c()
for(i in 1:k){
index <- sample(1:nrow(input),round(0.9*nrow(input)))
train.cv <- input[index,]
test.cv <- input[-index,]
P=apply(input[,2:ncol(input)],2,function(x) summary(glm(as.factor(input[,1])~x,family=binomial))$coefficients[2,4])
train.cv<-train.cv[,c(1,which(P<0.05/length(P))+1)]
RF <- randomForest(as.factor(phen) ~ ., data=train.cv, importance=TRUE,proximity=T)
imp<-RF$importance
head(imp)
imp<-imp[order(imp[,4],decreasing = T),]
write.table(imp,file=paste("RandomForest.VIP.",i,".txt",sep=""),sep="\t",quote=F,row.names = T,col.names = NA)
topvar<-match(rownames(imp)[1:30],colnames(input))
train.cv <- input[index,c(1,topvar)]
test.cv <- input[-index,c(1,topvar)]
n <- colnames(train.cv)
f <- as.formula(paste("phen ~", paste(n[!n %in% "phen"], collapse = " + ")))
nn <- neuralnet(f,data=train.cv,hidden=c(10,3),act.fct = "logistic",linear.output = T)
pr.nn <- neuralnet::compute(nn,test.cv)
trainRlt<-data.frame(phen=train.cv[,1],pred=unlist(nn$net.result[[1]][,1]))
testRlt<-data.frame(phen=test.cv[,1],pred=unlist(pr.nn$net.result[,1]))
rownames(trainRlt)=row.names(train.cv)
rownames(testRlt)=row.names(test.cv)
rlt1<-rbind(rlt1,trainRlt)
rlt2<-rbind(rlt2,testRlt)
pbar$step()
print(i)
}
data1<-na.omit(data.frame(rlt1))
data2<-na.omit(data.frame(rlt2))
model.glm1 <- bayesglm(phen~.,data=rlt1,family=binomial(),na.action=na.omit)
model.glm2 <- bayesglm(phen~.,data=rlt2,family=binomial(),na.action=na.omit)
pred1 <- predRisk(model.glm1)
pred2 <- predRisk(model.glm2)
par(mfrow=c(2,2),cex.lab=1.5,cex.axis=1.5)
plotROC(data=data1,cOutcome=1,predrisk=cbind(pred1))
plotROC(data=data2,cOutcome=1,predrisk=cbind(pred2))