Friday, August 23, 2013

The Delta Method Revisited

* In a previous post or two I have attempted to explore how to use the delta method to derive standard errors.  The purpose of those posts as with many of my posts was to understand the method better.  

* Unsurprisingly I made a fairly significant mistake.  Fortunately, Richard Hofler recently caught the mistake and has emailed me corrected code.  I have included the code (below).

* To see the original post: the-delta-method-to-estimate-standard

set seed 101

set obs 1000

gen x1 = rnormal()
gen x2 = rnormal() * 4

global b0 = 1
global b1 = 1.5
global b2 = .3

*          G = (3*b0-b1)*b2^2 
local true_G = (3*${b0}-${b1})*${b2}^2

di `true_G'

gen y = ${b0} + ${b1}*x1 + ${b2}*x2 + rnormal()*8

reg y x1 x2

* now retrieve the variance-covariance mtx for the estimated coefficients
* need this for var_hat(G) 
matrix Varcov = e(V)
* display the entire matrix
* note the lower diagonal form
matrix list Varcov

* put elements of Varcov into scalars for variances and covariances
scalar varb0=Varcov[3,3]
scalar varb1=Varcov[1,1]
scalar varb2=Varcov[2,2]
scalar covb1b2=Varcov[2,1]
scalar covb0b1=Varcov[3,1]
scalar covb0b2=Varcov[3,2]

*        G = (3*b0-b1)*b2^2 
local Ghat = (3*_b[_cons]-_b[x1])*_b[x2]^2

di "Ghat = `Ghat' is our estimate (true = `true_G')"

* Let's see if we can use the delta method to derive a standard error.
* correct expression 
* var_hat(G) = b2^2*(4*(3*b0 - b1)*b2*(3*covb0b2 - covb1b2) + 
* b2^2*(-6*covb0b1 + 9*varb0 + varb1) + 4*(-3*b0 + b1)^2*varb2)
scalar var_Ghat = _b[x2]^2*(4*(3*_b[_cons] - _b[x1])*_b[x2]*    /// 
(3*covb0b2 - covb1b2) + _b[x2]^2*(-6*covb0b1 + 9*varb0 + varb1) +     /// 
4*(-3*_b[_cons] + _b[x1])^2*varb2)

* take sq. root of var_Ghat to get standard error of G-hat
scalar se_Ghat = sqrt(var_Ghat)

di "Standard error by delta method is " se_Ghat

* Francis Smart - Coding again

* We can also easily calculate the standard error via bootstrap.

cap program drop gest
program define gest

  reg y x1 x2
  tempvar G
  gen `G' = (3*_b[_cons]-_b[x1])*_b[x2]^2
  sum `G'


bs r(mean), rep(200): gest
* I get a standard error of .105 which is quite close to the standard error derived by
* the delta method.

* However what we really want to be comparing our standard error estimates to is the true
* standard deviation of the estimate of G.

* This we can do because we are simulating our data.  Thus we can estimate G repeatedly
* by repeating the simulation.

cap program drop ggen
program define ggen

  set obs 1000

  gen x1 = rnormal()
  gen x2 = rnormal() * 4

  gen y = ${b0} + ${b1}*x1 + ${b2}*x2 + rnormal()*8
  reg y x1 x2

  gen G = (3*_b[_cons]-_b[x1])*_b[x2]^2
  sum G


simulate  r(mean), rep(200): ggen
* I get a standard deviation of G estimate of .097 which is quite close to our other methods of estimating G.

Formatted By Econometrics by Simulation

No comments:

Post a Comment