Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Dec 15, 2024
1 parent 12cc866 commit 433548a
Showing 1 changed file with 38 additions and 20 deletions.
58 changes: 38 additions & 20 deletions tests/testthat/test-estimate_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ test_that("estimate_means() - lm", {
# Simple
model <- lm(vs ~ cyl, data = dat)
estim1 <- suppressMessages(estimate_means(model))
expect_identical(dim(estim1), c(3L, 5L))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_identical(dim(estim1), c(3L, 5L))
expect_identical(dim(estim2), c(3L, 5L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
expect_equal(estim1$CI_low, estim2$CI_low, tolerance = 1e-3)

# Interaction (factor * continuous)
model <- lm(mpg ~ wt * gear, data = dat)
estim1 <- suppressMessages(estimate_means(model))
expect_identical(dim(estim1), c(3L, 5L))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_identical(dim(estim1), c(3L, 5L))
expect_identical(dim(estim2), c(3L, 5L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
expect_named(estim1, c("gear", "Mean", "SE", "CI_low", "CI_high"))
expect_named(estim2, c("gear", "Mean", "SE", "CI_low", "CI_high"))
Expand All @@ -32,10 +30,9 @@ test_that("estimate_means() - lm", {
# At specific levels
model <- lm(Sepal.Width ~ Species, data = iris)
estim1 <- suppressMessages(estimate_means(model, by = "Species=c('versicolor', 'virginica')"))
expect_identical(dim(estim1), c(2L, 5L))
estim2 <- suppressMessages(estimate_means(model, by = "Species=c('versicolor', 'virginica')", backend = "marginaleffects"))

Check warning on line 33 in tests/testthat/test-estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-estimate_means.R,line=33,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 125 characters.
expect_identical(dim(estim1), c(2L, 5L))
expect_identical(dim(estim2), c(2L, 5L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
expect_named(estim1, c("Species", "Mean", "SE", "CI_low", "CI_high"))
expect_named(estim2, c("Species", "Mean", "SE", "CI_low", "CI_high"))
Expand All @@ -48,8 +45,8 @@ test_that("estimate_means() - lm", {

model <- lm(Sepal.Width ~ Species * Petal.Length_factor, data = dat)
estim1 <- suppressMessages(estimate_means(model, by = "all"))
expect_identical(dim(estim1), c(6L, 6L))
estim2 <- suppressWarnings(suppressMessages(estimate_means(model, by = "all", backend = "marginaleffects")))
expect_identical(dim(estim1), c(6L, 6L))
expect_identical(dim(estim2), c(6L, 6L))
expect_equal(estim2$Mean, c(3.428, 3.79557, 2.54211, 2.90968, 2.60643, 2.974), tolerance = 1e-4)
expect_named(estim1, c("Species", "Petal.Length_factor", "Mean", "SE", "CI_low", "CI_high"))
Expand All @@ -58,29 +55,36 @@ test_that("estimate_means() - lm", {
# No interaction (two factors)
model <- lm(Petal.Length ~ Sepal.Width + Species, data = iris)
estim1 <- suppressMessages(estimate_means(model))
expect_identical(dim(estim1), c(3L, 5L))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_identical(dim(estim1), c(3L, 5L))
expect_identical(dim(estim2), c(3L, 5L))
expect_lt(max(estim1$Mean - estim2$Mean), 1e-10)
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
expect_equal(estim1$CI_low, estim2$CI_low, tolerance = 1e-3)
expect_named(estim2, c("Species", "Mean", "SE", "CI_low", "CI_high"))

# At specific levels of continuous
estim1 <- suppressMessages(estimate_means(model, by = "Sepal.Width"))
expect_equal(dim(estim1), c(10, 5))
estim2 <- suppressMessages(estimate_means(model, by = "Sepal.Width", backend = "marginaleffects"))
expect_equal(dim(estim1), c(10, 5))

Check warning on line 68 in tests/testthat/test-estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-estimate_means.R,line=68,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
expect_equal(dim(estim2), c(10, 5))

Check warning on line 69 in tests/testthat/test-estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-estimate_means.R,line=69,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
# Note that the absolute values are different here... for unclear reasons
expect_true(max(diff(estim1$Mean) - diff(estim2$Mean)) < 1e-10)

Check warning on line 71 in tests/testthat/test-estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-estimate_means.R,line=71,col=3,[expect_comparison_linter] expect_lt(x, y) is better than expect_true(x < y).
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)

model <- lm(Petal.Length ~ Sepal.Width + Species, data = iris)
estim <- suppressMessages(estimate_means(model))
expect_identical(dim(estim), c(3L, 5L))
estim1 <- suppressMessages(estimate_means(model))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_identical(dim(estim1), c(3L, 5L))
expect_identical(dim(estim2), c(3L, 5L))
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
expect_equal(estim1$CI_low, estim2$CI_low, tolerance = 1e-3)

estim <- suppressMessages(estimate_means(model, by = "all"))
expect_equal(dim(estim), c(30, 6))
estim1 <- suppressMessages(estimate_means(model, by = "all"))
estim2 <- suppressMessages(estimate_means(model, by = "all", backend = "marginaleffects"))
expect_equal(dim(estim1), c(30, 6))

Check warning on line 84 in tests/testthat/test-estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-estimate_means.R,line=84,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
## FIXME: marginaleffects does not return all values?
## Seems like marginaleffects only used data that actually exists
expect_equal(dim(estim2), c(20, 6))

Check warning on line 87 in tests/testthat/test-estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-estimate_means.R,line=87,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.

# In formula modification
# FIXME: this got broken but it seems just to tedious to fix. Don't use in formula transforms.
Expand All @@ -91,12 +95,26 @@ test_that("estimate_means() - lm", {
# One continuous and one factor
model <- lm(Petal.Length ~ Species * Sepal.Width, data = iris)

estim <- suppressMessages(estimate_means(model))
expect_identical(dim(estim), c(3L, 5L))
estim <- suppressMessages(estimate_means(model, fixed = "Sepal.Width"))
expect_identical(dim(estim), c(3L, 6L))
estim <- suppressMessages(estimate_means(model, by = c("Species", "Sepal.Width"), length = 2))
expect_identical(dim(estim), c(6L, 6L))
estim1 <- suppressMessages(estimate_means(model))
estim2 <- suppressMessages(estimate_means(model, backend = "marginaleffects"))
expect_identical(dim(estim1), c(3L, 5L))
expect_identical(dim(estim2), c(3L, 5L))
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
expect_equal(estim1$CI_low, estim2$CI_low, tolerance = 1e-3)
estim1 <- suppressMessages(estimate_means(model, fixed = "Sepal.Width"))
estim2 <- suppressMessages(estimate_means(model, fixed = "Sepal.Width", backend = "marginaleffects"))
expect_identical(dim(estim1), c(3L, 6L))
expect_identical(dim(estim2), c(3L, 5L))
expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
expect_equal(estim1$CI_low, estim2$CI_low, tolerance = 1e-3)
estim1 <- suppressMessages(estimate_means(model, by = c("Species", "Sepal.Width"), length = 2))
estim2 <- suppressMessages(estimate_means(model, by = c("Species", "Sepal.Width"), length = 2, backend = "marginaleffects"))

Check warning on line 111 in tests/testthat/test-estimate_means.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=tests/testthat/test-estimate_means.R,line=111,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 126 characters.
expect_identical(dim(estim1), c(6L, 6L))
## FIXME: marginaleffects does not return all values?
## Seems like marginaleffects only used data that actually exists
expect_identical(dim(estim2), c(2L, 6L))
# expect_equal(estim1$Mean, estim2$Mean, tolerance = 1e-4)
# expect_equal(estim1$CI_low, estim2$CI_low, tolerance = 1e-3)
estim <- suppressMessages(estimate_means(model, by = "Species=c('versicolor', 'setosa')"))
expect_identical(dim(estim), c(2L, 5L))
estim <- suppressMessages(estimate_means(model, by = "Sepal.Width=c(2, 4)"))
Expand Down

0 comments on commit 433548a

Please sign in to comment.