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

From |
Svend Juul <SJ@SOCI.AU.DK> |

To |
<statalist@hsphsun2.harvard.edu> |

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

Date |
Mon, 5 May 2008 18:52:59 +0200 |

Conner wrote: 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... ... If I can follow up my own question, I looked harder at my results, and the loop is generating the same two random numbers for each pair, but it is switching who received which number within each pair. Is there anyway I can keep this from happening? ========================================================= See: help sort and try: sort cluster farm , stable I believe it will solve your problem. Svend ________________________________________________________ Svend Juul Institut for Folkesundhed, Afdeling for Epidemiologi (Institute of Public Health, Department of Epidemiology) Vennelyst Boulevard 6 DK-8000 Aarhus C, Denmark Phone, work: +45 8942 6090 Phone, home: +45 8693 7796 Fax: +45 8613 1580 E-mail: sj@soci.au.dk _________________________________________________________ * * 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/

- Prev by Date:
**st: Re: reproducing results of a random number generation within a loop using seed** - Next by Date:
**Re: st: Re: reproducing results of a random number generation within a loop using seed** - Previous by thread:
**st: reproducing results of a random number generation within a loop using seed** - Next by thread:
**st: Survival analysis** - Index(es):

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