-------------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  c:\acdbookrevision\stata_final_programs_2013\racd13.txt
  log type:  text
 opened on:  19 Jan 2013, 16:35:28

. 
. ********** OVERVIEW OF racd13.do **********
. 
. * STATA Program 
. * copyright C 2013 by A. Colin Cameron and Pravin K. Trivedi 
. * used for "Regression Analyis of Count Data" SECOND EDITION
. * by A. Colin Cameron and Pravin K. Trivedi (2013)
. * Cambridge University Press
. 
. * This STATA program estimates measurement error models for chapter 13
. *   13.7 SIMULATION EXAMPLE: POISSON WITH MISMEASURED REGRESSOR
. 
. * To run you need no data (the data are generated)
. * and user-written Stata addons
. *   rcal, simex, qvf, cme, and gllamm (cme is a wappper for gllamm) 
. * in your directory
. 
. ********** SETUP **********
. 
. set more off

. version 12

. clear all

. * set linesize 82
. set scheme s1mono  /* Graphics scheme */

. * set maxvar 100 width 1000
. 
. ************
. 
. * This STATA program estimates some measurement error models
. *           TRUE: Poisson model with true regressor x1star
. *          NAIVE: Poisson model with observed x1 with measurement error
. *           RCAL: Regression calibration with duplicate observation z for x1star
. *          SIMEX: SIMEX with duplicate observation z for x1star
. *         NL2SLS: NLIV with instrument z for x1star
. *       IVAPPROX: Carroll's IV with duplicate observation (instrument) z for x1star
. * MLE STRUCTURAL: Maximum likelihood of normal structural model
. 
. * NOTE: SIMEX and MLE STRUCTURAL commented out as take a long time.
. * But results from these commands are included in comments below.
. * Can speed up SIMEX by reducing number of bootstrap replications
.  
. ********** DATA DESCRIPTION
. 
. *  The data are generated
. *    y ~ Poisson(mu)
. *    mu = 0 + 1*x1star + 1*x2
. 
. * For normally distibuted data
. *    x1star ~ N[0, .4^2]
. *    x1 = x1star + e1  where e1 ~ N[0, .2^2]
. *    z1 = x1star + e2  where e2 ~ N[0, .2^2] 
. *    x2 ~ N[0, .4^2]
. 
. * For rescaled chisquare data
. *    x1star ~ (0.4/sqrt(2)) x (w-1) where w ~ chi2(1)
. *    x1 = x1star + e1  where e1 ~ (0.4/sqrt(2)) x (w-1) where w ~ chi2(1)
. *    z1 = x1star + e2  where e2 ~ (0.4/sqrt(2)) x (w-1) where w ~ chi2(1)
. *    x2 ~ (0.4/sqrt(2)) x (w-1) where w ~ chi2(1)
. 
. ********** PART A: NORMALLY DISTRIBUTED DATA
. 
. clear

. set obs 10000
obs was 0, now 10000

. set seed 10101

. 
. generate x1star = rnormal(0, .4)

. generate e1 = rnormal(0, .4)

. generate e2 = rnormal(0, .4)

. generate x2 = rnormal(0, .4)

. generate x1 = x1star + e1

. generate z = x1star + e2

. generate mu = 0 + 1*x1star + 1*x2

. generate y = rpoisson(exp(mu))

. generate ynox2 = rpoisson(exp(0 + 1*x1star))

. generate ylinear = mu + rnormal(0,1)

. generate xgamma = rgamma(1,1)

. generate munegbin = xgamma*exp(mu)

. generate ynegbin = rpoisson(munegbin)

. 
. /*
> lowess ynox2 x1, msize(tiny) lineopts(lwidth(medthick)) lstyle(solid) saving(graph1, replace) xlabel(#6)
> lpoly ynox2 x1star, msize(tiny) lineopts(lwidth(medthick)) saving(graph2, replace) xlabel(#6)
> graph combine graph1.gph graph2.gph iscale(0.7) ysize(5) xsize(6) xcommon
> */
. 
. summarize

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
      x1star |     10000   -.0026665      .39794  -1.433627   1.763537
          e1 |     10000    -.003983    .3995721  -1.523171   1.548638
          e2 |     10000   -.0044249    .4002048  -1.596938   1.504384
          x2 |     10000   -.0044088    .3987985  -1.725232   1.627055
          x1 |     10000   -.0066495    .5637038  -2.111444   1.982391
