R Markdown values keeps changing when knitting as PDF even when using seed()

Hi

I have a issue with R Markdown were the values of my regression keep changing when i knitt as PDF or try to re-run the chunk I have set seed on each chunk. Here is a sample of my work. If someone can advise me where I am going wrong i would be very grateful. Many Thanks

CancerData=data.frame(
      Cl.thickness = c(0.197759762794049, 0.197759762794049,
                       -0.511268678089602, 0.552273983235875,
                       -0.156754457647776, 1.26130242411953, -1.22029711897325,
                       -0.865782898531427, -0.865782898531427, -0.156754457647776,
                       -1.22029711897325, -0.865782898531427, 0.197759762794049,
                       -1.22029711897325, 1.26130242411953, 0.9067882036777, -0.156754457647776,
                       -0.156754457647776, 1.97033086500318, 0.552273983235875,
                       0.9067882036777, 1.97033086500318, -0.511268678089602,
                       -1.22029711897325, 0.197759762794049, -0.511268678089602,
                       0.197759762794049, -0.865782898531427, -1.22029711897325,
                       -0.511268678089602, -0.865782898531427, 1.97033086500318,
                       -0.865782898531427, -0.511268678089602, -0.865782898531427,
                       1.97033086500318, 0.552273983235875, 0.197759762794049,
                       -0.865782898531427, 1.97033086500318, 0.552273983235875,
                       0.197759762794049, 1.97033086500318, -1.22029711897325,
                       -0.511268678089602, -1.22029711897325, -0.156754457647776,
                       0.9067882036777, 1.61581664456135, 0.197759762794049),
         Cell.size = c(-0.701697757221419, 0.277048808160941,
                       -0.701697757221419, 1.58204422867075,
                       -0.701697757221419, 2.23454193892566, -0.701697757221419,
                       -0.701697757221419, -0.701697757221419, -0.375448902093965,
                       -0.701697757221419, -0.701697757221419, -0.049200046966512,
                       -0.701697757221419, 1.2557953735433, 0.277048808160941,
                       -0.701697757221419, -0.701697757221419, 1.2557953735433,
                       -0.701697757221419, -0.049200046966512, 0.603297663288395,
                       -0.701697757221419, -0.701697757221419, -0.375448902093965,
                       -0.375448902093965, -0.701697757221419, -0.701697757221419,
                       -0.701697757221419, -0.701697757221419, -0.701697757221419,
                       1.2557953735433, -0.701697757221419, -0.701697757221419,
                       -0.701697757221419, 2.23454193892566, -0.375448902093965,
                       0.277048808160941, 0.603297663288395, 0.277048808160941,
                       2.23454193892566, 0.929546518415848, 2.23454193892566,
                       -0.701697757221419, 1.2557953735433, -0.701697757221419,
                       -0.701697757221419, 1.58204422867075, 0.603297663288395,
                       -0.049200046966512),
        Cell.shape = c(-0.74123039483746, 0.262590543048829, -0.74123039483746,
                       1.60101846023055, -0.74123039483746, 2.27023241882141,
                       -0.74123039483746, -0.40662341554203, -0.74123039483746,
                       -0.74123039483746, -0.74123039483746, -0.74123039483746,
                       -0.0720164362466006, -0.74123039483746, 0.597197522344259,
                       0.931804501639689, -0.74123039483746, -0.74123039483746,
                       1.26641148093512, -0.74123039483746, -0.40662341554203,
                       0.597197522344259, -0.74123039483746, -0.74123039483746,
                       -0.0720164362466006, -0.74123039483746, -0.74123039483746,
                       -0.74123039483746, -0.0720164362466006, -0.74123039483746,
                       -0.74123039483746, 1.26641148093512, -0.74123039483746,
                       -0.40662341554203, -0.74123039483746, 2.27023241882141,
                       -0.74123039483746, 0.262590543048829, -0.0720164362466006,
                       -0.0720164362466006, 2.27023241882141, 0.597197522344259,
                       2.27023241882141, -0.74123039483746, 1.26641148093512,
                       -0.74123039483746, -0.74123039483746, 1.26641148093512,
                       1.60101846023055, -0.0720164362466006),
     Marg.adhesion = c(-0.638897301750389, 0.757476640955261,
                       -0.638897301750389, -0.638897301750389,
                       0.0592896696024361, 1.8047570979845, -0.638897301750389,
                       -0.638897301750389, -0.638897301750389, -0.638897301750389,
                       -0.638897301750389, -0.638897301750389, 0.0592896696024361,
                       -0.638897301750389, 2.50294406933732, 0.408383155278848,
                       -0.638897301750389, -0.638897301750389, 1.10657012663167,
                       -0.638897301750389, 2.50294406933732, 0.0592896696024361,
                       -0.638897301750389, -0.638897301750389, 0.408383155278848,
                       -0.638897301750389, -0.638897301750389, -0.638897301750389,
                       -0.638897301750389, -0.638897301750389, -0.638897301750389,
                       0.0592896696024361, -0.289803816073976, -0.638897301750389,
                       -0.638897301750389, 1.8047570979845, -0.638897301750389,
                       2.15385058366091, 0.0592896696024361, -0.638897301750389,
                       -0.289803816073976, 1.10657012663167, 0.408383155278848,
                       -0.638897301750389, 0.408383155278848, -0.638897301750389,
                       0.0592896696024361, -0.289803816073976, -0.638897301750389,
                       0.408383155278848),
      Epith.c.size = c(-0.555201605659206, 1.69392470908123,
                       -0.555201605659206, -0.105376342711119,
                       -0.555201605659206, 1.69392470908123, -0.555201605659206,
                       -0.555201605659206, -0.555201605659206, -0.555201605659206,
                       -1.00502686860729, -0.555201605659206, -0.555201605659206,
                       -0.555201605659206, 1.69392470908123, 1.24409944613314,
                       -0.555201605659206, -0.555201605659206, 0.344448920236969,
                       -0.555201605659206, 0.794274183185057, 1.24409944613314,
                       -0.555201605659206, -0.555201605659206, -0.555201605659206,
                       -1.00502686860729, -0.555201605659206, -0.555201605659206,
                       -0.555201605659206, -1.00502686860729, -0.555201605659206,
                       2.14374997202932, -0.555201605659206, -0.555201605659206,
                       -0.555201605659206, 1.24409944613314, -1.00502686860729,
                       -0.555201605659206, 1.24409944613314, -0.105376342711119,
                       2.14374997202932, 3.0434004979255, 2.14374997202932,
                       -0.555201605659206, 0.344448920236969, -0.555201605659206,
                       -0.555201605659206, 0.344448920236969, -0.555201605659206,
                       -0.555201605659206),
       Bare.nuclei = c(-0.698341295402917, 1.77156891336678,
                       -0.423906827761839, 0.124962107520315,
                       -0.698341295402917, 1.77156891336678, 1.77156891336678, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917, -0.149472360120762,
                       -0.149472360120762, 1.4971344457257, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917, 1.77156891336678,
                       -0.698341295402917, 1.77156891336678, 0.948265510443546,
                       -0.698341295402917, -0.698341295402917, 0.948265510443546,
                       -0.698341295402917, -0.698341295402917, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917, -0.698341295402917,
                       0.399396575161392, -0.698341295402917, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917, -0.698341295402917,
                       1.77156891336678, 0.948265510443546, -0.149472360120762,
                       1.77156891336678, -0.698341295402917, -0.698341295402917,
                       -0.698341295402917, 1.4971344457257, -0.698341295402917,
                       -0.698341295402917, 1.22269997808462, -0.149472360120762,
                       0.124962107520315),
       Bl.cromatin = c(-0.181693999725951, -0.181693999725951,
                       -0.181693999725951, -0.181693999725951,
                       -0.181693999725951, 2.26758893079032, -0.181693999725951,
                       -0.181693999725951, -0.998121643231376, -0.589907821478663,
                       -0.181693999725951, -0.589907821478663, 0.226519822026761,
                       -0.181693999725951, 0.634733643779474, 0.226519822026761,
                       -0.589907821478663, -0.181693999725951, 0.226519822026761,
                       -0.181693999725951, 0.634733643779474, 1.4511612872849,
                       -0.589907821478663, -0.181693999725951, -0.181693999725951,
                       -0.589907821478663, -0.589907821478663, -0.589907821478663,
                       -0.998121643231376, -0.589907821478663, -0.181693999725951,
                       1.4511612872849, -0.181693999725951, -0.589907821478663,
                       -0.589907821478663, 1.85937510903761, 1.4511612872849,
                       0.634733643779474, 1.4511612872849, 1.04294746553219, 1.4511612872849,
                       -0.181693999725951, 1.85937510903761, -0.589907821478663,
                       0.226519822026761, -0.589907821478663,
                       -0.181693999725951, -0.181693999725951, -0.589907821478663,
                       -0.181693999725951),
   Normal.nucleoli = c(-0.612478497036736, -0.284896027595788,
                       -0.612478497036736, 1.35301631960895,
                       -0.612478497036736, 1.35301631960895, -0.612478497036736,
                       -0.612478497036736, -0.612478497036736, -0.612478497036736,
                       -0.612478497036736, -0.612478497036736, 0.370268911286108,
                       -0.612478497036736, 0.697851380727057, 0.0426864418451602,
                       -0.612478497036736, -0.612478497036736, -0.612478497036736,
                       -0.612478497036736, 0.370268911286108, 2.3357637279318,
                       -0.612478497036736, -0.612478497036736, 1.025433850168,
                       -0.612478497036736, -0.612478497036736, -0.612478497036736,
                       -0.612478497036736, -0.612478497036736, -0.612478497036736,
                       0.370268911286108, -0.612478497036736, -0.612478497036736,
                       -0.612478497036736, 2.00818125849085, -0.612478497036736,
                       1.025433850168, 0.697851380727057, 0.697851380727057,
                       0.0426864418451602, -0.612478497036736, 2.3357637279318,
                       -0.612478497036736, 1.6805987890499, -0.612478497036736,
                       -0.612478497036736, 1.6805987890499, -0.612478497036736,
                       0.370268911286108),
           Mitoses = c(-0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, 1.96042569442479, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, 1.38328313007474, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, 0.22899800137462,
                       -0.348144562975438, 1.38328313007474, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       0.806140565724679, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, 0.22899800137462,
                       0.806140565724679, -0.348144562975438, -0.348144562975438,
                       0.22899800137462, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, 0.22899800137462, 1.96042569442479,
                       -0.348144562975438),
                 y = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
                       0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1,
                       1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1)
)

 X1orig = CancerData[,1:9]
 X1 = scale(X1orig)
 
