Skip to content

Commit 6069df4

Browse files
authored
estimate_slopes() column naming breaks plotting brms (#530)
* estimate_slopes() column naming breaks plotting brms Fixes #529 * news, desc * fix * add test
1 parent 20c60b1 commit 6069df4

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: modelbased
33
Title: Estimation of Model-Based Predictions, Contrasts and Means
4-
Version: 0.11.2.15
4+
Version: 0.11.2.16
55
Authors@R:
66
c(person(given = "Dominique",
77
family = "Makowski",

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737

3838
* Fixed issues with object of class `aov`.
3939

40+
* Fixed issue with the `plot()` method for `estimate_slopes()` for Bayesian
41+
models.
42+
4043
# modelbased 0.11.2
4144

4245
## Changes

R/visualisation_recipe_internal.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@
5555
} else if ("estimate_means" %in% att$class) {
5656
aes$y <- att$coef_name
5757
} else if ("estimate_slopes" %in% att$class) {
58-
aes$y <- "Slope"
58+
# for frequentist models, we have "Slope" as column name, for Bayesian models
59+
# we have "Median", "Mean" or "MAP"
60+
valid_y_vars <- intersect(c("Slope", "Median", "Mean", "MAP"), colnames(data))
61+
# we wouldn't expect that there is more than one of these columns, but we
62+
# check for that anyway...
63+
if (length(valid_y_vars) == 0) {
64+
insight::format_error("Could not find a suitable column for the y-axis. Expected one of: 'Slope', 'Median', 'Mean', 'MAP'.")
65+
}
66+
aes$y <- valid_y_vars[1]
5967
if ("Comparison" %in% names(data)) {
6068
# Insert "Comparison" column as the 2nd by so that it gets plotted as color
6169
if (length(by) > 1) {
Lines changed: 63 additions & 0 deletions
Loading

tests/testthat/test-estimate_slopes.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ test_that("estimate_slopes, works with glmmTMB", {
168168
})
169169

170170

171-
test_that("estimate_slopes, works with glmmTMB", {
171+
test_that("estimate_slopes, works with glmmTMB and splines", {
172172
skip_if_not_installed("mgcv")
173173
data(iris)
174174
model <- mgcv::gam(Sepal.Width ~ s(Petal.Length, by = Species), data = iris)

tests/testthat/test-plot-slopes.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,23 @@ test_that("plot slopes, correct y axis labels", {
2424
plot(slopes)
2525
)
2626
})
27+
28+
29+
test_that("estimate_slopes, plotting works with brms", {
30+
skip_if_not_installed("brms")
31+
skip_if_not_installed("BH")
32+
skip_if_not_installed("RcppEigen")
33+
skip_if_not_installed("curl")
34+
skip_if_offline()
35+
skip_if_not_installed("httr2")
36+
37+
m <- insight::download_model("brms_slopes_1")
38+
skip_if(is.null(m))
39+
40+
set.seed(123)
41+
out <- estimate_slopes(m, "Murder", by = "Illiteracy", length = 4)
42+
vdiffr::expect_doppelganger(
43+
"plot-slopes-brms-1",
44+
plot(out)
45+
)
46+
})

0 commit comments

Comments
 (0)