Announcement

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

  • heteroskedasticity simulation

    Hi there,
    I would like to code up a simulation exercise to show that heteroskedasticity persists even as the sample size increases although the coefficient of the regression estimate remains unbiased.
    Could I get some help in coding this up?

    Many thanks
    Karen

  • #2
    Code:
    clear all
    
    program define sim
        args n
    
        // create an empty dataset with `n' observations
        drop _all
        set obs `n'
    
        // create the explanatory variables
        // x1 is binary, x2 is continuous
        // x1 and x2 are somewhat related
        gen byte x1 = runiform()<.5
        gen      x2 = rnormal(.5*x1)
        
        // create the error term
        gen e = rnormal(0,exp(.7+.2*x2))
        
        // create y
        gen y = .5 + 1.2*x1 -.5*x2 + e
    
        // estimate the model
        reg y x1 x2
    end
    
    // create a frame to store the different simulations
    frame create sim
    frame change sim
    set obs 1000
    gen rep = _n
    frame change default
    
    foreach n of numlist 20 50 100 200(200)1000 2000 5000 {
        // simulate a specific sample size
        simulate b`n'=_b[x2] se`n'= _se[x2], reps(20000) : sim `n'
        
        // store the results in the frame sim
        gen rep = _n
        frame change sim
        frlink 1:1 rep , frame(default)
        frget b`n' se`n', from(default)
        drop default
        frame change default
    }
        
    // admire the results
    frame change sim
    
    // needs simsum, type ssc install simsum
    // also see https://journals.sagepub.com/doi/pdf/10.1177/1536867X1001000305
    simsum b* , true(-.5) se(se*) bias cover mcse
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you so much Maarten, this is incredibly helpful.

      Comment

      Working...
      X