| 1 |
skip_if_not_installed("modeltests") |
| 2 |
library(modeltests) |
| 3 |
|
| 4 |
test_that("glm tidier arguments", { |
| 5 |
check_arguments(tidy.glm) # just points to tidy.lm |
| 6 |
check_arguments(glance.glm) |
| 7 |
check_arguments(augment.glm) |
| 8 |
}) |
| 9 |
|
| 10 |
data("mtcars") |
| 11 |
nrow_mtcars <- nrow(mtcars) |
| 12 |
glm_weights <- rep(c(0, 1), each = nrow_mtcars / 2) |
| 13 |
gfit <- glm(am ~ wt, mtcars, family = "binomial") |
| 14 |
gfit2 <- glm(cyl ~ wt + log(disp), mtcars, family = "poisson") |
| 15 |
gfit3 <- glm(am ~ wt, mtcars, family = "binomial", weights = glm_weights) |
| 16 |
|
| 17 |
# the gear term isn't defined for this fit |
| 18 |
na_row_data <- mtcars[c(1, 2, 13:15, 22), ] |
| 19 |
gfit_na_row <- glm(am ~ cyl * qsec + gear, data = na_row_data) |
| 20 |
|
| 21 |
test_that("tidy.glm works", { |
| 22 |
td <- tidy(gfit) |
| 23 |
td2 <- tidy(gfit2) |
| 24 |
tde <- tidy(gfit, exponentiate = TRUE) |
| 25 |
tde2 <- tidy(gfit2, exponentiate = TRUE) |
| 26 |
td_na_row <- tidy(gfit_na_row) |
| 27 |
|
| 28 |
check_tidy_output(td) |
| 29 |
check_tidy_output(td2) |
| 30 |
check_tidy_output(tde) |
| 31 |
check_tidy_output(tde2) |
| 32 |
check_tidy_output(td_na_row) |
| 33 |
|
| 34 |
check_dims(td, 2, 5) |
| 35 |
check_dims(td2, 3, 5) |
| 36 |
check_dims(td_na_row, expected_rows = 5) |
| 37 |
|
| 38 |
expect_equal(td$term, c("(Intercept)", "wt")) |
| 39 |
expect_equal(td2$term, c("(Intercept)", "wt", "log(disp)")) |
| 40 |
}) |
| 41 |
|
| 42 |
test_that("glance.glm works", { |
| 43 |
gl <- glance(gfit) |
| 44 |
gl2 <- glance(gfit2) |
| 45 |
|
| 46 |
check_glance_outputs(gl) |
| 47 |
check_glance_outputs(gl2) |
| 48 |
}) |
| 49 |
|
| 50 |
|
| 51 |
test_that("augment.glm", { |
| 52 |
check_augment_function( |
| 53 |
aug = augment.glm, |
| 54 |
model = gfit, |
| 55 |
data = mtcars, |
| 56 |
newdata = mtcars |
| 57 |
) |
| 58 |
|
| 59 |
check_augment_function( |
| 60 |
aug = augment.glm, |
| 61 |
model = gfit2, |
| 62 |
data = mtcars, |
| 63 |
newdata = mtcars |
| 64 |
) |
| 65 |
|
| 66 |
# check that `type.residuals` argument is passed correctly (#1147) |
| 67 |
expect_equal( |
| 68 |
augment(gfit2, type.residuals = "pearson") |> dplyr::pull(.std.resid), |
| 69 |
rstandard(gfit2, type = "pearson") |> unname() |
| 70 |
) |
| 71 |
|
| 72 |
expect_equal( |
| 73 |
augment(gfit2, type.residuals = "deviance") |> dplyr::pull(.std.resid), |
| 74 |
rstandard(gfit2, type = "deviance") |> unname() |
| 75 |
) |
| 76 |
}) |
| 77 |
|
| 78 |
test_that("glm tidiers warn informatively with glm.fit2 input", { |
| 79 |
gfit <- glm(am ~ wt, mtcars, family = "binomial") |
| 80 |
gfit$method <- "glm.fit2" |
| 81 |
|
| 82 |
expect_snapshot(.res <- tidy(gfit)) |
| 83 |
}) |
| 84 |
|
| 85 |
test_that("glm tidiers warn informatively with stanreg input", { |
| 86 |
gfit <- glm(am ~ wt, mtcars, family = "binomial") |
| 87 |
gfit$stan_function <- "howdy" |
| 88 |
|
| 89 |
expect_snapshot(error = TRUE, .res <- tidy(gfit)) |
| 90 |
}) |