library(glmnet) setwd("") train=read.table("train.txt",header=T,sep="\t",check.names=F,row.names=1) test=read.table("test.txt",header=T,sep="\t",check.names=F,row.names=1) test=test[,colnames(train)] rt=rbind(train,test) x=as.matrix(train[,c(2:ncol(train))]) y=train[,1] fit=glmnet(x, y, family = "binomial", alpha=1) cvfit=cv.glmnet(x, y, family="binomial", alpha=1,type.measure='auc',nfolds =10) pdf(file="cvfit.pdf",width=6,height=5.5) plot(cvfit) dev.off() coef=coef(fit, s = cvfit$lambda.min) index=which(coef != 0) actCoef=coef[index] lassoGene=row.names(coef)[index] coef=cbind(Gene=lassoGene,Coef=actCoef) write.table(coef,file="coef.txt",sep="\t",quote=F,row.names=F) write.table(lassoGene[-1],file="miRNAlist.txt",sep="\t",quote=F,row.names=F,col.names=F) threshold=0.5 bioStat=function(x=null, y=null, dataType=null){ pred=predict(cvfit,newx=x,s=cvfit$lambda.min,type = 'response') outTab=cbind(id=row.names(pred),TMB=as.character(y),pred) colnames(outTab)=c("id","MDR","value") sameGene=intersect(colnames(x),lassoGene) outTab=cbind(outTab,x[,sameGene]) write.table(outTab,file=paste0("value.",dataType,".txt"),sep="\t",quote=F,row.names=F) pred_new=as.integer(pred>threshold) tab=table(pred_new,y) TP=tab[2,2];TN=tab[1,1];FP=tab[2,1];FN=tab[1,2] Accuracy=round((TP+TN)/(TP+FN+FP+TN),4) SE=round(TP/(TP+FN),4) SP=round(TN/(TN+FP),4) PPV=round(TP/(TP+FP),4) NPV=round(TN/(TN+FN),4) rocobj1=roc(y, as.vector(pred)) AUC=auc(rocobj1) AUC=round(AUC,4) return(c(SE,SP,PPV,NPV,Accuracy,AUC)) } trainValue=bioStat(x=x,y=y,dataType="train") testX=as.matrix(test[,c(2:ncol(test))]) testY=test[,1] testValue=bioStat(x=testX,y=testY,dataType="test") totalX=as.matrix(rt[,c(2:ncol(rt))]) totalY=rt[,1] totalValue=bioStat(x=totalX,y=totalY,dataType="all") statTab=rbind(Train=trainValue,Test=testValue,Total=totalValue) colnames(statTab)=c("SE","SP","PPV","NPV","Accuracy","AUC") statTab=rbind(ID=colnames(statTab),statTab) write.table(statTab,file="accuracyStat.xls",sep="\t",col.names=F,quote=F)