Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cleanup part 2 #567

Merged
merged 13 commits into from
Mar 31, 2023
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Suggests:
ICS,
ICSOutlier,
ISLR,
ivreg,
lavaan,
lme4,
lmtest,
Expand All @@ -110,6 +111,7 @@ Suggests:
Matrix,
mclogit,
mclust,
metadat,
metafor,
mgcv,
mlogit,
Expand All @@ -132,7 +134,8 @@ Suggests:
survival,
testthat,
tweedie,
VGAM
VGAM,
withr
Encoding: UTF-8
Language: en-US
RoxygenNote: 7.2.3.9000
Expand Down
14 changes: 3 additions & 11 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
if (require("testthat", quietly = TRUE)) {
library(performance)
library(testthat)
library(performance)

is_dev_version <- length(strsplit(packageDescription("performance")$Version, "\\.")[[1]]) > 3L
if (is_dev_version) {
Sys.setenv("RunAllperformanceTests" = "yes")
} else {
Sys.setenv("RunAllperformanceTests" = "no")
}

test_check("performance")
}
test_check("performance")
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/check_collinearity.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# check_collinearity, ci = NULL

Code
print(out)
out
Output
# Check for Multicollinearity

Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/_snaps/mclogit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# model_performance

Code
model_performance(mod_mb)
Output
# Indices of model performance

AIC | BIC | Nagelkerke's R2 | RMSE | Sigma
-------------------------------------------------
38.823 | 47.618 | 0.836 | 0.298 | 1.016

---

Code
model_performance(mod_mc)
Output
# Indices of model performance

AIC | BIC | Nagelkerke's R2 | RMSE | Sigma
-------------------------------------------------
13.228 | 24.424 | 0.998 | 0.009 | 0.068

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/model_performance.rma.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# check_outliers.rma

Code
print(out)
out
Output
2 outliers detected: studies 4 (Hart & Sutherland) and 8 (TPT Madras).

11 changes: 11 additions & 0 deletions tests/testthat/_snaps/pkg-ivreg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Issue #530 from the `modelsummary` repo

Code
out
Output
# Indices of model performance

AIC | BIC | R2 | R2 (adj.) | RMSE | Sigma | Wu & Hausman | p (Wu_Hausman) | Weak instruments | p (weak_instruments)
-------------------------------------------------------------------------------------------------------------------------------
182.692 | 191.486 | 0.655 | 0.604 | 3.484 | 3.793 | 13.869 | < .001 | 19.958 | < .001

11 changes: 0 additions & 11 deletions tests/testthat/helper.R

This file was deleted.

256 changes: 132 additions & 124 deletions tests/testthat/test-check_collinearity.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,147 +23,155 @@ test_that("check_collinearity, correct order in print", {
})


