Announcement

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

  • Beginner Monte Carlo Simulation Help

    Hi all,

    I'm new at Stata and am having trouble running a simple Monte Carlo simulation. I'm looking to create 100 observations on uniformly-distributed variables with 100 and 1000 trials. I'm looking to plot the sample means. Though I'm not receiving an error message with this code, all of the sample means are coming up as missing values (dots), so I'm not getting any kind of actual simulated data. If anyone could help me out with where I'm going wrong, that would be much appreciated. My code is included below.

    .
    Click image for larger version

Name:	Screen Shot 2017-01-16 at 12.14.38 PM.png
Views:	1
Size:	21.9 KB
ID:	1370494


    Thanks,
    Claire

  • #2
    Cross-posted at http://stackoverflow.com/questions/4...ation-in-stata

    Please note our cross-posting policy, which is that you should tell us about it. Also, as commented there code in image form is not so helpful as code that can be copied and pasted. This also is explained in our FAQ Advice.

    Please do read http://www.statalist.org/forums/help before posting, as we ask.

    Fortunately, the problem is straightforward.

    There is no summarize statement to calculate a mean. So r(mean) is just never defined. Referring to a r-class result that is not defined is not itself illegal, but the result is necessarily missing. (In principle some other command could leave behind r(mean) too.)
    Last edited by Nick Cox; 16 Jan 2017, 10:45.

    Comment


    • #3
      And when you fix the problem pointed out by Nick, you will encounter another problem: you will get the same mean for every sample in your simulation. That's because you have -set seed- inside your program. So each time the program is called, the seed is reset to the same value and the exact same pseudo-random numbers are regenerated. You need to remove the -set seed- command from inside program montecarlo12. (The -set seed- command that immediately precedes your -simulate- command is fine, however, as it will only be executed once. Note, too, that -simulate- has a -seed()- option that you could use instead of a separate -set seed- command.)

      By the way, program montecarlo12 seems to generate two random variables, but it returns only one mean parameter (well, as written, as Nick points out, it returns none, but presumably you will fix that). What is the point of generating two different random variables here?

      Comment


      • #4
        Some time ago I gave a brief tutorial on doing simulations in Stata at the North American Stata Users' meeting. The materials can be found here: http://maartenbuis.nl/presentations/chicago08.html
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment

        Working...
        X