------------------------------------------------------------------------------------------------------
       log:  c:\Imbook\bwebpage\Section2\mma06p2Theil.txt
  log type:  text
 opened on:  18 May 2005, 17:45:50

. 
. ********** OVERVIEW OF MMA06P2THEIL.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.
. * Table 6.4 partial only
. *   (1) OLS        inconsistent
. *   (2) NL2SLS     consistent    NOT INCLUDED AS STATA DOES NOT DO
. *   (3) Wrong 2SLS 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
. 
. * (3A) Theil's 2sls where first regress x on z 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 ivwrong

. 
. ********** DISPLAY KEY RESULTS Table 6.4 p.199 **********
. 
. * Table 4.4 p.199
. estimates table olswrong olswrongrob ivwrong, b(%8.3f) se stats(N r2) keep(xsq xhatsq)

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

. 
. * (3B) IV with instrument xsq for zsq should work but Stata cannot do
. ivreg y (xsq = xsq), noconstant 

Instrumental variables (2SLS) regression

      Source |       SS       df       MS              Number of obs =     200
-------------+------------------------------           F(  1,   199) =       .
       Model |  1558.96322     1  1558.96322           Prob > F      =       .
    Residual |   137.83055   199  .692615831           R-squared     =       .
-------------+------------------------------           Adj R-squared =       .
       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
------------------------------------------------------------------------------
Instrumented:  xsq
Instruments:   xsq
------------------------------------------------------------------------------

. corr xsq xsq
(obs=200)

             |      xsq      xsq
-------------+------------------
         xsq |   1.0000
         xsq |   1.0000   1.0000


. corr xsq z
(obs=200)

             |      xsq        z
-------------+------------------
         xsq |   1.0000
           z |        .        .


. 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
------------------------------------------------------------------------------

. * ivreg y (xsq = z), noconstant 
. 
. gen one = 1

. regress y one, 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]
-------------+----------------------------------------------------------------
         one |   1.632794   .1709852     9.55   0.000     1.295618    1.969969
------------------------------------------------------------------------------

. 
. ********** CLOSE OUTPUT **********
. log close
       log:  c:\Imbook\bwebpage\Section2\mma06p2Theil.txt
  log type:  text
 closed on:  18 May 2005, 17:45:50
----------------------------------------------------------------------------------------------------
