Hey Statlisters,
I am using Stata 14 and trying to draw random numbers and post them observation by observation with the command -postfile- into a new data file. Now when a number is drawn I would like my loop to discard this number if it lies outside of a given interval and draw as long until it finally lies in it. I looked at different examples in the Stata Blog with random numbers being drawn from a uniform distribution out of a given interval but I would like to use -rnormal(m, s)- and could not figure it out.
This is how far I have come with my code:
Anybody with some advice on how to implement that?
Cheers
Philipp
I am using Stata 14 and trying to draw random numbers and post them observation by observation with the command -postfile- into a new data file. Now when a number is drawn I would like my loop to discard this number if it lies outside of a given interval and draw as long until it finally lies in it. I looked at different examples in the Stata Blog with random numbers being drawn from a uniform distribution out of a given interval but I would like to use -rnormal(m, s)- and could not figure it out.
This is how far I have come with my code:
Code:
set seed 1234 *Set the values of parameters scalar mean_d = 1 scalar sigma = 1.5 scalar ub = 3 *upper bound of interval scalar lb = 1 *lower bound of interval gen obs=. gen obsid =. postfile `sim' obsid obs control`n' using results, replace quietly { forvalues i = 1/10000 { replace obs = rnormal(`mean_d', `sigma') scalar control = obs<`ub' & obs>`lb' replace obsid = `i' post `sim' (obsid) (obs) (control) } } postclose `sim'
Cheers
Philipp
Comment