Announcement

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

  • random sampling

    Hello! So I have a dataset that has a total of 169 observations with 40 f and 129 m. now I have to randomize this into 3 groups such that each group has approximately an equal number of males & females. how must I go about doing this?

  • #2
    Code:
    clear 
    set obs 169 
    gen id = _n 
    gen female = _n <= 40 
    
    * you start here; change -female- to your own variable name if different 
    set seed 31459
    gen double rnd = runiform() 
    bysort female (rnd) : gen group = ceil(3 * _n / _N)
    
    tab group female 
    
    
               |        female
         group |         0          1 |     Total
    -----------+----------------------+----------
             1 |        43         13 |        56 
             2 |        43         13 |        56 
             3 |        43         14 |        57 
    -----------+----------------------+----------
         Total |       129         40 |       169 
    
    .

    Comment


    • #3
      As stated, your request is impossible. The males outnumber the females by more than 3:1, so you cannot possibly exhaustively assign them (randomly or otherwise) into groups containing nearly equal numbers of males and females.

      I think what you meant is to assign them to groups so that each sex is approximately evenly distributed across all three groups. You did not provide example data, so I have created a toy data set to illustrate the approach. Modify the code to adapt it to your actual data set.

      Code:
      //  CREATE TOY DEMONSTRATION DATA SET
      clear*
      set obs 169
      gen int id = _n
      gen sex = "f" if _n <= 40
      replace sex = "m" if missing(sex)
      
      
      //  DIVIDE INTO THREE RANDOM SUBGROUPS
      //  RANDOMIZE IN BLOCKS OF SIZE 6
      set seed 1234 // OR YOUR PREFERRED RANDOM NUMBER SEED
      by sex, sort: gen int block = ceil(_n/6)
      gen double shuffle = runiform()
      by sex block (shuffle), sort: gen byte group = ceil(_n/2)
      drop block shuffle
      sort id
      
      tab sex group, row
      Added: Crossed with #2. The approach shown there is a simple randomization. While it has a high probability of distributing the males and females nearly evenly across the groups, it is not guaranteed to do so. If you experiment with that approach using different random number seeds, you will find that some of the results will be substantially imbalanced. The block randomization approach shown here will always produce an almost even distribution of each sex across all three categories.

      Added: I should add that what you are trying to do here is not random sampling, it is randomization (also called random assignment).
      Last edited by Clyde Schechter; 27 May 2023, 11:03.

      Comment


      • #4
        I don't follow the criticism in #3. The machinery ceil(3 * _n/_N) divides into thirds, as closely as possible. Which people end in which group will naturally vary.

        Code:
        clear 
        set obs 169 
        gen id = _n 
        gen female = _n <= 40 
        
        * you start here; change -female- to your own variable name if different 
        
        
        foreach seed in 31459 42 666 271828 280352 { 
            set seed `seed'
            gen double rnd = runiform() 
            bysort female (rnd) : gen group = ceil(3 * _n / _N)
            di "{title:`seed'}"
            tab group female 
            drop rnd group
        } 
        
        31459
        
                   |        female
             group |         0          1 |     Total
        -----------+----------------------+----------
                 1 |        43         13 |        56 
                 2 |        43         13 |        56 
                 3 |        43         14 |        57 
        -----------+----------------------+----------
             Total |       129         40 |       169 
        42
        
                   |        female
             group |         0          1 |     Total
        -----------+----------------------+----------
                 1 |        43         13 |        56 
                 2 |        43         13 |        56 
                 3 |        43         14 |        57 
        -----------+----------------------+----------
             Total |       129         40 |       169 
        666
        
                   |        female
             group |         0          1 |     Total
        -----------+----------------------+----------
                 1 |        43         13 |        56 
                 2 |        43         13 |        56 
                 3 |        43         14 |        57 
        -----------+----------------------+----------
             Total |       129         40 |       169 
        271828
        
                   |        female
             group |         0          1 |     Total
        -----------+----------------------+----------
                 1 |        43         13 |        56 
                 2 |        43         13 |        56 
                 3 |        43         14 |        57 
        -----------+----------------------+----------
             Total |       129         40 |       169 
        280352
        
                   |        female
             group |         0          1 |     Total
        -----------+----------------------+----------
                 1 |        43         13 |        56 
                 2 |        43         13 |        56 
                 3 |        43         14 |        57 
        -----------+----------------------+----------
             Total |       129         40 |       169



        Comment


        • #5
          Sorry, Nick, you're right.

          Comment


          • #6
            You had me worried there until I verified...

            Comment


            • #7
              Thank you so much Nick and Clyde! This was super helpful. I was able to run the code shared by Nick and got the desired results.

              Comment

              Working...
              X