Closed Lrgnmllr closed 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
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
I noticed that when using
random()
, and therefore alsogridsearch()
withlcmm
, a random seed is generated. Note that it does not create the same seed everytime, which makes sense, sincegridsearch()
essentially uses therandom()
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 usingrm(.Random.seed)
.If no seed is in place, you will have the Warning message:
Second, load packages, and run a model that does not use
random()
, and thus does not generate a seed.Now save the workspace, delete all data and existing seeds to prepare for the next step
Now, run the following loop and see that you get different numbers each time:
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 seedNOTE: 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 numbersNow, save the workspace, delete all data and existing seeds, to prepare for the next step
Now, run the following loop and see that you get the same numbers each time:
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.