Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: Is there a better way of allocating individuals into different categories based on the probability of an event?


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: Is there a better way of allocating individuals into different categories based on the probability of an event?
Date   Sat, 23 Mar 2013 12:40:43 +0900

Gwinyai Masukume wrote:

Is there a better way of executing the code below which assigns
individuals into the Non-smoker and Smoker categories based on the
probability of smoking in pregnancy?

****0=Non-smoker, 1=Smoker***
**** Assuming 84% of women do not smoke during pregnancy
**** Johnston V, Thomas DP, McDonnell J, Andrews RM. Maternal smoking
and smoking in the household during pregnancy and postpartum: findings
from an Indigenous cohort in the Northern Territory. Med J Aust. 2011
May 16;194(10):556-9.
gen smoker1=int(1+(50000-1)*runiform())
gen smoker=0 if smoker1 < 42000
replace smoker=1 if smoker1 >= 42000
label define smoker_label 0"Non-smoker" 1"Smoker"
label values smoker smoker_label
label variable smoker "Smoking status"
drop smoker1

--------------------------------------------------------------------------------

How about the following?

    generate byte smoker = runiform() > 0.84

Or if you're maintaining left-to-right smaller-to-larger order of comparisons:*

    generate byte smoker = 0.84 < runiform()

Joseph Coveney

P.S. Be sure to set the seed in order to assure reproducibility.

*Recommended, as I recall, by Steve McConnell in his _Code Complete: A Practical
Handbook of Software Construction_ Second Edition.  (Redmond, Wash.: Microsoft,
2004).

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index