-------------+--------------------------------------------------------
           z |     10000   -.0070913    .5660889  -2.212548   2.194387
          mu |     10000   -.0070753    .5636274  -2.355915   2.077965
           y |     10000       1.157     1.28067          0         10
       ynox2 |     10000      1.0726    1.123679          0          8
     ylinear |     10000   -.0078468     1.14627  -5.075897   4.553571
-------------+--------------------------------------------------------
      xgamma |     10000     1.00228    1.013867   .0000238   11.05992
    munegbin |     10000    1.172875    1.521178   .0000254   25.49255
     ynegbin |     10000      1.1694    1.849391          0         22

. summarize y, detail

                              y
-------------------------------------------------------------
      Percentiles      Smallest
 1%            0              0
 5%            0              0
10%            0              0       Obs               10000
25%            0              0       Sum of Wgt.       10000

50%            1                      Mean              1.157
                        Largest       Std. Dev.       1.28067
75%            2              9
90%            3              9       Variance       1.640115
95%            4             10       Skewness       1.547291
99%            5             10       Kurtosis       6.607161

. tabulate y

          y |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      3,735       37.35       37.35
          1 |      3,210       32.10       69.45
          2 |      1,717       17.17       86.62
          3 |        791        7.91       94.53
          4 |        342        3.42       97.95
          5 |        116        1.16       99.11
          6 |         51        0.51       99.62
          7 |         15        0.15       99.77
          8 |         15        0.15       99.92
          9 |          6        0.06       99.98
         10 |          2        0.02      100.00
------------+-----------------------------------
      Total |     10,000      100.00

. 
. * Linear model with truth - not in book
. regress ylinear x1star x2

      Source |       SS       df       MS              Number of obs =   10000
-------------+------------------------------           F(  2,  9997) = 1618.41
       Model |  3213.39251     2  1606.69625           Prob > F      =  0.0000
    Residual |  9924.63757  9997  .992761585           R-squared     =  0.2446
-------------+------------------------------           Adj R-squared =  0.2444
       Total |  13138.0301  9999   1.3139344           Root MSE      =  .99637

------------------------------------------------------------------------------
     ylinear |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      x1star |   1.029784   .0250396    41.13   0.000     .9807012    1.078866
          x2 |   .9813376   .0249857    39.28   0.000     .9323607    1.030315
       _cons |  -.0007743   .0099646    -0.08   0.938    -.0203069    .0187582
------------------------------------------------------------------------------

. * Linear model with observed - not in book
. regress ylinear x1 x2

      Source |       SS       df       MS              Number of obs =   10000
-------------+------------------------------           F(  2,  9997) = 1073.98
       Model |  2323.58619     2  1161.79309           Prob > F      =  0.0000
    Residual |  10814.4439  9997  1.08176892           R-squared     =  0.1769
-------------+------------------------------           Adj R-squared =  0.1767
       Total |  13138.0301  9999   1.3139344           Root MSE      =  1.0401

------------------------------------------------------------------------------
     ylinear |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x1 |   .4984598   .0184532    27.01   0.000     .4622879    .5346317
          x2 |   .9908689   .0260836    37.99   0.000     .9397398    1.041998
       _cons |  -.0001637   .0104022    -0.02   0.987    -.0205541    .0202267
------------------------------------------------------------------------------

. 
. * TRUE: Poisson model with true regressor x1star
. poisson y x1star x2

Iteration 0:   log likelihood = -13035.343  
Iteration 1:   log likelihood = -13035.343  

Poisson regression                                Number of obs   =      10000
                                                  LR chi2(2)      =    3653.13
                                                  Prob > chi2     =     0.0000
Log likelihood = -13035.343                       Pseudo R2       =     0.1229

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      x1star |    .989535   .0235534    42.01   0.000     .9433712    1.035699
          x2 |   1.006646   .0233194    43.17   0.000     .9609411    1.052352
       _cons |  -.0052865   .0106147    -0.50   0.618     -.026091     .015518
------------------------------------------------------------------------------

. estimates store NTrue

. 
. * NAIVE: Poisson model with observed x1 with measurement error
. poisson y x1 x2

Iteration 0:   log likelihood = -13415.789  
Iteration 1:   log likelihood = -13415.789  

Poisson regression                                Number of obs   =      10000
                                                  LR chi2(2)      =    2892.24
                                                  Prob > chi2     =     0.0000