## Pick out response variable
y = CancerData[,10]
p = ncol(CancerData) - 1

## Fit model to full data
library(glmnet)
set.seed(1)
## Choose grid of values for the tuning parameter
grid = 10 ^ seq(-3, 0, length.out = 100)
ridge = glmnet(
  as.matrix(CancerData[1:9]),
  #BreastCancer[,10],
  y,
  alpha = 0,
  standardize = FALSE,
  lambda = grid
)

## Show how coefficients vary with the tuning parameter
plot(ridge,
     xvar = "lambda",
     col = rainbow(p),
     label = TRUE)

Hello there! Before diving into the code too abruptly, I notice that your set.seed(1) code is commented out; essentially, R is skipping that particular line. I would suggest trying a number with more than a single digit - there are no clear rules/norms about the psudo-RNGs and setting seeds but just some friendly advice!.

1 Like

Thanks! I have been trying things out and I forgot to uncomment the line. Can I set seed once on the top of my code or do need to set seed at every chuck where I am using the glmnet function. I have several regression within the same code.

1 Like
library(glmnet)
#> Loading required package: Matrix
#> Loaded glmnet 3.0-1
# helper function, assuming same tuning parameters

make_ridge <- function(x){
   set.seed(137)
   grid = 10 ^ seq(-3, 0, length.out = 100)
   ridge = glmnet(
      as.matrix(x), y, alpha = 0, standardize = FALSE, lambda = grid)
}

