Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: "bsample" and "set seed" together do not work!


From   Daniele Pacifico <[email protected]>
To   [email protected]
Subject   Re: st: "bsample" and "set seed" together do not work!
Date   Fri, 17 Sep 2010 15:35:41 +0000 (GMT)

Dear Maartin,
Starting from your example I think I found a way that works fine..

Here is what I did:

*start example*
clear all
set obs 1000
drawnorm x
local r=6
 forvalues i=1/`r'{
 preserve
  bsample
local o_seed `c(seed)'
 set seed 12345
gen uni=runiform()
  su x uni
  set seed `o_seed'
  restore
}
*break example*

This code does exactly what I wanted to do.. Indeed, now I always get different 
values for the mean of x, even if I set my own seed within the routine.. 

On the other end, with my previous code: 

*continue example*
local r=6
forvalues i=1/`r'{
preserve
  bsample
  su x
  set seed 12345
  gen uni=runiform()
  su x unirestore
}

*end example*

..I always got the same values for the mean of x...


Hence, within my ado file I first save the "starting" seed:


local original_seed `c(seed)'

Then, if the user wants to set a specific seed, he/she can provide the specific 
number, say 12345

Hence, in the ado file the next line: 
 
set seed `seed'

becomes

set seed 12345
gen double `unif'=runiform()

..and the file generates a random variable using the previous seed (12345)... 
However, since in the following lines I also typed:


set seed `original_seed'

..the ado files restores the starting seed.

In this way, I can eventually use the command "bsample" with my new command 
since the internal seed of my ado file is used only to generate the internal 
random variable.. Indeed, after that, the original seed (from bsample) is 
restored and everything works fine!
I think it actually works!


Thanks for your help!
Best,
Daniele





----- Original Message ----
From: Maarten buis <[email protected]>
To: [email protected]
Sent: Fri, 17 September, 2010 16:42:15
Subject: Re: st: "bsample" and "set seed" together do not work!

--- On Fri, 17/9/10, Daniele Pacifico wrote:
> Well, I actually need to set the seed inside my program..
> In your opinion, is there a way to create a "temporary 
> seed" within a ado file? I mean, something like a 
> temporary variable that disappears once the program
> ends?

I still don't think that this is the best way of doing 
what you want to do. Setting seeds inside general use 
programs still seems like bad practice, so I strongly
recommend that you rethink this (or tell us why you 
think this is necesary, so we can think about it).

Having said that, the current state of the seed can be
recovered using c(seed), so you can store that in a 
local and than set the seed to some constant, do your
stuff under the constant seed, and than set the seed
to the current state to continue where you where 
before you moved to the constant seed.

*------------ begin example -----------------
local seed `c(seed)'
forvalues is = 1/5 {
    set seed  `seed'
    di "should change: " runiform()
    local seed `c(seed)'
    set seed 12345
    di "should be constant: " runiform()
        di _n
}
*-------------- end example ------------------ 

Hope this helps,
Maarten

--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany

http://www.maartenbuis.nl
--------------------------


      

*
*   For searches and help try:
*  http://www.stata.com/help.cgi?search
*  http://www.stata.com/support/statalist/faq
*  http://www.ats.ucla.edu/stat/stata/



      


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index