Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why set.seed() affects sample() in R

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?

Question

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?

Here is an R code:

set.seed(123458)
x.y = rnorm(1e2)

sampled = sample(x = x.y, size = 20, replace = TRUE)

plot(sampled)
like image 324
rnorouzian Avatar asked May 17 '17 00:05

rnorouzian


People also ask

What is set seed () function in R and why to use it?

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.

How to generate random numbers with seed in R?

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

What happens if the seed is not specified in R?

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.

Why do researchers set a random seed at the beginning?

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.


2 Answers

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)
like image 139
thelatemail Avatar answered Oct 17 '22 19:10

thelatemail


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)
like image 1
dk. Avatar answered Oct 17 '22 17:10

dk.