CecileProust-Lima / lcmm

R package lcmm
https://CecileProust-Lima.github.io/lcmm/
54 stars 13 forks source link

gridsearch() and random() seem to generate a random seed. #74

Closed Lrgnmllr closed 3 years ago

Lrgnmllr commented 3 years ago

I noticed that when using random(), and therefore also gridsearch() with lcmm, a random seed is generated. Note that it does not create the same seed everytime, which makes sense, since gridsearch() essentially uses the random()several times (if I understand correctly).

Here is a reproducible example of what i mean. It assumes the package NormPsy is installed.

First, remove any existing random seed

One can check if a random seed is in place by running .Random.seed. One can remove it using rm(.Random.seed).

rm(.Random.seed)

If no seed is in place, you will have the Warning message:

Warning message:
In rm(.Random.seed) : object '.Random.seed' not found

Second, load packages, and run a model that does not use random(), and thus does not generate a seed.

library("NormPsy")
library("lcmm")

paquid$normMMSE <- normMMSE(paquid$MMSE)
paquid$age65 <- (paquid$age-65)/10

# ---- Model with 1 class:
mlin1 <- lcmm(CESD ~ age65 * male, random = ~ age65, subject = 'ID',
             data = paquid)

# ---- Model with 3 classes, using mlin1 as the starting value.
mlin3_0 <- lcmm(CESD ~ age65 * male, mixture = ~ age65, random = ~ age65, subject = 'ID',
                ng = 3, B = mlin1,
                data = paquid)

Now save the workspace, delete all data and existing seeds to prepare for the next step

save.image("example_noseed.RData")
rm(list = ls()) # this removes everything
.Random.seed # this should give an error message.
rm(.Random.seed)# this should give a warning message, since there is no seed to remove.

Now, run the following loop and see that you get different numbers each time:

for (i in 1:100)
{
  load("example_noseed.RData")
  print(sample(1:100, 10))
  rm(list = ls())
}

The above should give you different numbers everytime, as one should expect when loading a workspace that did not set any seed.

Third, run a model using random(), which will generate a seed

# ---- Reload the workspace (which contains our data), and remove any existing seed
load("example_noseed.RData")
rm(.Random.seed)

# ---- Model with 3 classes, using random(mlin1) as the starting value.
mlin3_1 <- lcmm(CESD ~ age65 * male, mixture = ~ age65, random = ~ age65, subject = 'ID',
                ng = 3, B = random(mlin1),
                data = paquid)
sample(1:100, 10) 

NOTE: this will give a different sample everytime you run this lcmm command, suggesting the seed is different everytime. Doing .Random.seed here should give a list of numbers

Now, save the workspace, delete all data and existing seeds, to prepare for the next step

save.image("example_seed.RData")
rm(list = ls()) # this removes everything
rm(.Random.seed) # this removes the (hidden) seed

Now, run the following loop and see that you get the same numbers each time:

for (i in 1:100)
{
  load("example_seed.RData")
  print(sample(1:100, 10))
  rm(list = ls())
}

I hope this works on your machine as well. This may not be relevant for most, but I think it was an issue worth reporting.

VivianePhilipps commented 3 years ago

Hello,

thank you for your message and for the detailed solution you give.

Saving the workspace will effectively save the seed, but I think this is not specific to lcmm. As soon as you generate a random number in R, the .Random.seed variable is assigned and the save.image() function will save it. See this example :

 a <- rnorm(2)
 save.image("test.RData")

 for(i in 1:10)
 {
   load("test.RData")
   print(sample(1:100, 10))
   rm(list=ls())
 }

The generated numbers are always the same, so the seed is stored. If you don't want it, use the save function to stored only the variables you want instead of saving the whole workspace :

 a <- rnorm(2)
 save(a, file="testsave.RData")

 for(i in 1:10)
 {
   load("testsave.RData")
   print(sample(1:100, 10))
   rm(list=ls())
 }

Here we generate different numbers at each iteration.

Best,

Viviane

Lrgnmllr commented 3 years ago

Thank you so much for taking the time to answer!

I had never noticed that fact, so thank you, it is good to know!

All the best