Announcement

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

  • How can I sample from a groupid?

    Dear statalists:
    I want to sample from a grouid without replacement and generate newids,but I can't find samilar examples for reference. The decription of my question is as following:

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    webuse nlswork
    egen groupid = group(race msp collgrad not_smsa c_city south )
    
    ****we will have 69 groups
    ****My question is:I want to sample 3 groups from groupid without replacement and recode them into new groupid
    ****For an example, after first sampling, whose groupid are 1,2,3 are sampled,it will have
    gen newgroupid1 = (groupid==1|groupid==2|groupid==3)
    ****after second sampling,whoes groupid are 2,4,7 are sampled,it will have
    gen newgroupid2 = (groupid==2|groupid==3|groupid==4)
    ****I want to repeat this sampling 100 times
    ------------------ copy up to and including the previous line ------------------


    I don't know how to write codes for the sampling process,.I wonder if you can give me some suggestions.Many thanks!
    Last edited by Bing Zhang; 30 Aug 2022, 04:48.

  • #2
    Bing, below please find a solution. Note that "group" is a 100-by-3 matrix where each row stores an outcome of sampling.

    Code:
    clear
    
    set obs 69
    forvalues i = 1/3 {
        gen g`i' = _n
    }
    
    fillin g1-g3
    keep if g2 > g1 & g3 > g2
    
    sample 100, count
    mkmat g1-g3, matrix(group)
    
    webuse nlswork, clear
    egen groupid = group(race msp collgrad not_smsa c_city south)
    
    forvalues i = 1/100 {
        gen newgroupid`i' = inlist(groupid, group[`i',1], group[`i',2], group[`i',3])
    }

    Comment


    • #3
      Originally posted by Fei Wang View Post
      Bing, below please find a solution. Note that "group" is a 100-by-3 matrix where each row stores an outcome of sampling.

      Code:
      clear
      
      set obs 69
      forvalues i = 1/3 {
      gen g`i' = _n
      }
      
      fillin g1-g3
      keep if g2 > g1 & g3 > g2
      
      sample 100, count
      mkmat g1-g3, matrix(group)
      
      webuse nlswork, clear
      egen groupid = group(race msp collgrad not_smsa c_city south)
      
      forvalues i = 1/100 {
      gen newgroupid`i' = inlist(groupid, group[`i',1], group[`i',2], group[`i',3])
      }
      many thanks!your solution is very exquisite!

      Comment

      Working...
      X