Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 25, 2023
1 parent 7ad5946 commit 79d29f9
Showing 1 changed file with 96 additions and 6 deletions.
102 changes: 96 additions & 6 deletions tests/testthat/test-binned_residuals.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
test_that("binned_residuals", {
data(mtcars)
model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
result <- binned_residuals(model)
result <- binned_residuals(model, ci_type = "gaussian", residuals = "response")
expect_named(
result,
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "ci_range", "CI_low", "CI_high", "group")
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "CI_low", "CI_high", "group")
)
expect_equal(
result$xbar,
Expand All @@ -16,16 +16,21 @@ test_that("binned_residuals", {
c(-0.03786, -0.09514, 0.07423, -0.07955, 0.28891, -0.13786),
tolerance = 1e-4
)
expect_equal(
result$CI_low,
c(-0.05686, -0.12331, -0.35077, -0.57683, 0.17916, -0.44147),
tolerance = 1e-4
)
})


test_that("binned_residuals, n_bins", {
data(mtcars)
model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
result <- binned_residuals(model, n_bins = 10)
result <- binned_residuals(model, ci_type = "gaussian", residuals = "response", n_bins = 10)
expect_named(
result,
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "ci_range", "CI_low", "CI_high", "group")
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "CI_low", "CI_high", "group")
)
expect_equal(
result$xbar,
Expand All @@ -49,10 +54,10 @@ test_that("binned_residuals, n_bins", {
test_that("binned_residuals, terms", {
data(mtcars)
model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
result <- binned_residuals(model, term = "mpg")
result <- binned_residuals(model, ci_type = "gaussian", residuals = "response", term = "mpg")
expect_named(
result,
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "ci_range", "CI_low", "CI_high", "group")
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "CI_low", "CI_high", "group")
)
expect_equal(
result$xbar,
Expand All @@ -65,3 +70,88 @@ test_that("binned_residuals, terms", {
tolerance = 1e-4
)
})


test_that("binned_residuals, deviance residuals, gaussian CI", {
data(mtcars)
model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
result <- binned_residuals(model, residuals = "deviance", ci_type = "gaussian")
expect_named(
result,
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "CI_low", "CI_high", "group")
)
expect_equal(
result$xbar,
c(0.03786, 0.09514, 0.25911, 0.47955, 0.71109, 0.97119),
tolerance = 1e-4
)
expect_equal(
result$ybar,
c(-0.26905, -0.44334, 0.03763, -0.19917, 0.81563, -0.23399),
tolerance = 1e-4
)
expect_equal(
result$ybar,
c(-0.26905, -0.44334, 0.03763, -0.19917, 0.81563, -0.23399),
tolerance = 1e-4
)
expect_equal(
result$CI_low,
c(-0.33985, -0.50865, -0.98255, -1.36025, 0.61749, -1.00913),
tolerance = 1e-4
)
})


test_that("binned_residuals, default", {
data(mtcars)
model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
result <- binned_residuals(model)
expect_named(
result,
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "CI_low", "CI_high", "group")
)
expect_equal(
result$xbar,
c(0.03786, 0.09514, 0.25911, 0.47955, 0.71109, 0.97119),
tolerance = 1e-4
)
expect_equal(
result$ybar,
c(-0.26905, -0.44334, 0.03763, -0.19917, 0.81563, -0.23399),
tolerance = 1e-4
)
expect_equal(
result$CI_low,
c(-0.52997, -0.70426, -0.32935, -0.59948, 0.55472, -0.55251),
tolerance = 1e-4
)
})


test_that("binned_residuals, bootstrapped CI", {
skip_on_cran()
data(mtcars)
model <- glm(vs ~ wt + mpg, data = mtcars, family = "binomial")
set.seed(123)
result <- binned_residuals(model, ci_type = "boot", iterations = 100)
expect_named(
result,
c("xbar", "ybar", "n", "x.lo", "x.hi", "se", "CI_low", "CI_high", "group")
)
expect_equal(
result$xbar,
c(0.03786, 0.09514, 0.25911, 0.47955, 0.71109, 0.97119),
tolerance = 1e-4
)
expect_equal(
result$ybar,
c(-0.26905, -0.44334, 0.03763, -0.19917, 0.81563, -0.23399),
tolerance = 1e-4
)
expect_equal(
result$CI_low,
c(-0.32623, -0.50543, -0.80879, -1.15154, 0.67569, -0.65748),
tolerance = 1e-4
)
})

0 comments on commit 79d29f9

Please sign in to comment.