Log likelihood = -13415.789                       Pseudo R2       =     0.0973

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x1 |   .5232958   .0164709    31.77   0.000     .4910134    .5555782
          x2 |    1.01995   .0233884    43.61   0.000     .9741098     1.06579
       _cons |   .0288349     .01033     2.79   0.005     .0085885    .0490813
------------------------------------------------------------------------------

. estimates store NNaive

. 
. * RCAL: Regression calibration with duplicate observation z for x1star
. * Standard errors vary with the seed
. rcal (y = x2) (w1: x1 z), family(poisson) bstrap brep(400) seed(10101)

Regression calibration                          No. of obs         =     10000
                                                Bootstrap reps     =       400

Residual df  =        9997                      Wald F(2,9997)     =   1417.20
                                                Prob > F           =    0.0000
               (IRLS EIM)                       Scale param        =  1.043864

Variance Function: V(u) = u                     [Poisson]
Link Function    : g(u) = ln(u)                 [Log]

------------------------------------------------------------------------------
             |              Bootstrap
           y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x2 |   1.012936   .0240201    42.17   0.000     .9658518     1.06002
          w1 |   .9999429   .0306092    32.67   0.000     .9399427    1.059943
       _cons |   .0223918   .0105701     2.12   0.034     .0016722    .0431113
------------------------------------------------------------------------------

. estimates store NRCAL

. 
. * SIMEX: SIMEX with duplicate observation z for x1star
. * Commented out as takes a long time
. * matrix theta=(0,.5,1,1.5,2,2.5,3,3.5)
. * simex (y = x2) (w1: x1 z), family(poisson) theta(theta) median bstrap brep(400) seed(10101)
. * Estimated coefficients vary with the seed
. * simex (y = x2) (w1: x1 z), family(poisson) theta(theta) median bstrap brep(400) seed(12345)
. 
. /* SIMEX Results. SIMEX commented out as takes a long time
> . * SIMEX: SIMEX with duplicate observation z for x1star
> . matrix theta=(0,.5,1,1.5,2,2.5,3,3.5)
> . simex (y = x2) (w1: x1 z), family(poisson) theta(theta) median bstrap brep(400) seed(10101)
> Estimated time to perform bootstrap: 15.5 minutes.
> Simulation extrapolation                        No. of obs         =     10000
>                                                 Bootstraps reps    =       400
> Residual df  =      9997                        Wald F(2,9997)     =   1501.29
>                                                 Prob > F           =    0.0000
> Variance Function: V(u) = u                        [Poisson]
> Link Function    : g(u) = ln(u)                    [Log]
> ------------------------------------------------------------------------------
>              |              Bootstrap
>            y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
>           x2 |   1.012512   .0240066    42.18   0.000     .9654542     1.05957
>           w1 |    .858099   .0238988    35.91   0.000     .8112526    .9049454
>        _cons |   .0062725    .009817     0.64   0.523    -.0129708    .0255157
> ------------------------------------------------------------------------------
> */
. 
. * NL2SLS: NLIV with instrument z for x1star
. gmm (y - exp({xb:x1 x2} + {b0})), instruments(z x2) onestep

Step 1
Iteration 0:   GMM criterion Q(b) =  .34138844  
Iteration 1:   GMM criterion Q(b) =  .29895765  
Iteration 2:   GMM criterion Q(b) =  .00095572  
Iteration 3:   GMM criterion Q(b) =  2.944e-07  
Iteration 4:   GMM criterion Q(b) =  2.614e-14  
Iteration 5:   GMM criterion Q(b) =  2.118e-28  

GMM estimation 

Number of parameters =   3
Number of moments    =   3
Initial weight matrix: Unadjusted                     Number of obs  =   10000

------------------------------------------------------------------------------
             |               Robust
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      /xb_x1 |   .9607275   .0363876    26.40   0.000     .8894091    1.032046
      /xb_x2 |   1.025419   .0269247    38.08   0.000     .9726474     1.07819
         /b0 |  -.0715327   .0150407    -4.76   0.000    -.1010118   -.0420535
------------------------------------------------------------------------------
Instruments for equation 1: z x2 _cons

. estimates store NNL2SLS

. 
. * IVAPPROX: Carroll's IV with duplicate observation (instrument) z for x1star
. qvf y x1 x2 (z x2), family(poisson)

IV Generalized linear models                       No. of obs      =     10000
Optimization     : MQL Fisher scoring              Residual df     =      9997
                   (IRLS EIM)                      Scale param     =         1
