R 中同时进行多个 LASSO 回归

Simultaneously multiple LASSO regressions in R

我正在尝试在 R 中执行多个 LASSO 回归。要计算模型的系数,我使用以下代码

library(glmnet)
A <- as.matrix(data)
fit_lasso <- glmnet(A[,-1] , A[,1] , standardize = TRUE , alpha = 0.9) #LASSO model
print(fit_lasso) #LASSO model for different lambdas

cvfit <- cv.glmnet( A[,-1] , A[,1] , standardize = TRUE , type.measure = "mse" , nfolds = 5 , alpha = 0.9) 
cvfit    
cvfit$lambda.min
coef(cvfit , s = "lambda.min") 

以下结果(除其他外)

> coef(cvfit , s = "lambda.min")
15 x 1 sparse Matrix of class "dgCMatrix"
                        1
(Intercept) -4.455556e+02
X2           .           
X3           2.869015e-05
X4           2.325949e-10
X5           .           
X6           5.955569e+00
X7           .           
X8           1.043362e+01
X9           .           
X10          3.313007e-01
X11          .           
X12          .           
X13          .           
X14          2.129794e-01
X15          .     

glmnet(A[,-1] , A[,1] , ...) 语句 A[,-1] 中符号化所有解释性 X 变量和 A[,1] 响应 Y 变量。我想创建一个循环,为所有可能的变量组合计算并显示与上述相同的结果(例如,第一列作为响应变量,所有其他列作为解释变量,第二列作为响应变量,所有其他列作为解释性的)。使用 for statement 我设法创建了以下内容,但似乎不起作用。有人可以帮我解决吗?

library(readxl)
data <-read_excel("example.xlsx")
data


library(glmnet)
A <- as.matrix(data)
for(i in 1:ncol(data)) fit_lasso[i] <- glmnet(A[,-i] , A[,i] , standardize = TRUE , alpha = 0.9)


for(i in 1:ncol(data)) cvfit[i] <- cv.glmnet( A[,-i] , A[,i] , standardize = TRUE , type.measure = "mse" , nfolds = 5 , alpha = 0.9) 


cvfit$lambda.min

coef(cvfit[i] , s = "lambda.min") 

试试这个:

results <- lapply(seq_len(ncol(A)), function(i) {
  list(
    fit_lasso = glmnet(A[, -i], A[, i], standardize = T, alpha = 0.9), 
    cvfit = cv.glmnet(A[, -i] , A[, i] , standardize = TRUE , type.measure = "mse" , nfolds = 5 , alpha = 0.9)
  )
})

获取一组结果:

# Must use "[[" and "]]" here. 
results[[3L]]$cvfit$lambda.min
coef(results[[3L]]$cvfit, s = "lambda.min") 

输出

> results[[3L]]$cvfit$lambda.min
[1] 1.542775
> coef(results[[3L]]$cvfit, s = "lambda.min") 
11 x 1 sparse Matrix of class "dgCMatrix"
                      1
(Intercept)  52.7322579
mpg           .        
cyl          15.1087471
hp            0.5848973
drat          .        
wt           72.9452152
qsec         -9.1803140
vs          -11.6195183
am            .        
gear          .        
carb        -23.8347410