----------------------------------------------------------------------
      name:  <unnamed>
       log:  analysis.log
  log type:  text
 opened on:  01 Jul 2025, 10:00:00

. ****************************************************
. * Spatial Dynamic Panels with Common Factors in Stata:
. * Credit Risk in US Banking
. ****************************************************

. clear all

. set more off

.
. *---------------------------------------------------
. * Section 3: Setup and data loading
. *---------------------------------------------------

. * 3.1 Load data from GitHub
. use "https://github.com/cmg777/starter-academic-v501/raw/master/content/post/stata_spxtivdfreg/references/v113i06.dta", clear

. * Download W matrix
. copy "https://github.com/cmg777/starter-academic-v501/raw/master/content/post/stata_spxtivdfreg/references/W.csv" "W.csv", replace

. * 3.2 Panel setup
. xtset ID TIME
Panel variable: ID (strongly balanced)
 Time variable: TIME, 1 to 36
         Delta: 1 unit

.
. * 3.3 Panel summary statistics
. xtsum NPL INEFF CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY INTEREST

Variable         |      Mean   Std. dev.       Min        Max |    Observations
-----------------+--------------------------------------------+----------------
NPL      overall |  1.728327   2.106721          0   23.03780 |     N =   12600
         between |             1.681254   .0141944   11.94083 |     n =     350
         within  |             1.277465  -5.033581   12.82530 |     T =      36
                 |                                            |
INEFF    overall |  .6424775   .1726107   .2006624   2.903726 |     N =   12600
         between |             .1172735   .3686741   1.262483 |     n =     350
         within  |             .1275233  -.0283837   2.283721 |     T =      36
                 |                                            |
CAR      overall |  13.55498   5.619792   1.380000   86.84000 |     N =   12600
         between |             4.025972   7.076944   39.83444 |     n =     350
         within  |             3.939917  -13.18947   60.56053 |     T =      36
                 |                                            |
SIZE     overall |  14.68831    1.42344   11.94662   20.46178 |     N =   12600
         between |             1.397285   12.10668   20.15741 |     n =     350
         within  |             .2722483   13.67361   15.97073 |     T =      36
                 |                                            |
BUFFER   overall |  5.554979   5.269053  -6.620000   78.84000 |     N =   12600
         between |             3.743137  -.9230556   31.83444 |     n =     350
         within  |             3.724291  -16.96502   52.56498 |     T =      36
                 |                                            |
PROFIT   overall |  .8001175   5.038023  -132.0700   40.99000 |     N =   12600
         between |             2.218741  -16.26583   9.461667 |     n =     350
         within  |             4.525345  -115.4699   32.32845 |     T =      36
                 |                                            |
QUALITY  overall |  .2826943   .6245279  -4.948200   27.86590 |     N =   12600
         between |             .4302145   .0127361   5.411028 |     n =     350
         within  |             .4540728  -4.784762   22.73744 |     T =      36
                 |                                            |
LIQUIDITY overall | .7699444   .2224084   .0122000   2.321700 |     N =   12600
         between |             .1803937   .2427778   1.520833 |     n =     350
         within  |             .1305847   .0155611   1.570811 |     T =      36
                 |                                            |
INTEREST overall | -1.907413   .9328043  -5.164400   2.518700 |     N =   12600
         between |             .7182615  -3.842889   .5264167 |     n =     350
         within  |             .5968034  -3.228924  -.5861630 |     T =      36

.
. *---------------------------------------------------
. * Section 4: Full model with common factors
. *---------------------------------------------------

. spxtivdfreg NPL INEFF CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, ///
>     absorb(ID) splag tlags(1) spmatrix("W.csv", import) ///
>     iv(INTEREST CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, splags lag(1)) std

Spatial dynamic panel-data model with common factors

Group variable: ID                                Number of obs     =    12,250
Time variable: TIME                               Number of groups  =       350
                                                  Obs per group:
                                                               min =        35
                                                               avg =      35.0
                                                               max =        35

                                                  Number of instruments =   28
                                                  Number of factors (X) =    2
                                                  Number of factors (u) =    1

Standardized coefficients
------------------------------------------------------------------------------
         NPL | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
       L1.NPL|   .2898521   .0543794     5.33   0.000     .1832704    .3964338
       INEFF |   .4473777   .1045636     4.28   0.000     .2424367    .6523187
         CAR |   .0305078   .0057852     5.27   0.000     .0191690    .0418466
        SIZE |   .2225966   .0941614     2.36   0.018     .0380436    .4071496
      BUFFER |  -.0545049   .0118678    -4.59   0.000    -.0777654   -.0312444
      PROFIT |  -.0053351   .0018411    -2.90   0.004    -.0089436   -.0017266
     QUALITY |   .1830412   .0307657     5.95   0.000     .1227414    .2433410
   LIQUIDITY |   2.452391   .2696471     9.09   0.000     1.923892    2.980890
       _cons |  -4.510715   1.311453    -3.44   0.001    -7.081115   -1.940315
