Announcement

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

  • runiform function and r(N) scalar

    Hi,

    I am trying to use the runiform function to randomally select 1/3 of the observations. Unfortunately, it seems that the use of the function affects unrelated results.
    For example, look at this picture:

    Click image for larger version

Name:	do file.png
Views:	1
Size:	6.2 KB
ID:	1537978


    As you can see, I am writing the exact same sum command in lines 19 and 25. These are the results of the return list:

    Click image for larger version

Name:	stata output.png
Views:	1
Size:	13.4 KB
ID:	1537979


    The return lists are totally different, even though this is the same sum command, and even though the ruinform command is not related in any way to the summed variable.
    Does anyone knows what may causes this?

    Thanks,
    Nir.

  • #2
    Nir:
    Stata returns this seemingly weird results because your second -sum- includes -RandomNumber-, too, as you can see from the following toy-example:
    Code:
    . use "C:\Program Files\Stata16\ado\base\a\auto.dta"
    (1978 Automobile Data)
    
    
    . sum if rep78==3 [fw=weight]
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
            make |          0
           price |     98,970    7003.154    3748.311       3291      15906
             mpg |     98,970    18.70183    4.011309         12         29
           rep78 |     98,970           3           0          3          3
        headroom |     98,970     3.20774    .7557963          2        4.5
    -------------+---------------------------------------------------------
           trunk |     98,970    15.72224    3.516451          9         23
          weight |     98,970    3464.887     716.731       1830       4840
          length |     98,970    198.1066    19.35086        142        233
            turn |     98,970    41.80277    4.130643         31         51
    displacement |     98,970    248.2548    91.34204         79        425
    -------------+---------------------------------------------------------
      gear_ratio |     98,970    2.797555    .3843342       2.24       3.72
         foreign |     98,970    .0609276    .2391986          0          1
    
    . return list
    
    scalars:
                      r(N) =  98970
                  r(sum_w) =  98970
                   r(mean) =  .0609275538041831
                    r(Var) =  .0572159651058491
                     r(sd) =  .2391985892639191
                    r(min) =  0
                    r(max) =  1
                    r(sum) =  6030
    
    . gen RND=runiform() if price>3000 & price!=.
    
    . sum if rep78==3 [fw=weight]
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
            make |          0
           price |     98,970    7003.154    3748.311       3291      15906
             mpg |     98,970    18.70183    4.011309         12         29
           rep78 |     98,970           3           0          3          3
        headroom |     98,970     3.20774    .7557963          2        4.5
    -------------+---------------------------------------------------------
           trunk |     98,970    15.72224    3.516451          9         23
          weight |     98,970    3464.887     716.731       1830       4840
          length |     98,970    198.1066    19.35086        142        233
            turn |     98,970    41.80277    4.130643         31         51
    displacement |     98,970    248.2548    91.34204         79        425
    -------------+---------------------------------------------------------
      gear_ratio |     98,970    2.797555    .3843342       2.24       3.72
         foreign |     98,970    .0609276    .2391986          0          1
             RND |     98,970    .5004157    .2550064   .1649395   .9881987
    
    . return list
    
    scalars:
                      r(N) =  98970
                  r(sum_w) =  98970
                   r(mean) =  .5004157180664237
                    r(Var) =  .0650282703071466
                     r(sd) =  .2550064122863317
                    r(min) =  .1649395227432251
                    r(max) =  .9881986975669861
                    r(sum) =  49526.14361703396
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment

    Working...
    X