Deviance         =  11861.55151                    (1/df) Deviance =  1.186511
Pearson          =  10881.85446                    (1/df) Pearson  =  1.088512

Variance Function: V(u) = u                        [Poisson]
Link Function    : g(u) = ln(u)                    [Log]
Standard Errors  : OIM Sandwich

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x2 |   1.025686   .0258009    39.75   0.000     .9751169    1.076254
          x1 |     .95795   .0349921    27.38   0.000     .8893668    1.026533
       _cons |   .0388165   .0108394     3.58   0.000     .0175717    .0600613
------------------------------------------------------------------------------

. estimates store NIVApprox

. 
. * MLE STRUCTURAL: Maximum likelihood of normal structural model
. * Takes a long time to run so commented out
. * cme y x2 (x1true: x1 z), link(log) family(poisson) robust
. 
. /* CME RESULTS. CME commented out as takes a long time
> . * MLE STRUCTURAL: Maximum likelihood of normal structural model
> . cme y x2 (x1true: x1 z), link(log) family(poisson) robust
> Running adaptive quadrature
> Iteration 0:    log likelihood = -29442.215
> Iteration 1:    log likelihood = -28836.534
> Iteration 2:    log likelihood =  -28802.87
> Iteration 3:    log likelihood = -28802.777
> Iteration 4:    log likelihood = -28802.777
> Adaptive quadrature has converged, running Newton-Raphson
> Iteration 0:   log likelihood = -28802.777  
> Iteration 1:   log likelihood = -28802.777  
> Iteration 2:   log likelihood = -28802.776  
> gllamm covariate measurement error model           No. of obs      = 10000
> log likelihood = -28802.776
> OUTCOME MODEL
> ------------------------------------------------------------------------------
>              |               Robust
>            y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
> y            |
>           x2 |    1.01163   .0248953    40.64   0.000     .9628365    1.060424
>       x1true |   .9904211     .02921    33.91   0.000     .9331706    1.047672
>        _cons |  -.0026743   .0110999    -0.24   0.810    -.0244297    .0190811
> ------------------------------------------------------------------------------
> TRUE COVARIATE MODEL
> ------------------------------------------------------------------------------
>       x1true |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
> x1true       |
>           x2 |  -.0033133   .0122784    -0.27   0.787    -.0273785     .020752
>        _cons |   -.006885   .0048946    -1.41   0.160    -.0164782    .0027082
> -------------+----------------------------------------------------------------
>    res. var. |   .1603168    .003648                      .1532465    .1675466
> ------------------------------------------------------------------------------
> MEASUREMENT MODEL
> ------------------------------------------------------------------------------
>   error var. |   .1587588   .0022676                      .1543761     .163266
>  reliability |   .5024414   .0075969                      .4875535    .5173258
> ------------------------------------------------------------------------------
> */
. 
. *** TABLE 13.1 RESULTS: PANEL A: POISSON MODEL AND NORMAL DGP
. 
. estimates table NTrue NNaive NRCAL NNL2SLS NIVApprox, b(%9.3f) se equations(1)

--------------------------------------------------------------------------
    Variable |   NTrue      NNaive       NRCAL      NNL2SLS    NIVApprox  
-------------+------------------------------------------------------------
#1           |
      x1star |     0.990                                                  
             |     0.024                                                  
          x2 |     1.007       1.020       1.013                   1.026  
             |     0.023       0.023       0.024                   0.026  
          x1 |                 0.523                               0.958  
             |                 0.016                               0.035  
          w1 |                             1.000                          
             |                             0.031                          
       _cons |    -0.005       0.029       0.022       0.961       0.039  
             |     0.011       0.010       0.011       0.036       0.011  
-------------+------------------------------------------------------------
xb_x2        |
       _cons |                                         1.025              
             |                                         0.027              
-------------+------------------------------------------------------------
b0           |
       _cons |                                        -0.072              
             |                                         0.015              
--------------------------------------------------------------------------
                                                              legend: b/se

. 
. 
. ********** PART B: RESCALED CHISQUARE DISTRIBUTED DATA
. 
. clear

. set obs 10000
obs was 0, now 10000

. set seed 10101

. 
. generate x1star = 0.4*(rchi2(1)-1)/sqrt(2)

. generate e1 = 0.4*(rchi2(1)-1)/sqrt(2)

. generate e2 = 0.4*(rchi2(1)-1)/sqrt(2)

