Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 8 additions & 28 deletions tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,19 @@ test_that("estimate_means - brms, multivariate", {
})


test_that("estimate_contrasts - brms, multivariate, many observations", {
test_that("estimate_contrasts - brms, multivariate, many observations and response names with dots", {
# the response values must not be used as response levels, else the
# regular expressions built from them grow too long and fail
# regular expressions built from them grow too long and fail. brms drops the
# dot from response names ("y.1" -> "y1"), and the contrast labels use the
# brms names
set.seed(123)
d <- data.frame(
y1 = rnorm(600),
y2 = rnorm(600),
y.1 = rnorm(600),
y.2 = rnorm(600),
g = factor(sample(c("a", "b", "c"), 600, replace = TRUE))
)
m <- suppressWarnings(brms::brm(
brms::mvbind(y1, y2) ~ g,
brms::mvbind(y.1, y.2) ~ g,
data = d,
refresh = 0,
iter = 500,
Expand All @@ -106,35 +108,13 @@ test_that("estimate_contrasts - brms, multivariate, many observations", {
))

out <- estimate_contrasts(m, "g", resp = "y1")
expect_identical(nrow(out), 3L)
expect_identical(as.character(out$Level1), c("b", "c", "c"))
expect_identical(as.character(out$Level2), c("a", "a", "b"))

out <- estimate_contrasts(m, "g")
expect_identical(nrow(out), 15L)
expect_true(all(c("Response1", "Response2") %in% colnames(out)))
expect_identical(levels(out$Response1), c("y1", "y2"))
})


test_that("estimate_contrasts - brms, multivariate, response names with dots", {
# brms drops the dot from response names ("Sepal.Length" -> "SepalLength"),
# and the contrast labels use the brms names
m <- suppressWarnings(brms::brm(
brms::mvbind(Sepal.Length, Petal.Length) ~ Species,
data = iris,
refresh = 0,
iter = 500,
chains = 1,
seed = 123
))

out <- estimate_contrasts(m, "Species")
expect_identical(nrow(out), 15L)
expect_true(all(c("Response1", "Response2") %in% colnames(out)))
expect_identical(levels(out$Response1), c("SepalLength", "PetalLength"))
expect_true(all(as.character(out$Level1) %in% levels(iris$Species)))
expect_true(all(as.character(out$Level2) %in% levels(iris$Species)))
expect_true(all(as.character(out$Level1) %in% levels(d$g)))
})


Expand Down
195 changes: 38 additions & 157 deletions tests/testthat/test-estimate_contrasts_slopes_filter.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,154 +6,60 @@ mt$g <- factor(mt$cyl)
mt$a <- factor(mt$am)
m2 <- lm(mpg ~ wt * g, data = mt)
m3 <- lm(mpg ~ wt * g * a, data = mt)
m6 <- lm(mpg ~ wt * g * a + hp, data = mt)

# filters for `by`, the matching `at` for emmeans, and the levels that each
# filter allows
filters <- list(
F1 = list(
by = c("g='4'", "a"),
at = list(g = "4"),
allowed = list(g = "4", a = c("0", "1"))
),
F2 = list(
by = c("g=c('4','6')", "a='1'"),
at = list(g = c("4", "6"), a = "1"),
allowed = list(g = c("4", "6"), a = "1")
),
F3 = list(
by = c("g", "a='1'"),
at = list(a = "1"),
allowed = list(g = c("4", "6", "8"), a = "1")
),
F4 = list(
by = "g=c('8','4')",
at = list(g = c("8", "4")),
allowed = list(g = c("8", "4"))
)
)

# identify the slope in a label like "4, 1", "6" or "g4 a1"; variables that
# the label leaves out take the one value their filter allows
.slope_key <- function(label, allowed) {
vars <- names(allowed)
tokens <- strsplit(trimws(label), ",\\s*|\\s+")[[1]]
prefixed <- vapply(
tokens,
function(tk) {
any(vapply(vars, function(v) startsWith(tk, v) && substring(tk, nchar(v) + 1) %in% allowed[[v]], logical(1)))
},
logical(1)
)
values <- list()
if (all(prefixed)) {
for (tk in tokens) {
v <- vars[vapply(vars, function(v) startsWith(tk, v), logical(1))][1]
values[[v]] <- substring(tk, nchar(v) + 1)
}
} else if (length(tokens) == length(vars)) {
values <- stats::setNames(as.list(tokens), vars)
} else {
values <- stats::setNames(as.list(tokens), vars[lengths(allowed) > 1])
}
for (v in vars) {
if (is.null(values[[v]])) {
values[[v]] <- allowed[[v]]
}
}
paste(vars, unlist(values[vars]), sep = "=", collapse = ";")
}

# check that `out` and the emmeans contrasts in `oracle` pair one to one,
# with the same two slopes, the same difference and the same p-value
expect_matches_oracle <- function(out, oracle, allowed) {
oracle <- as.data.frame(oracle)
expect_identical(nrow(out), nrow(oracle))
oracle_pairs <- strsplit(as.character(oracle$contrast), " - ", fixed = TRUE)
oracle_1 <- vapply(oracle_pairs, function(x) .slope_key(x[1], allowed), character(1))
oracle_2 <- vapply(oracle_pairs, function(x) .slope_key(x[2], allowed), character(1))
used <- logical(nrow(oracle))
for (i in seq_len(nrow(out))) {
key_1 <- .slope_key(as.character(out$Level1[i]), allowed)
key_2 <- .slope_key(as.character(out$Level2[i]), allowed)
same <- which(oracle_1 == key_1 & oracle_2 == key_2)
flipped <- which(oracle_1 == key_2 & oracle_2 == key_1)
expect_identical(length(same) + length(flipped), 1L)
if (length(same) + length(flipped) != 1L) {
next
}
j <- c(same, flipped)
sign <- if (length(same)) 1 else -1
expect_false(used[j])
used[j] <- TRUE
expect_equal(out$Difference[i], sign * oracle$estimate[j], tolerance = 1e-5, ignore_attr = TRUE)
expect_equal(out$p[i], oracle$p.value[j], tolerance = 1e-5, ignore_attr = TRUE)
}
expect_true(all(used))
}

.oracle <- function(model, filter) {
specs <- if (length(filter$allowed) == 1) ~g else ~ g * a
pairs(emmeans::emtrends(model, specs, "wt", at = filter$at), adjust = "none")
# modelbased orders and orients the pairs differently than emmeans
expect_slope_contrasts <- function(out, at) {
ref <- as.data.frame(pairs(
emmeans::emtrends(m3, ~ g * a, "wt", at = at),
adjust = "none"
))
expect_equal(sort(abs(out$Difference)), sort(abs(ref$estimate)), tolerance = 1e-5)
expect_equal(sort(out$p), sort(ref$p.value), tolerance = 1e-5)
}

probes <- list(
list(model = m3, filter = filters$F1),
list(model = m3, filter = filters$F2),
list(model = m3, filter = filters$F3),
list(model = m6, filter = filters$F1),
list(model = m6, filter = filters$F2),
list(model = m6, filter = filters$F3),
list(model = m2, filter = filters$F4)
)


test_that("estimate_contrasts, slopes, by-filters, marginaleffects", {
for (probe in probes) {
oracle <- .oracle(probe$model, probe$filter)
for (estimate in c("typical", "average")) {
out <- estimate_contrasts(probe$model, "wt", by = probe$filter$by, estimate = estimate)
expect_matches_oracle(out, oracle, probe$filter$allowed)
}
}
})
test_that("estimate_contrasts, slopes, by-filters", {
by <- c("g='4'", "a")
expect_slope_contrasts(
estimate_contrasts(m3, "wt", by = by, estimate = "average"),
list(g = "4")
)
expect_slope_contrasts(
estimate_contrasts(m3, "wt", by = by, backend = "emmeans"),
list(g = "4")
)

by <- c("g=c('4','6')", "a='1'")
at <- list(g = c("4", "6"), a = "1")
expect_slope_contrasts(estimate_contrasts(m3, "wt", by = by, estimate = "average"), at)
expect_slope_contrasts(estimate_contrasts(m3, "wt", by = by, backend = "emmeans"), at)

test_that("estimate_contrasts, slopes, by-filters, emmeans", {
for (probe in probes) {
oracle <- .oracle(probe$model, probe$filter)
out <- estimate_contrasts(probe$model, "wt", by = probe$filter$by, backend = "emmeans")
expect_matches_oracle(out, oracle, probe$filter$allowed)
}
by <- c("g=c('4','5')", "a")
expect_slope_contrasts(
estimate_contrasts(m3, "wt", by = by, estimate = "average"),
list(g = "4")
)
expect_slope_contrasts(
estimate_contrasts(m3, "wt", by = by, backend = "emmeans"),
list(g = "4")
)
})


test_that("estimate_contrasts, slopes, by-filters leave fewer than two slopes", {
calls <- list(
list(model = m2, by = "g='4'"),
list(model = m3, by = c("g='4'", "a='1'"))
expect_error(
estimate_contrasts(m2, "wt", by = "g='4'", estimate = "average"),
regexp = "at least two"
)
expect_error(
estimate_contrasts(m2, "wt", by = "g='4'", backend = "emmeans"),
regexp = "at least two"
)
for (cl in calls) {
expect_error(
estimate_contrasts(cl$model, "wt", by = cl$by, estimate = "typical"),
regexp = "at least two"
)
expect_error(
estimate_contrasts(cl$model, "wt", by = cl$by, estimate = "average"),
regexp = "at least two"
)
expect_error(
estimate_contrasts(cl$model, "wt", by = cl$by, backend = "emmeans"),
regexp = "at least two"
)
}
# spaces around "=", and `estimate = "specific"`
expect_error(
estimate_contrasts(m3, "wt", by = c("g = '4'", "a = '1'"), estimate = "specific"),
regexp = "at least two"
)
out <- estimate_contrasts(m3, "wt", by = c("g = '4'", "a"), estimate = "average")
expect_matches_oracle(out, .oracle(m3, filters$F1), filters$F1$allowed)
# with `estimate = "average"`, only slopes with data count: for 5 gears,
# all cars have am = 1
mt$gear <- factor(mt$gear)
Expand All @@ -162,39 +68,14 @@ test_that("estimate_contrasts, slopes, by-filters leave fewer than two slopes",
estimate_contrasts(m7, "wt", by = c("gear='5'", "a"), estimate = "average"),
regexp = "at least two"
)
expect_identical(nrow(estimate_contrasts(m7, "wt", by = c("gear='5'", "a"))), 1L)
})


test_that("estimate_contrasts, slopes, by-filter values that are not levels", {
oracle <- .oracle(m3, filters$F1)
out <- estimate_contrasts(m3, "wt", by = c("g=c('4','5')", "a"), estimate = "average")
expect_matches_oracle(out, oracle, filters$F1$allowed)
out <- estimate_contrasts(m3, "wt", by = c("g=c('4','5')", "a"), backend = "emmeans")
expect_matches_oracle(out, oracle, filters$F1$allowed)
})


test_that("estimate_contrasts, slopes, emmeans, numeric by or factors from the formula", {
# emmeans does not filter these, but still returns all contrasts
m_factor <- lm(mpg ~ wt * factor(cyl), data = mtcars)
out <- estimate_contrasts(m_factor, "wt", by = "cyl=c(4,6)", backend = "emmeans")
expect_identical(nrow(out), 3L)
expect_true(all(is.finite(out$Difference)))
m_numeric <- lm(mpg ~ wt * hp * g, data = mt)
out <- estimate_contrasts(m_numeric, "wt", by = c("g='4'", "hp"), backend = "emmeans")
expect_identical(nrow(out), 3L)
expect_true(all(is.finite(out$Difference)))
})


test_that("estimate_contrasts, slopes, no by-filter", {
oracle <- pairs(emmeans::emtrends(m3, ~ g * a, "wt"), adjust = "none")
allowed <- list(g = c("4", "6", "8"), a = c("0", "1"))
for (estimate in c("typical", "average")) {
out <- estimate_contrasts(m3, "wt", by = c("g", "a"), estimate = estimate)
expect_matches_oracle(out, oracle, allowed)
}
out <- estimate_contrasts(m3, "wt", by = c("g", "a"), backend = "emmeans")
expect_matches_oracle(out, oracle, allowed)
})
Loading
Loading