第12章のStataコード

第12章 操作変数法

サンプルデータ

prefecture.csv:1997-2019年における都道府県別の失業率と自殺率に関するデータ.

minimum_wage.csv:Card and Krueger (1995)のデータ.

12.2 パネルデータ回帰分析

12.2.3 Rによる固定効果モデルの推定

import delimited "/Users/kitagawa/Documents/bookwriting/empanar/prefecture.csv", case(preserve) clear 

sort pref_id year
list in 1/4
     +---------------------------------+
     |   pref   year   suicide   unemp |
     |---------------------------------|
  1. | 北海道   1997      19.6     3.7 |
  2. |   青森   1997      26.5     3.9 |
  3. |   岩手   1997      25.8     2.4 |
  4. |   宮城   1997      18.6     3.1 |
     +---------------------------------+
encode pref, gen(pref_id)

regress suicide unemp ibn.pref_id, noconstant
      Source |       SS           df       MS      Number of obs   =     1,081
-------------+----------------------------------   F(48, 1033)     =   1718.59
       Model |  567038.287        48  11813.2976   Prob > F        =    0.0000
    Residual |  7100.67017     1,033  6.87383366   R-squared       =    0.9876
-------------+----------------------------------   Adj R-squared   =    0.9871
       Total |  574138.957     1,081  531.118369   Root MSE        =    2.6218

------------------------------------------------------------------------------
     suicide | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
       unemp |   3.066965    .086185    35.59   0.000     2.897847    3.236083
             |
     pref_id |
       三重  |   10.84608   .6028334    17.99   0.000     9.663159    12.02899
       京都  |   6.187962   .6653313     9.30   0.000     4.882407    7.493517
       佐賀  |   11.35607   .6222227    18.25   0.000     10.13511    12.57704
       兵庫  |   7.097472   .6744254    10.52   0.000     5.774072    8.420873
     北海道  |   8.711331   .6837609    12.74   0.000     7.369612    10.05305
       千葉  |   8.475695   .6345959    13.36   0.000     7.230451    9.720939
     和歌山  |   13.67467     .61624    22.19   0.000     12.46544    14.88389
       埼玉  |   7.598172   .6548335    11.60   0.000     6.313216    8.883127
       大分  |   10.90964   .6295185    17.33   0.000     9.674362    12.14492
       大阪  |   5.509389   .7203893     7.65   0.000     4.095796    6.922983
       奈良  |   6.245202   .6433229     9.71   0.000     4.982833    7.507571
       宮城  |   7.912826   .6759649    11.71   0.000     6.586405    9.239247
       宮崎  |   15.64264   .6363151    24.58   0.000     14.39402    16.89126
       富山  |   15.37911   .6037843    25.47   0.000     14.19433     16.5639
       山口  |   12.98887   .6155497    21.10   0.000       11.781    14.19675
       山形  |   15.64775   .6116364    25.58   0.000     14.44755    16.84794
       山梨  |   13.30341   .6096331    21.82   0.000     12.10715    14.49967
       岐阜  |    12.6032   .6001765    21.00   0.000      11.4255     13.7809
       岡山  |   7.968494   .6280375    12.69   0.000     6.736119    9.200869
       岩手  |   18.20351   .6363151    28.61   0.000     16.95489    19.45213
       島根  |   18.44705   .5911101    31.21   0.000     17.28713    19.60696
       広島  |   9.585032   .6258364    15.32   0.000     8.356976    10.81309
       徳島  |   7.288137   .6378544    11.43   0.000     6.036499    8.539775
       愛媛  |   11.19369   .6313849    17.73   0.000     9.954745    12.43263
       愛知  |   8.684236    .615722    14.10   0.000     7.476027    9.892444
       新潟  |   17.17808    .624745    27.50   0.000     15.95217      18.404
       東京  |   7.037263   .6610885    10.64   0.000     5.740034    8.334493
       栃木  |   12.16242   .6263843    19.42   0.000     10.93329    13.39155
       沖縄  |   2.537746   .7875862     3.22   0.001     .9922947    4.083197
       滋賀  |    9.59004   .6148622    15.60   0.000     8.383518    10.79656
       熊本  |   9.569775   .6533933    14.65   0.000     8.287645     10.8519
       石川  |   10.60312   .6097991    17.39   0.000     9.406536    11.79971
     神奈川  |   6.676491   .6457045    10.34   0.000     5.409449    7.943533
       福井  |    12.5827   .5906834    21.30   0.000     11.42363    13.74178
       福岡  |   6.669192   .7033529     9.48   0.000     5.289028    8.049355
       福島  |   11.93389    .645107    18.50   0.000     10.66802    13.19976
       秋田  |   20.34367   .6564885    30.99   0.000     19.05546    21.63187
       群馬  |   13.10188   .6207967    21.10   0.000     11.88371    14.32005
       茨城  |   10.64122   .6315724    16.85   0.000     9.401912    11.88053
       長崎  |   9.680528   .6489149    14.92   0.000     8.407186    10.95387
       長野  |   12.76867   .6047422    21.11   0.000     11.58201    13.95533
       青森  |   12.89297   .7017055    18.37   0.000     11.51604     14.2699
       静岡  |   10.31733   .6091365    16.94   0.000     9.122044    11.51262
       香川  |   8.925075   .6199111    14.40   0.000     7.708646     10.1415
       高知  |   11.74309   .6569038    17.88   0.000     10.45407     13.0321
       鳥取  |   11.97814   .6167596    19.42   0.000      10.7679    13.18839
     鹿児島  |   11.66578   .6441142    18.11   0.000     10.40186     12.9297
