Fatal error with Negative Binomial model

Hi,

I am getting this error when trying to run Negative Binomial models. The problem seems to be related to certain packages like MASS.

Examples of code that causes the problem:

Example 1:

uni_models ← wk_warm %>%
group_split(zone, keep = TRUE) %>%
set_names(unique(wk_warm$zone)) %>%
imap(function(dfz, z) {
map(set_names(predictors), ~ safe_nb_fit(dfz, .x, zone = z))
})

Example 2:

multi_nb ← MASS::glm.nb(cases ~ tavg_s + rhavg_s + ns(idweek, df = 6) + offset(offset_log_pop),
data = wk_warm)

What I have already done:

  • I have uninstalled and installed all the packages
  • I have updated R and Rtools
  • I have restarted my device
  • I have checked on another device and the same error occurs
  • I have done some diagnostic tests to see if the problem is caused by my dataset: The error is not caused by the dataset

Thank you!

Could you please provide a reproducible example following this guide: How to make a reproducible R code example ? That would make it easier to understand the issue and find a solution.

It would also be helpful to have some additional information about your current session, such as the size of the main objects, which you can check as below, for example:

memory.size(wk_warm)

In my experience, R sessions sometimes abort due to memory issues, but there may be other causes as well.

Hi, thank you for the answer!

To make things simpler, irrespective my data, i am getting the error even with this test code at the summary(fit) command:

clean session

sessionInfo()

minimal MASS check

library(MASS)
fit ← glm.nb(Days ~ EthSexAge*Lrn, data = quine)
summary(fit)

Hey Christina and Lucca, following up on this topic!

When summary(fit) crashes for a model fit by MASS::glm.nb() even on the built-in quine data, it’s almost always an environment / method dispatch issue rather than your data.

First, it would be good to confirm you’re really calling MASS’s summary method.

glm.nb() returns an object of class c("negbin","glm","lm"). The S3 method that should run is MASS:::summary.negbin.

library(MASS)
fit <- glm.nb(Days ~ Eth * Sex* Age * Lrn, data = quine)

class(fit)                          # should be "negbin" "glm" "lm"
getS3method("summary", "negbin")    # should print MASS's summary.negbin
getAnywhere("summary.negbin")       # check where it’s coming from

If getS3method() errors or points outside MASS, you’ve likely got a conflicting function or a corrupted install.

That leads to the next thing to try, which is reinstalling packages to fix any corrupt install.

install.packages(c("MASS","Matrix","lme4"))

Here is a minimal crash reproducer, when you run this you shoud get the same outputs I am getting:

library(MASS)
print(packageVersion("MASS"))
#> [1] '7.3.61'
print(getAnywhere("summary.negbin"))
#> A single object matching 'summary.negbin' was found
#> It was found in the following places
#>   registered S3 method for summary from namespace MASS
#>   namespace:MASS
#> with value
#> 
#> function (object, dispersion = 1, correlation = FALSE, ...) 
#> {
#>     if (is.null(dispersion)) 
#>         dispersion <- 1
#>     summ <- c(summary.glm(object, dispersion = dispersion, correlation = correlation), 
#>         object[c("theta", "SE.theta", "twologlik", "th.warn")])
#>     class(summ) <- c("summary.negbin", "summary.glm")
#>     summ
#> }
#> <bytecode: 0x7fbf437daac0>
#> <environment: namespace:MASS>

