Skip to content

Commit 6d83a77

Browse files
committed
Next fix
1 parent d3bb65b commit 6d83a77

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
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: parameters
33
Title: Processing of Model Parameters
4-
Version: 0.28.0.9
4+
Version: 0.28.0.10
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

R/methods_marginaleffects.R

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,21 @@ model_parameters.marginaleffects <- function(model,
3232
colnames(marginaleffects::components(model, "modeldata"))
3333
)
3434
# columns we want to keep
35-
by_cols <- marginaleffects::components(model, "variable_names_by")
35+
by_cols <- union(
36+
marginaleffects::components(model, "variable_names_by"),
37+
marginaleffects::components(model, "variable_names_by_hypothesis")
38+
)
39+
40+
## FIXME: hack to workaround https://github.com/vincentarelbundock/marginaleffects/issues/1573
41+
duplicated_names <- grep(
42+
paste0("(", paste0(by_cols, "\\.\\d+", collapse = "|"), ")"),
43+
colnames(tidy_model),
44+
value = TRUE
45+
)
46+
# if we have duplicated "by" columns, we want to remove those as well
47+
if (length(duplicated_names) > 0) {
48+
all_data_cols <- c(all_data_cols, duplicated_names)
49+
}
3650

3751
# remove redundant columns
3852
to_remove <- setdiff(all_data_cols, by_cols)
@@ -126,6 +140,23 @@ model_parameters.predictions <- function(model,
126140
...
127141
))
128142
} else {
143+
# columns we want to keep
144+
by_cols <- union(
145+
marginaleffects::components(model, "variable_names_by"),
146+
marginaleffects::components(model, "variable_names_by_hypothesis")
147+
)
148+
149+
## FIXME: hack to workaround https://github.com/vincentarelbundock/marginaleffects/issues/1573
150+
duplicated_names <- grep(
151+
paste0("(", paste0(by_cols, "\\.\\d+", collapse = "|"), ")"),
152+
colnames(model),
153+
value = TRUE
154+
)
155+
# if we have duplicated "by" columns, we want to remove those as well
156+
if (length(duplicated_names) > 0) {
157+
model[duplicated_names] <- NULL
158+
}
159+
129160
# handle non-Bayesian models
130161
out <- .rename_reserved_marginaleffects(model)
131162
out <- datawizard::data_rename(out, "estimate", "predicted")

tests/testthat/test-marginaleffects.R

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ test_that("predictions, bmrs with special response formula", {
181181
skip_if_offline()
182182
skip_if_not_installed("httr2")
183183
skip_if_not_installed("brms")
184+
skip_if_not_installed("marginaleffects", minimum_version = "0.28.0.21")
184185

185186
m <- insight::download_model("brms_ipw_1")
186187
skip_if(is.null(m))
@@ -190,6 +191,29 @@ test_that("predictions, bmrs with special response formula", {
190191
expect_identical(dim(out), c(1L, 10L))
191192
})
192193

194+
195+
test_that("modelbased, tidiers work", {
196+
skip_if_not_installed("marginaleffects", minimum_version = "0.28.0.21")
197+
skip_if_not_installed("modelbased")
198+
skip_if(getRversion() < "4.5.0")
199+
200+
data(penguins)
201+
m <- lm(bill_len ~ island * sex + bill_dep + species, data = penguins)
202+
203+
## FIXME: Need to wait for https://github.com/vincentarelbundock/marginaleffects/issues/1573
204+
out <- modelbased::estimate_contrasts(m, "island", by = "sex", comparison = ratio ~ pairwise)
205+
expect_named(
206+
out,
207+
c("Level1", "Level2", "sex", "Ratio", "SE", "CI_low", "CI_high", "t", "df", "p")
208+
)
209+
expect_identical(dim(out), c(6L, 10L))
210+
211+
out <- modelbased::estimate_contrasts(m, "island", by = "sex", comparison = ratio ~ inequality)
212+
expect_named(out, c("sex", "Mean_Ratio", "SE", "CI_low", "CI_high", "z", "p"))
213+
expect_identical(dim(out), c(2L, 7L))
214+
})
215+
216+
193217
## TODO: check this test locally
194218

195219
# Following test may fail on CI, probably due to scoping issues?
@@ -210,6 +234,7 @@ test_that("predictions, using bayestestR #1063", {
210234
skip_if_offline()
211235
skip_if_not_installed("httr2")
212236
skip_if_not_installed("brms")
237+
skip_if_not_installed("marginaleffects", minimum_version = "0.28.0.21")
213238

214239
m <- insight::download_model("brms_mixed_3")
215240
skip_if(is.null(m))

0 commit comments

Comments
 (0)