Announcement

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

  • Storing the index of random seed in a loop for future replication

    Hi Everyone,

    I'm struggling with storing the index of a random seed in a file to be used for future replication. According to the help file of set seed, we can store the index using the command local and refer to it later. It worked well as below.
    (I cannot use set seed because I'll actually do it in a loop for 10000 times. If I set seed for each simulation, the result won't be totally random according to the help file of set seed. So the general idea here is that I do the 10000-times simulation totally randomly for the first time, and I would like to store the 10000 index used within. And the goal would be that in the future anybody can replicate my results by referring to the index I stored.)

    sample 5, count
    local state = `c(rngstate)'

    *replicate
    set rngstate `state'
    sample 5, count


    However, I need to store the index in a separate file instead of a local variable. I tried to do that by storing the `c(rngstate)' as a string variable.

    sample 5, count
    gen state = "`c(rngstate)'"
    keep state
    save "seed"

    I was hoping that others can retrieve the index by the following code:
    *replicate
    merge m:1 loop using "seed"
    set rngstate state
    sample 5, count

    By displaying, I confirmed that this string variable and the local variable mentioned above has the exact same format (very long string). State returned that 'state' is an invalid mt64 state after the line "set rngstate state".
    And I actually figured out why that error message pops up: c() is something that can only be referred to but not assigned. Now I have no idea how I should proceed.

    I really appreciate any advice!

  • #2
    And I actually figured out why that error message pops up: c() is something that can only be referred to but not assigned.
    I don't think that's correct.

    I think the problem with -set rngstate state- is that state is a variable and it expects instead a (long) string literal. I'm not entirely sure what you're trying to do, but here is some code that shows how you could save the random number generate state in a dataset (well, I did it in a frame, but a tempfile creating a data set would work the same way) and then reuse those states to reproduce the same results:

    Code:
    clear*
    
    sysuse auto, clear
    
    set seed 1234
    
    frame create seeds int loop strL state
    
    preserve
    forvalues i = 1/3 {  // RUN 3 SAMPLES
        frame post seeds (`i') (`"`c(rngstate)'"') // SAVE THE RNG STATE IN FRAME seeds
        sample 5, count
        list, noobs clean
        restore, preserve
    }
    restore, not
    
    frame seeds: list // SHOW WHAT YOU GOT
    frame seeds: save seed_file, replace // TO MAKE THESE SEEDS AVAILABLE TO OTHERS:
    
    clear
    forvalues i = 1/3 { // NOW REPLICATE THE SAME RANDOM SAMPLES
        frame change seeds
        set rngstate `=state[`i']' // PULL THE i'TH VALUE OF STATE
        frame change default
        sysuse auto, clear  // DO THE SAMPLE
        sample 5, count
        list, noobs clean  // SEE THE SAME RESULTS AS BEFORE
    }

    Comment


    • #3
      Thank you so much! That solves the problem!

      Comment

      Working...
      X