Replicating seed setting from Stata
Asked Answered
L

1

7

I'm trying to replicate in R a bit of code someone else wrote in Stata, and have hit a wall trying to predict the behavior of their p-RNG.

Their code has this snippet:

set seed 123456

Unfortunately, it's a bit nebulous exactly the algorithm used by Stata. This 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 question as well doesn't seem to have been completed.

Is it a fool's errand to try and replicate Stata's random numbers?

I don't know which version of Stata was used to create this.

Longdistance answered 23/3, 2016 at 17:33 Comment(2)
If you don't know which version was used, your problem is indeed more difficult as you want to replicate a program but you can't be precise on which program. blog.stata.com/2016/03/10/… gives an overview and underlines that the default method has changed in Stata 14. stata.com/manuals14/fn.pdf says more.Fortyish
One question you mention #35140308 was not tagged "Stata" and did not include Stata code. It just mentioned Stata in passing. So, it's not surprising that it received no response in terms of Stata.Fortyish
S
8

In short: Yes, it is a fool's errand.

Stata, being a proprietary software, hasn't released all of the details of its core components, like its random number generator. However, documentation is available (link for Stata 14), most pertinently:

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...

Recall also from ?Random in R that for Mersenne twister:

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

Stata internally controls the 624-dimensional set, which should be nearly impossible to guess.

I suggest you export these random numbers from Stata and read them into a vector/matrix/etc. in R using

library(haven)
mydata <- read_dta("mydata.dta")
Scenic answered 23/3, 2016 at 17:47 Comment(1)
Thanks. Unfortunately I don't have a local version of Stata, so I'll just move on.Longdistance

© 2022 - 2024 — McMap. All rights reserved.