Announcement

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

  • How to write a while statement for the purpose detailed in the post?

    Dear all,

    I have two variables: seq1 and u.

    seq1 is a repeating sequence: 1,2,3,4,5
    u is a random integer between 1 and 5, generated by u = 4*uniform() + 1

    Now I want to achieve this: when u equals to seq1, do the randomisation again until u has a different value as seq1.

    My code is:

    foreach var in u{
    foreach num in seq1{
    while `num' == `var'{
    replace `var' = 4*uniform() + 1)
    }
    }
    }

    No error in this code but it didn't work for my purpose. After I run it, nothing changed.

    So would you mind help me figure out why this code cannot work and what I need to do to achieve my purpose?

    Thank you very much!

  • #2
    I think simply doing

    Code:
    replace  u = 4*uniform() + 1 if u==seq1
    should achieve the goal you have in mind.

    Comment


    • #3
      Thanks for the suggestion.

      Yes, it indeed works but I have to manually run it several times in order to eliminate all repeating values in 2 columns. I'm wondering whether using while loop could make this process automatic?

      Look forward to alternative solutions.

      Comment


      • #4
        I'm not sure I agree with Joro, but that's largely because I'm totally unsure what you are trying to achieve.

        u is a random integer between 1 and 5, generated by u = 4*uniform() + 1
        No, u is not a random integer between 1 and 5. The uniform() function generates a random real number between 0 and 1. To generate a random integer between 1 and 5, you use the function runiformint(1,5).

        I do not understand what

        when u equals to seq1, do the randomisation again until u has a different value as seq1.
        Is seq1 also a variable in your dataset, taking the values 1 2 3 4 5 1 2 3 4 5 and so forth? So if in the third observation seq is 3 and u is also 3, you want to change u to one of 1 2 4 5, and if u is not 3, you want to leave it alone?

        In that case
        Code:
        generate temp = runiformint(1,4)
        replace u = temp + cond(temp<seq1,0,1) if u==seq1
        drop temp
        will do what you want. Or, you could use the following code to generate u to begin with:
        Code:
        generate u = runiformint(1,4)
        replace u = u + cond(u<seq1,0,1)

        Comment


        • #5
          Indeed there is some misunderstanding here. If you are doing what the command you show is doing, that is generating a continuous uniform variable u~(1,5), theoretically speaking the probability of this u coinciding with a set of 3 discrete values 2,3,4 is 0.

          In 1 million observations, I got 0 coincidences. So how many times you might possibly need to re-randomise if by some chance you got a coincidence?

          Code:
          . set obs 1000000
          number of observations (_N) was 0, now 1,000,000
          
          . set seed 69
          
          . egen seq1 = seq(), from(1) to(5)
          
          . gen u = 4*uniform() + 1
          
          . assert seq1!=u
          
          . summ u
          
              Variable |        Obs        Mean    Std. Dev.       Min        Max
          -------------+---------------------------------------------------------
                     u |  1,000,000    2.999572    1.155054    1.00001   4.999993
          
          . summ seq1
          
              Variable |        Obs        Mean    Std. Dev.       Min        Max
          -------------+---------------------------------------------------------
                  seq1 |  1,000,000           3    1.414214          1          5
          
          .

          Comment

          Working...
          X