fit <- glm.nb(Days ~ Eth * Sex * Age * Lrn, data = MASS::quine)
print(class(fit))
#> [1] "negbin" "glm"    "lm"
MASS:::summary.negbin(fit)   # try direct method
#> 
#> Call:
#> glm.nb(formula = Days ~ Eth * Sex * Age * Lrn, data = MASS::quine, 
#>     init.theta = 1.928360145, link = log)
#> 
#> Coefficients: (4 not defined because of singularities)
#>                       Estimate Std. Error z value Pr(>|z|)    
#> (Intercept)             3.0564     0.3760   8.128 4.38e-16 ***
#> EthN                   -0.1386     0.5334  -0.260 0.795023    
#> SexM                   -0.4914     0.5104  -0.963 0.335653    
#> AgeF1                  -0.6227     0.5125  -1.215 0.224334    
#> AgeF2                  -2.3632     1.0770  -2.194 0.028221 *  
#> AgeF3                  -0.3784     0.4546  -0.832 0.405215    
#> LrnSL                  -1.9577     0.9967  -1.964 0.049493 *  
#> EthN:SexM              -0.7524     0.7220  -1.042 0.297400    
#> EthN:AgeF1              0.1029     0.7123   0.144 0.885175    
#> EthN:AgeF2             -0.5546     1.6798  -0.330 0.741297    
#> EthN:AgeF3              0.0633     0.6396   0.099 0.921159    
#> SexM:AgeF1              0.4092     0.8299   0.493 0.621973    
#> SexM:AgeF2              3.1098     1.1655   2.668 0.007624 ** 
#> SexM:AgeF3              1.1145     0.6365   1.751 0.079926 .  
#> EthN:LrnSL              2.2588     1.3019   1.735 0.082743 .  
#> SexM:LrnSL              1.5900     1.1499   1.383 0.166750    
#> AgeF1:LrnSL             2.6421     1.0821   2.442 0.014618 *  
#> AgeF2:LrnSL             4.8585     1.4423   3.369 0.000755 ***
#> AgeF3:LrnSL                 NA         NA      NA       NA    
#> EthN:SexM:AgeF1        -0.3105     1.2055  -0.258 0.796735    
#> EthN:SexM:AgeF2         0.3469     1.7965   0.193 0.846875    
#> EthN:SexM:AgeF3         0.8329     0.8970   0.929 0.353092    
#> EthN:SexM:LrnSL        -0.1639     1.5250  -0.107 0.914411    
#> EthN:AgeF1:LrnSL       -3.5493     1.4270  -2.487 0.012876 *  
#> EthN:AgeF2:LrnSL       -3.3315     2.0919  -1.593 0.111256    
#> EthN:AgeF3:LrnSL            NA         NA      NA       NA    
#> SexM:AgeF1:LrnSL       -2.4285     1.4201  -1.710 0.087246 .  
#> SexM:AgeF2:LrnSL       -4.1914     1.6201  -2.587 0.009679 ** 
#> SexM:AgeF3:LrnSL            NA         NA      NA       NA    
#> EthN:SexM:AgeF1:LrnSL   2.1711     1.9192   1.131 0.257963    
#> EthN:SexM:AgeF2:LrnSL   2.1029     2.3444   0.897 0.369718    
#> EthN:SexM:AgeF3:LrnSL       NA         NA      NA       NA    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> (Dispersion parameter for Negative Binomial(1.9284) family taken to be 1)
#> 
#>     Null deviance: 272.29  on 145  degrees of freedom
#> Residual deviance: 167.45  on 118  degrees of freedom
#> AIC: 1097.3
#> 
#> Number of Fisher Scoring iterations: 1
#> 
#> 
#>               Theta:  1.928 
#>           Std. Err.:  0.269 
#> 
#>  2 x log-likelihood:  -1039.324
base::summary(fit)           # then the generic
#> 
#> Call:
#> glm.nb(formula = Days ~ Eth * Sex * Age * Lrn, data = MASS::quine, 
#>     init.theta = 1.928360145, link = log)
#> 
#> Coefficients: (4 not defined because of singularities)
#>                       Estimate Std. Error z value Pr(>|z|)    
#> (Intercept)             3.0564     0.3760   8.128 4.38e-16 ***
#> EthN                   -0.1386     0.5334  -0.260 0.795023    
#> SexM                   -0.4914     0.5104  -0.963 0.335653    
#> AgeF1                  -0.6227     0.5125  -1.215 0.224334    
#> AgeF2                  -2.3632     1.0770  -2.194 0.028221 *  
#> AgeF3                  -0.3784     0.4546  -0.832 0.405215    
#> LrnSL                  -1.9577     0.9967  -1.964 0.049493 *  
#> EthN:SexM              -0.7524     0.7220  -1.042 0.297400    
#> EthN:AgeF1              0.1029     0.7123   0.144 0.885175    
#> EthN:AgeF2             -0.5546     1.6798  -0.330 0.741297    
#> EthN:AgeF3              0.0633     0.6396   0.099 0.921159    
#> SexM:AgeF1              0.4092     0.8299   0.493 0.621973    
#> SexM:AgeF2              3.1098     1.1655   2.668 0.007624 ** 
#> SexM:AgeF3              1.1145     0.6365   1.751 0.079926 .  
#> EthN:LrnSL              2.2588     1.3019   1.735 0.082743 .  
#> SexM:LrnSL              1.5900     1.1499   1.383 0.166750    
#> AgeF1:LrnSL             2.6421     1.0821   2.442 0.014618 *  
#> AgeF2:LrnSL             4.8585     1.4423   3.369 0.000755 ***
#> AgeF3:LrnSL                 NA         NA      NA       NA    
#> EthN:SexM:AgeF1        -0.3105     1.2055  -0.258 0.796735    
#> EthN:SexM:AgeF2         0.3469     1.7965   0.193 0.846875    
#> EthN:SexM:AgeF3         0.8329     0.8970   0.929 0.353092    
#> EthN:SexM:LrnSL        -0.1639     1.5250  -0.107 0.914411    
#> EthN:AgeF1:LrnSL       -3.5493     1.4270  -2.487 0.012876 *  
#> EthN:AgeF2:LrnSL       -3.3315     2.0919  -1.593 0.111256    
#> EthN:AgeF3:LrnSL            NA         NA      NA       NA    
#> SexM:AgeF1:LrnSL       -2.4285     1.4201  -1.710 0.087246 .  
#> SexM:AgeF2:LrnSL       -4.1914     1.6201  -2.587 0.009679 ** 
#> SexM:AgeF3:LrnSL            NA         NA      NA       NA    
#> EthN:SexM:AgeF1:LrnSL   2.1711     1.9192   1.131 0.257963    
#> EthN:SexM:AgeF2:LrnSL   2.1029     2.3444   0.897 0.369718    
#> EthN:SexM:AgeF3:LrnSL       NA         NA      NA       NA    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> (Dispersion parameter for Negative Binomial(1.9284) family taken to be 1)
#> 
#>     Null deviance: 272.29  on 145  degrees of freedom
#> Residual deviance: 167.45  on 118  degrees of freedom
#> AIC: 1097.3
#> 
#> Number of Fisher Scoring iterations: 1
#> 
#> 
#>               Theta:  1.928 
#>           Std. Err.:  0.269 
#> 
#>  2 x log-likelihood:  -1039.324

Created on 2025-12-05 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.1 (2024-06-14)
#>  os       macOS 26.1
#>  system   x86_64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       America/Guatemala
#>  date     2025-12-05
#>  pandoc   3.6.3 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#>  quarto   1.7.32 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.5   2025-04-23 [1] CRAN (R 4.4.1)
#>  digest        0.6.37  2024-08-19 [2] CRAN (R 4.4.1)
#>  evaluate      1.0.5   2025-08-27 [1] CRAN (R 4.4.1)
#>  fastmap       1.2.0   2024-05-15 [2] CRAN (R 4.4.0)
#>  fs            1.6.6   2025-04-12 [1] CRAN (R 4.4.1)
#>  glue          1.8.0   2024-09-30 [2] CRAN (R 4.4.1)
#>  htmltools     0.5.8.1 2024-04-04 [2] CRAN (R 4.4.0)
#>  knitr         1.50    2025-03-16 [1] CRAN (R 4.4.1)
#>  lifecycle     1.0.4   2023-11-07 [2] CRAN (R 4.4.0)
#>  MASS        * 7.3-61  2024-06-13 [2] CRAN (R 4.4.0)
#>  reprex        2.1.1   2024-07-06 [2] CRAN (R 4.4.0)
#>  rlang         1.1.6   2025-04-11 [1] CRAN (R 4.4.1)
#>  rmarkdown     2.29    2024-11-04 [1] CRAN (R 4.4.1)
#>  rstudioapi    0.17.1  2024-10-22 [2] CRAN (R 4.4.1)
#>  sessioninfo   1.2.3   2025-02-05 [1] CRAN (R 4.4.1)
#>  withr         3.0.2   2024-10-28 [1] CRAN (R 4.4.1)
#>  xfun          0.53    2025-08-19 [1] CRAN (R 4.4.1)
#>  yaml          2.3.10  2024-07-26 [2] CRAN (R 4.4.0)
#> 
#>  [1] /Users/luisfernandoquezada/Library/R/x86_64/4.4/library
#>  [2] /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library
#>  * ── Packages attached to the search path.
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Let me know how it goes or if this is fixed!

Best,

Luis