------------------------------------------------------------------------------------------------------
       log:  c:\Imbook\bwebpage\Section4\mma17p1km.txt
  log type:  text
 opened on:  19 May 2005, 13:19:55

. 
. ********** OVERVIEW OF MMA17P1KM.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 17.2 (pages 574-5) and 17.5.1 (pages 581-3)
. * Nonparametric Duration Analysis
. * It provides  
. *   (1) Kaplan-Meier Survival Estimate Graph (Figure 17.1: kennanstrk.wmf) 
. *   (2) Nelson-Aalen Cumulative Hazard Estimate Graph
. *   (3) Kaplan-Meier Survivor Function Estimates (Table 17.3)
. *   (4) Shows that Cox regression on intercept gives same results
. 
. * To run this program you need data file
. *    strkdur.dta 
. 
. ********** SETUP **********
. 
. set more off

. version 8

. set scheme s1mono   /* Used for graphs */

.   
. ********** DATA DESCRIPTION
. 
. * The data is the same data as given in Table 1 of 
. *   J. Kennan, "The Duration of Contract strikes in U.S. Manufacturing",
. *   Journal of Econometrics, 1985, Vol. 28, pp.5-28.
. 
. * There are 566 observations from 1968-1976 with two variables
. * 1. dur  is duration of the strike in days
. * 2. gdp  is a measure of stage of business cycle
. *         (deviation of monthly log industrial production in manufacturing 
. *          from prediction from OLS on time, time-squared and monthly dummies)
. 
. * All observations are complete for these data. There is no censoring !!
. * For an example with censoring see mma17p2kmextra.do or mma17p4duration.do
. 
. ********** READ DATA **********
. 
. use strkdur.dta

. sum

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
         dur |       566    43.62367    44.66641          1        235
         gdp |       566    .0060411    .0499072    -.13996     .08554

. 
. * Create ASCII data set so that can use programs other than Stata
. outfile dur gdp using strkdur.asc, replace

. 
. ********* ANALYSIS: NONPARAMETRIC SURVIVAL CURVE AND HAZARD FUNCTION **********
. 
. * Stata st curves require defining the dependent variable
. stset dur

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

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

. 
. * The data here are complete. If dur is instead right-censored,
. * then also need to define a censoring indicator. For example 
. *   stset dur, fail(censor=1)
. * where the variable censor=1 if data are right-censored and =0 otherwise
. * See mma17p3duration.do
. 
. * (1) GRAPH KAPLAN-MEIER SURVIVAL CURVE
. 
. * Minimal command that gives 95% confidence bands
. sts graph, gwood

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

. 
. * Longer command for Figure 17.1 (page 575)
. * Nicer graphs and also confidence bands are bolder and easier to read
. sts gen surv = s

. sts gen lbsurv = lb(s)

. sts gen ubsurv = ub(s)

. sort dur

. graph twoway (line ubsurv dur, msize(vtiny) mstyle(p2) c(J) clstyle(p1) clcolor(gs10)) /* 
>   */ (line surv dur, msize(vtiny) mstyle(p1) c(J) clstyle(p1)) /* 
>   */ (line lbsurv dur, msize(vtiny) mstyle(p2) c(J) clstyle(p1) clcolor(gs10)), /*
>   */ scale(1.2) plotregion(style(none)) /*
>   */ title("Kaplan-Meier Survival Function Estimate") /*
>   */ xtitle("Strike duration in days", size(medlarge)) xscale(titlegap(*5)) /* 
>   */ ytitle("Survival Probability", size(medlarge)) yscale(titlegap(*5)) /*
>   */ ylabel(0.00(0.25)1.00,grid)/*
>   */ legend(pos(3) ring(0) col(1)) legend(size(small)) /*
>   */ legend( label(1 "Upper 95% confidence band") label(2 "Survival Function") /*
>   */         label(3 "Lower 95% confidence band") )

. graph export kennanstrk.wmf, replace
(file c:\Imbook\bwebpage\Section4\kennanstrk.wmf written in Windows Metafile format)

. 
. * (2) GRAPH NELSON-AALEN CUMULATIVE HAZARD FUNCTION 
. 
. * Minimal command that gives 95% confidence bands
. sts graph, cna

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

. 
. * Longer command gives nicer figure
. sts graph, cna /*
>   */ scale (1.2) plotregion(style(none)) /*
>   */ title("Nelson-Aalen Cumulative Hazard") /*
>   */ xtitle("Strike duration in days", size(medlarge)) xscale(titlegap(*5)) /* 
>   */ ytitle("Cumulative Hazard", size(medlarge)) yscale(titlegap(*5)) /*
>   */ legend(pos(12) ring(0) col(1)) legend(size(small)) /*
>   */ legend(label(1 "95% confidence bands") label(2 "Cumulative Hazard")) 

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

