大家好,我也碰到楼主的问题,结果不稳定。
我的代码如下:
rawdata<-read.csv("rawdata.csv",header = TRUE)
dev = rawdata[rawdata$category==0,]
vad = rawdata[rawdata$category==1,]
write.csv(dev, "dev.csv")
write.csv(vad, "vad.csv")
sapply(dev[,c(1:24)],function(x)which(is.na(x)))
dev<-dev[which(dev$SBP!="NA"),]
dev<-dev[which(dev$DBP!="NA"),]
dev<-dev[which(dev$TC!="NA"),]
dev<-dev[which(dev$positive.ANA!="NA"),]
for (i in names(dev)[c(5,15:17)]){dev[,i] <- as.factor(dev[,i])}
xfactors <- model.matrix(dev$MN~ dev$male + dev$microhematuria + dev$DM + dev$positive.ANA)[,-1]
x <- as.matrix(data.frame(dev[,c(6:14,18:24)], xfactors))
write.csv(x,file='xdata.csv')
y <- dev[,4]
fit<-glmnet(x,y,alpha=1,family='binomial')
cv.fit <- cv.glmnet(x,y,alpha=1)
cv.fit$lambda.min
Coefficients <- coef(fit, s = cv.fit$lambda.min)
Active.Index <- which(Coefficients != 0)
Active.Coefficients <- Coefficients[Active.Index]
Active.Index
Active.Coefficients
row.names(Coefficients)[Active.Index]
自己试着在代码前加set.seed(n), 结果就稳定了,但是不同的n值,结果又不一样。