if (requiet("glmmTMB") && getRversion() >= "4.0.0") {
data(Salamanders)
m1 <- glmmTMB(count ~ spp + mined + (1 | site),
test_that("check_collinearity", {
skip_if_not_installed("glmmTMB")
skip_if_not(getRversion() >= "4.0.0")

data(Salamanders, package = "glmmTMB")

m1 <- glmmTMB::glmmTMB(count ~ spp + mined + (1 | site),
ziformula = ~spp,
Salamanders,
family = poisson()
)
expect_equal(
suppressWarnings(check_collinearity(m1, component = "conditional", verbose = FALSE)$VIF),
c(1.00037354840318, 1.00037354840318),
tolerance = 1e-3
)
expect_equal(
suppressWarnings(check_collinearity(m1, component = "all", verbose = FALSE)$VIF),
c(1.00037354840318, 1.00037354840318),
tolerance = 1e-3
)
expect_null(check_collinearity(m1, verbose = FALSE, component = "zero_inflated"))
})

test_that("check_collinearity", {
expect_equal(
suppressWarnings(check_collinearity(m1, component = "conditional", verbose = FALSE)$VIF),
c(1.00037354840318, 1.00037354840318),
tolerance = 1e-3
)
expect_equal(
suppressWarnings(check_collinearity(m1, component = "all", verbose = FALSE)$VIF),
c(1.00037354840318, 1.00037354840318),
tolerance = 1e-3
)
expect_null(check_collinearity(m1, verbose = FALSE, component = "zero_inflated"))
})

m2 <- glmmTMB(

test_that("check_collinearity", {
skip_if_not_installed("glmmTMB")
skip_if_not(getRversion() >= "4.0.0")

data(Salamanders, package = "glmmTMB")

m2 <- glmmTMB::glmmTMB(
count ~ spp + mined + cover + (1 | site),
ziformula = ~ spp + mined + cover,
family = nbinom2,
family = glmmTMB::nbinom2,
data = Salamanders
)

test_that("check_collinearity", {
expect_equal(
suppressWarnings(check_collinearity(m2, component = "conditional", verbose = FALSE)$VIF),
c(1.09015, 1.2343, 1.17832),
tolerance = 1e-3
expect_equal(
suppressWarnings(check_collinearity(m2, component = "conditional", verbose = FALSE)$VIF),
c(1.09015, 1.2343, 1.17832),
tolerance = 1e-3
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "conditional", verbose = FALSE)$VIF_CI_low),
c(1.03392, 1.14674, 1.10105),
tolerance = 1e-3
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "all", verbose = FALSE)$VIF),
c(1.09015, 1.2343, 1.17832, 1.26914, 1, 1.26914),
tolerance = 1e-3
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "all", verbose = FALSE)$VIF_CI_low),
c(1.03392, 1.14674, 1.10105, 1.17565, 1, 1.17565),
tolerance = 1e-3
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "zero_inflated", verbose = FALSE)$VIF),
c(1.26914, 1, 1.26914),
tolerance = 1e-3
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "zero_inflated", verbose = FALSE)$Tolerance_CI_high),
c(0.85059, 1, 0.85059),
tolerance = 1e-3
)

suppressWarnings(coll <- check_collinearity(m2, component = "all", verbose = FALSE)) # nolint
expect_true(all(coll$Tolerance < coll$Tolerance_CI_high))
expect_true(all(coll$VIF > coll$VIF_CI_low))

expect_identical(
attributes(coll)$data$Component,
c("conditional", "conditional", "conditional", "zero inflated", "zero inflated", "zero inflated")
)
expect_identical(
colnames(attributes(coll)$CI),
c("VIF_CI_low", "VIF_CI_high", "Tolerance_CI_low", "Tolerance_CI_high", "Component")
)
})

test_that("check_collinearity | afex", {
skip_if_not_installed("afex", minimum_version = "1.0.0")

data(obk.long, package = "afex")

obk.long$treatment <- as.character(obk.long$treatment)
suppressWarnings(suppressMessages({
aM <- afex::aov_car(value ~ treatment * gender + Error(id / (phase * hour)),
data = obk.long
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "conditional", verbose = FALSE)$VIF_CI_low),
c(1.03392, 1.14674, 1.10105),
tolerance = 1e-3

aW <- afex::aov_car(value ~ Error(id / (phase * hour)),
data = obk.long
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "all", verbose = FALSE)$VIF),
c(1.09015, 1.2343, 1.17832, 1.26914, 1, 1.26914),
tolerance = 1e-3

aB <- afex::aov_car(value ~ treatment * gender + Error(id),
data = obk.long
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "all", verbose = FALSE)$VIF_CI_low),
c(1.03392, 1.14674, 1.10105, 1.17565, 1, 1.17565),
tolerance = 1e-3
}))

expect_message(ccoM <- check_collinearity(aM)) # nolint
expect_warning(expect_message(ccoW <- check_collinearity(aW))) # nolint
expect_message(ccoB <- check_collinearity(aB), regexp = NA) # nolint

expect_identical(nrow(ccoM), 15L)
expect_identical(nrow(ccoW), 3L)
expect_identical(nrow(ccoB), 3L)

