Announcement

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

  • Not able to replicate a sample drawn even when using set seed

    Hi Stata Listers,

    I drew a random sample from my dataset recently, using set seed first, but when I re-run the commands, a different sample is drawn.

    I am wondering why I'm not getting the same subset of my data each time.

    Here is a snippet of the code I'm using in Stata 13:

    *Sort the unique identifier
    sort ID
    *Set the seed using a random number
    set seed 66848309
    *Sample 60 records from each group, defined by the variable "group"
    bysort group: sample 60 if group==1 | group==2 | group==3, count

    I've read that this may be due to the sort not being stable, but I haven't been able to use the stable option with bysort. Any ideas?

    Thanks for reading.




  • #2
    So if you want to sample 60 records from each of groups 1, 2, and 3, just do:

    Code:
    keep if inlist(group, 1, 2, 3)
    set seed 66848309
    sample 60, count by(group)
    This will be reproducible.

    Your original code includes an initial sort on ID, which then just gets undone, so it has no effect. Then, you used -bysort group:- instead of the -by- option of the -sample- command. By invoking -bysort- you invited the unstable sort into the mix.

    Comment


    • #3
      If you haven't already done so, it's probably worth confirming that ID uniquely identifies the records.
      Code:
      isid ID

      Comment


      • #4
        Thank you, Clyde. Your suggestion solved the problem. I didn't want to drop data from the other groups in my dataset, but I'm only sampling from groups 1-3, so I modified your suggestion to:

        sample 60 if group==1 | group==2 | group==3, count by(group) and that is reproducible.


        Thanks for your comment, as well, William. ID does uniquely identify the records, but Clyde is correct that I was undoing the -sort ID- by following it with -bysort-



        Comment

        Working...
        X