Debian Codegraph @

tests/testthat/test-utilities.R

raw
1 skip_if_not_installed("modeltests")
2 library(modeltests)
3
4 test_that("ellipsis checking works", {
5 expect_snapshot(
6 check_ellipses("exponentiate", "tidy", "boop", exponentiate = TRUE)
7 )
8
9 expect_snapshot(
10 check_ellipses(
11 "exponentiate",
12 "tidy",
13 "boop",
14 exponentiate = TRUE,
15 quick = FALSE
16 )
17 )
18
19 expect_silent(check_ellipses("exponentiate", "tidy", "boop", hi = "pal"))
20 })
21
22 test_that("ellipsis checking works (whole game, tidy)", {
23 mod <- nls(mpg ~ k * e^wt, data = mtcars, start = list(k = 1, e = 2))
24
25 expect_snapshot(tidy(mod, exponentiate = TRUE))
26 })
27
28 test_that("ellipsis checking works (whole game, augment)", {
29 mod <- kmeans(mtcars, centers = 4)
30
31 expect_snapshot(
32 .res <- augment(mod, data = mtcars, newdata = mtcars)
33 )
34 })
35
36 test_that("augment_newdata can handle function calls in response term (lm)", {
37 mt_lm <- lm(data = mtcars, mpg ~ hp)
38 mt_lm_log <- lm(data = mtcars, log(mpg) ~ hp)
39
40 aug_mt_lm_none <- augment(mt_lm)
41 aug_mt_lm_data <- augment(mt_lm, data = mtcars)
42 aug_mt_lm_newdata <- augment(mt_lm, newdata = mtcars[1:20, ])
43 aug_mt_lm_no_resp <- augment(mt_lm, newdata = mtcars[1:20, 2:ncol(mtcars)])
44
45 aug_mt_lm_log_none <- augment(mt_lm_log)
46 aug_mt_lm_log_data <- augment(mt_lm_log, data = mtcars)
47 aug_mt_lm_log_newdata <- augment(mt_lm_log, newdata = mtcars[1:20, ])
48 aug_mt_lm_log_no_resp <- augment(
49 mt_lm_log,
50 newdata = mtcars[1:20, 2:ncol(mtcars)]
51 )
52
53 expect_true(inherits(aug_mt_lm_log_none, "tbl_df"))
54 expect_true(inherits(aug_mt_lm_log_data, "tbl_df"))
55 expect_true(inherits(aug_mt_lm_log_newdata, "tbl_df"))
56 expect_true(inherits(aug_mt_lm_log_no_resp, "tbl_df"))
57
58 expect_equal(
59 ".resid" %in% colnames(aug_mt_lm_log_none),
60 ".resid" %in% colnames(aug_mt_lm_none)
61 )
62 expect_equal(
63 ".resid" %in% colnames(aug_mt_lm_log_data),
64 ".resid" %in% colnames(aug_mt_lm_data)
65 )
66 expect_equal(
67 ".resid" %in% colnames(aug_mt_lm_log_newdata),
68 ".resid" %in% colnames(aug_mt_lm_newdata)
69 )
70 expect_equal(
71 ".resid" %in% colnames(aug_mt_lm_log_no_resp),
72 ".resid" %in% colnames(aug_mt_lm_no_resp)
73 )
74
75 expect_equal(
76 aug_mt_lm_log_none$.resid,
77 log(mtcars$mpg) - unname(fitted(mt_lm_log, mtcars))
78 )
79 expect_equal(
80 aug_mt_lm_log_data$.resid,
81 log(mtcars$mpg) - unname(fitted(mt_lm_log, mtcars))
82 )
83 expect_equal(
84 aug_mt_lm_log_newdata$.resid,
85 log(mtcars$mpg[1:20]) - unname(predict(mt_lm_log, mtcars[1:20, ]))
86 )
87 })
88
89 test_that("augment_newdata can handle function calls in response term (glm)", {
90 mt_glm <- glm(data = mtcars, mpg ~ .)
91 mt_glm_log <- glm(data = mtcars, log(mpg) ~ .)
92
93 aug_mt_glm_none <- augment(mt_glm)
94 aug_mt_glm_data <- augment(mt_glm, data = mtcars)
95 aug_mt_glm_newdata <- augment(mt_glm, newdata = mtcars[1:20, ])
96 aug_mt_glm_no_resp <- augment(mt_glm, newdata = mtcars[1:20, 2:ncol(mtcars)])
97
98 aug_mt_glm_log_none <- augment(mt_glm_log)
99 aug_mt_glm_log_data <- augment(mt_glm_log, data = mtcars)
100 aug_mt_glm_log_newdata <- augment(mt_glm_log, newdata = mtcars[1:20, ])
101 aug_mt_glm_log_no_resp <- augment(
102 mt_glm_log,
103 newdata = mtcars[1:20, 2:ncol(mtcars)]
104 )
105
106 expect_true(inherits(aug_mt_glm_log_none, "tbl_df"))
107 expect_true(inherits(aug_mt_glm_log_data, "tbl_df"))
108 expect_true(inherits(aug_mt_glm_log_newdata, "tbl_df"))
109 expect_true(inherits(aug_mt_glm_log_no_resp, "tbl_df"))
110
111 expect_equal(
112 ".resid" %in% colnames(aug_mt_glm_log_none),
113 ".resid" %in% colnames(aug_mt_glm_none)
114 )
115 expect_equal(
116 ".resid" %in% colnames(aug_mt_glm_log_data),
117 ".resid" %in% colnames(aug_mt_glm_data)
118 )
119 expect_equal(
120 ".resid" %in% colnames(aug_mt_glm_log_newdata),
121 ".resid" %in% colnames(aug_mt_glm_newdata)
122 )
123 expect_equal(
124 ".resid" %in% colnames(aug_mt_glm_log_no_resp),
125 ".resid" %in% colnames(aug_mt_glm_no_resp)
126 )
127
128 expect_equal(
129 aug_mt_glm_log_none$.resid,
130 log(mtcars$mpg) - unname(fitted(mt_glm_log, mtcars))
131 )
132 expect_equal(
133 aug_mt_glm_log_data$.resid,
134 log(mtcars$mpg) - unname(fitted(mt_glm_log, mtcars))
135 )
136 })
137
138 test_that("augment_newdata can handle function calls in response term (loess)", {
139 mt_loess <- loess(data = mtcars, mpg ~ hp + disp)
140 mt_loess_log <- loess(data = mtcars, log(mpg) ~ hp + disp)
141
142 aug_mt_loess_none <- augment(mt_loess)
143 aug_mt_loess_data <- augment(mt_loess, data = mtcars)
144 aug_mt_loess_newdata <- augment(mt_loess, newdata = mtcars[1:20, ])
145 aug_mt_loess_no_resp <- augment(
146 mt_loess,
147 newdata = mtcars[1:20, 2:ncol(mtcars)]
148 )
149
150 aug_mt_loess_log_none <- augment(mt_loess_log)
151 aug_mt_loess_log_data <- augment(mt_loess_log, data = mtcars)
152 aug_mt_loess_log_newdata <- augment(mt_loess_log, newdata = mtcars[1:20, ])
153 aug_mt_loess_log_no_resp <- augment(
154 mt_loess_log,
155 newdata = mtcars[1:20, 2:ncol(mtcars)]
156 )
157
158 expect_true(inherits(aug_mt_loess_log_none, "tbl_df"))
159 expect_true(inherits(aug_mt_loess_log_data, "tbl_df"))
160 expect_true(inherits(aug_mt_loess_log_newdata, "tbl_df"))
161 expect_true(inherits(aug_mt_loess_log_no_resp, "tbl_df"))
162
163 expect_equal(
164 ".resid" %in% colnames(aug_mt_loess_log_none),
165 ".resid" %in% colnames(aug_mt_loess_none)
166 )
167 expect_equal(
168 ".resid" %in% colnames(aug_mt_loess_log_data),
169 ".resid" %in% colnames(aug_mt_loess_data)
170 )
171 expect_equal(
172 ".resid" %in% colnames(aug_mt_loess_log_newdata),
173 ".resid" %in% colnames(aug_mt_loess_newdata)
174 )
175 expect_equal(
176 ".resid" %in% colnames(aug_mt_loess_log_no_resp),
177 ".resid" %in% colnames(aug_mt_loess_no_resp)
178 )
179
180 expect_equal(
181 aug_mt_loess_log_none$.resid,
182 log(mtcars$mpg) - unname(fitted(mt_loess_log, mtcars))
183 )
184 expect_equal(
185 aug_mt_loess_log_data$.resid,
186 log(mtcars$mpg) - unname(fitted(mt_loess_log, mtcars))
187 )
188 expect_equal(
189 aug_mt_loess_log_newdata$.resid,
190 log(mtcars$mpg[1:20]) - unname(predict(mt_loess_log, mtcars[1:20, ]))
191 )
192 })
193
194 test_that("as_glance_tibble", {
195 df1 <- as_glance_tibble(x = 1, y = 1, na_types = "rr")
196 df2 <- as_glance_tibble(x = 1, y = NULL, na_types = "rc")
197 df3 <- as_glance_tibble(x = 1, y = NULL, na_types = "rr")
198
199 expect_equal(
200 purrr::map(df1, class),
201 purrr::map(df3, class)
202 )
203
204 expect_true(class(df1$y) == class(df3$y))
205
206 expect_false(class(df1$y) == class(df2$y))
207
208 expect_snapshot(
209 error = TRUE,
210 as_glance_tibble(x = 1, y = 1, na_types = "rrr")
211 )
212 })
213
214 test_that("appropriate warning on (g)lm-subclassed models", {
215 x <- 1
216 class(x) <- c("boop", "glm")
217
218 expect_snapshot(warn_on_subclass(x, "tidy"))
219
220 # only displayed once per session, per unique dispatch
221 expect_silent(warn_on_subclass(x, "tidy"))
222
223 class(x) <- c("bop", "glm", "lm")
224
225 expect_snapshot(warn_on_subclass(x, "tidy"))
226
227 # only displayed once per session, per unique dispatch
228 expect_silent(
229 warn_on_subclass(x, "tidy")
230 )
231 })
232
233 test_that("as_augment_tibble errors informatively", {
234 m <- matrix(1:4, ncol = 2)
235 expect_snapshot(error = TRUE, as_augment_tibble(m))
236 })