set.seed在不同版本的R(和Ubuntu)上是否一致? [英] Is set.seed consistent over different versions of R (and Ubuntu)?

查看:572
本文介绍了set.seed在不同版本的R(和Ubuntu)上是否一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在运行R版本3.1.0(在Ubuntu 12.04 LTS上),并且由于我的R版本和操作系统都已经过时,我计划同时更新两者.但是,我有很多依赖set.seed()的模拟,我希望它们在更新R和操作系统后仍能给我相同的随机数.

所以我的问题是三个方面.

  1. 是否可以在不更改每个种子生成数字的情况下更新R?
  2. 我可以对操作系统做同样的事情吗?
  3. 如果对1)或2)否定,是否有办法以与旧种子相同的方式更改代码中的种子?

解决方案

跨OS一致性:是

如果将R安装在两个不同的操作系统上,而没有手动更改默认值或RProfile,则使用set.seed()时应获得相同的结果.

R版本之间的一致性:不一定

过去,set.seed()在R版本中会给出相同的结果,但是由于R 3.6.0中的一个小宣布更新,这种情况通常不再适用.因此,您可以比较R 3.6.0之前的结果获得跨版本一致性,但是如果将set.seed()在3.6.0之后的使用与set.seed()在3.6.0之前的使用进行比较,则会得到不同的结果./p>

您可以在下面的示例中看到这一点:

R 3.2.0

> set.seed(1999)
> sample(LETTERS, 3)
[1] "T" "N" "L"

R 3.5.3

> set.seed(1999)
> sample(LETTERS, 3)
[1] "T" "N" "L"

R 3.6.0

set.seed(1999)
sample(LETTERS, 3)
[1] "D" "Z" "R"

不一致的原因是在R 3.6.0中,引擎盖下随机数生成器的默认类型已更改.现在,为了从set.seed()获取结果以进行匹配,您必须首先调用函数RNGkind(sample.kind = "Rounding").

R 3.6.0

> RNGkind(sample.kind = "Rounding")
Warning message:
In RNGkind(sample.kind = "Rounding") : non-uniform 'Rounding' sampler used
> set.seed(1999)
> sample(Letters, 3)
[1] "T" "N" "L"

I am currently running R version 3.1.0 (on Ubuntu 12.04 LTS) and as both my R version and my operating system is getting rather old, I plan on updating both. However, I have a lot of simulations that rely on set.seed() and I would like them to still give me the same random numbers after updating both R and my operating system.

So my question is three-fold.

  1. Can I update R without changing which numbers are generated from each seed?
  2. Can I do the same for my operating system?
  3. If no to either 1) or 2), is there a way to change the seeds in my code in such a way that they are consistent with the olds seeds?

解决方案

Cross-OS consistency: yes

If you installed R on two different operating systems without manually changing defaults or the RProfile, you should get the same results when using set.seed().

Consistency over versions of R: not necessarily

It used to be the case that set.seed() would give the same results across R versions, but that's no longer generally true thanks to a little-announced update in R 3.6.0. So you can get cross version consistency comparing results before R 3.6.0, but if you compare a post-3.6.0 use of set.seed() to a pre-3.6.0 use of set.seed(), you will get different results.

You can see that in the examples below:

R 3.2.0

> set.seed(1999)
> sample(LETTERS, 3)
[1] "T" "N" "L"

R 3.5.3

> set.seed(1999)
> sample(LETTERS, 3)
[1] "T" "N" "L"

R 3.6.0

set.seed(1999)
sample(LETTERS, 3)
[1] "D" "Z" "R"

The reason for the inconsistency is that in R 3.6.0, the default kind of under-the-hood random-number generator was changed. Now, in order to get the results from set.seed() to match, you have to first call the function RNGkind(sample.kind = "Rounding").

R 3.6.0

> RNGkind(sample.kind = "Rounding")
Warning message:
In RNGkind(sample.kind = "Rounding") : non-uniform 'Rounding' sampler used
> set.seed(1999)
> sample(Letters, 3)
[1] "T" "N" "L"

这篇关于set.seed在不同版本的R(和Ubuntu)上是否一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