Announcement

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

  • Stratified random sampling with different number of samples in each strata

    Hi,

    I have a dataset with respondents from different geo locations(District).

    E.g,
    ID District Age
    1 A 10
    2 A 12
    3 A 15
    4 B 16
    5 B 17
    6 B 18
    7 A 19
    8 B 20
    9 A 21
    10 B 32

    I want to randomly draw 3 observations from District A and 2 observations from District B. What is the command in stata to attain the following sample?

    Thanks in advance.
    Last edited by Nelson Mathews; 15 Sep 2022, 06:16.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id str2 district byte age
     2 "A" 12
     9 "A" 21
     7 "A" 19
     1 "A" 10
     3 "A" 15
     5 "B" 17
    10 "B" 32
     8 "B" 20
     4 "B" 16
     6 "B" 18
    end
    
    
    gen wanted_size = 3 if district == "A"
    replace wanted_size = 2 if district == "B"
    
    set seed 1234 // OR WHATEVER SEED YOU LIKE
    gen double shuffle = runiform()
    by district (shuffle), sort: keep if _n <= wanted_size
    drop shuffle
    
    list, noobs clean
    In the future, when showing data examples, please use the -dataex- command to do so, as I have done here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment

    Working...
    X