# Your data, scroll down to see application of make_ridge

CancerData=data.frame(
      Cl.thickness = c(0.197759762794049, 0.197759762794049,
                       -0.511268678089602, 0.552273983235875,
                       -0.156754457647776, 1.26130242411953, -1.22029711897325,
                       -0.865782898531427, -0.865782898531427, -0.156754457647776,
                       -1.22029711897325, -0.865782898531427, 0.197759762794049,
                       -1.22029711897325, 1.26130242411953, 0.9067882036777, -0.156754457647776,
                       -0.156754457647776, 1.97033086500318, 0.552273983235875,
                       0.9067882036777, 1.97033086500318, -0.511268678089602,
                       -1.22029711897325, 0.197759762794049, -0.511268678089602,
                       0.197759762794049, -0.865782898531427, -1.22029711897325,
                       -0.511268678089602, -0.865782898531427, 1.97033086500318,
                       -0.865782898531427, -0.511268678089602, -0.865782898531427,
                       1.97033086500318, 0.552273983235875, 0.197759762794049,
                       -0.865782898531427, 1.97033086500318, 0.552273983235875,
                       0.197759762794049, 1.97033086500318, -1.22029711897325,
                       -0.511268678089602, -1.22029711897325, -0.156754457647776,
                       0.9067882036777, 1.61581664456135, 0.197759762794049),
         Cell.size = c(-0.701697757221419, 0.277048808160941,
                       -0.701697757221419, 1.58204422867075,
                       -0.701697757221419, 2.23454193892566, -0.701697757221419,
                       -0.701697757221419, -0.701697757221419, -0.375448902093965,
                       -0.701697757221419, -0.701697757221419, -0.049200046966512,
                       -0.701697757221419, 1.2557953735433, 0.277048808160941,
                       -0.701697757221419, -0.701697757221419, 1.2557953735433,
                       -0.701697757221419, -0.049200046966512, 0.603297663288395,
                       -0.701697757221419, -0.701697757221419, -0.375448902093965,
                       -0.375448902093965, -0.701697757221419, -0.701697757221419,
                       -0.701697757221419, -0.701697757221419, -0.701697757221419,
                       1.2557953735433, -0.701697757221419, -0.701697757221419,
                       -0.701697757221419, 2.23454193892566, -0.375448902093965,
                       0.277048808160941, 0.603297663288395, 0.277048808160941,
                       2.23454193892566, 0.929546518415848, 2.23454193892566,
                       -0.701697757221419, 1.2557953735433, -0.701697757221419,
                       -0.701697757221419, 1.58204422867075, 0.603297663288395,
                       -0.049200046966512),
        Cell.shape = c(-0.74123039483746, 0.262590543048829, -0.74123039483746,
                       1.60101846023055, -0.74123039483746, 2.27023241882141,
                       -0.74123039483746, -0.40662341554203, -0.74123039483746,
                       -0.74123039483746, -0.74123039483746, -0.74123039483746,
                       -0.0720164362466006, -0.74123039483746, 0.597197522344259,
                       0.931804501639689, -0.74123039483746, -0.74123039483746,
                       1.26641148093512, -0.74123039483746, -0.40662341554203,
                       0.597197522344259, -0.74123039483746, -0.74123039483746,
                       -0.0720164362466006, -0.74123039483746, -0.74123039483746,
                       -0.74123039483746, -0.0720164362466006, -0.74123039483746,
                       -0.74123039483746, 1.26641148093512, -0.74123039483746,
                       -0.40662341554203, -0.74123039483746, 2.27023241882141,
                       -0.74123039483746, 0.262590543048829, -0.0720164362466006,
                       -0.0720164362466006, 2.27023241882141, 0.597197522344259,
                       2.27023241882141, -0.74123039483746, 1.26641148093512,
                       -0.74123039483746, -0.74123039483746, 1.26641148093512,
                       1.60101846023055, -0.0720164362466006),
     Marg.adhesion = c(-0.638897301750389, 0.757476640955261,
                       -0.638897301750389, -0.638897301750389,
                       0.0592896696024361, 1.8047570979845, -0.638897301750389,
                       -0.638897301750389, -0.638897301750389, -0.638897301750389,
                       -0.638897301750389, -0.638897301750389, 0.0592896696024361,
                       -0.638897301750389, 2.50294406933732, 0.408383155278848,
                       -0.638897301750389, -0.638897301750389, 1.10657012663167,
                       -0.638897301750389, 2.50294406933732, 0.0592896696024361,
                       -0.638897301750389, -0.638897301750389, 0.408383155278848,
                       -0.638897301750389, -0.638897301750389, -0.638897301750389,
                       -0.638897301750389, -0.638897301750389, -0.638897301750389,
                       0.0592896696024361, -0.289803816073976, -0.638897301750389,
                       -0.638897301750389, 1.8047570979845, -0.638897301750389,
                       2.15385058366091, 0.0592896696024361, -0.638897301750389,
                       -0.289803816073976, 1.10657012663167, 0.408383155278848,
                       -0.638897301750389, 0.408383155278848, -0.638897301750389,
                       0.0592896696024361, -0.289803816073976, -0.638897301750389,
                       0.408383155278848),
      Epith.c.size = c(-0.555201605659206, 1.69392470908123,
                       -0.555201605659206, -0.105376342711119,
                       -0.555201605659206, 1.69392470908123, -0.555201605659206,
                       -0.555201605659206, -0.555201605659206, -0.555201605659206,
                       -1.00502686860729, -0.555201605659206, -0.555201605659206,
                       -0.555201605659206, 1.69392470908123, 1.24409944613314,
                       -0.555201605659206, -0.555201605659206, 0.344448920236969,
                       -0.555201605659206, 0.794274183185057, 1.24409944613314,
                       -0.555201605659206, -0.555201605659206, -0.555201605659206,
                       -1.00502686860729, -0.555201605659206, -0.555201605659206,
                       -0.555201605659206, -1.00502686860729, -0.555201605659206,
                       2.14374997202932, -0.555201605659206, -0.555201605659206,
                       -0.555201605659206, 1.24409944613314, -1.00502686860729,
                       -0.555201605659206, 1.24409944613314, -0.105376342711119,
                       2.14374997202932, 3.0434004979255, 2.14374997202932,
                       -0.555201605659206, 0.344448920236969, -0.555201605659206,
                       -0.555201605659206, 0.344448920236969, -0.555201605659206,
                       -0.555201605659206),
       Bare.nuclei = c(-0.698341295402917, 1.77156891336678,
                       -0.423906827761839, 0.124962107520315,
                       -0.698341295402917, 1.77156891336678, 1.77156891336678, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917, -0.149472360120762,
                       -0.149472360120762, 1.4971344457257, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917, 1.77156891336678,
                       -0.698341295402917, 1.77156891336678, 0.948265510443546,
                       -0.698341295402917, -0.698341295402917, 0.948265510443546,
                       -0.698341295402917, -0.698341295402917, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917, -0.698341295402917,
                       0.399396575161392, -0.698341295402917, -0.698341295402917,
                       -0.698341295402917, -0.698341295402917, -0.698341295402917,
                       1.77156891336678, 0.948265510443546, -0.149472360120762,
                       1.77156891336678, -0.698341295402917, -0.698341295402917,
                       -0.698341295402917, 1.4971344457257, -0.698341295402917,
                       -0.698341295402917, 1.22269997808462, -0.149472360120762,
                       0.124962107520315),
       Bl.cromatin = c(-0.181693999725951, -0.181693999725951,
                       -0.181693999725951, -0.181693999725951,
                       -0.181693999725951, 2.26758893079032, -0.181693999725951,
                       -0.181693999725951, -0.998121643231376, -0.589907821478663,
                       -0.181693999725951, -0.589907821478663, 0.226519822026761,
                       -0.181693999725951, 0.634733643779474, 0.226519822026761,
                       -0.589907821478663, -0.181693999725951, 0.226519822026761,
                       -0.181693999725951, 0.634733643779474, 1.4511612872849,
                       -0.589907821478663, -0.181693999725951, -0.181693999725951,
                       -0.589907821478663, -0.589907821478663, -0.589907821478663,
                       -0.998121643231376, -0.589907821478663, -0.181693999725951,
                       1.4511612872849, -0.181693999725951, -0.589907821478663,
                       -0.589907821478663, 1.85937510903761, 1.4511612872849,
                       0.634733643779474, 1.4511612872849, 1.04294746553219, 1.4511612872849,
                       -0.181693999725951, 1.85937510903761, -0.589907821478663,
                       0.226519822026761, -0.589907821478663,
                       -0.181693999725951, -0.181693999725951, -0.589907821478663,
                       -0.181693999725951),
   Normal.nucleoli = c(-0.612478497036736, -0.284896027595788,
                       -0.612478497036736, 1.35301631960895,
                       -0.612478497036736, 1.35301631960895, -0.612478497036736,
                       -0.612478497036736, -0.612478497036736, -0.612478497036736,
                       -0.612478497036736, -0.612478497036736, 0.370268911286108,
                       -0.612478497036736, 0.697851380727057, 0.0426864418451602,
                       -0.612478497036736, -0.612478497036736, -0.612478497036736,
                       -0.612478497036736, 0.370268911286108, 2.3357637279318,
                       -0.612478497036736, -0.612478497036736, 1.025433850168,
                       -0.612478497036736, -0.612478497036736, -0.612478497036736,
                       -0.612478497036736, -0.612478497036736, -0.612478497036736,
                       0.370268911286108, -0.612478497036736, -0.612478497036736,
                       -0.612478497036736, 2.00818125849085, -0.612478497036736,
                       1.025433850168, 0.697851380727057, 0.697851380727057,
                       0.0426864418451602, -0.612478497036736, 2.3357637279318,
                       -0.612478497036736, 1.6805987890499, -0.612478497036736,
                       -0.612478497036736, 1.6805987890499, -0.612478497036736,
                       0.370268911286108),
           Mitoses = c(-0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, 1.96042569442479, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, 1.38328313007474, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, 0.22899800137462,
                       -0.348144562975438, 1.38328313007474, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       0.806140565724679, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, -0.348144562975438, 0.22899800137462,
                       0.806140565724679, -0.348144562975438, -0.348144562975438,
                       0.22899800137462, -0.348144562975438, -0.348144562975438,
                       -0.348144562975438, 0.22899800137462, 1.96042569442479,
                       -0.348144562975438),
                 y = c(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
                       0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1,
                       1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1)
)

