修复整个会话的set.seed [英] Fixing set.seed for an entire session

查看:114
本文介绍了修复整个会话的set.seed的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R使用蒙特卡洛过程构建基于代理的模型.这意味着我有许多使用某种随机引擎的函数.为了获得可重现的结果,我必须修复种子.但是,据我了解,我必须在每次随机抽奖或抽样之前设置种子.这是一个真正的脖子痛.有办法修复种子吗?

I am using R to construct an agent based model with a monte carlo process. This means I got many functions that use a random engine of some kind. In order to get reproducible results, I must fix the seed. But, as far as I understand, I must set the seed before every random draw or sample. This is a real pain in the neck. Is there a way to fix the seed?

set.seed(123)
print(sample(1:10,3))
# [1] 3 8 4
print(sample(1:10,3))
# [1]  9 10  1
set.seed(123)
print(sample(1:10,3))
# [1] 3 8 4

推荐答案

有多种选择,具体取决于您的实际需求.我怀疑最简单的第一个选项是不够的,但是我的第二个和第三个选项可能更合适,而第三个选项最易于自动化.

There are several options, depending on your exact needs. I suspect the first option, the simplest is not sufficient, but my second and third options may be more appropriate, with the third option the most automatable.

如果您事先知道使用/创建随机数的函数将始终绘制相同的数字,并且您不对函数调用进行重新排序或在现有调用之间插入新的调用,则只需设置播种一次.确实,您可能不希望继续重置种子,因为您将继续为每个函数调用获取相同的随机数集.

If you know in advance that the function using/creating random numbers will always draw the same number, and you don't reorder the function calls or insert a new call in between existing ones, then all you need do is set the seed once. Indeed, you probably don't want to keep resetting the seed as you'll just keep on getting the same set of random numbers for each function call.

例如:

> set.seed(1)
> sample(10)
 [1]  3  4  5  7  2  8  9  6 10  1
> sample(10)
 [1]  3  2  6 10  5  7  8  4  1  9
> 
> ## second time round
> set.seed(1)
> sample(10)
 [1]  3  4  5  7  2  8  9  6 10  1
> sample(10)
 [1]  3  2  6 10  5  7  8  4  1  9

选项2

如果您确实要确保函数使用相同的种子,并且只想设置一次,则将种子作为参数传递:

Option 2

If you really want to make sure that a function uses the same seed and you only want to set it once, pass the seed as an argument:

foo <- function(...., seed) {
  ## set the seed
  if (!missing(seed)) 
    set.seed(seed) 
  ## do other stuff
  ....
}

my.seed <- 42
bar <- foo(...., seed = my.seed)
fbar <- foo(...., seed = my.seed)

(其中....表示函数的其他arg;这是伪代码).

(where .... means other args to your function; this is pseudo code).

如果要进一步实现自动化,则可以滥用options机制,如果仅在脚本中执行此操作就可以了(对于包,您应该使用自己的options对象).然后,您的函数可以寻找该选项.例如

If you want to automate this even more, then you could abuse the options mechanism, which is fine if you are just doing this in a script (for a package you should use your own options object). Then your function can look for this option. E.g.

foo <- function() {
  if (!is.null(seed <- getOption("myseed")))
    set.seed(seed)
  sample(10)
}

然后在使用中,我们有:

Then in use we have:

> getOption("myseed")
NULL
> foo()
 [1]  1  2  9  4  8  7 10  6  3  5
> foo()
 [1]  6  2  3  5  7  8  1  4 10  9
> options(myseed = 42)
> foo()
 [1] 10  9  3  6  4  8  5  1  2  7
> foo()
 [1] 10  9  3  6  4  8  5  1  2  7
> foo()
 [1] 10  9  3  6  4  8  5  1  2  7
> foo()
 [1] 10  9  3  6  4  8  5  1  2  7

这篇关于修复整个会话的set.seed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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