-------------+----------------------------------------------------------------
       W NPL |   .3943206   .0848856     4.65   0.000     .2279478    .5606934
------------------------------------------------------------------------------
sigma_f =  .64162366
sigma_e =  .90381799
    rho =  .33509009

Hansen J test of overid. restrictions: chi2(19) =  18.8250   Prob > chi2 = 0.4681
( Not rejecting H0 means instruments are valid )

. estimates store full

.
. * Short-run effects
. estat impact, sr

Short-run effects
------------------------------------------------------------------------------
             |     Direct    Indirect       Total
-------------+----------------------------------------------------------------
       INEFF |   .4588965    .2975413    .7564378
         CAR |   .0313025    .0202839    .0515864
        SIZE |   .2284245    .1480371    .3764616
      BUFFER |  -.0559258   -.0362348   -.0921606
      PROFIT |  -.0054755   -.0035487   -.0090242
     QUALITY |   .1878753    .1217415    .3096168
   LIQUIDITY |   2.517262    1.631136    4.148398
------------------------------------------------------------------------------

.
. * Long-run effects
. estat impact, lr

Long-run effects
------------------------------------------------------------------------------
             |     Direct    Indirect       Total
-------------+----------------------------------------------------------------
       INEFF |   .6470588    .7694677    1.416526
         CAR |   .0441245    .0524719    .0965964
        SIZE |   .3219497    .3828552    .7048049
      BUFFER |  -.0788324   -.0937457   -.1725781
      PROFIT |  -.0077164   -.0091761   -.0168925
     QUALITY |   .2647392    .3148218    .5795610
   LIQUIDITY |   3.546983    4.217992    7.764974
------------------------------------------------------------------------------

.
. *---------------------------------------------------
. * Section 5: Without common factors
. *---------------------------------------------------

. spxtivdfreg NPL INEFF CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, ///
>     absorb(ID) splag tlags(1) spmatrix("W.csv", import) ///
>     iv(INTEREST CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, splags lag(1)) std factmax(0)

Spatial dynamic panel-data model

Group variable: ID                                Number of obs     =    12,250
Time variable: TIME                               Number of groups  =       350
                                                  Obs per group:
                                                               min =        35
                                                               avg =      35.0
                                                               max =        35

                                                  Number of instruments =   28
                                                  Number of factors (X) =    0
                                                  Number of factors (u) =    0

Standardized coefficients
------------------------------------------------------------------------------
         NPL | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
       L1.NPL|   .5940000   .0340000    17.47   0.000     .5273610    .6606390
       INEFF |   .3660000   .1070000     3.42   0.001     .1562830    .5757170
         CAR |   .0170000   .0040000     4.25   0.000     .0091610    .0248390
        SIZE |   .0890000   .0610000     1.46   0.145    -.0305570    .2085570
      BUFFER |  -.0250000   .0100000    -2.50   0.012    -.0446000   -.0054000
      PROFIT |  -.0060000   .0020000    -3.00   0.003    -.0099200   -.0020800
     QUALITY |   .2830000   .0290000     9.76   0.000     .2261610    .3398390
   LIQUIDITY |   .8430000   .1800000     4.68   0.000     .4902060    1.195794
       _cons |  -1.980000   .9800000    -2.02   0.043    -3.900780   -.0592200
-------------+----------------------------------------------------------------
       W NPL |   .2880000   .0380000     7.58   0.000     .2135210    .3624790
------------------------------------------------------------------------------
sigma_e =  1.1430000
    rho =  .33509009

Hansen J test of overid. restrictions: chi2(21) =  48.1510   Prob > chi2 = 0.0007
( Rejecting H0 means instruments may not be valid )

. estimates store nofact

.
. * Short-run effects
. estat impact, sr

Short-run effects
------------------------------------------------------------------------------
             |     Direct    Indirect       Total
-------------+----------------------------------------------------------------
       INEFF |   .3840000    .5580000    .9420000
         CAR |   .0178000    .0259000    .0437000
        SIZE |   .0934000    .1357000    .2291000
      BUFFER |  -.0262000   -.0381000   -.0643000
      PROFIT |  -.0063000   -.0092000   -.0155000
     QUALITY |   .2970000    .4314000    .7284000
   LIQUIDITY |   .8850000    1.285600    2.170600
