Skip to content

Latest commit

 

History

History
66 lines (58 loc) · 2.36 KB

1_FS.md

File metadata and controls

66 lines (58 loc) · 2.36 KB

Elastic Net using

library(caret)
library(glmnet)

links:

  1. StatQues
  2. Manuscript on Elastic-Net Regression
  3. North Carolina State University
  4. Kaggle1
  5. Kaggle2
  6. Kaggle3
  7. Kaggle4
  8. Kaggle5
  9. Kaggle6
  10. Book1
  11. Book2
  12. Book3
  13. Tuning Elastic Net

code-1

cl <- makeCluster(detectCores()-1)
registerDoParallel(cl)
# Set multiple seeds
multiple_seeds <- function(seed) {
  set.seed(seed)
  list.of.fits <- list()
  for (i in 0:100) {
    fit.name <- paste0("alpha", i/100)
    list.of.fits[[fit.name]] <- cv.glmnet(
      x.train,
      y.train,
      alpha = i/100,
      standardize = TRUE,
      nfolds = 10,
      type.measure = "class",
      family = "binomial",
      parallel = TRUE
    )
  }
  
  results_df <- data.frame()
  
  for (fit.name in names(list.of.fits)) {
    fit <- list.of.fits[[fit.name]]
    lambda <- fit$lambda
    measure <- fit$cvm
    fit_df <- data.frame(seed = seed, alpha = fit.name, lambda = lambda, measure = measure)
    results_df <- rbind(results_df, fit_df)
  }
  
  return(results_df)
}

#total_seeds <- 10
results_list <- list()

for (s in 1:100) {
  results_list[[s]] <- multiple_seeds(s)
}
results_df <- do.call(rbind, results_list)
stopCluster(cl)
write.csv(results_df,"results_df.csv")