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

Re: st: replacement task


From   Richard Williams <[email protected]>
To   [email protected], [email protected]
Subject   Re: st: replacement task
Date   Wed, 21 Jan 2004 19:09:31 -0500

At 03:54 PM 1/21/2004 -0600, Dimitriy V. Masterov wrote:
I could not find a command that performs this simple task. I was hoping if
someone might have some advice. I have a variable named emp that looks
like this

emp     Freq.
0       93,404
1       55,537
2       36,556

I want to replace emp=2 at random with 0 or 1 depending on the relative
number of 1s and 0s in the sample. My outcome should look like this

emp     Freq.
0       116,329
1       69,168
Incidentally, if you happen to have Nick Cox's -egenmore- installed (type -findit egenmore-) this will also work (the rndsub fnc as shown below randomly assigns cases to 2 groups, with 62.71% going to group 1 and the rest to group 2; this assignment only occurs where emp = 2).

. egen x = rndsub() if emp==2, ng(2) p(62.712081)
(148941 missing values generated)

. replace emp = x-1 if emp ==2
(36556 real changes made)

. tab emp

emp | Freq. Percent Cum.
------------+-----------------------------------
0 | 116,329 62.71 62.71
1 | 69,168 37.29 100.00
------------+-----------------------------------
Total | 185,497 100.00

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