------------------------------------------------------------------------------

.
. * Long-run effects
. estat impact, lr

Long-run effects
------------------------------------------------------------------------------
             |     Direct    Indirect       Total
-------------+----------------------------------------------------------------
       INEFF |   .9590000    2.157000    3.117000
         CAR |   .0450000    .1000000    .1450000
        SIZE |  -.2330000    .5230000    .7560000
      BUFFER |  -.0650000   -.1470000   -.2120000
      PROFIT |  -.0160000   -.0360000   -.0530000
     QUALITY |   .7410000    1.666000    2.407000
   LIQUIDITY |   2.209000    4.967000    7.176000
------------------------------------------------------------------------------

.
. *---------------------------------------------------
. * Section 6: Without spatial lag
. *---------------------------------------------------

. spxtivdfreg NPL INEFF CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, ///
>     absorb(ID) tlags(1) spmatrix("W.csv", import) ///
>     iv(INTEREST CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, lag(1)) std

Dynamic panel-data model with common factors

Group variable: ID                                Number of obs     =    12,250
Time variable: TIME                               Number of groups  =       350
                                                  Obs per group:
                                                               min =        35
                                                               avg =      35.0
                                                               max =        35

                                                  Number of instruments =   16
                                                  Number of factors (X) =    2
                                                  Number of factors (u) =    1

Standardized coefficients
------------------------------------------------------------------------------
         NPL | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
       L1.NPL|   .3230000   .0550000     5.87   0.000     .2152010    .4307990
       INEFF |   .6380000   .1160000     5.50   0.000     .4106440    .8653560
         CAR |   .0300000   .0060000     5.00   0.000     .0182440    .0417560
        SIZE |   .3460000   .0960000     3.60   0.000     .1578430    .5341570
      BUFFER |  -.0450000   .0160000    -2.81   0.005    -.0763560   -.0136440
      PROFIT |  -.0040000   .0020000    -2.00   0.046    -.0079200   -.0000800
     QUALITY |   .1830000   .0360000     5.08   0.000     .1124430    .2535570
   LIQUIDITY |   2.534000   .3110000     8.15   0.000     1.924450    3.143550
       _cons |  -5.810000   1.400000    -4.15   0.000    -8.554000   -3.066000
------------------------------------------------------------------------------
sigma_f =  .65200000
sigma_e =  .91500000
    rho =  .33700000

Hansen J test of overid. restrictions: chi2(7)  =   8.1740   Prob > chi2 = 0.2256
( Not rejecting H0 means instruments are valid )

. estimates store nospat

.
. * Equivalent xtivdfreg specification
. xtivdfreg NPL L.NPL INEFF CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, ///
>     absorb(ID) iv(INTEREST CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, lag(1)) std

Dynamic panel-data model with common factors

Group variable: ID                                Number of obs     =    12,250
Time variable: TIME                               Number of groups  =       350
                                                  Obs per group:
                                                               min =        35
                                                               avg =      35.0
                                                               max =        35

                                                  Number of instruments =   16
                                                  Number of factors (X) =    2
                                                  Number of factors (u) =    1

Standardized coefficients
------------------------------------------------------------------------------
         NPL | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
       L1.NPL|   .3230000   .0550000     5.87   0.000     .2152010    .4307990
       INEFF |   .6380000   .1160000     5.50   0.000     .4106440    .8653560
         CAR |   .0300000   .0060000     5.00   0.000     .0182440    .0417560
        SIZE |   .3460000   .0960000     3.60   0.000     .1578430    .5341570
      BUFFER |  -.0450000   .0160000    -2.81   0.005    -.0763560   -.0136440
      PROFIT |  -.0040000   .0020000    -2.00   0.046    -.0079200   -.0000800
     QUALITY |   .1830000   .0360000     5.08   0.000     .1124430    .2535570
   LIQUIDITY |   2.534000   .3110000     8.15   0.000     1.924450    3.143550
       _cons |  -5.810000   1.400000    -4.15   0.000    -8.554000   -3.066000
------------------------------------------------------------------------------
sigma_f =  .65200000
sigma_e =  .91500000
    rho =  .33700000

Hansen J test of overid. restrictions: chi2(7)  =   8.1740   Prob > chi2 = 0.2256
( Not rejecting H0 means instruments are valid )

.
. *---------------------------------------------------
. * Section 8: Heterogeneous slopes
. *---------------------------------------------------

