------------------------------------------------------------------------------------------------------
       log:  c:\Imbook\bwebpage\Section2\mma06p2twostage.txt
  log type:  text
 opened on:  18 May 2005, 17:59:06

. 
. ********** OVERVIEW OF MMA06P2TWOSTAGE.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 
. 
. * NOTE: Stata does not have a NL2SLS command
. 
. * Chapter 6.5.4 nonlinear 2SLS example on pages 198-9.
. 
. * Table 6.4 partial only
. *   (1) OLS        inconsistent
. *   (2) NL2SLS     consistent    NOT INCLUDED AS STATA DOES NOT DO
. *   (3) Twostage   Here 2SLS using Theil's interpretation of 2SLS is inconsistent
. 
. * To run this program you need data set
. *           mma06p1nl2sls.asc
. * generated by Limdep program MMA06P1NL2SLS.LIM
. 
. * Some of the analysis is done in Limdep which (unlike Stata) has 
. * an NL2SLS command
. 
. ********** SETUP ********** 
. 
. set more off

. version 8.0

. 
. ********** READ DATA and SUMMARIZE **********
. 
. * Model is  y = 1*x^2 + u
. *           x = 1*z + v
. * where  u and v are joint normal (0,0,1,1,0.8)
. 
. infile y x xsq z zsq u v using mma06p1nl2sls.asc
(200 observations read)

. 
. * Descriptive Statistics
. describe

Contains data
  obs:           200                          
 vars:             7                          
 size:         6,400 (99.9% of memory free)
-------------------------------------------------------------------------------
              storage  display     value
variable name   type   format      label      variable label
-------------------------------------------------------------------------------
y               float  %9.0g                  
x               float  %9.0g                  
xsq             float  %9.0g                  
z               float  %9.0g                  
zsq             float  %9.0g                  
u               float  %9.0g                  
v               float  %9.0g                  
-------------------------------------------------------------------------------
Sorted by:  
     Note:  dataset has changed since last saved

. summarize

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
           y |       200    1.632794    2.418096  -2.332656   9.354863
           x |       200    .9970513    .8330302  -1.908285   2.696363
         xsq |       200    1.684581    1.638509   .0000948   7.270374
           z |       200           1           0          1          1
         zsq |       200           1           0          1          1
-------------+--------------------------------------------------------
           u |       200   -.0517871    .9427286  -2.816687   2.202356
           v |       200   -.0029487    .8330302  -2.908285   1.696363

. 
. ********** DO THE ANALYSIS: ESTIMATE MODELS **********
. 
. * (1) OLS is inconsistent (first column of Table 4.4)
. regress y xsq, noconstant

      Source |       SS       df       MS              Number of obs =     200
-------------+------------------------------           F(  1,   199) = 2250.83
       Model |  1558.96322     1  1558.96322           Prob > F      =  0.0000
    Residual |   137.83055   199  .692615831           R-squared     =  0.9188
-------------+------------------------------           Adj R-squared =  0.9184
       Total |  1696.79377   200  8.48396883           Root MSE      =  .83224

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         xsq |   1.189495   .0250721    47.44   0.000     1.140054    1.238936
------------------------------------------------------------------------------

. estimates store olswrong

. regress y xsq, noconstant robust

Regression with robust standard errors                 Number of obs =     200
                                                       F(  1,   199) = 3850.71
                                                       Prob > F      =  0.0000
                                                       R-squared     =  0.9188
                                                       Root MSE      =  .83224

------------------------------------------------------------------------------
             |               Robust
           y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         xsq |   1.189495   .0191687    62.05   0.000     1.151695    1.227295
------------------------------------------------------------------------------

. estimates store olswrongrob

. 
. * (2) NL2SLS command Stata does not have
. *     See LIMDEP program MMA06P1NL2SLS.LIM
. *     See also code further down
. 
. * (3A) Theil's 2sls where first regress x on z 
. *      and then use xhat^2 as instrument for x^2 is inconsistent
.  
. regress x z, noconstant

      Source |       SS       df       MS              Number of obs =     200
