Announcement

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

  • New package: simulate2 and psimulate2

    Thanks to Kit Baum a new package is available on SSC. simulate2 and psimulate2 enhance and parallalise Stata's build-in simulate command.

    simulate2 follows the syntax of simulate and eases Monte-Carlo Type simulations. The novelties of simulate2 are that it allows the programs called by simulate2 to return macros (strings) to r() and e() which are then saved into the dataset in use. In addition it has advanced options to assign random number states (seed, seed stream and type) to a specific repetition of the simulation. Seeds can be saved to a Stata dataset (dta) or a frame to allow for a seamless replication of simulation results. Simulation results can be directly saved into frames.

    psimulate2 parallels simulate2. The number of replications are split into equal blocks and each block is run on a separate Stata instance. This speeds up simulations and allows for a more time efficient use of computational power if the computer in use has two or more cores. psimulate2 creates for each block a do file and a batch file. The batch file is then called in the separate Stata instance.

    Currently, psimulate2 only works in Windows machines. Both psimulate2 and simulate2 require Stata 16.

    Examples
    Make a dataset containing the OLS coefficient, standard error, the current time and save the seeds in a frame called seed_frame. Perform the experiment 1000 times:

    Code:
    program define testsimul, rclass
                    version 16
                    syntax anything
                    clear
                    set obs `anything'
                    gen x = rnormal(1,4)
                    gen y = 2 + 3*x + rnormal()
                    reg y x
                    matrix b = e(b)
                    matrix se = e(V)
                    ereturn clear
                    return scalar b = b[1,1]
                    return scalar V = se[1,1]
                    return local time "`r(current_time)'"
    end
    Run the simulation 1000 times and save the seed in a frame:
    Code:
    simulate2 time = r(time) b = r(b) V = r(V), reps(1000) saveseed(seed_frame,frame): testsimul 100
    Run the simulation 1000 times but with two parallel instance and use the seed:
    Code:
     psimulate2 time = r(time) b = r(b) V = r(V),  reps(500) parallel(2) seed(seed_frame seed, frame): testsimul 100
    For more info, please see the help file.

    simulate2 was inspired by comments from Alan Riley (StataCorp) and Tim Morris to a discussion during the Stata User Group meeting 2019 in London.

  • #2
    A new version of simulate2 and psimulate2 is available on SSC.

    The new version supports MacOSX and Unix operating systems. Kit Baum assisted in the implementation and his help is greatly appreciated. The following further changes were made:
    • mata defined functions are moved from the parent to the child instance
    • psimulate2 can run on Stata 15. In this case it uses simulate rather than simulate2 to run the simulations
    • minor bugs fixes

    Comment


    • #3
      Thanks to Kit Baum a new version of simulate2 and psimulate2 is available on SSC.
      The following changes were made:
      • added option seedstream() for psimulate2 to allow for parallel use of psimulate2 on different machines using the same seed
      • improved nesting into loops for psimulate2

      Comment


      • #4
        Thanks to Kit Baum a new version of simulate and psimulate2 is available on SSC.

        Comment


        • #5
          An update is available on GitHub and will be on SSC in due time.

          It can be installed by typing:

          Code:
          net install simulate2, from(https://janditzen.github.io/simulate2/)

          Comment


          • #6
            An update is available on GitHub and will be on SSC in due time.

            It can be installed by typing:

            Code:
            net install simulate2, from(https://janditzen.github.io/simulate2/)
            The update improves the use of (p)simulate2 on Unix servers and gives an example in the helpfile.

            Comment


            • #7
              Hi. psimulate2 throws the following error when running the ancillary file "psimulate2_examples.do":
              file /e do "E:\Temp/psim2_DoFile_1.do" not found
              However, the file is located in the temporary directory!! I am using STATA 11, BE. I have tried different placements of the temporary directory without success. Any suggestions? (I am using a Windows 11 operating system on an HP Envy with four cores. I also tried with a lenovo with 4 cores, same error)
              Thanks

              Comment


              • #8
                Hi Emiro,
                thank you very much for your interest in (p)simulate2. I am afraid, the package requires at least Stata 15 and for the parallel version Stata 16. The error occurs because the program is not able to find valid Stata exe.
                Hope that helps.
                Best.
                Jan

                Comment


                • #9
                  Dear Jan : Thanks, I am afraid I wrote the wrong Stata version: I am using STATA 18 BE on A Windows 11 laptop. I also tried on a different, laptop ,with a different stata 18 BE installation to be sure it was not a problem of my version. I had the same error.

                  Comment


                  • #10
                    Dear Emiro,
                    indeed, that makes more sense. If you were actually using Stata 11 there should have been another error message. I think the problem is the BE version. Can you please update your version using GitHub and try again?
                    Thanks!
                    Jan

                    Comment


                    • #11
                      Dear Jan: thanks very much indeed. After your comment I changed the exe() option to include the exe file: exe("C:\Program Files\Stata18\StataBE-64.exe") and it worked! I then installed the update from GitHub and it works fine without the exe() option. Thanks again. I am running some very large simulations with survival data and the parallel option is time-saver.

                      Comment


                      • #12
                        Awesome, glad it is working. Happy to help!

                        Comment

                        Working...
                        X