------------------------------------------------------------------------------------------------------
       log:  c:\Imbook\bwebpage\Section2\mma05p4margeffects.txt
  log type:  text
 opened on:  17 May 2005, 13:57:02

. 
. ********** OVERVIEW OF MMA05P4MARGINALEFFECTS.DO **********
. 
. * STATA Program 
. * copyright C 2005 by A. Colin Cameron and Pravin K. Trivedi 
. * used for "Microeconometrics: Methods and Applications" 
. * by A. Colin Cameron and Pravin K. Trivedi (2005)
. * Cambridge University Press 
. 
. * Chapter 5.9.4 pp.162-3
. * Marginal effects analysis for a nonlinear model (here exponential regression).
. 
. * Provides 
. *   (1) Sample average marginal effect using derivative
. *   (2) Sample average marginal effect using first difference
. *   (3) Marginal effect evaluated at the sample mean
. *   (4) Marginal effects (1)-(3) when model estimated by Stata ml command
. * using generated data (see below)
. 
. * Related programs:
. *   mma05p1mle.do      OLS and MLE for the same data 
. *   mma05p2nls.do      NLS, WNLS, FGNLS for same data using nl command
. *   mma05p3nlsbyml.do  NLS for same data using ml command
. 
. * To run this program you need data and dictionary files
. *    mma05data.asc    ASCII data set generated by mma05p1mle.do
. 
. ********** SETUP **********
. 
. set more off

. version 8

. 
. ********** READ IN DATA and SUMMARIZE **********
. 
. * Model is  y ~ exponential(exp(a + bx))
. *           x ~ N[mux, sigx^2]
. *        f(y) = exp(a + bx)*exp(-y*exp(a + bx))
. *      lnf(y) = (a + bx) - y*exp(a + bx)
. *        E[y] = exp(-(a + bx))    note sign reversal for the mean
. *        V[y] = exp(-(a + bx)) = E[y]
. * Here a = 2, b = -1  and  x ~ N[mux=1, sigx^21]
. * and Table 5.7 uses N=10,000 
. 
. * Data was generated by program mma05p1mle.do
. infile y x using mma05data.asc
(10000 observations read)

. 
. * Descriptive Statistics
. describe

