title: How can you obtain same coefficients with different set.seed in elasticnet in R?
body: <p>This question was closed at CrossValidation as it focussed on programming, so it is more suitable here:</p>
  <p>I am running an elastic-net logistic regression on my data. I have looked into how to get replicable coefficients every time I run the same model on the same data. But, it does not seem to happen. I have tried to set <code>nfolds</code> and <code>foldid</code> but once I change the <code>set.seed</code> the coefficients change.</p>
  <p>I understand, how the cross-validation works and how the <code>set.seed</code> potentially change the whole ting. Some have suggested setting the <code>foldid</code> as done in my code, but it does not help in my case once the <code>set.seed</code> changes.</p>
  <p>What are the possibilities of getting the same coefficients for every run or statistically sound measure for the model coefficients?</p>
  <pre><code>df &lt;- read_csv(&quot;data.csv&quot;)
  View(df)
  
  set.seed(123)
  library(caret)
  library(tidyverse)
  library(glmnet)
  library(ROCR)
  library(doParallel)
  registerDoParallel(4, cores = 4)
  training.samples &lt;- df$V1 %&gt;% createDataPartition(p = 0.8, list = FALSE)
  train &lt;- df[training.samples, ]
  test &lt;- df[-training.samples, ]
  x.train &lt;- data.frame(train[, names(train) != &quot;V1&quot;])
  x.train &lt;- data.matrix(x.train)
  y.train &lt;- train$V1
  x.test &lt;- data.frame(test[, names(test) != &quot;V1&quot;])
  x.test &lt;- data.matrix(x.test)
  y.test &lt;- test$V1
  foldid &lt;- sample(rep(seq(10), length.out = nrow(train)))
   
  list.of.fits &lt;- list()
  for (i in 0:10){
      fit.name &lt;- paste0(&quot;alpha&quot;, i/10) 
      list.of.fits[[fit.name]] &lt;- cv.glmnet(x.train, y.train, type.measure = &quot;dev&quot;,
      alpha = i/10, family = &quot;binomial&quot;, nfolds = 10, foldid = foldid, parallel = TRUE)
  }
  coef &lt;- coef(list.of.fits[[fit.name]], s = list.of.fits[[fit.name]]$lambda.1se)
  coef
  </code></pre>
  <p>My output ends up like this:</p>
  <pre><code>set.seed(123)
  
  (Intercept) -18.533050
  V2          -0.0049142
  V3          -0.0013228
  V4          -0.0029664
  V5           0.0123987
  V6           0.1433817
  V7           .           
  V8          -0.0188888
  V9           0.0007504
  V10         -0.0626482
  set.seed(42)
  
  (Intercept) -22.16271709
  V2          -0.005898701
  V3          -0.001332854
  V4          -0.003506514
  V5           0.013343484
  V6           0.097911065
  V7          -0.269346185
  V8          -0.024876785
  V9           0.027937690
  V10         -0.070759818
  </code></pre>