07.02.2014 Views

Introduction to Stata 8 - (GRIPS

Introduction to Stata 8 - (GRIPS

Introduction to Stata 8 - (GRIPS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15.4. Random samples, simulations<br />

Random number functions<br />

<strong>Stata</strong> can create 'pseudo-random' numbers:<br />

[R] Functions<br />

gen y=uniform() Uniformly distributed in the interval 0-1<br />

gen y=invnorm(uniform()) Normal distribution, mean=0, SD=1<br />

gen y=10+2*invnorm(uniform()) Normal distribution, mean=10, SD=2<br />

If you run the same command twice it will yield different numbers. If you need <strong>to</strong> reproduce<br />

the same series of 'random' numbers, initialize the seed (a large integer used for the initial<br />

calculations):<br />

set seed 654321<br />

Random samples and randomization<br />

You may use sample <strong>to</strong> select a random sample of your data set:<br />

sample 10<br />

Selects an approximately 10 percent random sample.<br />

sample 53 , count Selects exactly 53 observations at random.<br />

You may assign observations randomly <strong>to</strong> two treatments:<br />

generate y=uniform()<br />

generate treat=1<br />

replace treat=2 if y>0.5<br />

And you may sort your observations in random sequence:<br />

generate y=uniform()<br />

sort y<br />

Generating artifical data sets<br />

You may use set obs <strong>to</strong> create empty observations. The following sequence defines a file<br />

with 10,000 observations, used <strong>to</strong> study the behaviour of the difference (dif) between two<br />

measurements (x1, x2), given information about components of variance (sdwithin,<br />

sdbetw).<br />

set obs 10000<br />

generate sdbetw = 20<br />

generate sdwithin = 10<br />

generate sd<strong>to</strong>tal = sqrt(sdbetw^2 + sdwithin^2)<br />

generate x0 = 50 + sdbetw*invnorm(uniform())<br />

generate x1 = x0 + sdwithin*invnorm(uniform())<br />

generate x2 = x0 + sdwithin*invnorm(uniform())<br />

generate dif = x2 - x1<br />

summarize<br />

See another example in section 14.7 (the twoway rspike graph)<br />

Advanced simulations<br />

With simulate you may set up quite complex Monte Carlo simulations.<br />

[R] simulate<br />

59

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!