. generate x2 = 0.4*(rchi2(1)-1)/sqrt(2)

. 
. generate x1 = x1star + e1

. generate z = x1star + e2

. generate mu = 0 + 1*x1star + 1*x2

. generate y = rpoisson(exp(mu))

. generate ynox2 = rpoisson(exp(0 + 1*x1star))

. generate ylinear = mu + rnormal(0,1)

. generate xgamma = rgamma(1,1)

. generate munegbin = xgamma*exp(mu)

. generate ynegbin = rpoisson(munegbin)

. 
. /*
> lowess ynox2 x1, msize(tiny) lineopts(lwidth(medthick)) lstyle(solid) saving(graph1, replace) xlabel(#6)
> lpoly ynox2 x1star, msize(tiny) lineopts(lwidth(medthick)) saving(graph2, replace) xlabel(#6)
> graph combine graph1.gph graph2.gph iscale(0.7) ysize(5) xsize(6) xcommon
> */
. 
. summarize

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
      x1star |     10000    .0029802    .4087211  -.2828427   3.859689
          e1 |     10000   -.0010369    .3980761  -.2828427   3.471328
          e2 |     10000   -.0020615    .3934839  -.2828427   3.519124
          x2 |     10000   -.0021314    .4045407  -.2828427   4.583592
          x1 |     10000    .0019433    .5705487  -.5656648   4.456134
-------------+--------------------------------------------------------
           z |     10000    .0009187    .5649666  -.5656725    4.09623
          mu |     10000    .0008488    .5805357  -.5656208   4.882773
           y |     10000       1.324    2.806637          0        118
       ynox2 |     10000      1.1516    1.713683          0         51
     ylinear |     10000   -.0077314    1.171113  -3.818903   5.461312
-------------+--------------------------------------------------------
      xgamma |     10000    1.011231    1.015599   .0002631   8.811442
    munegbin |     10000    1.337701    4.323259   .0003677   244.9276
     ynegbin |     10000      1.3504    4.500694          0        241

. summarize y, detail

                              y
-------------------------------------------------------------
      Percentiles      Smallest
 1%            0              0
 5%            0              0
10%            0              0       Obs               10000
25%            0              0       Sum of Wgt.       10000

50%            1                      Mean              1.324
                        Largest       Std. Dev.      2.806637
75%            2             62
90%            3             94       Variance       7.877212
95%            4             98       Skewness       18.76434
99%            9            118       Kurtosis       610.5811

. tabulate y

          y |      Freq.     Percent        Cum.
------------+-----------------------------------
          0 |      3,812       38.12       38.12
          1 |      3,220       32.20       70.32
          2 |      1,660       16.60       86.92
          3 |        636        6.36       93.28
          4 |        261        2.61       95.89
          5 |        137        1.37       97.26
          6 |         75        0.75       98.01
          7 |         47        0.47       98.48
          8 |         37        0.37       98.85
          9 |         20        0.20       99.05
         10 |         19        0.19       99.24
         11 |         13        0.13       99.37
         12 |          8        0.08       99.45
         13 |         11        0.11       99.56
         14 |          3        0.03       99.59
         15 |          4        0.04       99.63
         16 |          6        0.06       99.69
         17 |          2        0.02       99.71
         18 |          4        0.04       99.75
         19 |          4        0.04       99.79
         20 |          3        0.03       99.82
         21 |          2        0.02       99.84
         23 |          1        0.01       99.85
         24 |          2        0.02       99.87
         26 |          1        0.01       99.88
         28 |          2        0.02       99.90
         29 |          1        0.01       99.91
         31 |          1        0.01       99.92
         33 |          1        0.01       99.93
         43 |          1        0.01       99.94
         44 |          1        0.01       99.95
         49 |          1        0.01       99.96
         62 |          1        0.01       99.97
         94 |          1        0.01       99.98
         98 |          1        0.01       99.99
        118 |          1        0.01      100.00
------------+-----------------------------------
      Total |     10,000      100.00

. 
. * Linear model with truth - not in book
. regress ylinear x1star x2

      Source |       SS       df       MS              Number of obs =   10000
-------------+------------------------------           F(  2,  9997) = 1723.45
       Model |  3516.06716     2  1758.03358           Prob > F      =  0.0000
    Residual |  10197.6248  9997   1.0200685           R-squared     =  0.2564
-------------+------------------------------           Adj R-squared =  0.2562
       Total |   13713.692  9999  1.37150635           Root MSE      =    1.01

