Announcement

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

  • Monte Carlo experiment with different rho values?

    Hello Stata Forum

    I have been searching the internet but I can't find an answer to my question. I have the following assignment:
    Click image for larger version

Name:	Skærmbillede 2018-05-01 kl. 11.58.52.png
Views:	1
Size:	209.1 KB
ID:	1442199



    The problem that I have is the line "Consider the following values of ρ: -0.5, 0, 0.5 and 1". I have entered the following in STATA but this is only with rho= -0,5. There must be a easy setup so I can run the experiment 4 times with the different rho values instead of inserting the STATA code 4 times in a row with different rho values?

    This is my STATA:

    Code:
    *PROBLEM 5
    *DEFINE PROGRAM THAT SPECIFIES THE DGP
    program dgp, rclass
        drop _all
        
        *SET NUMBER OF CURRENT OBSERVATIONS 
        set obs 1000
        
        *DATA GENERATING PROCESS
        gen xstar=1+2*rnormal()
        
        gen y=4+3*xstar+rnormal()
        
        gen x=xstar+rnormal()
        
        gen z=1*xstar+rnormal()
        
        gen eta=-0.5*rnormal()+rnormal()
        
        *CALCULATE OLS ESTIMATES
        regress y x
        return scalar b_ols=_b[x]
        
        *CALCULATE IV ESTIMATES
        ivregress 2sls y (x=z)
        return scalar b_iv=_b[x]
        
    end
    
    *NOW RUN PROGRAM 500 TIMES USING SIMULATE
    simulate b_ols=r(b_ols) b_iv=r(b_iv), seed(117) reps(500) nodots:dgp
    su

  • #2
    Rather than search the internet, you want to read the basic Stata documentation - Chapter 18, Programming Stata, in the Stata User's Guide PDF included with your Stata installation and accessible through Stata's Help menu. There you will learn about program arguments, which will allow you to write code like
    Code:
    simulate b_ols=r(b_ols) b_iv=r(b_iv), seed(117) reps(500) nodots:dgp -0.5
    and modify your program to use the value of rho passed into the program, rather than have it hardcoded.

    Comment


    • #3
      I have now looked in the manual but I can't get it to work? The code you provided in your post does that take all four values in mind?

      Comment


      • #4
        I started to provide you additional advice, but on inspecting the code of your dgp program, it is clear that you have failed to understand your assignment. Here's a hint: you need to first generate mu and epsilon, and then use those values to generate eta. That's the first of many similar changes that are needed to your program.

        In general, Statalist refrains from being a homework service. I gave you a minimal hint of where to look for advice on programming Stata, but that's as far as I feel comfortable going.

        Comment

        Working...
        X