Announcement

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

  • Create a new variable with random probability and with probability

    I have to create 2 variables

    the first one is a random variable (e1) between 1-17
    the second is a variable (e2) between 1-17 and with probability of 0.8.

    But the condition of the second variable have to be < of e1

    gen e1 = 0+int((17-0+1) * runiform())

    gen e2 = rbinomial(16, 0.8)<e1

    but e2 give me only 0 and 1

    Thanks

  • #2
    I am not sure of exactly what you want, but in general

    Code:
    gen whatever = x < y
    will yield 1 if x < y and 0 otherwise.

    This may help in terms of technique:

    Code:
    clear 
    set obs 1000 
    set seed 2803 
    set scheme s1color 
    
    gen e1 = runiformint(0, 16)
    gen e2 = rbinomial(16, 0.8)
    
    quantile e1, name(G1) yla(0/16, ang(h)) 
    quantile e2, name(G2) yla(0/16, ang(h))
    graph combine G1 G2, ycommon
    Click image for larger version

Name:	random.png
Views:	1
Size:	29.2 KB
ID:	1486902


    Comment


    • #3
      The statement :
      Code:
      gen e2 = rbinomial(16, 0.8)<e1
      Stata evaluates it, returning 1 if the statement is true and 0 if it is false.

      Perhaps you want:
      Code:
      clear
      set seed 123123
      set obs 100000
      gen e1 = 0+int((17-0+1) * runiform())
      gen e2 = e1 if rbinomial(16, 0.8) <e1
      sum
      tab e2
      Given the probability of success and the number of trials, the probability of observing 8 of fewer successes is less than one percent
      Code:
      . disp binomial(16,8, .8)
      .00700356

      Comment


      • #4
        thanks but e2 need to be little than e1 (<)

        Sorry my english is not the best but it's ok

        Thanks for your support

        Comment


        • #5
          Thanks Mr Merryman, I'm very near of the goal but now my e2 is equal to my e1... E2 can be equal or lower of e1 but not equal each time

          Comment


          • #6
            It's possible to use rnormal and change the probability?

            Thanks

            Comment


            • #7
              It seems to me you are saying that you first draw a uniform integer e1 between 1 and 17, and then draw a binomial random variable e2 with N = e1-1 and p=17 so that e2 will be less than e1.

              Two problems with this formulation.

              First, if e1 is 1 then for e2 to be less than e1, e2 is forced to be 0, while you say it must be 1 or more.

              Second, if e2 is forced to be 0, then it cannot be binomial random variable in that case but must be handled differently.

              Perhaps the following code points in the direction you want. I have changed 17 to 5 in generating e1 so that the tabulation will fit the page.
              Code:
              cls
              clear 
              set obs 1000 
              set seed 2803 
              set scheme s1color 
              
              generate e1 = runiformint(1, 5)
              generate e2 = rbinomial(e1-1, 0.8)
              replace  e2 = 0 if e1==1
              
              tab e1 e2, missing
              Code:
              . tab e1 e2, missing
              
                         |                           e2
                      e1 |         0          1          2          3          4 |     Total
              -----------+-------------------------------------------------------+----------
                       1 |       187          0          0          0          0 |       187 
                       2 |        38        173          0          0          0 |       211 
                       3 |        10         51        132          0          0 |       193 
                       4 |         2         16         83        104          0 |       205 
                       5 |         0          1         22         86         95 |       204 
              -----------+-------------------------------------------------------+----------
                   Total |       237        241        237        190         95 |     1,000

              Comment


              • #8
                Hello everyone thanks for your help, i have another issue, the formula gen id= int(runiform(1,499999))-1 for another problem work in stata 15 but did'nt work in stata 13. Do you have some idea of what I have to use for satat 13

                Thanks

                Comment


                • #9
                  Please post this as a new topic.

                  Before doing that, please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], as described in section 12 of the FAQ.

                  Section 12.1 is particularly pertinent

                  12.1 What to say about your commands and your problem

                  Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
                  ...
                  Never say just that something "doesn't work" or "didn't work", but explain precisely in what sense you didn't get what you wanted.
                  The more you help others understand your problem, the more likely others are to be able to help you solve your problem.




                  Comment

                  Working...
                  X