------------------------------------------------------------------------------
     ylinear |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      x1star |   .9761178   .0247166    39.49   0.000     .9276683    1.024567
          x2 |   1.065811    .024972    42.68   0.000     1.016861    1.114761
       _cons |  -.0083688   .0101003    -0.83   0.407    -.0281673    .0114298
------------------------------------------------------------------------------

. * Linear model with observed - not in book
. regress ylinear x1 x2

      Source |       SS       df       MS              Number of obs =   10000
-------------+------------------------------           F(  2,  9997) = 1262.82
       Model |  2765.85198     2  1382.92599           Prob > F      =  0.0000
    Residual |    10947.84  9997  1.09511253           R-squared     =  0.2017
-------------+------------------------------           Adj R-squared =  0.2015
       Total |   13713.692  9999  1.37150635           Root MSE      =  1.0465

------------------------------------------------------------------------------
     ylinear |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x1 |   .5083372   .0183464    27.71   0.000     .4723745    .5442999
          x2 |   1.069799   .0258751    41.34   0.000     1.019079    1.120519
       _cons |  -.0064391    .010465    -0.62   0.538    -.0269525    .0140744
------------------------------------------------------------------------------

. 
. * TRUE: Poisson model with true regressor x1star
. poisson y x1star x2

Iteration 0:   log likelihood = -35375.388  
Iteration 1:   log likelihood = -20529.053  
Iteration 2:   log likelihood =  -13502.43  
Iteration 3:   log likelihood = -13151.675  
Iteration 4:   log likelihood = -13145.302  
Iteration 5:   log likelihood =   -13145.3  
Iteration 6:   log likelihood =   -13145.3  

Poisson regression                                Number of obs   =      10000
                                                  LR chi2(2)      =   11796.45
                                                  Prob > chi2     =     0.0000
Log likelihood =   -13145.3                       Pseudo R2       =     0.3097

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      x1star |   .9746038   .0104697    93.09   0.000     .9540836    .9951241
          x2 |   .9869814   .0101441    97.30   0.000     .9670993    1.006863
       _cons |   .0096458   .0101329     0.95   0.341    -.0102144     .029506
------------------------------------------------------------------------------

. estimates store CTrue

. 
. * NAIVE: Poisson model with observed x1 with measurement error
. poisson y x1 x2

Iteration 0:   log likelihood = -29491.768  
Iteration 1:   log likelihood = -18776.217  
Iteration 2:   log likelihood = -14265.803  
Iteration 3:   log likelihood = -14054.319  
Iteration 4:   log likelihood =  -14053.53  
Iteration 5:   log likelihood =  -14053.53  

Poisson regression                                Number of obs   =      10000
                                                  LR chi2(2)      =    9979.99
                                                  Prob > chi2     =     0.0000
Log likelihood =  -14053.53                       Pseudo R2       =     0.2620

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x1 |   .6923382    .009804    70.62   0.000     .6731228    .7115537
          x2 |   1.000488   .0101882    98.20   0.000     .9805198    1.020457
       _cons |   .0332065   .0101038     3.29   0.001     .0134034    .0530096
------------------------------------------------------------------------------

. estimates store CNaive

. 
. * RCAL: Regression calibration with duplicate observation z for x1star
. * Standard errors vary with the seed
. rcal (y = x2) (w1: x1 z), family(poisson) bstrap brep(400) seed(10101)

Regression calibration                          No. of obs         =     10000
                                                Bootstrap reps     =       400

Residual df  =        9997                      Wald F(2,9997)     =   2736.83
                                                Prob > F           =    0.0000
               (IRLS EIM)                       Scale param        =  1.113003

Variance Function: V(u) = u                     [Poisson]
Link Function    : g(u) = ln(u)                 [Log]

------------------------------------------------------------------------------
             |              Bootstrap
           y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x2 |   .9800205   .0167146    58.63   0.000     .9472566    1.012784
          w1 |    1.25432   .0246016    50.99   0.000     1.206096    1.302544
       _cons |   .0148296   .0109865     1.35   0.177    -.0067061    .0363653
------------------------------------------------------------------------------

. estimates store CRCAL

