Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Mar 15, 2024
1 parent 0b8fcfc commit 6a369e3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/testthat/_snaps/include_reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# include_reference, on-the-fly factors

Code
print(out1, include_reference = TRUE)
Output
Parameter | Coefficient | SE | 95% CI | t(27) | p
------------------------------------------------------------------
(Intercept) | 27.48 | 1.97 | [23.43, 31.53] | 13.92 | < .001
gear [3] | 0.00 | | | |
gear [4] | 0.08 | 1.83 | [-3.68, 3.83] | 0.04 | 0.967
gear [5] | 2.39 | 2.38 | [-2.50, 7.29] | 1.00 | 0.324
am [0] | 0.00 | | | |
am [1] | 4.14 | 1.81 | [ 0.42, 7.85] | 2.29 | 0.030
hp | -0.06 | 0.01 | [-0.09, -0.04] | -6.24 | < .001
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.

---

Code
print(out2, include_reference = TRUE)
Output
Parameter | Coefficient | SE | 95% CI | t(27) | p
------------------------------------------------------------------
(Intercept) | 27.48 | 1.97 | [23.43, 31.53] | 13.92 | < .001
gear [3] | 0.00 | | | |
gear [4] | 0.08 | 1.83 | [-3.68, 3.83] | 0.04 | 0.967
gear [5] | 2.39 | 2.38 | [-2.50, 7.29] | 1.00 | 0.324
am [0] | 0.00 | | | |
am [1] | 4.14 | 1.81 | [ 0.42, 7.85] | 2.29 | 0.030
hp | -0.06 | 0.01 | [-0.09, -0.04] | -6.24 | < .001
Message
Uncertainty intervals (equal-tailed) and p-values (two-tailed) computed
using a Wald t-distribution approximation.

30 changes: 30 additions & 0 deletions tests/testthat/test-include_reference.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
test_that("include_reference, on-the-fly factors", {
data(mtcars)
d <- as.data.frame(mtcars)
d$gear <- as.factor(d$gear)
d$am <- as.factor(d$am)

m1 <- lm(mpg ~ as.factor(gear) + factor(am) + hp, data = mtcars)
m2 <- lm(mpg ~ gear + am + hp, data = d)

out1 <- model_parameters(m1)
out2 <- model_parameters(m2)

expect_snapshot(print(out1, include_reference = TRUE))
expect_snapshot(print(out2, include_reference = TRUE))

out1 <- model_parameters(m1, include_reference = TRUE)
out2 <- model_parameters(m2, include_reference = TRUE)

expect_equal(attributes(out1)$pretty_names, attributes(out2)$pretty_names, ignore_attr = TRUE)
expect_equal(out1$Coefficient, out2$Coefficient, tolerance = 1e-4)

skip_if(getRversion() < "4.3.0")
skip_if_not_installed("datawizard")
out3 <- mtcars |>
datawizard::data_modify(gear = factor(gear), am = as.factor(am)) |>
lm(mpg ~ gear + am + hp, data = _) |>
model_parameters(include_reference = TRUE)

expect_equal(attributes(out1)$pretty_names, attributes(out3)$pretty_names, ignore_attr = TRUE)
})

0 comments on commit 6a369e3

Please sign in to comment.