-------------+------------------------------           F(  1,   199) =  286.51
       Model |  198.822258     1  198.822258           Prob > F      =  0.0000
    Residual |  138.093918   199  .693939288           R-squared     =  0.5901
-------------+------------------------------           Adj R-squared =  0.5881
       Total |  336.916176   200  1.68458088           Root MSE      =  .83303

------------------------------------------------------------------------------
           x |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
           z |   .9970513   .0589041    16.93   0.000     .8808949    1.113208
------------------------------------------------------------------------------

. predict xhat
(option xb assumed; fitted values)

. gen xhatsq = xhat*xhat

. regress y xhatsq, noconstant

      Source |       SS       df       MS              Number of obs =     200
-------------+------------------------------           F(  1,   199) =   91.19
       Model |  533.203113     1  533.203113           Prob > F      =  0.0000
    Residual |  1163.59065   199  5.84718921           R-squared     =  0.3142
-------------+------------------------------           Adj R-squared =  0.3108
       Total |  1696.79377   200  8.48396883           Root MSE      =  2.4181

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      xhatsq |   1.642466   .1719981     9.55   0.000     1.303293    1.981638
------------------------------------------------------------------------------

. estimates store twostage

. 
. ********** DISPLAY KEY RESULTS Table 6.4 p.199 **********
. 
. * Table 4.4 p.199 first and third columns
. estimates table olswrong twostage, b(%8.3f) se stats(N r2) keep(xsq xhatsq)

------------------------------------
    Variable | olswrong   twostage  
-------------+----------------------
         xsq |    1.189             
             |    0.025             
      xhatsq |               1.642  
             |               0.172  
-------------+----------------------
           N |  200.000    200.000  
          r2 |    0.919      0.314  
------------------------------------
                        legend: b/se

. 
. ********** FURTHER ANALYSIS **********
. 
. * For this particular example there are ways to get linear IV to work
. * as the problem is not very nonlinear
. 
. * (2A) regress xsq on z giving xsqhat and then regress y on xsqhat
. *      Gives nl2sls estimator though not correct standard errors
. 
. * Note we get estimator 0.969 which is correct - Table 6.4 had typo
. regress xsq z, noconstant

      Source |       SS       df       MS              Number of obs =     200
-------------+------------------------------           F(  1,   199) =  211.41
       Model |  567.562553     1  567.562553           Prob > F      =  0.0000
    Residual |  534.257348   199  2.68471029           R-squared     =  0.5151
-------------+------------------------------           Adj R-squared =  0.5127
       Total |   1101.8199   200  5.50909951           Root MSE      =  1.6385

------------------------------------------------------------------------------
         xsq |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
           z |   1.684581   .1158601    14.54   0.000      1.45611    1.913052
------------------------------------------------------------------------------

. predict xsqhat
(option xb assumed; fitted values)

. regress y xsqhat, noconstant

      Source |       SS       df       MS              Number of obs =     200
-------------+------------------------------           F(  1,   199) =   91.19
       Model |  533.203113     1  533.203113           Prob > F      =  0.0000
    Residual |  1163.59065   199  5.84718921           R-squared     =  0.3142
-------------+------------------------------           Adj R-squared =  0.3108
       Total |  1696.79377   200  8.48396883           Root MSE      =  2.4181

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      xsqhat |   .9692582   .1015002     9.55   0.000     .7691043    1.169412
------------------------------------------------------------------------------

. 
. * (2B) IV with instrument z for xsq should work but Stata cannot do
. *      for some reason due to here z = 1 which has no variation
. ivreg y (xsq = z), noconstant 
note: z dropped due to collinearity
equation not identified; must have at least as many instruments not in
the regression as there are instrumented variables
r(481);

end of do-file
r(481);

. exit, clear