------------------------------------------------------------------------------
bysort pref_id: egen suicidebar = mean(suicide)
bysort pref_id: egen unempbar   = mean(unemp)
generate suicide2 = suicide - suicidebar
generate unemp2   = unemp - unempbar

regress suicide2 unemp2, noconstant
      Source |       SS           df       MS      Number of obs   =     1,081
-------------+----------------------------------   F(1, 1080)      =   1323.97
       Model |   8704.6819         1   8704.6819   Prob > F        =    0.0000
    Residual |  7100.67017     1,080   6.5746946   R-squared       =    0.5507
-------------+----------------------------------   Adj R-squared   =    0.5503
       Total |  15805.3521     1,081  14.6210472   Root MSE        =    2.5641

------------------------------------------------------------------------------
    suicide2 | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
      unemp2 |   3.066965   .0842889    36.39   0.000     2.901577    3.232354
------------------------------------------------------------------------------
reghdfe suicide unemp, absorb(pref_id year) vce(cluster pref)
(MWFE estimator converged in 2 iterations)

HDFE Linear regression                            Number of obs   =      1,081
Absorbing 2 HDFE groups                           F(   1,     46) =       6.66
Statistics robust to heteroskedasticity           Prob > F        =     0.0131
                                                  R-squared       =     0.8710
                                                  Adj R-squared   =     0.8621
                                                  Within R-sq.    =     0.0228
Number of clusters (pref)    =         47         Root MSE        =     1.8218

                                  (Std. err. adjusted for 47 clusters in pref)
------------------------------------------------------------------------------
             |               Robust
     suicide | Coefficient  std. err.      t    P>|t|     [95% conf. interval]
-------------+----------------------------------------------------------------
       unemp |   .7708683    .298724     2.58   0.013     .1695681    1.372168
       _cons |   19.59688   1.132139    17.31   0.000       17.318    21.87576
------------------------------------------------------------------------------

Absorbed degrees of freedom:
-----------------------------------------------------+
 Absorbed FE | Categories  - Redundant  = Num. Coefs |
-------------+---------------------------------------|
     pref_id |        47          47           0    *|
        year |        23           0          23     |
