Debian Codegraph @

tests/testthat/test-mgcv.R

raw
1 skip_on_cran()
2
3 skip_if_not_installed("modeltests")
4 library(modeltests)
5
6 skip_if_not_installed("mgcv")
7
8 fit <- mgcv::gam(weight ~ s(Time) + factor(Diet), data = ChickWeight)
9
10 test_that("mgcv tidier arguments", {
11 check_arguments(tidy.gam)
12 check_arguments(glance.gam)
13 check_arguments(augment.gam, strict = FALSE)
14 })
15
16 test_that("tidy.gam", {
17 td <- tidy(fit)
18 tdp <- tidy(fit, parametric = TRUE, conf.int = TRUE)
19 tdp_exp <- tidy(fit, parametric = TRUE, conf.int = TRUE, exponentiate = TRUE)
20
21 check_tidy_output(td, strict = FALSE)
22 check_tidy_output(tdp)
23
24 # test coef exponentiated
25 expect_equal(
26 as.matrix(tdp_exp[, c("estimate", "conf.low", "conf.high")]),
27 exp(as.matrix(tdp[, c("estimate", "conf.low", "conf.high")]))
28 )
29 })
30
31 test_that("glance.gam", {
32 gl <- glance(fit)
33 check_glance_outputs(gl)
34 })
35
36 test_that("augment.gam", {
37 suppressWarnings(
38 check_augment_function(
39 augment.gam,
40 fit,
41 data = ChickWeight,
42 newdata = ChickWeight
43 )
44 )
45 })
46
47 test_that("tidy.gam handles messages informatively", {
48 x <- fit <- mgcv::gam(weight ~ s(Time) + factor(Diet), data = ChickWeight)
49 expect_snapshot(.res <- tidy(x, conf.int = TRUE))
50 expect_snapshot(.res <- tidy(x, exponentiate = TRUE))
51 expect_snapshot(.res <- tidy(x, conf.int = TRUE, exponentiate = TRUE))
52 })