. 
. * SIMEX: SIMEX with duplicate observation z for x1star
. * matrix theta=(0,.5,1,1.5,2,2.5,3,3.5)
. * simex (y = x2) (w1: x1 z), family(poisson) theta(theta) median bstrap brep(400) seed(10101)
. * Estimated coefficients vary with the seed
. * simex (y = x2) (w1: x1 z), family(poisson) theta(theta) median bstrap brep(400) seed(12345)
. 
. /* SIMEX Results. SIMEX commented out as takes a long time
> * SIMEX: SIMEX with duplicate observation z for x1star
> . simex (y = x2) (w1: x1 z), family(poisson) theta(theta) median bstrap brep(400) seed(10101)
> Estimated time to perform bootstrap: 15.5 minutes.
> Simulation extrapolation                        No. of obs         =     10000
>                                                 Bootstraps reps    =       400
> Residual df  =      9997                        Wald F(2,9997)     =   3289.04
>                                                 Prob > F           =    0.0000
> Variance Function: V(u) = u                        [Poisson]
> Link Function    : g(u) = ln(u)                    [Log]
> ------------------------------------------------------------------------------
>              |              Bootstrap
>            y |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
>           x2 |    .983736   .0178388    55.15   0.000     .9487683    1.018704
>           w1 |    .956486   .0147981    64.64   0.000     .9274788    .9854933
>        _cons |   .0078898   .0106569     0.74   0.459    -.0129999    .0287794
> ------------------------------------------------------------------------------
> */
. 
. * NL2SLS: NLIV with instrument z for x1star
. gmm (y - exp({xb:x1 x2} + {b0})), instruments(z x2) onestep

Step 1
Iteration 0:   GMM criterion Q(b) =   2.239997  
Iteration 1:   GMM criterion Q(b) =  .43695283  
Iteration 2:   GMM criterion Q(b) =  .18524262  
Iteration 3:   GMM criterion Q(b) =  .00003971  
Iteration 4:   GMM criterion Q(b) =  4.188e-10  
Iteration 5:   GMM criterion Q(b) =  1.505e-20  

GMM estimation 

Number of parameters =   3
Number of moments    =   3
Initial weight matrix: Unadjusted                     Number of obs  =   10000

------------------------------------------------------------------------------
             |               Robust
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
      /xb_x1 |   .9793202   .0227306    43.08   0.000     .9347691    1.023871
      /xb_x2 |    .993116    .024999    39.73   0.000     .9441189    1.042113
         /b0 |  -.1076062   .0182696    -5.89   0.000     -.143414   -.0717984
------------------------------------------------------------------------------
Instruments for equation 1: z x2 _cons

. estimates store CNL2SLS

. 
. * IVAPPROX: Carroll's IV with duplicate observation (instrument) z for x1star
. qvf y x1 x2 (z x2), family(poisson)

IV Generalized linear models                       No. of obs      =     10000
Optimization     : MQL Fisher scoring              Residual df     =      9997
                   (IRLS EIM)                      Scale param     =         1
Deviance         =  13030.90317                    (1/df) Deviance =  1.303481
Pearson          =  12070.42779                    (1/df) Pearson  =  1.207405

Variance Function: V(u) = u                        [Poisson]
Link Function    : g(u) = ln(u)                    [Log]
Standard Errors  : OIM Sandwich

------------------------------------------------------------------------------
           y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          x2 |   .9755163   .0235816    41.37   0.000     .9292973    1.021735
          x1 |   1.356062   .0465995    29.10   0.000     1.264729    1.447396
       _cons |   .0322457   .0133714     2.41   0.016     .0060383     .058453
------------------------------------------------------------------------------

. estimates store CIVApprox

