Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generate random variable

    I would like to generate 2000 latent variable a which is defined as a = b + 3c + d

    where d ~ N[0,3] and c ~ uniform [0, 1].

    I should choose a variable "b" so that I can generate nearly 30% of "a" to be negative.

    --------
    What I write in stata is as follows:

    set obs 2000

    generate c = runiform(0,1)

    generate d = rnormal(0,3)

    How can I write this code?
    Last edited by Sean Bilic; 17 Jun 2022, 13:39.

  • #2
    Code:
    clear
    
    set obs 2000
    
    set seed 1234
    
    generate c = runiform(0,1)
    
    generate d = rnormal(0,3)
    
    gen check = 3*c + d
    centile check, c(30)
    
    gen b = rnormal(-`r(c_1)', 0.01)
    
    gen a = b + 3*c + d
    gen byte a_negative = a < 0
    tab a_negative
    The approach I have taken is to first check out the achieved distribution of 3c + d, and then, in effect, choose b to shift the whole distribution up so that the 30th percentile moves to zero. In fact, if, at that point, we were to set b to be the negative of the 30th percentile of 3c + d, constant, you would have exactly 30% of the values of a negative. In fact, a constant is a random variable, so technically within the constraints of the problem posed. But if you think of that as a cheat, the main thing is to center b around that same value, the negative of the 30th percentile of 3c+d, but with very small variance, so that the 30th percentile doesn't get moved too far away from 0.

    Added: By the way, to assure that you can reproduce the same results every time you run the code, don't forget to set the random number generator seed.

    Comment

    Working...
    X