Announcement

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

  • Code help: Monte Carlo simulation

    I've attached a small Do file for a Monte Carlo Simulation I am running for an economics class. For some reason the code errors. Can anyone explain what may be causing the issue?
    Attached Files

  • #2
    Please read the Forum FAQ for excellent advice on how to maximize your chances of getting a timely and helpful response. Among the things you will learn there:
    1. Attachments are deprecated here. The preferred way to show code is by copy/pasting it into your post, between code delimiters. The use of code delimiters, if you are not already familiar with it, is explained in the FAQ.
    2. Saying that the code "errors" is simply not helpful. There are an enormous number of possibilities concerning both where the code may have gone wrong and in what way it may have gone wrong. Did it cause Stata to hang? To crash? Did you get any error messages? Did it run but produce results that are incorrect? If so what are those results and what is incorrect about them? You need to give as much information as possible about what the code is actually doing, and what you want it to do in order for people to help you. The best way to do that is to actually copy/paste the response you got from Stata into your post, again, between code delimiters. Do that exactly: don't edit it in any way--there is no such thing as a "minor change."

    Comment


    • #3
      Thank you for clarifying! The stata error is
      . quietly {
      Working on 1 out of 1000
      observation number out of range
      Observation number must be between 0 and 2,147,483,619. (Observation
      numbers are typed without commas.)
      r(198);


      The code is as follows:

      clear
      program drop _all
      cd "###############"
      capture log close
      log using simulation2.log, text replace

      *Endogenous x2 variable: OLS
      *write a program
      program define simu1
      clear
      set obs 5000
      generate e1=rnormal()
      generate x1=rnormal()
      generate x2=rnormal()+e1
      generate y=1+1*x1+1*x2+e1
      regress y x1 x2
      end
      *execute the program
      simu1
      simulate, rep(100) seed(8964): simu1
      summarize

      *Endogenous x2 variable: IV
      program define simu2
      clear
      set obs 5000
      generate e1=rnormal()
      generate z=rnormal()
      generate x1=rnormal()
      generate x2=rnormal()+e1+z
      generate y=1+1*x1+1*x2+e1
      eregress y x1, endogenous(x2=z x1)
      end

      simu2
      simulate, rep(10000) seed(368): simu2
      summarize

      log close

      Comment


      • #4
        Harrison, your code intends to simulate a regression 10,000 times on a sample of 2,000 observations. Below please find a simpler code.

        Code:
        cap program drop mysim
        program mysim, eclass
            drop _all
            set obs 2000    
            gen x = rnormal()*3 + 6
            gen e = runiform() - 0.5
            gen y = 3 + 4*x + e
            reg y x
        end
        
        simulate data_store_x=(_b[x]) data_store_cons=(_b[_cons]), reps(10000): mysim
        
        sum data_store_x data_store_cons
        ADD: The code above is a revision of the OP, not for #3.
        Last edited by Fei Wang; 26 Sep 2022, 18:41.

        Comment


        • #5
          Harrison, the error in #3 occurs because you are trying to run a part of the code at a time, and at that point, Stata does not have the local mc in its memory. You should either run your full code at one go, or alternatively, execute it using the "Execute (include)" option available on the top-right of the toolbar in the do-file editor -- also available from the menu at View > Do-file Editor > Execute (include).

          Comment

          Working...
          X