. 
. * MLE STRUCTURAL: Maximum likelihood of normal structural model
. * Takes a long time to run so commented out
. * cme y x2 (x1true: x1 z), link(log) family(poisson) robust
. 
. /* CME RESULTS. CME commented out as takes a long time
> . * MLE STRUCTURAL: Maximum likelihood of normal structural model
> . cme y x2 (x1true: x1 z), link(log) family(poisson) robust
> Running adaptive quadrature
> Iteration 0:    log likelihood = -29704.418
> Iteration 1:    log likelihood = -29107.377
> Iteration 2:    log likelihood = -29037.282
> Iteration 3:    log likelihood = -29036.541
> Iteration 4:    log likelihood = -29036.541
> Adaptive quadrature has converged, running Newton-Raphson
> Iteration 0:   log likelihood = -29036.541  
> Iteration 1:   log likelihood = -29036.541  (backed up)
> Iteration 2:   log likelihood = -29036.538  
> Iteration 3:   log likelihood = -29036.538  
> gllamm covariate measurement error model           No. of obs      = 10000
> log likelihood = -29036.538
> OUTCOME MODEL
> ------------------------------------------------------------------------------
>              |               Robust
>            y |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
> y            |
>           x2 |   .9698153   .0157735    61.48   0.000     .9388999    1.000731
>       x1true |   1.213485      .0215    56.44   0.000     1.171346    1.255624
>        _cons |  -.0178152   .0115338    -1.54   0.122     -.040421    .0047907
> ------------------------------------------------------------------------------
> TRUE COVARIATE MODEL
> ------------------------------------------------------------------------------
>       x1true |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
> -------------+----------------------------------------------------------------
> x1true       |
>           x2 |   .0253178    .012859     1.97   0.049     .0001147     .050521
>        _cons |    .001485   .0049441     0.30   0.764    -.0082053    .0111753
> -------------+----------------------------------------------------------------
>    res. var. |   .1658849   .0065873                      .1532253    .1790469
> ------------------------------------------------------------------------------
> MEASUREMENT MODEL
> ------------------------------------------------------------------------------
>   error var. |   .1563348    .004127                      .1484518    .1646365
>  reliability |   .5148191   .0124117                      .4904801     .539103
> ------------------------------------------------------------------------------
> */
. 
. *** TABLE 13.1 RESULTS: PANEL B: POISSON MODEL AND CHISQUARE DGP
. 
. estimates table CTrue CNaive CRCAL CNL2SLS CIVApprox, b(%9.3f) se equations(1)

--------------------------------------------------------------------------
    Variable |   CTrue      CNaive       CRCAL      CNL2SLS    CIVApprox  
-------------+------------------------------------------------------------
#1           |
      x1star |     0.975                                                  
             |     0.010                                                  
          x2 |     0.987       1.000       0.980                   0.976  
             |     0.010       0.010       0.017                   0.024  
          x1 |                 0.692                               1.356  
             |                 0.010                               0.047  
          w1 |                             1.254                          
             |                             0.025                          
       _cons |     0.010       0.033       0.015       0.979       0.032  
             |     0.010       0.010       0.011       0.023       0.013  
-------------+------------------------------------------------------------
xb_x2        |
       _cons |                                         0.993              
             |                                         0.025              
-------------+------------------------------------------------------------
b0           |
       _cons |                                        -0.108              
             |                                         0.018              
--------------------------------------------------------------------------
                                                              legend: b/se

. 
. /* NOT IN BOOK .....
> 
> * REPEAT FOR NEGATIVE BINOMIAL D.G.P. - BUT USING POISSON METHODS
> 
> * Negative binomial model with truth
> nbreg ynegbin x1star x2
> * Negative binomial model with observed
> nbreg ynegbin x1 x2
> 
> * Regression calibration with duplicate observation z for x1star
> rcal (ynegbin = x2) (w1: x1 z), family(poisson) bstrap brep(100)
> 
> * SIMEX with duplicate observation z for x1star
> matrix theta=(0,.5,1,1.5,2,2.5,3,3.5)
> simex (ynegbin = x2) (w1: x1 z), family(poisson) theta(theta) median bstrap brep(10)
> 
> * Carroll's IV with duplicate observation (instrument) z for x1star
> qvf ynegbin x1 x2 (z x2), family(poisson)
> 
> * NLIV with instrument z for x1star
> gmm (ynegbin - exp({xb:x1 x2} + {b0})), instruments(z x2) onestep
> 
> * REPEAT FOR NEGATIVE BINOMIAL - BUT USING NBINOMIAL
> 
> * Negative binomial model with truth
> nbreg ynegbin x1star x2
> * Negative binomial model with observed
> nbreg ynegbin x1 x2
> 
> * Regression calibration with duplicate observation z for x1star
> rcal (ynegbin = x2) (w1: x1 z), family(nbinomial) bstrap brep(100)
> 
> * SIMEX with duplicate observation z for x1star
> matrix theta=(0,.5,1,1.5,2,2.5,3,3.5)
> simex (ynegbin = x2) (w1: x1 z), family(nbinomial) theta(theta) median bstrap brep(10)
> 
> * Carroll's IV with duplicate observation (instrument) z for x1star
> qvf ynegbin x1 x2 (z x2), family(nbinomial)
> 
> */
. 
. ********** CLOSE OUTPUT
. 
. * log close
. * clear
. * exit
. 
end of do-file

. exit, clear
