這是2014年發表在nature communication上的一張圖,大意如下:
a:作者想用181個假基因預測癌癥亞型,將306個樣本分成訓練集(223個),驗證集(83個)
b: 用訓練集的數據(n=223)進行分析,采用五折交叉驗證避免過擬合,選用隨機森林,支持向量機,邏輯回歸三種機器學習方法進行分類。
c: 三種機器學習方法的AUC曲線,發現SVM的效果最好。(AUC=0.962)
d: 用整個訓練集(n=223)對驗證集(n=83)進行分析,用SVM進行分類,AUC為0.922。
(原文題目:The Pan-Cancer analysis of pseudogene expression reveals biologically and clinically relevant tumour subtypes)
我把這個思路搬運到我的課題里,復現圖如下:
相似度:75%。a和b是用AI畫的,所以今天分享下c和d的代碼
一 差異分析
假設我們有了這樣的一個表達矩陣,叫Expr_all。以及表示表型數據的temp (character)。差異分析你可以用DESeq,但本次我就用T檢驗。
temp
##轉置
Expr_all<-t(Expr_all)
##將每列變成numeric
Expr_all[,1]<-as.numeric(Expr_all[,1])
for(i in 2:535){Expr_all[,i]<-as.numeric(Expr_all[,i])}
##T檢驗
pvalue<-sapply(1:266,function(i){t.test(Expr_all[which(temp=="cancer"),i],Expr_all[which(temp=="normal"),i])[[3]]})
##將T檢驗得到的P值進行矯正,降低假陽性
pvalue1<-p.adjust(pvalue1)
##挑出P<0.05的基因
t_expr<-Expr_all[,which(pvalue1<0.05)]
t_expr<-as.data.frame(t_expr)
t_expr<-t(t_expr)
t_expr<-cbind(temp,t_expr)
得到的t_expr矩陣如下
t_expr
二 構建三種機器學習模型
1. 劃分訓練集和驗證集
library(randomForest)
library(caret)
library(pROC)
library(caret)
inTrain<-createDataPartition(y=t_expr[,1],p=0.25,list=F)
test<-t_expr[inTrain,]
train<-t_expr[-inTrain,]
2. 構建五折交叉驗證RF模型
folds<-createFolds(y=train[,1],k=5)
fc<-as.numeric()
mod_pre<-as.numeric()
for(i in 1:5){
fold_test<-train[folds[[i]],]
fold_train<-train[-folds[[i]],]
model<-randomForest(temp~.,data=fold_train,proximity=T,importance=T)
model_pre<-predict(model,newdata = fold_test,type="prob")
fc<-append(fc,as.numeric(fold_test$temp))
mod_pre<-append(mod_pre,model_pre[,1])
}
df<-cbind(fc,as.numeric(mod_pre))
3. 構建五折交叉驗證SVM模型
fc<-as.numeric()
mod_pre<-as.numeric()
for(i in 1:5){
fold_test<-t_expr[folds[[i]],]
fold_train<-t_expr[-folds[[i]],]
model<-svm(temp~.,data=fold_train,probability=T)
model_pre<-predict(model,newdata = fold_test,decision.values = TRUE, probability = TRUE)
fc<-append(fc,as.numeric(fold_test$temp))
mod_pre<-append(mod_pre,as.numeric(attr(model_pre, "probabilities")[,2]))
}
df<-cbind(df,cbind(fc,mod_pre))
4. 構建五折交叉驗證LR模型
max=0
num=0
fc<-as.numeric()
mod_pre<-as.numeric()
for(i in 1:5){
fold_test<-train[folds[[i]],]
fold_train<-train[-folds[[i]],]
model<-glm(temp~.,family=binomial(link=logit),data=fold_train)
model_pre<-predict(model,type='response',newdata=fold_test)
fc<-append(fc,fold_test$temp)
mod_pre<-append(mod_pre,as.numeric(model_pre))
}
df<-cbind(df,cbind(fc,mod_pre))
5. 畫三者的AUC曲線
pdf("/Users/baiyunfan/desktop/ROC.pdf",height=6,width=6)
mycol <- c("slateblue","seagreen3","dodgerblue","firebrick1","lightgoldenrod","magenta","orange2")
x<-plot.roc(df[,1],df[,2],
smooth=F,
lwd=2,
ylim=c(0,1),
xlim=c(1,0),
legacy.axes=T,
main="",
col=mycol[2])
x<-plot.roc(df[,3],df[,4],
smooth=F,
add=T,
lwd=2,
ylim=c(0,1),
xlim=c(1,0),
legacy.axes=T,
main="",
col=mycol[3])
x<-plot.roc(df[,5],df[,6],
smooth=F,
add=T,
lwd=2,
ylim=c(0,1),
xlim=c(1,0),
legacy.axes=T,
main="",
col=mycol[4])
legend.name <- c(paste("RF","AUC",0.9836,sep=" "),paste("SVM","AUC",0.9903,sep=" "),paste("LR","AUC",0.9986,sep=" "))
legend("bottomright",
legend=legend.name,
col = mycol[2:4],
lwd = 2,
bty="n")
dev.off()
發現LR的AUC最高,于是單拿出來用整個train對test畫
6. 用整個訓練集對驗證集建立LR模型以及AUC曲線
model<-glm(temp~.,data=train,family=binomial(link=logit))
model_pre<-predict(model,type='response',newdata=test)
pdf("/Users/baiyunfan/desktop/RF.pdf",height=6,width=6)
x<-plot.roc(test[,1],model_pre,
smooth=F,
lwd=2,
ylim=c(0,1),
xlim=c(1,0),
legacy.axes=T,
main="",
col=mycol[4])
legend.name <- paste("LR","AUC",0.9557,sep=" ")
legend("bottomright",
legend=legend.name,
col = mycol[4],
lwd = 2,
bty="n")
dev.off()
AUC掉下來了,0.9557,大家來探討下為什么吧~