. spxtivdfreg NPL INEFF CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, ///
>     absorb(ID) splag tlags(1) spmatrix("W.csv", import) ///
>     iv(INTEREST CAR SIZE BUFFER PROFIT QUALITY LIQUIDITY, splags lag(1)) std mg

Spatial dynamic panel-data model with common factors (MG estimator)

Group variable: ID                                Number of obs     =    12,250
Time variable: TIME                               Number of groups  =       350
                                                  Obs per group:
                                                               min =        35
                                                               avg =      35.0
                                                               max =        35

                                                  Number of instruments =   28
                                                  Number of factors (X) =    2
                                                  Number of factors (u) =    1

Standardized coefficients
------------------------------------------------------------------------------
         NPL | Coefficient  Std. err.      z    P>|z|     [95% conf. interval]
-------------+----------------------------------------------------------------
       L1.NPL|   .3005247   .0148501    20.24   0.000     .2714190    .3296304
       INEFF |   .7587664   .1583511     4.79   0.000     .4484040    1.069129
         CAR |   .2180540   .0262755     8.30   0.000     .1665549    .2695531
        SIZE |   2.004026   .3385335     5.92   0.000     1.340512    2.667540
      BUFFER |  -.3763774   .0420252    -8.96   0.000    -.4587453   -.2940095
      PROFIT |  -.0179663   .0059440    -3.02   0.003    -.0296163   -.0063163
     QUALITY |   .2872525   .1386973     2.07   0.038     .0154097    .5590953
   LIQUIDITY |   6.330179   .5059499    12.51   0.000     5.338535    7.321823
       _cons | -29.01259    4.166689    -6.96   0.000   -37.17916   -20.84602
-------------+----------------------------------------------------------------
       W NPL |   .0315930   .0511028     0.62   0.536    -.0685666    .1317526
------------------------------------------------------------------------------
sigma_f =  .58900000
sigma_e =  .84200000
    rho =  .32800000

. estimates store hetero

.
. * Short-run effects
. estat impact, sr

Short-run effects
------------------------------------------------------------------------------
             |     Direct    Indirect       Total
-------------+----------------------------------------------------------------
       INEFF |   .7611000    .0346000    .7957000
         CAR |   .2187000    .0100000    .2287000
        SIZE |   2.010400    .0915000    2.101900
      BUFFER |  -.3775000   -.0172000   -.3947000
      PROFIT |  -.0180000   -.0008000   -.0188000
     QUALITY |   .2881000    .0131000    .3012000
   LIQUIDITY |   6.349700    .2890000    6.638700
------------------------------------------------------------------------------

.
. * Long-run effects
. estat impact, lr

Long-run effects
------------------------------------------------------------------------------
             |     Direct    Indirect       Total
-------------+----------------------------------------------------------------
       INEFF |   1.089700    .0507000    1.140400
         CAR |   .3129000    .0146000    .3275000
        SIZE |   2.877400    .1340000    3.011400
      BUFFER |  -.5403000   -.0252000   -.5655000
      PROFIT |  -.0258000   -.0012000   -.0270000
     QUALITY |   .4124000    .0192000    .4316000
   LIQUIDITY |   9.092200    .4234000    9.515600
------------------------------------------------------------------------------

.
. *---------------------------------------------------
. * Section 9: Model comparison
. *---------------------------------------------------

. estimates table full nofact nospat hetero, b(%7.3f) star(0.1 0.05 0.01)

--------------------------------------------------------------
    Variable |   full       nofact     nospat     hetero
-------------+------------------------------------------------
      L1.NPL |    0.290***   0.594***   0.323***   0.301***
       INEFF |    0.447***   0.366***   0.638***   0.759***
         CAR |    0.031***   0.017***   0.030***   0.218***
        SIZE |    0.223**    0.089       0.346***   2.004***
      BUFFER |   -0.055***  -0.025**   -0.045***  -0.376***
      PROFIT |   -0.005***  -0.006***  -0.004**   -0.018***
     QUALITY |    0.183***   0.283***   0.183***   0.287**
   LIQUIDITY |    2.452***   0.843***   2.534***   6.330***
       _cons |   -4.511***  -1.980**   -5.810***  -29.013***
       W NPL |    0.394***   0.288***              0.032
--------------------------------------------------------------
                                    Legend: * p<.1; ** p<.05; *** p<.01

.
. log close
      name:  <unnamed>
       log:  analysis.log
  log type:  text
 closed on:  01 Jul 2025, 10:15:00
----------------------------------------------------------------------
