Announcement

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

  • Generating dichotomous random variable with 1 or 2

    Hi everyone,

    I want to generate a dichotomous random variable with the manifestations 1 or 2.
    I only find runiform (a,b), but these Stata command gives me numbers between 1 and 2. I only want 1 OR 2.

    Given: Variable with 1 to 5 manifestations. And the middle categorie (3) schould dedicated randomly to categorie 1 or 2.

    gen Var =.
    replace Var = 1 if Var2==1 // Agree
    replace Var = 1 if Var2==2
    replace Var = (?random variable?) if Var2==3 //Middle
    replace Var = 2 if Var2==4 // Disagree
    replace Var = 2 if Var2==5

    Thank you for helping!

  • #2
    Ann-Marie:
    welcome to this forum.
    You may want to try:
    Code:
    . set obs 100
    
    . g A=runiform()
    
    . g B=1 if runiform()<=.5
    
    . replace B=2 if B==.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Carlo gave a great approach.

      As an alternative (based on this text), you may type as well:

      Code:
      . set obs 10
      number of observations (_N) was 0, now 10
      
      . set seed 1234
      
      . generate mybin = floor((2-1+1)*runiform() + 1)
      
      . list
      
           +-------+
           | mybin |
           |-------|
        1. |     2 |
        2. |     1 |
        3. |     2 |
        4. |     2 |
        5. |     1 |
           |-------|
        6. |     2 |
        7. |     2 |
        8. |     2 |
        9. |     1 |
       10. |     2 |
           +-------+
      Best regards,

      Marcos

      Comment


      • #4
        Originally posted by Ann-Marie Ullein View Post
        I only find runiform (a,b)
        What version of Stata are you using? I think that runiformint(a, b) has been around since Release 14. It should be there in the same help file and user manual entry as runiform().

        Comment


        • #5
          Yet another approach:


          Code:
          gen wanted = cond(runiform() < 0.5, 1, 2)

          Comment


          • #6
            Ceil function seems the good choice for this issue, since it could be applied for any integer rather than just n=2.
            Code:
            gen wanted = ceil(2*runiform())
            Last edited by Romalpa Akzo; 27 Nov 2017, 08:24.

            Comment


            • #7
              Everyone (me too) is taking #1 to imply equal probabilities. But dichomotous (binary too) just means two possibilities. I've often used ceil() in this and other contexts, but it doesn't generalise so easily to unequal probabilties.

              Comment


              • #8
                Dear Nick,

                Taking into account the targeted output, it seems to me that ceil would have the same meaning with other solutions in this thread,

                An actually, ceil, among almost everything of stata, is truly the thing I learned from you somewhere. I do appreciate your great and brilliant knowledge.

                Comment


                • #9
                  Thanks for the (very) generous compliment.

                  I am just thinking aloud and focusing on (very) small details here. Suppose you want a binary variable and probabilities 0.73 and 0.27.

                  Then one method is awkward and the other isn't.

                  True in reverse for e.g. a uniform on the integers 1 and 42 (or 666, or whatever).
                  Last edited by Nick Cox; 27 Nov 2017, 08:58.

                  Comment

                  Working...
                  X