Statalist


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

st: reproducing results of a random number generation within a loop using seed


From   "Conner Mullally" <[email protected]>
To   [email protected]
Subject   st: reproducing results of a random number generation within a loop using seed
Date   Mon, 05 May 2008 10:44:10 -0500

Hello,
I am trying to generate random numbers in a way that can be
replicated later (I am drawing random samples), and having
some trouble. I have two steps in my program. First, I
collapse the data set by group and randomly select groups
using the following code:
collapse $numvarssector, by($cluster)
set seed 54234
generate randomc=uniform()
sort randomc
g id2=_n
g ctreat=.
replace ctreat=1 if id2<=13
replace ctreat=0 if id2>13
sort cluster
drop randomc

I have no problem replicating the results of this step.
Next, after merging the grouped data with the individual
data, so that each person now has a variable "ctreat" which
indicates whether or not he or she is in a treatment group,
I draw random samples from within each treatment group.
What I want to do is the following: order each observation
within the groups by the size of a continuous variable,
"farm." Then, I want to break the observations into nearest
neighbor pairs using this variable, and randomly assign one
person from each pair to the treatment group:
sort cluster farm
by cluster: g id3=_n
sort $cluster id3
g invite=.
levelsof cluster, local(ctlevels)
foreach i of local ctlevels {
qui su id3 if cluster==`i'
forvalues j=1(2)`r(max)' {
set seed 54234
qui generate random`j'`i'=uniform() if
id3==`j'&$cluster==`i'|id3==(`j'+1)&$cluster==`i'
sort random`j'`i'
drop random`j'`i'
qui g d`j'_`i'=_n if
id3==`j'&cluster==`i'|id3==(`j'+1)&cluster==`i'
qui replace invite=1 if d`j'_`i'==1
qui replace invite=0 if d`j'_`i'==2
drop d`j'_`i'
}
}

I get a different sample every time I do this. Note that I
have already dropped the groups with ctreat==0 before doing
this step. Am I setting the seed in the wrong spot? Or
perhaps there is a better way to do this that I am not
seeing...
Thanks in advance
Conner

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