I always thought set.seed()
only makes random variable generators (e.g., rnorm
) to generate a unique sequence for any specific set of input values.
However, I'm wondering, why when we set the set.seed()
, then the function sample()
doesn't do its job correctly?
Specifically, given the below example, is there a way I can use set.seed
before the rnorm
but sample
would still produce new random samples from this rnorm
if sample
is run multiple times?
set.seed(123458)
x.y = rnorm(1e2)
sampled = sample(x = x.y, size = 20, replace = TRUE)
plot(sampled)
What is set.seed () function in R and why to use it ? : set.seed () function in R is used to reproduce results i.e. it produces the same sample again and again.
The basic installation of the R programming language provides the set.seed function. Within the set.seed function, we simply have to specify a numeric value to set a seed. Have a look at the following R code: set.seed(12345) # Set seed for reproducibility rpois (5, 3) # Generate random numbers with seed # 4 5 4 5 3
If the seed is not specified, R uses the clock of the system to establish one. Run again the previous example where we sampled five random numbers from a Normal distribution, but now specify a seed before: If you execute the previous code, you will obtain the same output.
This can make it impossible to replicate a specific output and that makes it difficult to retrace the steps the programmer of the R code has made. For that reason, it is considered as best practice for researchers to set a random seed at the beginning of R scripts that involve random processes.
As per the help file at ?set.seed
"If called with seed = NULL it re-initializes (see ‘Note’) as if no seed had yet been set."
So, since rnorm
and sample
are both affected by set.seed()
, you can do:
set.seed(639245)
rn <- rnorm(1e2)
set.seed(NULL)
sample(rn,5)
Instead of resetting the seed with NULL
, I think it makes more sense to save the current state and restore it.
x <- .Random.seed
set.seed(639245)
rn <- rnorm(1e2)
.Random.seed <- x
sample(rn,5)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With