Ernesto F
Ernesto F

Reputation: 21

set.seed on R 4.2.1

I am having some issues with "set.seed()". My R version is 4.2.1 so in order to get the same results as in R 3.5 or older

set.seed(1)

my understanding is that I should use

set.seed(1, sample.kind = "Rounding")

but R returns the following Warning:

Warning message: In set.seed(1, sample.kind = "Rounding") : non-uniform 'Rounding' sampler used

but, results still don't match. I am doing a test online so I don't know the answer they expect me to provide but with the warning message I am somehow confident it has something to do with "set.seed()".

Upvotes: 2

Views: 475

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368181

From help(set.seed):

 ‘sample.kind’ can be ‘"Rounding"’ or ‘"Rejection"’, or partial    
 matches to these.  The former was the default in versions prior to
 3.6.0: it made ‘sample’ noticeably non-uniform on large
 populations, and should only be used for reproduction of old
 results.  See PR#17494 for a discussion. 

To actually get R 3.6.0 or later to reproduce the prior draws, a different argument is needed. It's been a few years but I think we discussed this here at SO before...

Edit: Nothing that a little probing with Docker, and the help pages, cannot solve (given that I as its author have a few r-base images here):

edd@rob:~$ docker run --rm -ti r-base:3.4.4 Rscript -e 'set.seed(123); sample(1:10, 3)'
[1] 3 8 4
edd@rob:~$ docker run --rm -ti r-base:4.2.1 Rscript -e 'set.seed(123); sample(1:10, 3)'
[1]  3 10  2
edd@rob:~$ docker run --rm -ti r-base:4.2.1 Rscript -e 'RNGversion("3.4.4"); set.seed(123); sample(1:10, 3)'
Warning message:
In RNGkind("Mersenne-Twister", "Inversion", "Rounding") :
  non-uniform 'Rounding' sampler used
[1] 3 8 4
edd@rob:~$ 

Here we have

  1. A set of 3 8 4 sampled from 1:10 under 3.4.4 (before the change)
  2. A different set 3 10 2 sampled from 1:10 (under the same seed) using current 4.2.1 demonstrating that "yes, Veronica, sample() changed"
  3. Because R is serious, we can in fact under the current version get the old behavior back if we ask for it.

Not bad. But yes, the warning you saw under current R is apparently par for the course.

Upvotes: 5

Related Questions