X1orig = CancerData[,1:9]
X1 = scale(X1orig)

y = CancerData[,10]
p = ncol(CancerData) - 1

first <- make_ridge(CancerData[,1:9])
second <- make_ridge(CancerData[,1:9])

first$dev.ratio == second$dev.ratio
#>   [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#>  [16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#>  [31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#>  [46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#>  [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#>  [76] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#>  [91] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

Created on 2019-11-27 by the reprex package (v0.3.0)

You're welcome! But to answer your second question: it depends on how you plan to use the code and handle changes (I know, vague)...

Setting the seed is mostly for reproducibility. Personally, I keep set.seed() calls outside of my functions because it's a lot easier to know what is going on with your code as you're developing it - as you get to know R you'll see that it's quite quirky compared to most traditional programming languages (written by statisticians for statisticians).

I'm not all too familiar with ridge regression, but it seems like you actually provided a range of parameter estimates for the model with your grid object (non-random) so your output should be the same each time you run the glmnet function. You would set the seed when, let's say, splitting your sample into training and testing datasets. Any packages you use in R to split samples requires the use of the psedu-RNGs, so setting the seed is necessary to ensure the same observations that you specified are used each time in your model thus yielding consistent results.

3 Likes

A comment on "quirky." What makes R an odd one out of the majority of languages is that it has a functional, rather than procedural/imperative, face. (Under the hood, there's plenty of procedural/imperative work, but the intent is to isolate that from the user.)

I liken it to the difference between dealing with an obsessive compulsive personality and a point-and-shoot type who can figure it out herself. In the first case, you patiently lay out the steps to get to your result: first do this, then do that, except for when you do this. In the second, here is the result I want from this input--apply this function and give me the answer.

In a word, school algebra writ large: f(x) = y

2 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.