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

From |
"Conner Mullally" <mullally@primal.ucdavis.edu> |

To |
statalist@hsphsun2.harvard.edu |

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

Date |
Mon, 05 May 2008 11:31:02 -0500 |

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? ----- Original Message Follows ----- From: "Conner Mullally" <mullally@primal.ucdavis.edu> To: statalist@hsphsun2.harvard.edu Subject: 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/

**Follow-Ups**:**Re: st: Re: reproducing results of a random number generation within a loop using seed***From:*Steven Samuels <sjhsamuels@earthlink.net>

- Prev by Date:
**Re: st: mfx in ivprobit** - Next by Date:
**Re: st: reproducing results of a random number generation within a loop using seed** - Previous by thread:
**st: Puja Vasudeva Dutta is out of the office.** - Next by thread:
**Re: st: Re: reproducing results of a random number generation within a loop using seed** - Index(es):

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