-----------------------------------------------------+
* = FE nested within cluster; treated as redundant for DoF computation
list pref_id __hdfe1__ if mod(_n, 23) == 0
      +----------------------+
      | pref_id    __hdfe1__ |
      |----------------------|
  23. |    三重   -1.9823115 |
  46. |    京都   -3.3060939 |
  69. |    佐賀   -.32426722 |
  92. |    兵庫   -1.9772961 |
 115. |  北海道    .05584997 |
      |----------------------|
 138. |    千葉   -2.5357812 |
 161. |  和歌山    1.6549044 |
 184. |    埼玉   -2.3950357 |
 207. |    大分   -.37137483 |
 230. |    大阪   -1.5887391 |
      |----------------------|
 253. |    奈良   -4.3170379 |
 276. |    宮城   -1.0920616 |
 299. |    宮崎    4.7210108 |
 322. |    富山    2.6106225 |
 345. |    山口    .92918042 |
      |----------------------|
 368. |    山形    3.3584413 |
 391. |    山梨    .89431274 |
 414. |    岐阜   -.39489948 |
 437. |    岡山    -3.392388 |
 460. |    岩手    7.2818804 |
      |----------------------|
 483. |    島根    4.8399829 |
 506. |    広島   -1.8956471 |
 529. |    徳島   -3.5536278 |
 552. |    愛媛    .01250052 |
 575. |    愛知   -3.3654756 |
      |----------------------|
 598. |    新潟    5.6375061 |
 621. |    東京    -2.656453 |
 644. |    栃木    .71168929 |
 667. |    沖縄   -1.9548115 |
 690. |    滋賀   -2.5095867 |
      |----------------------|
 713. |    熊本   -.49331359 |
 736. |    石川   -1.7959955 |
 759. |  神奈川   -3.7659529 |
 782. |    福井   -1.0543099 |
 805. |    福岡   -1.1377319 |
      |----------------------|
 828. |    福島    1.4614935 |
 851. |    秋田    10.430325 |
 874. |    群馬    1.3416761 |
 897. |    茨城    -.5299815 |
 920. |    長崎   -.60218704 |
      |----------------------|
 943. |    長野    .06007812 |
 966. |    青森    5.0161638 |
 989. |    静岡   -2.1217193 |
1012. |    香川   -2.8850441 |
1035. |    高知    1.8497092 |
      |----------------------|
1058. |    鳥取   -.01167187 |
1081. |  鹿児島    1.1434689 |
      +----------------------+
sort pref_id year
list year __hdfe2__ in 1/23
     +-------------------+
     | year    __hdfe2__ |
     |-------------------|
  1. | 1997   -2.1605963 |
  2. | 1998    3.4228034 |
  3. | 1999    2.4048345 |
  4. | 2000    1.6042981 |
  5. | 2001    .79790084 |
     |-------------------|
  6. | 2002    1.4016126 |
  7. | 2003    3.3661109 |
  8. | 2004    2.1530947 |
  9. | 2005    2.5850204 |
 10. | 2006    2.6350294 |
     |-------------------|
 11. | 2007    3.0396469 |
 12. | 2008    2.4702245 |
 13. | 2009    2.0611943 |
 14. | 2010    .68827919 |
 15. | 2011    .32122874 |
     |-------------------|
 16. | 2012   -1.3296468 |
 17. | 2013   -1.2747591 |
 18. | 2014   -2.3618466 |
 19. | 2015   -3.1123704 |
 20. | 2016    -4.336074 |
     |-------------------|
 21. | 2017   -4.5363731 |
 22. | 2018   -4.8443857 |
 23. | 2019   -4.9952265 |
     +-------------------+
quietly reghdfe suicide unemp, absorb(year)
estimates store POLS

quietly reghdfe suicide unemp, absorb(year pref_id) vce(cluster pref_id)
estimates store TWFE

etable, estimates(POLS TWFE) keep(unemp) showstars showstarsnote column(estimates) title(Dependent Var. = Suicide Rate)
Dependent Var. = Suicide Rate
--------------------------
         POLS       TWFE  
--------------------------
unemp   0.513 **   0.771 *
      (0.130)    (0.299)  
N        1081       1081  
--------------------------
** p<.01, * p<.05

Rによるデータ演習

import delimited "minimum_wage.csv", case(preserve) clear 

list in 1/4
     +-------------------------------------+
     | store_id   post    emp   price   nj |
     |-------------------------------------|
  1. |        6      0      5    4.72    1 |
  2. |       14      0     16     4.4    1 |
  3. |       26      0   41.5    2.95    1 |
  4. |       27      0     13    4.25    1 |
     +-------------------------------------+
generate postnj = post * nj
reghdfe emp postnj, absorb(store_id post)