Home > Software engineering >  Output is lagging when trying to get lambda and alpha values after running Elastic-Net Regression Mo
Output is lagging when trying to get lambda and alpha values after running Elastic-Net Regression Mo

Time:03-01

I am new to R and Elastic-Net Regression Model. I am running Elastic-Net Regression Model on the default dataset, titanic. I am trying to obtain the Alpha and Lambda values after running the train function. However when I run the train function, the output keeps on lagging and I had to wait for the output but there is no output at all. it is empty.... I am trying Tuning Parameters.

data(Titanic)
example<- as.data.frame(Titanic)
example['Country'] <- NA
countryunique <- array(c("Africa","USA","Japan","Australia","Sweden","UK","France"))
new_country <- c()
#Perform looping through the column, TLD
for(loopitem in example$Country)
{
    #Perform random selection of an array, countryunique 
    loopitem <- sample(countryunique, 1)
    #Load the new value to the vector
    new_country<- c(new_country,loopitem)
}
#Override the Country column with new data
example$Country<- new_country

example$Class<- as.factor(example$Class)
example$Sex<- as.factor(example$Sex)
example$Age<- as.factor(example$Age)
example$Survived<- as.factor(example$Survived)
example$Country<- as.factor(example$Country)
example$Freq<- as.numeric(example$Freq)

set.seed(12345678)
trainRowNum <- createDataPartition(example$Survived, #The outcome variable
#proportion of example to form the training set
p=0.3,
#Don't store the result in a list
list=FALSE);
# Step 2: Create the training mydataset
trainData <- example[trainRowNum,]
# Step 3: Create the test mydataset
testData <- example[-trainRowNum,]

alphas <- seq(0.1,0.9,by=0.1);
lambdas <- 10^seq(-3,3,length=100) 
#Logistic Elastic-Net Regression
en <- train(Survived~. , 
            data = trainData, 
            method = "glmnet", 
            preProcess = NULL,
            trControl = trainControl("repeatedcv",
                        number = 10,
                        repeats = 5),
            tuneGrid = expand.grid(alpha = alphas,
                                   lambda = lambdas)
)

Could you please kindly advise on what values are recommended to assign to Alpha and lambda? Thank you

CodePudding user response:

I'm not quite sure what the problem is. Your code runs fine for me. If I look at the en object it says:

Accuracy was used to select the optimal model using the
 largest value.
The final values used for the model were alpha = 0.1 and lambda
 = 0.1.

It didn't take long to run for me. Do you have a lot stored in your R session memory that could be slowing down your system and causing it to lag? Maybe try re-starting RStudio and running the above code from scratch.

To see the full results table with Accuracy for all combinations of Alpha and Lambda, look at en$results

As a side-note, you can easily carry out cross-validation directly in the glmnet package, using the cv.glmnet function. A helper package called glmnetUtils is also available, that lets you select the optimal Alpha and Lambda values simultaneously using the cva.glmnet function. This allows for parallelisation, so may be quicker than doing the cross-validation via caret.

  • Related