. 
. * (3) LIST SURVIVOR and NELSON-AALEN CUMULATIVE HAZARD ESTIMATES
. 
. * Gives a lot of output
. 
. * Table 17.2: Kaplan-Meier Survivor Function (page 583)
. sts list

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

           Beg.          Net            Survivor      Std.
  Time    Total   Fail   Lost           Function     Error     [95% Conf. Int.]
-------------------------------------------------------------------------------
     1      566     10      0             0.9823    0.0055     0.9674    0.9905
     2      556     21      0             0.9452    0.0096     0.9230    0.9612
     3      535     16      0             0.9170    0.0116     0.8910    0.9369
     4      519     17      0             0.8869    0.0133     0.8578    0.9104
     5      502     18      0             0.8551    0.0148     0.8234    0.8816
     6      484      9      0             0.8392    0.0154     0.8063    0.8670
     7      475     12      0             0.8180    0.0162     0.7837    0.8474
     8      463     12      0             0.7968    0.0169     0.7613    0.8277
     9      451     13      0             0.7739    0.0176     0.7371    0.8061
    10      438      8      0             0.7597    0.0180     0.7223    0.7928
    11      430      9      0             0.7438    0.0183     0.7058    0.7777
    12      421     10      0             0.7261    0.0187     0.6874    0.7609
    13      411     11      0             0.7067    0.0191     0.6673    0.7424
    14      400     11      0             0.6873    0.0195     0.6473    0.7237
    15      389     12      0             0.6661    0.0198     0.6256    0.7033
    16      377      8      0             0.6519    0.0200     0.6111    0.6896
    17      369      6      0             0.6413    0.0202     0.6003    0.6793
    18      363      8      0             0.6272    0.0203     0.5860    0.6656
    19      355      7      0             0.6148    0.0205     0.5734    0.6535
    20      348      7      0             0.6025    0.0206     0.5609    0.6415
    21      341      5      0             0.5936    0.0206     0.5519    0.6328
    22      336     11      0             0.5742    0.0208     0.5324    0.6137
    23      325     10      0             0.5565    0.0209     0.5146    0.5964
    24      315      8      0             0.5424    0.0209     0.5004    0.5824
    25      307      4      0             0.5353    0.0210     0.4934    0.5754
    26      303      7      0             0.5230    0.0210     0.4810    0.5632
    27      296      6      0             0.5124    0.0210     0.4704    0.5527
    28      290      9      0             0.4965    0.0210     0.4546    0.5369
    29      281      5      0             0.4876    0.0210     0.4458    0.5281
    30      276      5      0             0.4788    0.0210     0.4371    0.5193
    31      271      8      0             0.4647    0.0210     0.4231    0.5051
    32      263      5      0             0.4558    0.0209     0.4144    0.4963
    33      258      6      0             0.4452    0.0209     0.4039    0.4857
    34      252      5      0             0.4364    0.0208     0.3952    0.4768
    35      247      4      0             0.4293    0.0208     0.3883    0.4697
    36      243      6      0             0.4187    0.0207     0.3779    0.4590
    37      237      6      0             0.4081    0.0207     0.3675    0.4483
    38      231      8      0             0.3940    0.0205     0.3537    0.4340
    39      223      3      0             0.3887    0.0205     0.3485    0.4287
    40      220      1      0             0.3869    0.0205     0.3468    0.4269
    41      219      4      0             0.3799    0.0204     0.3399    0.4197
    42      215      8      0             0.3657    0.0202     0.3261    0.4053
    43      207      4      0             0.3587    0.0202     0.3193    0.3981
    44      203      9      0             0.3428    0.0200     0.3039    0.3819
    45      194      3      0             0.3375    0.0199     0.2988    0.3765
    46      191      4      0             0.3304    0.0198     0.2919    0.3693
    47      187      5      0             0.3216    0.0196     0.2834    0.3602
    48      182      3      0             0.3163    0.0195     0.2783    0.3548
    49      179      5      0             0.3074    0.0194     0.2698    0.3457
    50      174      8      0             0.2933    0.0191     0.2563    0.3312
    51      166      1      0             0.2915    0.0191     0.2546    0.3293
    52      165      8      0             0.2774    0.0188     0.2411    0.3147
    53      157      6      0             0.2668    0.0186     0.2310    0.3037
    54      151      1      0             0.2650    0.0186     0.2294    0.3019
    55      150      2      0             0.2615    0.0185     0.2260    0.2982
    56      148      3      0             0.2562    0.0183     0.2210    0.2927
    57      145      3      0             0.2509    0.0182     0.2159    0.2872
    58      142      1      0             0.2491    0.0182     0.2143    0.2854
    59      141      4      0             0.2420    0.0180     0.2076    0.2780
    60      137      6      0             0.2314    0.0177     0.1976    0.2669
    61      131      5      0             0.2226    0.0175     0.1893    0.2577
    62      126      2      0             0.2191    0.0174     0.1860    0.2540
    63      124      2      0             0.2155    0.0173     0.1827    0.2503
    64      122      5      0             0.2067    0.0170     0.1744    0.2410
    65      117      3      0             0.2014    0.0169     0.1695    0.2354
    67      114      1      0             0.1996    0.0168     0.1678    0.2335
    68      113      1      0             0.1979    0.0167     0.1662    0.2317
    70      112      4      0             0.1908    0.0165     0.1596    0.2242
    71      108      1      0             0.1890    0.0165     0.1580    0.2223
    72      107      1      0             0.1873    0.0164     0.1563    0.2205
    74      106      1      0             0.1855    0.0163     0.1547    0.2186
    75      105      1      0             0.1837    0.0163     0.1530    0.2167
    77      104      3      0             0.1784    0.0161     0.1481    0.2111
    82      101      2      0             0.1749    0.0160     0.1449    0.2073
    83       99      1      0             0.1731    0.0159     0.1432    0.2055
    84       98      3      0             0.1678    0.0157     0.1384    0.1998
    85       95      2      0             0.1643    0.0156     0.1351    0.1960
    86       93      1      0             0.1625    0.0155     0.1335    0.1942
    87       92      1      0             0.1608    0.0154     0.1319    0.1923
    88       91      1      0             0.1590    0.0154     0.1302    0.1904
    90       90      1      0             0.1572    0.0153     0.1286    0.1885
    91       89      2      0             0.1537    0.0152     0.1254    0.1847
    92       87      1      0             0.1519    0.0151     0.1238    0.1828
    94       86      1      0             0.1502    0.0150     0.1222    0.1809
    98       85      3      0             0.1449    0.0148     0.1173    0.1752
    99       82      3      0             0.1396    0.0146     0.1125    0.1695
   100       79      2      0             0.1360    0.0144     0.1093    0.1657
   101       77      3      0             0.1307    0.0142     0.1045    0.1600
   102       74      2      0             0.1272    0.0140     0.1013    0.1561
   103       72      1      0             0.1254    0.0139     0.0997    0.1542
   104       71      3      0             0.1201    0.0137     0.0950    0.1485
   105       68      1      0             0.1184    0.0136     0.0934    0.1465
   106       67      2      0             0.1148    0.0134     0.0902    0.1427
   107       65      2      0             0.1113    0.0132     0.0871    0.1388
   108       63      2      0             0.1078    0.0130     0.0839    0.1349
   109       61      2      0             0.1042    0.0128     0.0808    0.1311
   111       59      1      0             0.1025    0.0127     0.0792    0.1291
   112       58      1      0             0.1007    0.0126     0.0777    0.1272
   114       57      1      0             0.0989    0.0126     0.0761    0.1252
   115       56      1      0             0.0972    0.0124     0.0745    0.1233
   116       55      1      0             0.0954    0.0123     0.0730    0.1213
   117       54      2      0             0.0919    0.0121     0.0699    0.1174
   118       52      1      0             0.0901    0.0120     0.0683    0.1155
   119       51      1      0             0.0883    0.0119     0.0668    0.1135
   122       50      3      0             0.0830    0.0116     0.0622    0.1076
   123       47      1      0             0.0813    0.0115     0.0606    0.1056
   124       46      1      0             0.0795    0.0114     0.0591    0.1037
   125       45      2      0             0.0760    0.0111     0.0561    0.0997
   126       43      1      0             0.0742    0.0110     0.0545    0.0977
   127       42      2      0             0.0707    0.0108     0.0515    0.0937
   130       40      2      0             0.0671    0.0105     0.0485    0.0897
   131       38      1      0             0.0654    0.0104     0.0470    0.0877
   133       37      1      0             0.0636    0.0103     0.0455    0.0857
   135       36      1      0             0.0618    0.0101     0.0440    0.0837
   136       35      2      0             0.0583    0.0098     0.0410    0.0797
   139       33      2      0             0.0548    0.0096     0.0381    0.0756
   140       31      1      0             0.0530    0.0094     0.0366    0.0736
   141       30      3      0             0.0477    0.0090     0.0323    0.0675
   142       27      1      0             0.0459    0.0088     0.0308    0.0654
   143       26      1      0             0.0442    0.0086     0.0294    0.0633
   146       25      2      0             0.0406    0.0083     0.0265    0.0592
   147       23      1      0             0.0389    0.0081     0.0251    0.0571
   148       22      2      0             0.0353    0.0078     0.0223    0.0529
   151       20      1      0             0.0336    0.0076     0.0209    0.0508
   152       19      1      0             0.0318    0.0074     0.0196    0.0487
   153       18      2      0             0.0283    0.0070     0.0169    0.0444
   154       16      1      0             0.0265    0.0068     0.0155    0.0423
   160       15      1      0             0.0247    0.0065     0.0142    0.0401
   163       14      2      0             0.0212    0.0061     0.0116    0.0357
   165       12      1      0             0.0194    0.0058     0.0103    0.0335
   168       11      1      0             0.0177    0.0055     0.0091    0.0312
   174       10      1      0             0.0159    0.0053     0.0079    0.0290
   175        9      1      0             0.0141    0.0050     0.0067    0.0267
   179        8      1      0             0.0124    0.0046     0.0055    0.0244
   191        7      1      0             0.0106    0.0043     0.0044    0.0220
   192        6      1      0             0.0088    0.0039     0.0034    0.0196
   205        5      1      0             0.0071    0.0035     0.0024    0.0171
   208        4      1      0             0.0053    0.0031     0.0015    0.0146
   216        3      1      0             0.0035    0.0025     0.0007    0.0121
   226        2      1      0             0.0018    0.0018     0.0002    0.0095
   235        1      1      0             0.0000         .          .         .
