Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: FW: random number sequence within a loop


From   "Sergiy Radyakin" <[email protected]>
To   <[email protected]>
Subject   st: Re: FW: random number sequence within a loop
Date   Mon, 5 Mar 2007 21:28:10 +0100

Hello Raoul,

I have difficulties understanding what you are doing in your cycle.

But see -help sample-

e.g.
sample 1000, count
will create a subsample with 1000 observations (without replacement)

A tutorial is here: http://www.ats.ucla.edu/stat/Stata/faq/sample.htm

If for some reason you want to do-it-all-yourself:

gen rrr=uniform()
sort rrr
keep if _n<= `subsamplesize'

will keep `subsamplesize' random observations.

Set seed to the same constant if you want to restart the random numbers generator.
Set seed to timer if you want to get different sequences of random numbers
setting seed to uniform() does not mean assign a random number to seed!

local first=ceil(uniform()*10000)

set seed `first'

di uniform()

-->.27345261



When you restart Stata and run the same commands again, you get the same number.

Best regards, Sergiy






----- Original Message ----- From: "Raoul C Reulen" <[email protected]>
To: <[email protected]>
Sent: Monday, March 05, 2007 6:47 PM
Subject: st: FW: random number sequence within a loop



Dear Stata users,

I'm trying to sample controls for a case-controls study. For each case I
would like to sample one control. I use a loop for this, and "set seed"
to a random number. If I wouldn't do this then STATA will produce the
same random number sequence for each case within the loop, and the
control selection wouldn't be random. Therefore I have used:

.local seed = ceil(10000*uniform())
.set seed `seed'

to define the seed within the loop (see also below).This should ensure
that everytime stata goes through the loop, a different random number
sequence is generated (i hope this is correct). However, the problem is
that every time I run stata it will give me a different control for each
case. So, I can't reproduce my outcome. Is there any solution for this?
Thanks.
See my loop below:



forvalues i = 1/`case_num'{
local seed=ceil(10000*uniform())
set seed `seed'
gen random=uniform()
sort random
gen list = _n
replace control`i'=1 if list==`i'
}

Thanks

Raoul

*
* 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/
*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index