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

Re: st: generating random number with restriction to add to a variable in current dataset


From   Richard Williams <Richard.A.Williams.5@nd.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: generating random number with restriction to add to a variable in current dataset
Date   Wed, 23 Feb 2005 22:34:59 -0500

At 05:45 PM 2/23/2005 -0300, Fabio Soares wrote:
Dear all,

I want to generate random numbers to add to the linear
index of a probit model (xb) so that I can predict
precisely the 1/0 observed outcomes.
Therefore, for y=1 I want to draw a random number
(vi)from a N~(0,1) so that xb+v>0 and for y=0 I want
to draw a random number (vi) so that xb+v<=0. Any idea
on how I can draw these numbers and add them to the
index in few steps?
Here is a coding I think I like better than what I suggested before (although I am not sure if either one is what you want). After running a probit,

predict xb, xb
gen xb2=xb+invnorm(uniform())
egen y1adjust = min(xb2 - .001) if y==1
egen y0adjust = max(xb2 + .001) if y==0
replace xb2 = xb2 - y1adjust if y==1
replace xb2 = xb2 - y0adjust if y==0
bysort y: sum xb2

Basically, you are adding a random N(0,1) variable to your predicted values, and then adding an adjustment factor that will make the new prediction always positive when y = 1 and always negative when y = 0.

Using the auto data:

. use http://www.stata-press.com/data/r8/auto.dta, clear
. predict xb, xb
. gen xb2=xb+invnorm(uniform())
. egen y1adjust = min(xb2 - .001) if foreign==1
(52 missing values generated)
. egen y0adjust = max(xb2 + .001) if foreign==0
(22 missing values generated)
. replace xb2 = xb2 - y1adjust if foreign==1
(22 real changes made)
. replace xb2 = xb2 - y0adjust if foreign==0
(52 real changes made)

. bysort foreign: sum xb2

-----------------------------------------------
-> foreign = Domestic

Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
xb2 | 52 -4.894559 1.709001 -8.809644 -.0009999

-----------------------------------------------
-> foreign = Foreign

Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
xb2 | 22 2.23402 1.401082 .0009999 5.177322


*
* 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index