真是神了奇了,还能被审稿人问到Lasso系数的置信区间的信息,还好有现成的工具可以计算
# load library
library(selectiveInference)
library(xlsx)
library(glmnet)
# load data
setwd("E:\\UAI_Program\\2-ZhongshanHospital\\12-xiaoyuyao系数置信区间")
Data <- read.xlsx("R.xlsx",sheetIndex=1)
dim(Data)
x <- as.matrix(Data[,-1])
y <- as.matrix(Data[,1])
# lasso anallysis
set.seed(100)
cvfit <- cv.glmnet(x, y, type.measure = "mse", nfolds = 10, alpha = 1)
lambda <- cvfit $ lambda.min
Variable <- coef(cvfit$glmnet.fit,s=lambda,exact = F)@Dimnames[[1]]
coefficients <- coef(cvfit$glmnet.fit,s=lambda ,exact = F)
COl <- which(coefficients!=0) #系数不为0的特征索引
VariableLast <- data.frame(Factor=Variable[COl],coefficients[COl])
# lasso models for CI
x <- as.matrix(Data[,which(names(Data) %in% VariableLast[-1,1])])
set.seed(100)
cvfit2 <- glmnet(x, y, type.measure = "mse", nfolds = 10, alpha = 1,family='gaussian',standardize=T)
beta <- coef(cvfit2, x=x, y=y, s=lambda/nrow(Data), exact=T)[-1]
out <- fixedLassoInf(x,y,beta,lambda,sigma=1,family='gaussian')
out