Contains data
  obs:        10,000                          
 vars:             2                          
 size:       120,000 (98.8% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
y               float  %9.0g                  
x               float  %9.0g                  
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

. summarize

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           y |     10000    .6194352    1.291416   .0000445   30.60636
           x |     10000    1.014313    1.004905  -2.895741   4.994059

. 
. ********** MARGINAL EFFECTS for CHAPTER 5.9.4 **********
. 
. ** (1) DERIVATIVE METHOD FOR SAMPLE AVERAGE MARGINAL EFFECT 
. 
. * (1A) METHOD A: Use analytical results
. * Since E[y] = exp(-(a + bx))   Note: here sign reversal for the mean !!
. *      dE[y]/dx = -b*exp(-(a + bx)) = -b*E[y]
. 
. * Estimate the model
. * The Stata code for exponential regression is unusual as st command
. * Need to declare data to be st data with dependent variable y
. stset y

     failure event:  (assumed to fail at time=y)
obs. time interval:  (0, y]
 exit on or before:  failure

------------------------------------------------------------------------------
    10000  total obs.
        0  exclusions
------------------------------------------------------------------------------
    10000  obs. remaining, representing
    10000  failures in single record/single failure data
 6194.352  total analysis time at risk, at risk from t =         0
                             earliest observed entry t =         0
                                  last observed exit t =  30.60636

. quietly streg x, distribution(exponential)  nohr

. gen dEydxanalyticalderivative = -_b[x]*exp(-_b[_cons] - _b[x]*x)

. * Alternative is to (1) predict the mean and (2) multiply by -_b[x]
. quietly sum dEydxanalyticalderivative

. scalar mesaad = r(mean)

. di "Sample average marginal effect by analytical derivative = " mesaad
Sample average marginal effect by analytical derivative = .60976598

. 
. * (1B) METHOD B: Use numerical derivative (here one-sided)
. * This is same as first difference code, except have small change in x
. * Note: precision problems can arise with small changes in x
. * The following code tries to minimize such problems
. * Change in x will be 0.0001 times the standard deviation of x
. egen sdx = sd(x)

. quietly streg x, distribution(exponential) nohr

. * Need to tell streg to predict the mean as this is not the default.
. predict y0, mean time     

. gen xoriginal = x

. replace x = x+0.0001*sdx
(10000 real changes made)

. predict y1, mean time

. gen dEydxnumericalderivative = (y1 - y0)/(0.0001*sdx)

. quietly sum dEydxnumericalderivative

. scalar mesand = r(mean)

. di "Sample average marginal effect by numerical derivative = " mesand
Sample average marginal effect by numerical derivative = .60949044

. replace x = xoriginal
(10000 real changes made)

. drop xoriginal sdx y0 y1

. 
. ** (2) FINITE DIFFERENCE METHOD FOR SAMPLE AVERAGE MARGINAL EFFECT 
. 
. streg x, distribution(exponential) nohr    /* y is dependent variable */

         failure _d:  1 (meaning all fail)
   analysis time _t:  y

Iteration 0:   log likelihood = -20754.005  
Iteration 1:   log likelihood = -17232.884  
Iteration 2:   log likelihood = -15760.556  
Iteration 3:   log likelihood = -15752.193  
Iteration 4:   log likelihood =  -15752.19  
Iteration 5:   log likelihood =  -15752.19  

Exponential regression -- log relative-hazard form 

No. of subjects =        10000                     Number of obs   =     10000
No. of failures =        10000
Time at risk    =  6194.352464
                                                   LR chi2(1)      =  10003.63
Log likelihood  =    -15752.19                     Prob > chi2     =    0.0000

------------------------------------------------------------------------------
          _t |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
           x |  -.9896276   .0098692  -100.27   0.000    -1.008971   -.9702842
       _cons |   1.982921   .0141496   140.14   0.000     1.955188    2.010654
------------------------------------------------------------------------------

. 
. * The following method can be used following many stata estimation commands
. * 1. Predict y using sample data.
. *    Need to say predict the mean as this is not the streg default.
. predict y0, mean time

. * 2. Predict y with regressor of x increased by one
. gen xoriginal = x

. replace x = x+1
(10000 real changes made)

. predict y1, mean time

. replace x = xoriginal  /* Put x back to initial value for later analysis */
(10000 real changes made)

. * 3. Calculate difference
. gen dEydxfinitedifference = y1 - y0

. quietly sum dEydxfinitedifference 

. scalar mesafd = r(mean)

. di "Sample average marginal effect by first differences = " mesafd
Sample average marginal effect by first differences = 1.0414485

. drop xoriginal y0 y1

. 
. ** (3) DERIVATIVE METHOD FOR MARGINAL EFFECT AT SAMPLE MEAN 
. 
. * (3A) Use Stata command mfx
. quietly streg x, distribution(exponential) nohr

. * Need to tell mfx to predict the mean as this is not the streg default.
. mfx compute, dydx predict(mean time)

Marginal effects after ereg
      y  = predicted mean _t (predict, mean time)
         =  .37563828
------------------------------------------------------------------------------
variable |      dy/dx    Std. Err.     z    P>|z|  [    95% C.I.   ]      X
---------+--------------------------------------------------------------------
       x |    .371742      .00525   70.81   0.000   .361452  .382032   1.01431
------------------------------------------------------------------------------

. di "Marginal effect by analytical derivative at mean of x using mfx: "
Marginal effect by analytical derivative at mean of x using mfx: 

. matrix list e(Xmfx_dydx)

symmetric e(Xmfx_dydx)[1,1]
          x
r1  .371742

. 
. * (3B) Write ones own code
. quietly streg x, distribution(exponential) nohr

. quietly sum x

. scalar meanx = r(mean)

. scalar dEydxatmeanx = -_b[x]*exp(-_b[_cons] - _b[x]*meanx)

. di "Marginal effect by analytical derivative at mean of x done manually: "
Marginal effect by analytical derivative at mean of x done manually: 

. di dEydxatmeanx
.371742

. 
. ** (4) MARGINAL EFFECTS AFTER ML COMMAND
. 
. * Preceding (1) - (3) presume there is a built-in command to get MLE.
. * Now consider ML estimation using Stata's ml command.
. * After ml command cannot use predict or mfx. 
. * Need to be more manual, as follows.
. 
. * Estimate model by ml: for details see mma0p1mle.do
. program define mleexp0
  1.   version 8.0
  2.   args lnf theta      /* Must use lnf while could use name other than theta */
  3.   quietly replace `lnf' = `theta' - $ML_y1*exp(`theta')
  4. end