-------------------------------------------------------------------------------

. 
. * And Nelson-Aalen Integrated Hazard
. * sts list, na
. 
. * (4) STCOX REGRESS ON INTERCEPT GIVES SAME RESULTS AS ABOVE
. 
. * Cox Regression on an intercept
. gen one = 1

. stcox one, basesurv(coxbasesurv) basechazard(coxbasecumhaz) basehc(coxbasehaz) 

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

note: one dropped due to collinearity
Iteration 0:   log likelihood =  -3032.134
Refining estimates:
Iteration 0:   log likelihood =  -3032.134

Cox regression -- Breslow method for ties

No. of subjects =          566                     Number of obs   =       566
No. of failures =          566
Time at risk    =        24691
                                                   LR chi2(0)      =      0.00
Log likelihood  =    -3032.134                     Prob > chi2     =         .

------------------------------------------------------------------------------
          _t | Haz. Ratio   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
------------------------------------------------------------------------------

. 
. * Instead use sts which analyzes dependent in isolation
. * sts gen surv = s
. sts gen cumhaz = na

. sts gen haz = h

. 
. * Compare to verify that same answers
. sum surv coxbasesurv cumhaz coxbasecumhaz haz coxbasehaz

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
        surv |       566     .493014    .2848417          0   .9823322
 coxbasesurv |       566     .493014    .2848417          0   .9823322
      cumhaz |       566           1    .9834583   .0176678   6.871446
coxbasecum~z |       566           1    .9834583   .0176678   6.871446
         haz |       566    .0345186    .0515235   .0045455          1
-------------+--------------------------------------------------------
  coxbasehaz |       566    .0345186    .0515235   .0045455          1

. corr surv coxbasesurv
(obs=566)

             |     surv coxbas~v
-------------+------------------
        surv |   1.0000
 coxbasesurv |   1.0000   1.0000


. corr cumhaz coxbasecumhaz
(obs=566)

             |   cumhaz cox~mhaz
-------------+------------------
      cumhaz |   1.0000
coxbasecum~z |   1.0000   1.0000


. corr haz coxbasehaz
(obs=566)

             |      haz cox~ehaz
-------------+------------------
         haz |   1.0000
  coxbasehaz |   1.0000   1.0000


. 
. * (5) ESTIMATE HAZARD FUNCTION 
. 
. * sts graph does not give the true hazard function - it instead gives the 
. * difference in the cumulative hazard (without division by time difference).
. 
. ********** CLOSE OUTPUT
. log close
       log:  c:\Imbook\bwebpage\Section4\mma17p1km.txt
  log type:  text
 closed on:  19 May 2005, 13:20:01
----------------------------------------------------------------------------------------------------
