Announcement

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

  • Bug in stata runiform()?

    Dear Statalist members,

    To select all observations, per ID, in my panel data set, I've written the following code:

    Code:
    local idrandom round(runiform()*10) // generate random number to select unique ID
    tab id if id == $idrandom, nolabel // show results of corresponding ID
    display $idrandom // display the random number used
    This results in the following output

    Click image for larger version

Name:	weird_result_runiform.gif
Views:	2
Size:	5.8 KB
ID:	18684
    The BvD ID number is an unique identifier per firm (10 years of observations per firm).

    Therefore, it should return only one BvD ID number, in this case, 3

    Strangely enough, the display $idrandom does give only one result.

    Could someone explain to my why Stata 12 returns TRUE in the IF statement for more values that ID == 3?

    Thanks!

    Kind regards,

    Matthijs Alderliefste

  • #2
    First, this is the wrong forum, this is the Mata forum.

    Second, I'll point out that $idrandom references a global macro, not the local macro assigned in the first line. The global must be assigned the name of a variable. Here's how I reproduced your problem:

    Code:
    clear
    set obs 100
    gen id=round(_n/10,1)
    gen id2=round(_n/20,1)
    global idrandom id2
    
    
    local idrandom round(runiform()*10) // generate random number to select unique ID
    tab id if id == $idrandom           // show results of corresponding ID
    display $idrandom                   // display the random number used


    hth,
    Jeph

    Comment


    • #3
      As a third point, I would like to point out that round(runiform() * 10) does not what you probably want it to do. You probably want to select an integer from 1 to 10 included, all with equal probability. However, with round(runiform() * 10) you select a number from 0 up to 10 included, where 0 and 10 have a smaller probability of being chosen than 1 to 9 (only the uniform numbers from 0 to 0.05 - epsilon map to zero and 0.95 to 1 map to 10). You probably want to use ceil(runiform() * 10) or floor(runiform() * 10 + 1) if the fact that runiform() returns up to 1 - 2 ^ (-32) concerns you.

      Comment


      • #4
        Matthijs,

        your global is a name of the variable. display will not show you more than one value of course, but rather than giving an error message it shows the first value of that variable.
        instead you may want to use:
        Code:
        list $idrandom
        display "$idrandom"
        No error or problem or bug on Stata's side.

        Best, Sergiy Radyakin

        Comment

        Working...
        X