Announcement

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

  • how to randomly sample from an unbalanced dataset

    Hey colleages,

    I would randomly sample 1 observation from the following dataset:

    identifier number of obs.
    1 1004
    2 321
    3 5
    4 1
    5 16
    6 114
    .....

    I had expected to sample 1 obs. for each identifier, but I soon notice that the data points are very unevenly distributed.

    I would like to know how I should sample under such circumstances. Note: I sample in order to construct a negative case dataset for my research.

    Thanks a lot!

    Raymon Lucas

  • #2
    Raymon:
    you may want to try:
    Code:
    sample 1, count by(identifier)
    As expected, the -id- with one observation only will return one and only one value, as you can see from the following toy-example:
    Code:
    . set ob 11
    Number of observations (_N) was 0, now 11.
    
    . g id=1 in 1/10
    
    
    . replace id=2 in 11
    
    
    . g A=runiform()
    
    . list
    
         +---------------+
         | id          A |
         |---------------|
      1. |  1   .8712187 |
      2. |  1   .7664683 |
      3. |  1   .2512555 |
      4. |  1   .1663648 |
      5. |  1   .7437958 |
         |---------------|
      6. |  1   .9805113 |
      7. |  1   .7295772 |
      8. |  1   .9011049 |
      9. |  1   .2643649 |
     10. |  1   .8856509 |
         |---------------|
     11. |  2    .882112 |
         +---------------+
    
    . sample 1, count by(id)
    (9 observations deleted)
    
    . list
    
         +---------------+
         | id          A |
         |---------------|
      1. |  1   .9805113 |
      2. |  2    .882112 |
         +---------------+
    
    .
    Your problem, if any, has nothing to do with the random sampling approach, but it is data related (and I fear there's nothing you can do about that).
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Carlo, many thanks for your helpful reply, which makes me reassured about my choice. Thank you again! Raymon

      Comment

      Working...
      X