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

Re: st: problem with swor and by


From   Gabriel Demombynes <[email protected]>
To   [email protected]
Subject   Re: st: problem with swor and by
Date   Mon, 20 Dec 2004 06:58:42 -0800 (PST)

Yes, I see that sample has been updated to allow for
drawing fixed numbers of obs, not just percentages. It
seems like a better idea to adjust my routine to use
-sample- rather than keep trying to figure out why my
data doesn't behave properly with a defuct .ado file
from an old STB.

Unfortunately, -sample- is missing one nice feature of
-swor-, the ability to mark selected observations
rather than delete them. This was useful to me because
what I am doing is drawing a sample w/o replacement,
using probability weights for the sampling.
Specifically, I am drawing a sample of "communities"
weighting by each community's population. This is how
the first stage of a two-stage household survey
sampling design. 

To do this, I was working with a dataset of all
households in all communities. Thus, no additional
weights are needed. I was using -swor- to first draw
one household, marking that household as selected.
Then I marked all households in that household's
community as selected. Then I draw a 2nd household
from the remaining households, marking all households
in that household's community as selected. I continue
this way until I've drawn the desired number of
households. With -sample-, it will take a series of
merge statements to do the same thing. Or is there a
more elegant way to do this? My original code is
copied below.
Thanks,
Gabriel

gen select=0
local ctselect=0

while `ctselect'=0 {
* Draw an individual
swor 1 if select==0, gen(newselect) keep
*Mark all individuals in the community as selected
egen newselect2=max(newselect), by(comid)
*Change the permanent selected variable to selected
for those individuals
replace select=1 if newselect2
*Mark one observation per selected community
egen markloc==1 & select==1
*Count communities already selected
count if markloc==1 & select==1
* Change counter to have that value
local ctselect=r(N)
di `ctselect'
* Drop temporary variables
drop newselect newselect2 markloc
 }




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



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