Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in nnet #109

Closed
ddsjoberg opened this issue Feb 24, 2021 · 4 comments
Closed

Error in nnet #109

ddsjoberg opened this issue Feb 24, 2021 · 4 comments

Comments

@ddsjoberg
Copy link
Collaborator

ddsjoberg commented Feb 24, 2021

Hey hey,

This issue was reported today for a multinomial model. I didn't get a chance to look into the details yet, but wanted to record it here.

library(nnet)
library(MASS)
library(magrittr)

test1 <- multinom(race ~ age + lwt + bwt, data = birthwt)
#> # weights:  15 (8 variable)
#> initial  value 207.637723 
#> iter  10 value 168.608572
#> final  value 168.587348 
#> converged

broom::tidy(test1) %>% knitr::kable()
y.level term estimate std.error statistic p.value
2 (Intercept) 2.0502611 0.0009135 2244.476995 0.0000000
2 age -0.1464569 0.0394259 -3.714736 0.0002034
2 lwt 0.0205184 0.0068180 3.009432 0.0026174
2 bwt -0.0009939 0.0002851 -3.485562 0.0004911
3 (Intercept) 3.9546842 0.0004048 9769.456437 0.0000000
3 age -0.0553396 0.0285068 -1.941279 0.0522245
3 lwt -0.0129022 0.0059785 -2.158092 0.0309207
3 bwt -0.0004768 0.0002111 -2.258674 0.0239037
broom.helpers::tidy_plus_plus(test1)
#> Error: Join columns must be present in data.
#> x Problem with `term`.

Created on 2021-02-24 by the reprex package (v1.0.0)

@larmarange
Copy link
Owner

Thanks for the feedback. It seems related to add_n feature.

library(nnet)
library(MASS)
library(broom.helpers)

test1 <- multinom(race ~ age + lwt + bwt, data = birthwt)
#> # weights:  15 (8 variable)
#> initial  value 207.637723 
#> iter  10 value 168.608572
#> final  value 168.587348 
#> converged
test1 %>% tidy_plus_plus()
#> Error: Join columns must be present in data.
#> x Problem with `term`.
test1 %>% tidy_plus_plus(add_n = FALSE)
#> # A tibble: 6 x 17
#>   y.level term  variable var_label var_class var_type var_nlevels contrasts
#>   <chr>   <chr> <chr>    <chr>     <chr>     <chr>          <int> <chr>    
#> 1 2       age   age      age       integer   continu~          NA <NA>     
#> 2 2       lwt   lwt      lwt       integer   continu~          NA <NA>     
#> 3 2       bwt   bwt      bwt       integer   continu~          NA <NA>     
#> 4 3       age   age      age       integer   continu~          NA <NA>     
#> 5 3       lwt   lwt      lwt       integer   continu~          NA <NA>     
#> 6 3       bwt   bwt      bwt       integer   continu~          NA <NA>     
#> # ... with 9 more variables: contrasts_type <chr>, reference_row <lgl>,
#> #   label <chr>, estimate <dbl>, std.error <dbl>, statistic <dbl>,
#> #   p.value <dbl>, conf.low <dbl>, conf.high <dbl>
test1 %>% tidy_and_attach() %>% tidy_add_n()
#> Error: Join columns must be present in data.
#> x Problem with `term`.

Created on 2021-02-24 by the reprex package (v1.0.0)

@larmarange
Copy link
Owner

OK the problem comes from the fact that race is not coded as a factor. Easy solution: transform race before computing the model

library(nnet)
library(MASS)
library(broom.helpers)

tmp <- birthwt
tmp$race <- factor(tmp$race)
test2 <- multinom(race ~ age + lwt + bwt, data = tmp)
#> # weights:  15 (8 variable)
#> initial  value 207.637723 
#> iter  10 value 168.608572
#> final  value 168.587348 
#> converged
test2 %>% tidy_plus_plus()
#> # A tibble: 6 x 19
#>   y.level term  variable var_label var_class var_type var_nlevels contrasts
#>   <chr>   <chr> <chr>    <chr>     <chr>     <chr>          <int> <chr>    
#> 1 2       age   age      age       integer   continu~          NA <NA>     
#> 2 2       lwt   lwt      lwt       integer   continu~          NA <NA>     
#> 3 2       bwt   bwt      bwt       integer   continu~          NA <NA>     
#> 4 3       age   age      age       integer   continu~          NA <NA>     
#> 5 3       lwt   lwt      lwt       integer   continu~          NA <NA>     
#> 6 3       bwt   bwt      bwt       integer   continu~          NA <NA>     
#> # ... with 11 more variables: contrasts_type <chr>, reference_row <lgl>,
#> #   label <chr>, n_obs <dbl>, n_event <dbl>, estimate <dbl>, std.error <dbl>,
#> #   statistic <dbl>, p.value <dbl>, conf.low <dbl>, conf.high <dbl>

Created on 2021-02-24 by the reprex package (v1.0.0)

@larmarange
Copy link
Owner

Should be fixed now

library(nnet)
library(MASS)
library(broom.helpers)

test1 <- multinom(race ~ age + lwt + bwt, data = birthwt)
#> # weights:  15 (8 variable)
#> initial  value 207.637723 
#> iter  10 value 168.608572
#> final  value 168.587348 
#> converged
test1 %>% tidy_plus_plus()
#> # A tibble: 6 x 19
#>   y.level term  variable var_label var_class var_type var_nlevels contrasts
#>   <chr>   <chr> <chr>    <chr>     <chr>     <chr>          <int> <chr>    
#> 1 2       age   age      age       integer   continu~          NA <NA>     
#> 2 2       lwt   lwt      lwt       integer   continu~          NA <NA>     
#> 3 2       bwt   bwt      bwt       integer   continu~          NA <NA>     
#> 4 3       age   age      age       integer   continu~          NA <NA>     
#> 5 3       lwt   lwt      lwt       integer   continu~          NA <NA>     
#> 6 3       bwt   bwt      bwt       integer   continu~          NA <NA>     
#> # ... with 11 more variables: contrasts_type <chr>, reference_row <lgl>,
#> #   label <chr>, n_obs <dbl>, n_event <dbl>, estimate <dbl>, std.error <dbl>,
#> #   statistic <dbl>, p.value <dbl>, conf.low <dbl>, conf.high <dbl>

Created on 2021-02-24 by the reprex package (v1.0.0)

@ddsjoberg
Copy link
Collaborator Author

Wowow, so fast! Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants