jacob-long / jtools

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

Cannot exponentiate coefficients of MASS:polr object with export_summs #109

Closed samuelsaari closed 2 months ago

samuelsaari commented 3 years ago

Brief description of the problem

Cannot exponentiate the coefficients of MASS::polr-object (whether after multiple impuation or not) with export_summs function

I've asked this on Stackoverflow but later thought that this could potentially be a bug. Here I have extended the code to make debugging easier.

library(mice)
#> 
#> Attaching package: 'mice'
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following objects are masked from 'package:base':
#> 
#>     cbind, rbind
library(MASS)
library(jtools)
library(reprex)
set.seed(3)

# creating a dataset and imputing it
y=sample(c(1:4,NA),size=50, replace=T)
y=as.ordered(x)
#> Error in is.ordered(x): object 'x' not found
x=sample(c(1:4,NA),size=50, replace=T)
z=sample(c(1,2),size=50, replace=T)
q <- runif(50)
d <- data.frame(cbind(x,y,z,q))
mi_d <- mice(d,m=2,maxit=2)
#> 
#>  iter imp variable
#>   1   1  x  y
#>   1   2  x  y
#>   2   1  x  y
#>   2   2  x  y

# ordinal regression
polr_model <- polr(as.factor(y) ~ x , Hess=TRUE)
mi_polr <- with(mi_d,polr(as.factor(y) ~ x , Hess=TRUE))
POOLED_P <- pool(mi_polr)

# exponientiating coefficients works fine when plotting
plot_summs(POOLED_P,exp=T)
#> Loading required namespace: broom.mixed

#.. but not with exporting the table
jtools::export_summs(POOLED_P,exp=T) 
#> Warning in nobs.default(m, use.fallback = TRUE): no 'nobs' method is available
#> Warning in knit_print.huxtable(x, ...): Unrecognized output format "gfm". Using `to_screen` to print huxtables.
#> Set options("huxtable.knitr_output_format") manually to "latex", "html", "rtf", "docx", "pptx", "md" or "screen".
           ─────────────────────────────────────────────────
                                                   Model 1  
                                   ─────────────────────────
             x                                      0.49    
                                                   (0.40)   
             1|2                                    0.12    
                                                   (0.93)   
             2|3                                    1.48    
                                                   (0.94)   
             3|4                                    2.28 *  
                                                   (0.89)   
                                   ─────────────────────────
             nobs                                   0       
             nimp                                   2.00    
             nobs.1                                50.00    
           ─────────────────────────────────────────────────
             *** p < 0.001; ** p < 0.01; * p < 0.05.        

Column names: names, Model 1

jtools::export_summs(polr_model, exp=T)
#> Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
#> Using compatibility `.name_repair`.
#> Warning in FUN(X[[i]], ...): tidy() does not return p values for models of class
#> data.frame; significance stars not printed.
#> Warning in knit_print.huxtable(x, ...): Unrecognized output format "gfm". Using `to_screen` to print huxtables.
#> Set options("huxtable.knitr_output_format") manually to "latex", "html", "rtf", "docx", "pptx", "md" or "screen".
           ─────────────────────────────────────────────────
                                                   Model 1  
                                   ─────────────────────────
             x                                       0.39   
                                                    (0.34)  
             1|2                                    -0.27   
                                                    (0.86)  
             2|3                                     0.97   
                                                    (0.86)  
             3|4                                     1.93   
                                                    (0.94)  
                                   ─────────────────────────
             nobs                                   29.00   
             edf                                     4.00   
             logLik                                -39.32   
             AIC                                    86.65   
             BIC                                    92.12   
             deviance                               78.65   
             df.residual                            25.00   
             nobs.1                                 29.00   
           ─────────────────────────────────────────────────
             *** p < 0.001; ** p < 0.01; * p < 0.05.        

Column names: names, Model 1

# let's try with regular glm
glm_model <- glm(q~z,data=d,family=binomial())
#> Warning in eval(family$initialize): non-integer #successes in a binomial glm!
jtools::export_summs(glm_model,exp=F)
#> Warning in eval(family$initialize): non-integer #successes in a binomial glm!
#> Warning in eval(family$initialize): non-integer #successes in a binomial glm!
#> Warning in knit_print.huxtable(x, ...): Unrecognized output format "gfm". Using `to_screen` to print huxtables.
#> Set options("huxtable.knitr_output_format") manually to "latex", "html", "rtf", "docx", "pptx", "md" or "screen".
           ─────────────────────────────────────────────────
                                                   Model 1  
                                   ─────────────────────────
             (Intercept)                             0.07   
                                                    (0.93)  
             z                                      -0.04   
                                                    (0.57)  
                                   ─────────────────────────
             N                                      50      
             AIC                                    73.35   
             BIC                                    77.17   
             Pseudo R2                              -0.00   
           ─────────────────────────────────────────────────
             *** p < 0.001; ** p < 0.01; * p < 0.05.        

Column names: names, Model 1

jtools::export_summs(glm_model, exp=T) # recognizes model1 as a glm object and exponentiates the coefficients
#> Warning in eval(family$initialize): non-integer #successes in a binomial glm!

#> Warning in eval(family$initialize): Unrecognized output format "gfm". Using `to_screen` to print huxtables.
#> Set options("huxtable.knitr_output_format") manually to "latex", "html", "rtf", "docx", "pptx", "md" or "screen".
           ─────────────────────────────────────────────────
                                                   Model 1  
                                   ─────────────────────────
             (Intercept)                             1.08   
                                                    (0.93)  
             z                                       0.96   
                                                    (0.57)  
                                   ─────────────────────────
             N                                      50      
             AIC                                    73.35   
             BIC                                    77.17   
             Pseudo R2                              -0.00   
           ─────────────────────────────────────────────────
             *** p < 0.001; ** p < 0.01; * p < 0.05.        

Column names: names, Model 1

Created on 2021-05-07 by the reprex package (v2.0.0)

jacob-long commented 2 years ago

I'll take a look at whether I can do an interim solution here although there's a chance I can't deal with this without making polr models a "first class" supported model in the package, which is something I plan to do (#16)

jacob-long commented 2 months ago

In the absence of a viable stopgap, I'm closing this issue until I can better support polr models across the board (#16)