larmarange / broom.helpers

A set of functions to facilitate manipulation of tibbles produced by broom
https://larmarange.github.io/broom.helpers/
GNU General Public License v3.0
21 stars 8 forks source link

tidy marginal predictions of multinomial and ordinal logistic models #215

Closed tguerra closed 1 year ago

tguerra commented 1 year ago

Hi !

I've tried to use broom.helpers and ggstats to generate marginal predictions from a multinomial model (using nnet package) and graph it (similarly to what is done with logistic regression on this webpage).

Any idea what's going on? I don't understand if the problem is located in broom.helpers, ggstats or elsewhere.

library(nnet)

mod <- multinom(Species ~ ., data = iris)
#> # weights:  18 (10 variable)
#> initial  value 164.791843 
#> iter  10 value 16.177348
#> iter  20 value 7.111438
#> iter  30 value 6.182999
#> iter  40 value 5.984028
#> iter  50 value 5.961278
#> iter  60 value 5.954900
#> iter  70 value 5.951851
#> iter  80 value 5.950343
#> iter  90 value 5.949904
#> iter 100 value 5.949867
#> final  value 5.949867 
#> stopped after 100 iterations

ggstats::ggcoef_multinom(
  model = mod,
  tidy_fun = broom.helpers::tidy_marginal_predictions,
  show_p_values = FALSE,
  signif_stars = FALSE,
  significance = NULL,
  vline = FALSE)
#> Error in `dplyr::left_join()`:
#> ! Join columns in `x` must be present in the data.
#> ✖ Problem with `y.level`.

mod |>
  broom.helpers::tidy_marginal_predictions()
#> 
#>  Term Estimate Std. Error   z   Pr(>|z|) CI low CI high
#>   4.3   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   5.1   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   5.8   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   6.4   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   7.9   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>     2   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   2.8   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>     3   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   3.3   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   4.4   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>     1   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   1.6   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>  4.35   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   5.1   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   6.9   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   0.1   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   0.3   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   1.3   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   1.8   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#>   2.5   0.3333          0 Inf < 2.22e-16 0.3333  0.3333
#> 
#> Prediction type:  
#> Columns: variable, term, type, estimate, std.error, statistic, p.value, conf.low, conf.high
larmarange commented 1 year ago

Hi

some adaptation of the code will be required for compatibility with multinomial model.

I will try to work on it next week.

larmarange commented 1 year ago

Hi @tguerra

you could try this dev version #219 that could be installed with remotes::install_github("larmarange/broom.helpers#219")

tguerra commented 1 year ago

It works very well with multinomials, thank you!