Skip to content

Commit

Permalink
Merge pull request #282 from larmarange/tt3
Browse files Browse the repository at this point in the history
Use testthat version 3
  • Loading branch information
larmarange authored Jan 8, 2025
2 parents a928fd3 + 5d737fa commit 8f2b41a
Show file tree
Hide file tree
Showing 23 changed files with 905 additions and 975 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Suggests:
spelling,
survey,
survival,
testthat,
testthat (>= 3.0.0),
tidycmprsk,
VGAM
VignetteBuilder:
Expand All @@ -89,3 +89,4 @@ Language: en-US
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Config/testthat/edition: 3
8 changes: 8 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(broom.helpers)

Expand Down
101 changes: 50 additions & 51 deletions tests/testthat/test-add_coefficients_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,91 @@ test_that("tidy_add_coefficients_type() works for common models", {
res <- mod |>
tidy_and_attach() |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "generic")
expect_equivalent(attr(res, "coefficients_label"), "Beta")
expect_equal(attr(res, "coefficients_type"), "generic")
expect_equal(attr(res, "coefficients_label"), "Beta")

mod <- glm(Sepal.Length ~ Sepal.Width, iris, family = gaussian)
res <- mod |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "generic")
expect_equivalent(attr(res, "coefficients_label"), "exp(Beta)")
expect_equal(attr(res, "coefficients_type"), "generic")
expect_equal(attr(res, "coefficients_label"), "exp(Beta)")

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = binomial)
res <- mod |>
tidy_and_attach() |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "logistic")
expect_equivalent(attr(res, "coefficients_label"), "log(OR)")
expect_equal(attr(res, "coefficients_type"), "logistic")
expect_equal(attr(res, "coefficients_label"), "log(OR)")
res <- mod |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "logistic")
expect_equivalent(attr(res, "coefficients_label"), "OR")
expect_equal(attr(res, "coefficients_type"), "logistic")
expect_equal(attr(res, "coefficients_label"), "OR")

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = binomial(probit))
res <- mod |>
tidy_and_attach() |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "generic")
expect_equivalent(attr(res, "coefficients_label"), "Beta")
expect_equal(attr(res, "coefficients_type"), "generic")
expect_equal(attr(res, "coefficients_label"), "Beta")
res <- mod |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "generic")
expect_equivalent(attr(res, "coefficients_label"), "exp(Beta)")
expect_equal(attr(res, "coefficients_type"), "generic")
expect_equal(attr(res, "coefficients_label"), "exp(Beta)")

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = binomial(log))
res <- mod |>
tidy_and_attach() |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "relative_risk")
expect_equivalent(attr(res, "coefficients_label"), "log(RR)")
expect_equal(attr(res, "coefficients_type"), "relative_risk")
expect_equal(attr(res, "coefficients_label"), "log(RR)")
res <- mod |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "relative_risk")
expect_equivalent(attr(res, "coefficients_label"), "RR")
expect_equal(attr(res, "coefficients_type"), "relative_risk")
expect_equal(attr(res, "coefficients_label"), "RR")

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = binomial(cloglog))
res <- mod |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "prop_hazard")
expect_equivalent(attr(res, "coefficients_label"), "HR")
expect_equal(attr(res, "coefficients_type"), "prop_hazard")
expect_equal(attr(res, "coefficients_label"), "HR")

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = poisson)
res <- mod |>
tidy_and_attach() |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "poisson")
expect_equivalent(attr(res, "coefficients_label"), "log(IRR)")
expect_equal(attr(res, "coefficients_type"), "poisson")
expect_equal(attr(res, "coefficients_label"), "log(IRR)")
res <- mod |>
tidy_and_attach(exponentiate = TRUE) |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "poisson")
expect_equivalent(attr(res, "coefficients_label"), "IRR")
expect_equal(attr(res, "coefficients_type"), "poisson")
expect_equal(attr(res, "coefficients_label"), "IRR")

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = poisson("identity"))
res <- mod |>
tidy_and_attach(conf.int = FALSE) |>
tidy_add_coefficients_type()
expect_equivalent(attr(res, "coefficients_type"), "generic")
expect_equivalent(attr(res, "coefficients_label"), "Beta")
expect_equal(attr(res, "coefficients_type"), "generic")
expect_equal(attr(res, "coefficients_label"), "Beta")

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = quasipoisson)
res <- mod |>
tidy_and_attach() |>
tidy_add_coefficients_type(exponentiate = TRUE)
expect_equivalent(attr(res, "coefficients_type"), "poisson")
expect_equivalent(attr(res, "coefficients_label"), "IRR")
expect_equal(attr(res, "coefficients_type"), "poisson")
expect_equal(attr(res, "coefficients_label"), "IRR")

mod <- glm(response ~ age + grade * trt, gtsummary::trial, family = quasibinomial)
res <- mod |>
tidy_and_attach() |>
tidy_add_coefficients_type(exponentiate = TRUE)
expect_equivalent(attr(res, "coefficients_type"), "logistic")
expect_equivalent(attr(res, "coefficients_label"), "OR")
expect_equal(attr(res, "coefficients_type"), "logistic")
expect_equal(attr(res, "coefficients_label"), "OR")
})

