According to User Guide [U], page 118, "uniform() gives the same
sequence of random numbers in every Stata session (measured from the
start of the session) unless you reinitialize the seed."
Does the use of -preserve- count as a new session?
Motivation.
In code fragment (1) below with Stata's auto data set, I got different
sequences of random numbers in a two situations where I had expected
them to be the same (compare variables a and aa). I contrast the
results of a -generate a = uniform() if foreign- command, with
generation of aa via -preserve-, -keep if foreign-, -gen aa =
uniform()-. I thought the -set seed- command would "carry over"
the second set of commands generating aa (it's the same session).
[I would like it to do so -- I am writing a command that uses random
values, and I would like the command to provide the same results using
-if- option as with the -preserve-/-keep- alternative.]
By contrast, code fragment (2) provides the expected results (compare
variables b and bb) -- but only after I had issued a new -set seed-
command after the -preserve- and -keep-. Am I missing something?
* CODE FRAGMENT (1)
use d:\home\stephenj\data\auto, clear
ge id = _n
set seed 123456789 /* as per Stata default */
ge a = uniform() if foreign
sum id foreign a
list id a foreign
preserve
keep if foreign
ge aa = uniform()
sum id foreign aa
list id aa foreign
restore
* VERSUS CODE FRAGMENT (2)
use d:\home\stephenj\data\auto, clear
ge id = _n
set seed 123456789 /* as per Stata default */
ge b = uniform() if foreign
sum id foreign b
list id b foreign
preserve
keep if foreign
set seed 123456789 /* <<----NB added -set seed- */
ge bb = uniform()
sum id foreign bb
list id bb foreign
restore
summ
... and, by the way, can one access the current seed value via
"macro extended functions for accessing Stata system parameters" (or
somehow else)? The list of possibilities listed in Ref Man [P], page
128, does not include the seed. All the things listed appear to
correspond to what is listed by -query- (which also does not report the
seed).
Stephen
----------------------
Professor Stephen P. Jenkins <[email protected]>
Institute for Social and Economic Research (ISER)
University of Essex, Colchester, CO4 3SQ, UK
Tel: +44 (0)1206 873374. Fax: +44 (0)1206 873151.
http://www.iser.essex.ac.uk
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/