. quietly ml model lf mleexp0 (y = x)

. quietly ml search

. quietly ml maximize

. 
. * Note that here the mean is in fact exp(-a-b*x)
. 
. * (1A) Sample average marginal effect by calculus methods
. gen mldEydxanalyticalderivative = -_b[x]*exp(-_b[_cons] - _b[x]*x)

. quietly sum mldEydxanalyticalderivative

. scalar mlmesaad = r(mean)

. di "Sample average marginal effect by analytical derivative = " mlmesaad
Sample average marginal effect by analytical derivative = .60976598

. 
. * (1B) Sample average marginal effect by numerical derivative
. egen sdx = sd(x)

. gen y0 = exp(-_b[_cons] - _b[x]*x)

. gen xoriginal = x

. replace x = x+0.0001*sdx
(10000 real changes made)

. gen y1 = exp(-_b[_cons] - _b[x]*x)

. gen mldEydxnumericalderivative = (y1 - y0)/(0.0001*sdx)

. quietly sum mldEydxnumericalderivative

. scalar mlmesand = r(mean)

. di "ML sample average marginal effect by numerical derivative = " mlmesand
ML sample average marginal effect by numerical derivative = .60949063

. replace x = xoriginal
(10000 real changes made)

. drop xoriginal sdx y0 y1

. 
. * (2) Sample average marginal effect by increase x by one unit (finite difference)
. gen mldEydxfinitedifference = exp(-_b[_cons]-_b[x]*(x+1)) - exp(-_b[_cons]-_b[x]*x)

. quietly sum mldEydxfinitedifference

. scalar mlmesafd = r(mean)

. di "Sample average marginal effect by first differnce = " mlmesafd
Sample average marginal effect by first differnce = 1.0414485

. 
. * (3) Marginal effect estimated at the sample mean of x
. quietly sum x

. scalar meanx = r(mean)

. scalar mldEydxatmeanx = -_b[x]*exp(-_b[_cons] - _b[x]*meanx)

. di "ML marginal effect at mean of x by analytical derivative: "
ML marginal effect at mean of x by analytical derivative: 

. di mldEydxatmeanx
.371742

. 
. ********** DISPLAY RESULTS on p.162-3 **********
. 
. di "Marginal Effects: (1A) Analytical deriv (1B) Numerical Deriv (2) First diff" 
Marginal Effects: (1A) Analytical deriv (1B) Numerical Deriv (2) First diff

. sum dEydxfinitedifference dEydxanalyticalderivative dEydxnumericalderivative

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
dEydxfinit~e |     10000    1.041449    1.373144     .01325   32.59646
dEydxanaly~e |     10000     .609766    .8039727   .0077578   19.08516
dEydxnumer~e |     10000    .6094904    .8035654   .0077479   19.11325

. 
. di "KEY RESULTS FOR CHAPTER 5.9.4 pp.162-3 FOLLOW"
KEY RESULTS FOR CHAPTER 5.9.4 pp.162-3 FOLLOW

. di "(1A) Sample average marginal effect by analytical derivative = " mesaad
(1A) Sample average marginal effect by analytical derivative = .60976598

. di "(1B) Sample average marginal effect by numerical derivative =  " mesand
(1B) Sample average marginal effect by numerical derivative =  .60949044

. di "(2)  Sample average marginal effect by first differences =     " mesafd
(2)  Sample average marginal effect by first differences =     1.0414485

. di "(3)  Marginal effect at mean of x by analytical derivative =   " dEydxatmeanx
(3)  Marginal effect at mean of x by analytical derivative =   .371742

. 
. ********** CLOSE OUTPUT **********
. log close
       log:  c:\Imbook\bwebpage\Section2\mma05p4margeffects.txt
  log type:  text
 closed on:  17 May 2005, 13:57:06
----------------------------------------------------------------------------------------------------