suppressWarnings(suppressMessages({
aM <- afex::aov_car(value ~ treatment * gender + Error(id / (phase * hour)),
include_aov = TRUE,
data = obk.long
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "zero_inflated", verbose = FALSE)$VIF),
c(1.26914, 1, 1.26914),
tolerance = 1e-3

aW <- afex::aov_car(value ~ Error(id / (phase * hour)),
include_aov = TRUE,
data = obk.long
)
expect_equal(
suppressWarnings(check_collinearity(m2, component = "zero_inflated", verbose = FALSE)$Tolerance_CI_high),
c(0.85059, 1, 0.85059),
tolerance = 1e-3

aB <- afex::aov_car(value ~ treatment * gender + Error(id),
include_aov = TRUE,
data = obk.long
)
}))

suppressWarnings(coll <- check_collinearity(m2, component = "all", verbose = FALSE)) # nolint
expect_true(all(coll$Tolerance < coll$Tolerance_CI_high))
expect_true(all(coll$VIF > coll$VIF_CI_low))
expect_message(ccoM <- check_collinearity(aM)) # nolint
expect_warning(expect_message(ccoW <- check_collinearity(aW))) # nolint
expect_message(ccoB <- check_collinearity(aB), regexp = NA) # nolint

expect_identical(
attributes(coll)$data$Component,
c("conditional", "conditional", "conditional", "zero inflated", "zero inflated", "zero inflated")
)
expect_identical(
colnames(attributes(coll)$CI),
c("VIF_CI_low", "VIF_CI_high", "Tolerance_CI_low", "Tolerance_CI_high", "Component")
)
})

if (requiet("afex") && utils::packageVersion("afex") >= package_version("1.0.0")) {
test_that("check_collinearity | afex", {
data(obk.long, package = "afex")

obk.long$treatment <- as.character(obk.long$treatment)
suppressWarnings(suppressMessages({
aM <- afex::aov_car(value ~ treatment * gender + Error(id / (phase * hour)),
data = obk.long
)

aW <- afex::aov_car(value ~ Error(id / (phase * hour)),
data = obk.long
)

aB <- afex::aov_car(value ~ treatment * gender + Error(id),
data = obk.long
)
}))

expect_message(ccoM <- check_collinearity(aM)) # nolint
expect_warning(expect_message(ccoW <- check_collinearity(aW))) # nolint
expect_message(ccoB <- check_collinearity(aB), regexp = NA) # nolint

expect_identical(nrow(ccoM), 15L)
expect_identical(nrow(ccoW), 3L)
expect_identical(nrow(ccoB), 3L)

suppressWarnings(suppressMessages({
aM <- afex::aov_car(value ~ treatment * gender + Error(id / (phase * hour)),
include_aov = TRUE,
data = obk.long
)

aW <- afex::aov_car(value ~ Error(id / (phase * hour)),
include_aov = TRUE,
data = obk.long
)

aB <- afex::aov_car(value ~ treatment * gender + Error(id),
include_aov = TRUE,
data = obk.long
)
}))

expect_message(ccoM <- check_collinearity(aM)) # nolint
expect_warning(expect_message(ccoW <- check_collinearity(aW))) # nolint
expect_message(ccoB <- check_collinearity(aB), regexp = NA) # nolint

expect_identical(nrow(ccoM), 15L)
expect_identical(nrow(ccoW), 3L)
expect_identical(nrow(ccoB), 3L)
})
}

test_that("check_collinearity, ci = NULL", { # 518
data(npk)
m <- lm(yield ~ N + P + K, npk)
out <- check_collinearity(m, ci = NULL)

expect_identical(
colnames(out),
c(
"Term", "VIF", "VIF_CI_low", "VIF_CI_high", "SE_factor", "Tolerance",
"Tolerance_CI_low", "Tolerance_CI_high"
)
expect_identical(nrow(ccoM), 15L)
expect_identical(nrow(ccoW), 3L)
expect_identical(nrow(ccoB), 3L)
})

test_that("check_collinearity, ci = NULL", { # 518
data(npk)
m <- lm(yield ~ N + P + K, npk)
out <- check_collinearity(m, ci = NULL)

expect_identical(
colnames(out),
c(
"Term", "VIF", "VIF_CI_low", "VIF_CI_high", "SE_factor", "Tolerance",
"Tolerance_CI_low", "Tolerance_CI_high"
)
expect_snapshot(print(out))
})
}
)
expect_snapshot(out)
})
Loading