Announcement

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

  • global or local seed setting in loop

    Dear all,

    I want to run a loop in which the seed changes for every single run:

    Code:
    forvalues mc = 1/100 {
    
    
        * global - seed setter! - same seed in every dofile
        global mc `mc'
        set seed `mc'
    
        use "${data}run_monte_carlo.dta", clear
        
        xtset pid syear
        
        *** run do-files
        
        do "${do}dofile_1.do"
        
        do "${do}dofile_2.do"
        
            save "${data}loop.dta", replace
        
            use "${data}monte_carlo.dta", clear
        
            merge 1:1 pid syear using "${data}loop.dta", keep (1 3) keepusing (varnames) nogen
            
            save "${data}monte_carlo.dta", replace
            }
    where the seed should be equal to `mc'.

    I also want to set the seed in the dofiles 1 and 2 to `mc'.

    How can I do that? Any help would be really appreciated.
    Thank your very much in advance.

  • #2
    I am afraid I do not understand.

    mc is defined somewhere out...
    mc is not changing every round
    mc is dereferences as a local, not a global macro...

    Comment


    • #3
      Code:
      forvalues mc = 1/100 {
      
      
      * global - seed setter! - same seed in every dofile
      global mc `mc'
      set seed `mc'
      Setting the seed here is unnecessary if it is to be reset in the do-files later because there is nothing random in the code that follows.

      You can pass arguments to do-files much like you pass arguments to Stata commands; try something like

      Code:
      ...
      *** run do-files
      
      do "${do}dofile_1.do" `mc'
      
      do "${do}dofile_2.do" `mc'
      ...
      the in the do-files, refer to `1', e.g.

      Code:
      *** first line of do-file
      
      version 15
      
      set seed `1'
      
      ...
      Best
      Daniel

      Comment


      • #4
        I thought that since the code start with:
        Code:
         forvalues mc = 1/100 {
        that mc would change from being 1 in the first round to 2 in the second round and so on ...

        And in every round I would like to set the seed to mc - so to 1 in the first round, to 2 in the second round and so on.

        However, since I use the global "do" to run my dofiles, I am not sure how to make sure that the seed for the estimations in the dofiles 1 and 2 is also set to mc.

        Comment


        • #5
          Toni, does then not what you are doing work by itself? I mean if you have defined a global macro, in my understanding then it is available everywhere, so you just need to reference to it in the sub-do files, some somewhere in both do files you should have the seed set to the dereferenced value of the global?

          This is my understanding of how globals work from reading the manual, this is why they are global because they are globally available... But to be honest I never use globals, so I dont have much experience. Here is a link explaining the difference between global and local.

          https://blog.stata.com/2015/11/03/pr...-local-macros/

          Comment


          • #6
            Thank you Daniel!

            Maybe it was misleading - the dofiles are different from each other and they do different things, however the seed in the dofiles should be `mc' and should therefore change in every round.

            So instead of dofile_1 and dofile_2, imagine them as "dofile_a" and "dofile_b"

            Comment


            • #7
              Notwithstanding the curious fact of Stata that Daniel just taught us (I did not know that one can pass arguments to do files...)...

              I re-read the Stata blog post I referenced, and it seems to me that what I was saying is correct.

              You just start defining the global macro as you have done in your code in #1 and then all you have to have is one line of code before the relevant randomisation starts in both

              dofile_1.do and
              dofile_2.do saying
              Code:
               set seed $mc

              Comment


              • #8
                Toni

                I am sorry, I cannot make sense of your latest post; nothing of what I have written concerns the do-file names. I was trying to say:

                1. You do not need to define global macros; local macro will suffice.
                2. You do not need to set the seed inside the loop you show in your original post since none of the commands that you show relies on any random numbers
                3. You pass arguments, in your case the contents of local macro mc, to the sub-do-files by just adding them after the do-file's names (whatever these names may be).
                4. You refer to these arguments inside the sub-do-files by position: `1' is the first argument, `2' is the second, and so on; `0' contains all arguments.

                If you prefer global macros (these can mess things up and lead to hard to identify bugs, especially when used with cryptic, simple names such as mc) that is your choice. In this case, Joro is right, and you can just reference the global macros inside your sub-do-files. There is still no need to set the seed inside teh loop, though.

                Best
                Daniel
                Last edited by daniel klein; 04 Jan 2019, 10:40.

                Comment


                • #9
                  Originally posted by Joro Kolev View Post
                  Notwithstanding the curious fact of Stata that Daniel just taught us (I did not know that one can pass arguments to do files...)...
                  It is documented quite clearly. I see that this is easily overlooked because you typically do not bother reading

                  Code:
                  help do
                  as its common use is pretty intuitive.

                  Best
                  Daniel

                  Comment


                  • #10
                    Toni Moreno Without understanding anything about the objective of your code, I feel obliged to point out the following discussion about the advisability, or lack of advisability, of setting the seed repeatedly, in the output of help set seed (for Stata 15.1, at least).

                    If you have not reviewed that material with reference to the work you describe in post #1, I recommend it to your attention. Or better yet, the full discussion in the Stata Base Reference Manual PDF included in your Stata installation and accessible from Stata's Help menu.

                    Comment


                    • #11
                      Thank you very much everybody for your help. And thank you William for pointing out the discussion about the advisability of setting the seed repeatedly. I will have a read.

                      Comment

                      Working...
                      X