从 Stata 复制种子设置

Replicating seed setting from Stata

我试图在 R 中复制一些其他人在 Stata 中编写的代码,但在尝试预测他们的 p-RNG 行为时遇到了困难。

他们的代码有这个片段:

set seed 123456

不幸的是,Stata 使用的算法有点模糊。 question suggests it's a KISS algorithm, but didn't manage to replicate in the end (and some of the links there seem to be dead/outdated). And the manual from Stata for set seed doesn't mention anything about algorithms. This 问题似乎也没有完成。

尝试复制 Stata 的随机数是徒劳的吗?

我不知道是用哪个版本的 Stata 创建的。

简而言之:是的,这是徒劳的。

Stata 作为一种专有软件,尚未发布其核心组件的所有详细信息,例如其随机数生成器。但是,documentation is available(Stata 14 的 link),最相关的是:

runiform() is the basis for all the other random-number functions because all the other random- number functions transform uniform (0, 1) random numbers to the specified distribution.

runiform() implements the Mersenne Twister 64-bit (MT64) and the “keep it simple stupid” 32-bit (KISS32) algorithms for generating uniform (0, 1) random numbers. runiform() uses the MT64 algorithm by default.

runiform() uses the KISS32 algorithm only when the user version is less than 14 or when the random-number generator has been set to kiss32...

还记得 R 中的 ?Random 对于 Mersenne twister:

The ‘seed’ is a 624-dimensional set of 32-bit integers plus a current position in that set.

Stata 内部控制着 624 维集,这应该是几乎不可能猜到的。

我建议您从 Stata 中导出这些随机数并将它们读入 vector/matrix/etc。在 R 中使用

library(haven)
mydata <- read_dta("mydata.dta")