LASSO回归:使用L1正则化控制过拟合

发布于 2022-08-10  124 次阅读


安装补充包

LASSO回归

library(glmnet)
library(survival)
dat <- readRDS('../../../COXPH/data/PRAD.rds')
y <- Surv(round(dat$meta$pfs_time,2), dat$meta$pfs_status == 1)
y <- data.matrix(y)
pfg <- readRDS('../../fig.2/D_ref_A_fiig.2_D/x.rds')
x <- dat$data[,pfg$RRA]
x <- as.matrix(x)
# x <- model.matrix(~.,clinical)
lasso <- glmnet(x = x, y = y, family = 'cox', alpha = 1)

LASSO图确定λ与变量个数的关系

lasso
pdf(file = 'lasso_p1.pdf');plot(lasso, xvar="lambda", label=T);dev.off()
lasso.coef <- predict(lasso, s = 0.039720, type = 'coefficients')
lasso.coef

交叉验证选择λ

set.seed(0)
lasso.cv <- cv.glmnet(x,y,family="cox", alpha=1,nfolds=10)
pdf(file = 'lasso_cv.pdf');plot(lasso.cv);dev.off()
coef(lasso.cv, s="lambda.min")
coef(lasso.cv, s="lambda.1se")
lasso.cv$lambda.min
lasso.cv$lambda.1se

其他交叉验证评价指标

set.seed(0)
lasso.cv_auc <- cv.glmnet(x,y,family="cox", alpha=1,nfolds=10, measure = 'auc')
plot(lasso.cv_auc)

医学生