amrei-stammann / alpaca

An R-package for fitting glm's with high-dimensional k-way fixed effects
43 stars 6 forks source link

Ensuring convergence #7

Closed zauster closed 5 years ago

zauster commented 5 years ago

Hey, I am trying a few different specifications of a gravity model. However, some specifications do not converge (I set the iter.max to 500). As suggested by you, I did center the dependent variable which actually helped for some specifications (converging now in ~20 iterations), but not for all.

?feglmControl states that step.tol is deprecated and from the descriptions of dev.tol, center.tol and rho.tol I am not sure what to change/tweak. Do you have any suggestions what I could try so that the algorithm converges?

Maybe this can help: the following table contains four specifications, the "CR" column is the specification that converged.

> screenreg(l = res.list[["entry"]][[1]])

============================================================================================================
                   FI                          CR             CM                     DM                     
------------------------------------------------------------------------------------------------------------
mTariffrate                         -0.00          -0.00                  -0.00                    -0.00    
                                    (0.00)         (0.00)                 (0.00)                   (0.00)   
ADP                                 -1.85 ***       0.06                  -0.00 ***                -0.03 ***
                                    (0.51)         (0.12)                 (0.00)                   (0.01)   
CV                                   1.49           0.32 *                 0.00 ***                 0.02 *  
                                    (0.95)         (0.16)                 (0.00)                   (0.01)   
LIC                                  0.81          -0.05                   0.00 **   1592487684065077.75 ***
                                    (0.86)         (0.03)                 (0.00)      (95859685957737.45)   
SG                    8694776536929047.00          -0.36      -6570414465919.87 ***  -330971581943956.69 ***
                     (5204241607941789.00)         (0.25)      (935747670905.69)      (61030033680165.66)   
SPS                                  7.77          -0.77 ***               0.00                    -0.04    
                                    (4.32)         (0.03)                 (0.00)                   (0.53)   
SPSSTC                              -1.03 ***       0.27                  -0.00 ***                -0.02 *  
                                    (0.30)         (0.14)                 (0.00)                   (0.01)   
SSG                -108779506535076880.00 ***       0.27       -851107651805.04 **    218485560245663.50 ***
                     (4450539728806149.00)         (0.29)      (299816426621.45)      (56079880011381.86)   
STE                  24256570314180068.00 ***       0.04      -1215720444397.88 ***  1359680284562489.25 ***
                     (3664961912643176.00)         (0.09)      (135708147505.24)      (54144032256695.34)   
TBTSTC                               0.18 ***       0.00                   0.00                    -0.06 ***
                                    (0.04)         (0.03)                 (0.00)                   (0.01)   
TBT                    718844803026497.50 ***       0.06 ***     -2132657670.11 ***  1090895219986788.25 ***
                       (36549854297461.68)         (0.02)         (495391923.06)     (122741193389396.80)   
TRQ                  11472027289095974.00 ***      -0.02      -4954935000286.38 ***   991978762302774.12 ***
                     (1253575685882488.25)         (0.11)      (156105220860.02)      (74878297330201.75)   
XS                  -11482880783191476.00 ***      -0.92 ***    373397738655.01 **   -272160643952146.91 ***
                      (769999558582706.50)         (0.14)      (114862955225.97)      (55444465840418.32)   
competition.entry                    0.08 **        0.08 **                0.07 **                  0.08 ***
                                    (0.02)         (0.02)                 (0.02)                   (0.02)   
full_fta.entry                      -0.29 ***      -0.25 ***              -0.29 ***                -0.21 ***
                                    (0.03)         (0.03)                 (0.03)                   (0.03)   
investments.entry                    0.05 *         0.06 **                0.04 *                   0.02    
                                    (0.02)         (0.02)                 (0.02)                   (0.02)   
iprs.entry                           0.07 **        0.06 **                0.06 *                   0.05 *  
                                    (0.02)         (0.02)                 (0.02)                   (0.02)   
procurement.entry                   -0.07 ***      -0.07 **               -0.05 **                 -0.06 ** 
                                    (0.02)         (0.02)                 (0.02)                   (0.02)   
standards.entry                      0.31 ***       0.27 ***               0.31 ***                 0.23 ***
                                    (0.02)         (0.02)                 (0.02)                   (0.02)   
services.entry                      -0.12 ***      -0.12 ***              -0.11 ***                -0.09 ***
                                    (0.02)         (0.02)                 (0.02)                   (0.02)   
------------------------------------------------------------------------------------------------------------
Deviance                         23144.52       23727.83               23066.04                 22783.63    
Num. obs.                       613019         613019                 613019                   613019       
Num groups:expFE                  5053           5053                   5053                     5053       
Num groups:impFE                  2965           2965                   2965                     2965       
Num groups:bilFE                 36777          36777                  36777                    36777       
Num groups:yearFE                   22             22                     22                       22       
===========================================================================

mTariffrate is a double, all variables ending in "entry" are dummies. The variables in between are doubles (in FI and CR spec), integer (in CM) or dummies (in DM).

amrei-stammann commented 5 years ago

Hello,

first a small explanation of the arguments in feglmControl():

To achieve convergence you could try the following:

  1. a larger value of 'dev.tol'; maybe 1.0e-05 or larger
  2. if the algorithm does not converge because step-halving fails, you may try to use a lower value of 'rho.tol' like 1.0e-15 which increases the number of tries to improve the deviance.
  3. maybe try another rescaling of your dependent variable. For instance if your trade flows are measured in billions you can devide your trade flows by a billion.
  4. you could also try different starting values for beta like 'beta.start = numeric(20)'

However, if I got your model specification correctly than you might have a specification issue. Assuming that "bilFE" are the pair/dyadic fixed effects than it looks strange that you incorporate importer and exporter FE along with pairs. Pairs can be perfectly explained by the importer and exporter FE.

zauster commented 5 years ago

Thanks for the explanation!

2\. if the algorithm does not converge because step-halving fails, you may try to use a lower value of 'rho.tol' like 1.0e-15 which increases the number of tries to improve the deviance.

How do I know if the step-halving fails?

I will try your other recommendations and report.

However, if I got your model specification correctly than you might have a specification issue. Assuming that "bilFE" are the pair/dyadic fixed effects than it looks strange that you incorporate importer and exporter FE along with pairs. Pairs can be perfectly explained by the importer and exporter FE.

Well spotted, but that's just poorly named by me: expFE are exporter-time fixed effects. Since this is a panel, this should be fine.

amrei-stammann commented 5 years ago

Step-halving: In the case that step-halving fails you get an error and a notification that it has failed.

Model specification: Ok, then I assume that impFE are also importer-time FE, but what does yearFE do in the model? Or is it also named poorly?

zauster commented 5 years ago

Since I got no notification, step-halving seems to work fine. True, impFE are importer-time FE. yearFE stands for year fixed effects (a not-so-poor naming, I think ;-))

amrei-stammann commented 5 years ago

In this case impFE and expFE perfectly explain yearFE. As far as I know you can't identify anything with this specification and it is no surprise that the routine does not converge.

zauster commented 5 years ago

Yeah, you're right. In ?feglm you even state that "if it doesn't converge, the specification is probably wrong"... I've eliminated the year fixed effects and re-estimated the model, which still doesn't converge. But I suspect that some of the variables might (more or less) coincide with some of the fixed effects. I will need to investigate more here. Thanks for your help though!