Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: consistency of sort?


From   Gabriel Demombynes <[email protected]>
To   [email protected]
Subject   st: consistency of sort?
Date   Mon, 20 Dec 2004 08:39:07 -0800 (PST)

After further experimentation, I've found that my
earlier problem with -swor- is due to the fact that
-sort- is not doing the same sort every time on my
data. Thus when -swor- sorts the data (or I sort with
-sort- before using -sample-), I get different draws
because the data is in a different order.
Specifically, consider the following:

use temp, clear
sort cluster
li

This produces different results each time. Although
the dataset is sorted by cluster each time, the order
within cluster is different.

Is this a general phenomenon, or is this somehow
particular to my data? This is an older dataset, with
variables originally created in version 6.

I was able to get around the problem by creating a
variable equal to the original observation number and
subsorting on that.

use temp, clear
gen counter=_n
sort cluster counter
li

Thanks,
Gabriel

--- Gabriel Demombynes <[email protected]> wrote:

> Oops, on closer consideration, I remembered that the
> swor and by problem was in the 2nd stage of my two
> 2-stage sampling routine, so I can leave the first
> swor statement (with no by) as is. 
> 
> For the second stage, I can replace
> 
> swor 10, by(cluster)
> 
> with
> 
> by cluster: sample 10, count
> 
> Thanks for the help! I'd still be interested to know
> how one might handle the general problem of random
> sampling with varying probabilities of selection.
> Isn't this a common issue for survey design?
> 
> Gabriel
> 
> 
> --- n j cox <[email protected]> wrote:
> 
> > -swor- was published in STB-59.
> > I would have sworn that you don't
> > now need it for your purpose,
> > as -sample- has since been modified
> > to support a -count- option.
> > 
> > However, I cannot reproduce your problem.
> > I can get -swor- to select the same sample
> > again and again.
> > 
> > . forval i = 1/10 {
> >    2. qui sysuse auto, clear
> >    3. set seed 28031952
> >    4. qui swor 1, gen(select) by(foreign)
> >    5. levels make, clean
> >    6. }
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > Buick Riviera VW Rabbit
> > 
> > Try as I can, the Rabbit keeps coming
> > out of the hat.
> > 
> > Nick
> > [email protected]
> > 
> > Gabriel Demombynes wrote
> > 
> > I'm using the user-written command swor to draw
> > random
> > samples with
> > replacement. I've discovered that even when I've
> set
> > the seed, the
> > command does not always draw the same samples in
> > repeated runs when
> > using by. It does not have this problem when not
> > using
> > by.
> > 
> > When I do the following, I get different results
> > each
> > time I run the
> > program:
> > 
> > set seed 6662
> > use alldata, clear
> > swor 1, gen(select) by(sex)
> > li
> > 
> > The problem seems to be particular to the
> variables
> > in
> > my dataset.
> > When I create a new group variable, e.g. assigning
> > group=1 for the
> > first half of the observations, and group=2 for
> the
> > second half, and
> > then run the analysis by(group) there's no
> problem;
> > I
> > can reproduce the same results on multiple draws.
> > But the problem
> > persists even when I create a new group variable
> > based
> > on a pre-
> > existing variable and use that, e.g.
> > 
> > set seed 6662
> > use alldata, clear
> > gen group=1 if sex==1
> > replace group=2 if sex==2
> > swor 1, gen(select) by(group)
> > li
> > 
> > I've looked at the swor ado file, but I can't
> figure
> > out what might be wrong. Any suggestions?
> > *
> > *   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/
> > 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam
> protection around 
> http://mail.yahoo.com 
> *
> *   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/
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
*
*   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