rparametersapplyglmnetregularized

Use specific alpha parameters for multiple elastic net in R


I'm trying to perform multiple elastic net simultaneously in R. I have a 10x15 matrix each column is called Xi e.g. X1,X2,...,X15 and perform elastic net in order to obtain the optimal alpha and lambda parameters. Then i save the alpha values in a dataframe called alphas in my environment and looks like the following

alpha
1  0.001
2  0.000
3  0.000
4  0.064
5  0.729
6  0.729
7  1.000
8  0.001
9  0.000
10 0.000
11  0.001
12  0.000
13  0.000
14  0.064
15  0.729

My goal is to perform multiple cv.glmnet (one for each Xi) with the corresponding alpha i already found. e.g. use alpha=0.001 for cv.glmnet model of Xi, etc. How can i replace the alpha value in the below code in order to use all the obtained alpha values?

data<-matrix(rnorm(36),nrow=10,ncol = 15)
colnames(data) <- c("X1", "X2", "X3", "X4", "X5", "X6","X7","X8","X9","X10","X11","X12","X13","X14","X15")
data #random data
library(glmnet)
library(coefplot)

A <- as.matrix(data)
set.seed(1234)
results <- lapply(seq_len(ncol(A)), function(i) {
  list(
    cvfit = cv.glmnet(A[, -i] , A[, i] , standardize = TRUE , type.measure = "mse" , nfolds = 10 , alpha = 1)
  )
})

Solution

  • If we want to do multiple sets, use the same sequence of column index to subset the alphas as the length of alphaset is the same as the number of columns of the matrix 'A'

    alphaset  <- c( 0.001, 0, 0, 0.064, 0.729, 0.729, 1.0, 
               0.001, 0, 0, 0.001, 0, 0,  0.064, 0.729)
    lst_out <- lapply(seq_len(ncol(A)), function(i) {
      list(
         cvfit = cv.glmnet(A[, -i] , A[, i] , standardize = TRUE , 
          type.measure = "mse" , nfolds = 10 , alpha = alphaset[i])
        )
            })