test_that("test tidy_add_coefficients_type() checks", {
Expand All @@ -103,9 +103,8 @@ test_that("test tidy_add_coefficients_type() checks", {
expect_error(mod |> broom::tidy() |> tidy_attach_model(mod) |> tidy_add_coefficients_type())

# could be apply twice (no error)
expect_error(
mod |> tidy_and_attach() |> tidy_add_coefficients_type() |> tidy_add_coefficients_type(),
NA
expect_no_error(
mod |> tidy_and_attach() |> tidy_add_coefficients_type() |> tidy_add_coefficients_type()
)
})

Expand All @@ -114,7 +113,7 @@ test_that("model_get_coefficients_type() works with lme4::lmer", {
skip_if_not_installed("lme4")
mod <- lme4::lmer(Reaction ~ Days + (Days | Subject), lme4::sleepstudy)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "generic")
expect_equal(res, "generic")
})


Expand All @@ -125,31 +124,31 @@ test_that("model_identify_variables() works with lme4::glmer", {
family = binomial, data = lme4::cbpp
)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")

mod <- lme4::glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
family = binomial("probit"), data = lme4::cbpp
)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "generic")
expect_equal(res, "generic")

mod <- lme4::glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
family = binomial("log"), data = lme4::cbpp
)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "relative_risk")
expect_equal(res, "relative_risk")

mod <- lme4::glmer(response ~ trt + (1 | grade), gtsummary::trial, family = poisson)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "poisson")
expect_equal(res, "poisson")
})


test_that("model_get_coefficients_type() works with survival::coxph", {
df <- survival::lung |> dplyr::mutate(sex = factor(sex))
mod <- survival::coxph(survival::Surv(time, status) ~ ph.ecog + age + sex, data = df)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "prop_hazard")
expect_equal(res, "prop_hazard")
})

test_that("model_get_coefficients_type() works with survival::survreg", {
Expand All @@ -159,7 +158,7 @@ test_that("model_get_coefficients_type() works with survival::survreg", {
dist = "exponential"
)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "generic")
expect_equal(res, "generic")
})


Expand All @@ -175,23 +174,23 @@ test_that("model_get_coefficients_type() works with survival::clogit", {
mod <- survival::clogit(case ~ tocc + tocc:education + strata(id), logan2)

res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")
})


test_that("model_get_coefficients_type() works with nnet::multinom", {
skip_if_not_installed("nnet")
mod <- nnet::multinom(grade ~ stage + marker + age, data = gtsummary::trial, trace = FALSE)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")
})

test_that("model_get_coefficients_type() works with survey::svyglm", {
skip_if_not_installed("survey")
df <- survey::svydesign(~1, weights = ~1, data = gtsummary::trial)
mod <- survey::svyglm(response ~ age + grade * trt, df, family = quasibinomial)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")
})


Expand All @@ -203,7 +202,7 @@ test_that("model_get_coefficients_type() works with survey::svycoxph", {
design = dpbc
)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "prop_hazard")
expect_equal(res, "prop_hazard")
})

test_that("tidy_plus_plus() works with survey::svyolr", {
Expand All @@ -213,27 +212,27 @@ test_that("tidy_plus_plus() works with survey::svyolr", {
fpc <- update(fpc, mealcat = cut(meals, c(0, 25, 50, 75, 100)))
mod <- survey::svyolr(mealcat ~ avg.ed + mobility + stype, design = fpc)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")
})

test_that("model_get_coefficients_type() works with ordinal::clm", {
mod <- ordinal::clm(rating ~ temp * contact, data = ordinal::wine)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")
})


test_that("model_get_coefficients_type() works with ordinal::clmm", {
mod <- ordinal::clmm(rating ~ temp * contact + (1 | judge), data = ordinal::wine)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")
})


test_that("model_get_coefficients_type() works with MASS::polr", {
mod <- MASS::polr(Sat ~ Infl + Type + Cont, weights = Freq, data = MASS::housing)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")

mod <- MASS::polr(
Sat ~ Infl + Type + Cont,
Expand All @@ -242,7 +241,7 @@ test_that("model_get_coefficients_type() works with MASS::polr", {
method = "probit"
)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "generic")
expect_equal(res, "generic")
})


Expand All @@ -257,7 +256,7 @@ test_that("model_get_coefficients_type() works with geepack::geeglm", {
)

res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "poisson")
expect_equal(res, "poisson")
})


Expand All @@ -266,14 +265,14 @@ test_that("model_get_coefficients_type() works with gam::gam", {
data(kyphosis, package = "gam")
mod <- gam::gam(Kyphosis ~ gam::s(Age, 4) + Number, family = binomial, data = kyphosis)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "logistic")
expect_equal(res, "logistic")

mod <- suppressWarnings(gam::gam(
Ozone^(1 / 3) ~ gam::lo(Solar.R) + gam::lo(Wind, Temp),
data = datasets::airquality, na = gam::na.gam.replace
))
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "generic")
expect_equal(res, "generic")
})


Expand All @@ -290,5 +289,5 @@ test_that("model_get_coefficients_type() works with lavaan::lavaan", {
auto.cov.lv.x = TRUE
)
res <- mod |> model_get_coefficients_type()
expect_equivalent(res, "generic")
expect_equal(res, "generic")
})
Loading

0 comments on commit 8f2b41a

Please sign in to comment.