Convergence Issues

Author

AS

Packages

library("MASS") 
library("stargazer")

Please cite as: 
 Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
 R package version 5.2.3. https://CRAN.R-project.org/package=stargazer 
library("car") 
Loading required package: carData
set.seed(123)

Data

df <- mtcars

Model

If you run into modelling issues, try to -

  • Relax the convergence criterion (epsilon = 1e-4) - keep it less than 0 though.

    • Try to increase the maximum iterations (upto a reasonable level).
  • Check for collinearity (VIF) and remove highly correlated predictors.

  • Inspect data for extreme values and handle them appropriately.

  • Use Poisson regression to generate better starting values.

  • Simplify the model by removing less relevant predictors.

?distributions 
model1 <- glm.nb( formula = carb ~ mpg + cyl + wt +qsec +vs +am,
                  data = df 
                  )
Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
control$trace > : iteration limit reached
Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
control$trace > : iteration limit reached
model2 <- glm.nb( formula = carb ~ mpg + cyl + wt +qsec +vs +am, 
                  data = df, 
                  control = glm.control(maxit = 1000,   # keep uptil 1000 
                                        epsilon = 1e-6, # keep between 1e-8 and 1e-1
                                        trace = TRUE    # Enable debugging to track what happens during the optimization process 
                                        ) 
                  )
Warning in sqrt(1/i): NaNs produced
Warning in sqrt(1/i): NaNs produced
Theta(1) = 4304520000000.000000, 2(Ls - Lm) = 9.343750
stargazer(model1, model2, 
          type = "text"
          )

===================================================================
                                 Dependent variable:               
                  -------------------------------------------------
                                        carb                       
                              (1)                      (2)         
-------------------------------------------------------------------
mpg                         -0.038                   -0.038        
                            (0.053)                  (0.053)       
                                                                   
cyl                         -0.106                   -0.106        
                            (0.198)                  (0.198)       
                                                                   
wt                           0.258                    0.258        
                            (0.271)                  (0.271)       
                                                                   
qsec                        -0.213                   -0.213        
                            (0.158)                  (0.158)       
                                                                   
vs                           0.093                    0.093        
                            (0.464)                  (0.464)       
                                                                   
am                           0.255                    0.255        
                            (0.422)                  (0.422)       
                                                                   
Constant                     5.191                    5.191        
                            (3.523)                  (3.523)       
                                                                   
-------------------------------------------------------------------
Observations                  32                       32          
Log Likelihood              -50.206                  -50.031       
theta             132,293.200 (3,255,983.000) 4,304,518,627,158.000
Akaike Inf. Crit.           114.413                  114.062       
===================================================================
Note:                                   *p<0.1; **p<0.05; ***p<0.01

Control the convergence process

  • If convergence fails, try to increase the maxit (maxium iterations), and increase the error (relax the epsilon constraint by increasing its magnitude).

  • A very high value of Theta (dispersion parameter - controls the variance of negative binomial distribution) implies that the variance is approaching the variance of a Poisson distribution, suggesting that overdispersion is minimal or that the optimization algorithm is struggling to fit Theta correctly.

    hist(df$carb)

    var(df$carb)/mean(df$carb)
    [1] 0.9275986

Either way, look at the output.

Common things to check for -

No Extreme Outliers

boxplot(df) 

3 * IQR is a good rule of thumb.

No Multicollinearity

vif(glm(carb ~ mpg + cyl + wt + qsec + vs + am, data = df)) 
     mpg      cyl       wt     qsec       vs       am 
6.721984 9.710670 6.483070 5.585198 4.502740 4.042162 

Not an issue here, and thus no variables have to be removed.

Fit a Poisson model to generate starting values

poisson_model <- glm(carb ~ mpg + cyl + wt + qsec + vs + am, family = poisson, data = df)

Use coefficients from Poisson model as starting values

model3 <- glm.nb( formula = carb ~ mpg + cyl + wt + qsec + vs + am, 
                  data = df, 
                  start = coef(poisson_model) 
                  )
Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
control$trace > : iteration limit reached
Warning in theta.ml(Y, mu, sum(w), w, limit = control$maxit, trace =
control$trace > : iteration limit reached

Use coefficients from Poisson model as starting values and control convergence

model4 <- glm.nb( formula = carb ~ mpg + cyl + wt + qsec + vs + am, 
                  data = df, 
                  start = coef(poisson_model), 
                  control = glm.control(maxit = 300, # keep uptil 1000 
                                        epsilon = 1e-8, # keep between 1e-8 and 1e-1
                                        trace = TRUE # Enable debugging to track what happens
                                        )
                  )
Warning in sqrt(1/i): NaNs produced
Warning in sqrt(1/i): NaNs produced
Theta(1) = 124691000.000000, 2(Ls - Lm) = 9.046400

Try reducing model complexity

If the model includes too many predictors relative to the number of observations, it may be overparameterized. Consider simplifying the model by removing less important predictors.

stargazer(model1, model2, model3, model4, 
          type = "text")

===============================================================================================================
                                                       Dependent variable:                                     
                  ---------------------------------------------------------------------------------------------
                                                              carb                                             
                              (1)                      (2)                      (3)                   (4)      
---------------------------------------------------------------------------------------------------------------
mpg                         -0.038                   -0.038                   -0.038                -0.038     
                            (0.053)                  (0.053)                  (0.053)               (0.053)    
                                                                                                               
cyl                         -0.106                   -0.106                   -0.106                -0.106     
                            (0.198)                  (0.198)                  (0.198)               (0.198)    
                                                                                                               
wt                           0.258                    0.258                    0.258                 0.258     
                            (0.271)                  (0.271)                  (0.271)               (0.271)    
                                                                                                               
qsec                        -0.213                   -0.213                   -0.213                -0.213     
                            (0.158)                  (0.158)                  (0.158)               (0.158)    
                                                                                                               
vs                           0.093                    0.093                    0.093                 0.093     
                            (0.464)                  (0.464)                  (0.464)               (0.464)    
                                                                                                               
am                           0.255                    0.255                    0.255                 0.255     
                            (0.422)                  (0.422)                  (0.422)               (0.422)    
                                                                                                               
Constant                     5.191                    5.191                    5.191                 5.191     
                            (3.523)                  (3.523)                  (3.523)               (3.523)    
                                                                                                               
---------------------------------------------------------------------------------------------------------------
Observations                  32                       32                       32                    32       
Log Likelihood              -50.206                  -50.031                  -50.206               -50.206    
theta             132,293.200 (3,255,983.000) 4,304,518,627,158.000 132,293.700 (3,255,978.000) 124,690,658.000
Akaike Inf. Crit.           114.413                  114.062                  114.413               114.412    
===============================================================================================================
Note:                                                                               *p<0.1; **p<0.05; ***p<0.01