Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many times to use set.seed(123)

Tags:

r

I have 60 lines of code. Throughout the code there are several calls to random number generators including rnorm(). Is it enough to put set.seed(x) in the very beginning of the code or do I need to set.seed every time random number generation occurs in the code?

like image 914
user1984076 Avatar asked Sep 09 '13 10:09

user1984076


People also ask

What does set seed 123 do in R?

set seed (value) where value specifies the initial value of the random number seed. In the above line,123 is set as the random number value. The main point of using the seed is to be able to reproduce a particular sequence of 'random' numbers. and sed(n) reproduces random numbers results by seed.

Do you need to set seed everytime?

So the short answer to the question is: if you want to set a seed to create a reproducible process then do what you have done and set the seed once; however, you should not set the seed before every random draw because that will start the pseudo-random process again from the beginning.

What is the use of set seed?

The use of set. seed is to make sure that we get the same results for randomization. If we randomly select some observations for any task in R or in any statistical software it results in different values all the time and this happens because of randomization.

Do I need to set seed in every chunk?

It is standard advice to set a random seed so that results can be reproduced. However, since the seed is advanced as pseudo-random numbers are drawn, the results could change if any piece of code draws an additional number.


1 Answers

It really depends on how you foresee the code changing in the future.

If you expect that you will be including commands at an earlier point in the code that will require random number generation and you want to replicate the results you were getting earlier before inserting that code, you should use set.seed() at the appropriate points in your code.

Example:

set.seed(1)
A <- rnorm(10)
B <- rnorm(10)
C <- rnorm(10) ## I always want "C" to be the results I get here

set.seed(1)
AA <- rnorm(10); BB <- rnorm(10); CC <- rnorm(10)

identical(A, AA)
# [1] TRUE
identical(B, BB)
# [1] TRUE
identical(C, CC)
# [1] TRUE

set.seed(1)
A <- rnorm(10); B <- rnorm(10); C <- rnorm(10)

set.seed(1)
AA <- rnorm(10); BB <- rnorm(10); BA <- rnorm(10); CC <- rnorm(10)

identical(A, AA)
# [1] TRUE
identical(B, BB)
# [1] TRUE
identical(C, CC)
# [1] FALSE

In the above, if I wanted "C" to always be the same no matter what comes before it, I should set the seed just before that.

Note that since I hadn't reset the seed before creating C or CC, and there is a new function requiring random number generation between BB and CC in the second example, the values for C and CC are now different. If you wanted them to be the same, you would have to insert another set.seed just before creating C and CC, as follows:

set.seed(1)
A <- rnorm(10)
B <- rnorm(10)
set.seed(2)
C <- rnorm(10) ## I always want "C" to be the results I get here

set.seed(1)
AA <- rnorm(10); BB <- rnorm(10); BA <- rnorm(10);
set.seed(2)
CC <- rnorm(10)

identical(A, AA)
# [1] TRUE
identical(B, BB)
# [1] TRUE
identical(C, CC)
# [1] TRUE
like image 69
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 12 '22 01:10

A5C1D2H2I1M1N2O1R2T1