Announcement

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

  • Create n random samples from existing list

    Hi,

    I am quite new to STATA and my issue might be easy to solve.

    I created a list of random numbers using

    Code:
    set obs 10000
    generate X = runiform(0,1000)
    Now I would like to draw n samples of this list, each with 100 observations. For each sample, a new variable should be created. The samples should be independently drawn, so with replacement. I tried
    Code:
    bsample 100
    however the population (e.g. my list of 10,000 random numbers) is deleted each time a sample is drawn and I couldn't find a way yet to define a variable for each sample.

    Thanks!

  • #2
    The user-written program -gsample- (-ssc describe gsample-) is one way to do this. -gsample- allows you to create a new variable that marks the frequency with which each observation is to be included in a with-replacement sample (0 or 1 or 2 or ... times), where this frequency variable sums to _N for each sample. One could then do whatever analysis is desired by using that variable as what Stata calls a "frequency weight." In many applications this approach is preferable to actually creating a new variable to hold the *value* of your sampled variable. This approach might or might not be convenient for your application, but here's an illustration, using the sample mean as the desired "analysis."

    Code:
    ssc install moremata // another user-written program required by the gsample program
    ssc install gsample
    //
    clear
    set obs 10000
    generate X = runiform(0,1000)
    // Illustration
    set seed 48768 // whatever
    local reps = 3
    forval i = 1/`reps' {
        gsample 100, gen(f`i')
    }     
    //
    summ X [fw = f1]
    summ X [fw = f2]
    summ X [fw = f3]





    Comment


    • #3
      If you really want samples, not weights, and want to keep the original data in memory, here's one approach:

      Code:
      clear
      set obs 10000
      set seed 1234
      generate X = runiform(0,1000)
      local n=20           // n samples of 100
      forv k=1/20 {
          gen rnd=runiform()
          sort rnd
          gen byte sample_`k'=_n<=100
          drop rnd    
      }
      this will create 20 different indicators, one for each sample of 100.

      hth,
      Jeph

      Comment


      • #4
        Thank you very much, very helpful answers!

        Comment

        Working...
        X