Announcement

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

  • Monte Carlo Simulation with IV 2SLS

    Hi,

    I'm working on a Monte Carlo simulation with an endogenous x, so that I need to do 2SLS in my loop. My purpose is to compare the value and graphs of OLS b1 with 2SLS b1, including the discussion about the effect of sample size on b1.
    The pop linear reg is y = 0.5 + x + u. n = 40 or 200. x has correlation with u.
    For IV z: x = a0 + 0.1 * z + v. The error terms u and v are distributed as standard normal random variables with a correlation coefficient 0.7 between them.

    Right now I'm writing the OLS part and I don't know where I should put IV part and how to show the 0.7 between u and v into the loop. Can anyone help me? Thank you!

    My code so far:

    clear
    set obs 5000

    gen sample_id = _n

    foreach n in 40 200 {
    expand `n'
    sort sample_id
    gen X = uniform()
    gen U = uniform()
    gen Y = 0.5 + X + U

    gen b1_n_`n' = .
    gen se_heter_`n' = .

    quietly forvalues i = 1/5000 {
    quietly reg Y X if sample_id == `i', robust
    replace b1_n_`n' = _b[X] if sample_id == `i'
    replace se_heter_`n' = _se[X] if sample_id == `i'
    }

    duplicates drop sample_id, force

    drop X U Y
    }

  • #2
    Hi Nathan
    Perhaps this will do what you have in mind
    Code:
    capture program drop mcsimolsiv
    program mcsimolsiv, eclass
    clear 
    ** change this for Nobs
    set obs 1000
    * So, assume that z is normally distributed (since you have no other priors about them)
    gen z=rnormal()
    * create u and v based on the given correlation, and assume that otherwise the variance is 1
    matrix corruv=[1,.7\.7,1]
    drawnorm u v , corr(corruv)
    *  generate X
    gen x=1+0.1*z+v
    * generate y
    gen y=0.5+x+u
    * estimate models
     
    reg y x
    matrix b=  _b[x]
    ivregress 2sls y (x=z)
    matrix b=b,_b[x]
    matrix colname b= ols iv
    ereturn post b
    end
    ** and here for changing the number of repetitions
    simulate, reps(100): mcsimolsiv
    two kdensity _b_ols || kdensity _b_iv
    HTH

    Comment

    Working...
    X