Skip to content

Commit

Permalink
Now tidy_add_n() works with multinomial models when y is not code…
Browse files Browse the repository at this point in the history
…d as a factor

fix #109
  • Loading branch information
larmarange committed Feb 24, 2021
1 parent 2070a45 commit 74554e7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Better identification of term labels for interaction terms using sum
contrasts (#108)
- Now `tidy_add_n()` works with multinomial models when `y` is not coded as
a factor (#109)

# broom.helpers 1.2.0

Expand Down
1 change: 1 addition & 0 deletions R/model_get_n.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ model_get_n.multinom <- function(model) {

w <- model %>% model_get_weights()
y <- model %>% model_get_response()
if (!is.factor(y)) y <- factor(y)

n <- purrr::map_df(
levels(y)[-1],
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-add_n.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ test_that("tidy_add_n() works with nnet::multinom", {
res$n_event,
c(57, 21, 16, 8, 57, 57, 58, 12, 18, 12, 58, 58)
)

# when y is not coded as a factor
mod <- multinom(race ~ age + lwt + bwt, data = MASS::birthwt)
expect_error(
mod %>% tidy_and_attach() %>% tidy_add_n(),
NA
)

})

test_that("tidy_add_n() works with survey::svyglm", {
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-model_get_n.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ test_that("model_get_n() works with nnet::multinom", {
res$n_event,
c(57, 16, 8, 12, 57, 57, 21, 58, 18, 12, 16, 58, 58, 12)
)

# when y is not coded as a factor
mod <- multinom(race ~ age + lwt + bwt, data = MASS::birthwt)
expect_true(mod %>% model_get_n() %>% nrow() > 0)
})

test_that("model_get_n() works with survey::svyglm", {
Expand Down

0 comments on commit 74554e7

Please sign in to comment.