jacob-long / jtools

Tools for summarizing/visualizing regressions and other helpful stuff
https://jtools.jacob-long.com
GNU General Public License v3.0
165 stars 22 forks source link

problem with jtools::summ and MASS::glm.nb() #142

Closed martinzuba closed 11 months ago

martinzuba commented 1 year ago

I have encountered a problem when using jtools::summ() with MASS::glm.nb(). There seems to be some parameter mismatch in the glm.control() function.

The reprex is from Francis L Huang

articles <- rio::import("https://raw.githubusercontent.com/flh3/pubdata/main/miscdata/articles.csv")
negb <- MASS::glm.nb(art ~ fem + ment + phd + mar + kid5, data = articles) #negative binomial
jtools::summ(negb, exp = T)

In the code outlined in the example, jtools::summ runs perfectly fine with the given data. When I run the command, I encounter a problem with glm.control(), and I do not get any values for R squared. All other values in the table are correct.

I therefore suppose that it is a conflict with any of the packages that I have loaded, or an issue caused by an update of jtools or MASS since the tutorial was written.

Output:

Error in glm.control(...) : 
  unused argument (family = list("Negative Binomial(2.2644)", "log", function (mu) 
log(mu), function (eta) 
pmax(exp(eta), .Machine$double.eps), function (mu) 
mu + mu^2/.Theta, function (y, mu, wt) 
2 * wt * (y * log(pmax(1, y)/mu) - (y + .Theta) * log((y + .Theta)/(mu + .Theta))), function (y, n, mu, wt, dev) 
{
    term <- (y + .Theta) * log(mu + .Theta) - y * log(mu) + lgamma(y + 1) - .Theta * log(.Theta) + lgamma(.Theta) - lgamma(.Theta + y)
    2 * sum(term * wt)
}, function (eta) 
pmax(exp(eta), .Machine$double.eps), expression({
    if (any(y < 0)) stop("negative values not allowed for the negative binomial family")
    n <- rep(1, nobs)
    mustart <- y + (y == 0)/6
}), function (mu) 
all(mu > 0), function (eta) 
TRUE, function (object, nsim) 
{
    ftd <- fitted(object)
    rnegbin(nsim * length(ftd), ftd, .Theta)
}))
MODEL INFO:
Observations: 915
Dependent Variable: art
Type: Generalized linear model
  Family: Negative Binomial(2.2644) 
  Link function: log 

MODEL FIT:
χ²(NA) = NA, p = NA
Pseudo-R² (Cragg-Uhler) = NA
Pseudo-R² (McFadden) = NA
AIC = 3135.92, BIC = 3169.65 

Standard errors: MLE
------------------------------------------------------------
                    exp(Est.)   2.5%   97.5%   z val.      p
----------------- ----------- ------ ------- -------- ------
(Intercept)              1.29   0.99    1.69     1.86   0.06
fem                      0.81   0.70    0.93    -2.98   0.00
ment                     1.03   1.02    1.04     9.05   0.00
phd                      1.02   0.95    1.09     0.43   0.67
mar                      1.16   0.99    1.37     1.83   0.07
kid5                     0.84   0.76    0.93    -3.34   0.00
------------------------------------------------------------
Warning message:
Something went wrong when calculating the pseudo R-squared. Returning NA instead. 
>sessionInfo()

R version 4.3.1 (2023-06-16 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2019 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=German_Austria.1252  LC_CTYPE=German_Austria.1252    LC_MONETARY=German_Austria.1252 LC_NUMERIC=C                    LC_TIME=German_Austria.1252    

time zone: Europe/Vienna
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] jtools_2.2.2       rio_0.5.29         reshape2_1.4.4     summarytools_1.0.1 pscl_1.5.5.1       ggplot2_3.4.3      MASS_7.3-60        fstcore_0.9.14     readxl_1.4.3       eurostat_3.8.2     beepr_1.3          odbc_1.3.5         comorbidity_1.0.7 
[14] magrittr_2.0.3     tsibble_1.1.3      tidyr_1.3.0        purrr_1.0.2        fst_0.9.8          dplyr_1.1.2        ztools_0.6.0      

loaded via a namespace (and not attached):
 [1] DBI_1.1.3          tcltk_4.3.1        sandwich_3.0-2     rlang_1.1.1        tidytable_0.10.1   matrixStats_1.0.0  e1071_1.7-13       compiler_4.3.1     vctrs_0.6.3        stringr_1.5.0      pkgconfig_2.0.3    crayon_1.5.2       fastmap_1.1.1     
[14] backports_1.4.1    dbplyr_2.3.3       magick_2.7.5       ellipsis_0.3.2     ISOweek_0.6-2      pander_0.6.5       utf8_1.2.3         tzdb_0.4.0         haven_2.5.3        anytime_0.3.9      bit_4.0.5          jsonlite_1.8.7     RefManageR_1.4.0  
[27] covtools_0.6.0     blob_1.2.4         pryr_0.1.6         broom_1.0.5        parallel_4.3.1     R6_2.5.1           stringi_1.7.12     lubridate_1.9.2    cellranger_1.1.0   Rcpp_1.0.11        assertthat_0.2.1   zoo_1.8-12         audio_0.1-11      
[40] fortunes_1.5-4     base64enc_0.1-3    pacman_0.5.1       readr_2.1.4        timechange_0.2.0   tidyselect_1.2.0   codetools_0.2-19   curl_5.0.2         lattice_0.21-8     tibble_3.2.1       plyr_1.8.8         withr_2.5.0        foreign_0.8-84    
[53] proxy_0.4-27       zip_2.3.0          xml2_1.3.5         pillar_1.9.0       KernSmooth_2.23-21 checkmate_2.2.0    generics_0.1.3     rprojroot_2.0.3    hms_1.1.3          munsell_0.5.0      regions_0.1.8      scales_1.2.1       class_7.3-22      
[66] glue_1.6.2         tools_4.3.1        data.table_1.14.8  openxlsx_4.2.5.2   forcats_1.0.0      rapportools_1.1    grid_4.3.1         bibtex_0.5.1       colorspace_2.1-0   nlme_3.1-162       cli_3.6.1          fansi_1.0.4        rematch_1.0.1     
[79] countrycode_1.5.0  gtable_0.3.4       rmsfact_0.0.3      digest_0.6.33      cowsay_0.8.2       classInt_0.4-9     librarian_1.8.1    htmltools_0.5.6    lifecycle_1.0.3    httr_1.4.7         here_1.0.1         bit64_4.0.5 
jacob-long commented 11 months ago

Interesting, I hadn't explicitly intended to support glm.nb models but I'm glad to see they are almost working. With that in mind, I don't have a good idea about why it would have regressed like this.

However, this was a fairly easy fix so I have an update coming that will both get the pseudo-R2 back in there and add the missing chi-squared test as well.