Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

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

st: simulation: finding the winner, and generating unique values

From   Michael McCulloch <>
Subject   st: simulation: finding the winner, and generating unique values
Date   Fri, 24 May 2013 12:30:58 -0700


* I have three questions related to the following data simulation.
* 1. How can I simulate B so that only unique values of 1-5 are created?
* 2. Is there a more elegant way to code simulation of variable B? 
* 3. How can I identify which variable B had the highest total count of C, within levels of A?

set seed 12358
* simulate A
set obs 20		//create 20 observations
gen 	a=1 in 1/5
replace a=2 in 6/10
replace a=3 in 11/15
replace a=4 in 16/20

* simulate variable: B (values 1-5 sampled without replacement)
gen b=.
replace b=(1 + int(5*runiform())) if a==1 	
replace b=(1 + int(5*runiform())) if a==2 	
replace b=(1 + int(5*runiform())) if a==3 	
replace b=(1 + int(5*runiform())) if a==4 	

* simulate variable: C
gen c1=uniform()	//Generate n (equal chance of 0-1)
	gen c=.
	replace c=0 if c1<0.5
	replace c=1 if c1>=0.5
	drop c1
list, noobs

Best wishes,
Michael McCulloch, LAc MPH PhD

Pine Street Foundation, since 1989
124 Pine Street | San Anselmo | California | 94960-2674  
P: (415) 407-1357 | F: (206) 338-2391 |

*   For searches and help try:

© Copyright 1996–2016 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index