简体   繁体   English

在 R 4.2.1 上设置种子

[英]set.seed on R 4.2.1

I am having some issues with "set.seed()".我对“set.seed()”有一些问题。 My R version is 4.2.1 so in order to get the same results as in R 3.5 or older我的 R 版本是 4.2.1,所以为了获得与 R 3.5 或更早版本相同的结果

set.seed(1)

my understanding is that I should use我的理解是我应该使用

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

but R returns the following Warning:但 R 返回以下警告:

Warning message: In set.seed(1, sample.kind = "Rounding"): non-uniform 'Rounding' sampler used警告消息:在 set.seed(1, sample.kind = "Rounding") 中:使用了非均匀的“舍入”采样器

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()".我正在在线进行测试,所以我不知道他们希望我提供的答案,但是对于警告消息,我有点确信它与“set.seed()”有关。

From help(set.seed) :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.要真正获得 R 3.6.0 或更高版本来重现先前的绘图,需要不同的参数。 It's been a few years but I think we discussed this here at SO before...已经有几年了,但我想我们之前在 SO 上讨论过这个......

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):编辑:没有什么可以用 Docker 和帮助页面来解决的(因为我作为它的作者这里有一些r-base图像):

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) 3.4.4下1:10采样的一组3 8 4 (改前)
  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"使用当前 4.2.1 从 1:10(在同一种子下)采样的不同集合3 10 2证明“是的,Veronica, sample()已更改”
  3. Because R is serious, we can in fact under the current version get the old behavior back if we ask for it.因为 R 是严重的,如果我们要求它,我们实际上可以在当前版本下恢复的行为。

Not bad.不错。 But yes, the warning you saw under current R is apparently par for the course.但是,是的,您在当前 R 下看到的警告显然与课程相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM