Skip to content

Commit e544983

Browse files
committed
fix
1 parent 970de0a commit e544983

File tree

4 files changed

+161
-30
lines changed

4 files changed

+161
-30
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.3.4
4+
Version: 0.28.3.5
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
* Fixed issues with Tukey-p-value adjustment for *emmeans* objects.
2121

22+
* Fixed unintended removal of columns in `model_parameters()` for objects from
23+
package *marginaleffects*. This happened, when a variable in a model was named
24+
`Type`.
25+
2226
# parameters 0.28.3
2327

2428
* fixed bug in `standardize_info(<fixest>)` that was preventing

R/methods_marginaleffects.R

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ model_parameters.marginaleffects <- function(
1818

1919
if (is_bayesian) {
2020
# Bayesian
21-
out <- suppressWarnings(bayestestR::describe_posterior(model, ci = ci, verbose = verbose, ...))
21+
out <- suppressWarnings(bayestestR::describe_posterior(
22+
model,
23+
ci = ci,
24+
verbose = verbose,
25+
...
26+
))
2227
} else {
2328
# non-Bayesian
2429
out <- as.data.frame(model)
@@ -123,7 +128,12 @@ model_parameters.predictions <- function(
123128

124129
if (is_bayesian) {
125130
# Bayesian
126-
out <- suppressWarnings(bayestestR::describe_posterior(model, ci = ci, verbose = verbose, ...))
131+
out <- suppressWarnings(bayestestR::describe_posterior(
132+
model,
133+
ci = ci,
134+
verbose = verbose,
135+
...
136+
))
127137
} else {
128138
# columns we want to keep
129139
by_cols <- .keep_me_columns(model)
@@ -141,7 +151,11 @@ model_parameters.predictions <- function(
141151
colnames(out) <- gsub("#####$", "", colnames(out))
142152

143153
# remove and reorder some columns
144-
out$rowid <- out$Type <- out$rowid_dedup <- NULL
154+
out$rowid <- out$rowid_dedup <- NULL
155+
# need to remove "Type", but only if it's not a valid variable name
156+
if (!"Type" %in% by_cols) {
157+
out$Type <- NULL
158+
}
145159

146160
# find at-variables
147161
at_variables <- insight::compact_character(c(
@@ -151,7 +165,9 @@ model_parameters.predictions <- function(
151165

152166
# find cofficient name - differs for Bayesian models
153167
coef_name <- intersect(c("Predicted", "Coefficient"), colnames(out))[1]
154-
if (!is.null(at_variables) && !is.na(coef_name) && all(at_variables %in% colnames(out))) {
168+
if (
169+
!is.null(at_variables) && !is.na(coef_name) && all(at_variables %in% colnames(out))
170+
) {
155171
out <- datawizard::data_relocate(out, select = at_variables, after = coef_name)
156172
}
157173

@@ -235,10 +251,7 @@ model_parameters.predictions <- function(
235251
)
236252
# and newdata, if specified
237253
if (!is.null(marginaleffects::components(model, "call")$newdata)) {
238-
by_cols <- union(
239-
by_cols,
240-
colnames(marginaleffects::components(model, "newdata"))
241-
)
254+
by_cols <- union(by_cols, colnames(marginaleffects::components(model, "newdata")))
242255
}
243256
by_cols
244257
}

tests/testthat/test-marginaleffects.R

Lines changed: 135 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,16 @@ test_that("marginaleffects()", {
5252
expect_named(
5353
out,
5454
c(
55-
"Parameter", "Comparison", "Coefficient", "SE", "Statistic",
56-
"p", "S", "CI", "CI_low", "CI_high"
55+
"Parameter",
56+
"Comparison",
57+
"Coefficient",
58+
"SE",
59+
"Statistic",
60+
"p",
61+
"S",
62+
"CI",
63+
"CI_low",
64+
"CI_high"
5765
)
5866
)
5967
mfx <- marginaleffects::avg_slopes(model, variables = "Petal.Length", by = "Species")
@@ -62,8 +70,17 @@ test_that("marginaleffects()", {
6270
expect_named(
6371
out,
6472
c(
65-
"Parameter", "Comparison", "Species", "Coefficient", "SE", "Statistic",
66-
"p", "S", "CI", "CI_low", "CI_high"
73+
"Parameter",
74+
"Comparison",
75+
"Species",
76+
"Coefficient",
77+
"SE",
78+
"Statistic",
79+
"p",
80+
"S",
81+
"CI",
82+
"CI_low",
83+
"CI_high"
6784
)
6885
)
6986
})
@@ -74,10 +91,21 @@ test_that("predictions()", {
7491
p <- marginaleffects::avg_predictions(x, by = "Species")
7592
out <- model_parameters(p)
7693
expect_identical(nrow(out), 3L)
77-
expect_named(out, c(
78-
"Predicted", "Species", "SE", "CI", "CI_low", "CI_high", "S", "Statistic",
79-
"df", "p"
80-
))
94+
expect_named(
95+
out,
96+
c(
97+
"Predicted",
98+
"Species",
99+
"SE",
100+
"CI",
101+
"CI_low",
102+
"CI_high",
103+
"S",
104+
"Statistic",
105+
"df",
106+
"p"
107+
)
108+
)
81109
out <- model_parameters(p, exponentiate = TRUE)
82110
expect_equal(out$Predicted, c(30.81495, 15.95863, 19.57004), tolerance = 1e-4)
83111
})
@@ -124,16 +152,28 @@ test_that("hypotheses()", {
124152
test_that("slopes()", {
125153
m <- lm(Sepal.Width ~ Species * Petal.Length, data = iris)
126154

127-
x <- marginaleffects::slopes(m,
155+
x <- marginaleffects::slopes(
156+
m,
128157
variables = "Petal.Length",
129158
newdata = insight::get_datagrid(m, by = "Species")
130159
)
131160
out <- model_parameters(x)
132161
expect_named(
133162
out,
134163
c(
135-
"rowid", "Parameter", "Comparison", "Coefficient", "SE", "Statistic",
136-
"p", "S", "CI", "CI_low", "CI_high", "Species", "Petal.Length",
164+
"rowid",
165+
"Parameter",
166+
"Comparison",
167+
"Coefficient",
168+
"SE",
169+
"Statistic",
170+
"p",
171+
"S",
172+
"CI",
173+
"CI_low",
174+
"CI_high",
175+
"Species",
176+
"Petal.Length",
137177
"Predicted"
138178
)
139179
)
@@ -160,7 +200,10 @@ test_that("multiple contrasts: Issue #779", {
160200
test_that("model_parameters defaults to FALSE: Issue #916", {
161201
data(mtcars)
162202
mod <- lm(mpg ~ wt, data = mtcars)
163-
pred <- marginaleffects::predictions(mod, newdata = marginaleffects::datagrid(wt = c(1, 2)))
203+
pred <- marginaleffects::predictions(
204+
mod,
205+
newdata = marginaleffects::datagrid(wt = c(1, 2))
206+
)
164207
out1 <- model_parameters(pred)
165208
out2 <- model_parameters(pred, exponentiate = FALSE)
166209
expect_equal(out1$Predicted, out2$Predicted, tolerance = 1e-4)
@@ -206,7 +249,11 @@ test_that("predictions, bmrs with special response formula", {
206249
m <- insight::download_model("brms_ipw_1")
207250
skip_if(is.null(m))
208251

209-
x <- marginaleffects::avg_predictions(m, variables = "treatment", hypothesis = ~pairwise)
252+
x <- marginaleffects::avg_predictions(
253+
m,
254+
variables = "treatment",
255+
hypothesis = ~pairwise
256+
)
210257
out <- model_parameters(x)
211258
expect_identical(dim(out), c(1L, 10L))
212259
})
@@ -220,7 +267,12 @@ test_that("modelbased, tidiers work", {
220267
data(penguins)
221268
m <- lm(bill_len ~ island * sex + bill_dep + species, data = penguins)
222269

223-
out <- modelbased::estimate_contrasts(m, "island", by = "sex", comparison = ratio ~ pairwise)
270+
out <- modelbased::estimate_contrasts(
271+
m,
272+
"island",
273+
by = "sex",
274+
comparison = ratio ~ pairwise
275+
)
224276
expect_named(
225277
out,
226278
c("Level1", "Level2", "sex", "Ratio", "SE", "CI_low", "CI_high", "t", "df", "p")
@@ -238,13 +290,27 @@ test_that("modelbased, tidiers work", {
238290
expect_named(
239291
params,
240292
c(
241-
"Parameter", "Predicted", "SE", "CI", "CI_low", "CI_high",
242-
"S", "Statistic", "df", "p", "sex"
293+
"Parameter",
294+
"Predicted",
295+
"SE",
296+
"CI",
297+
"CI_low",
298+
"CI_high",
299+
"S",
300+
"Statistic",
301+
"df",
302+
"p",
303+
"sex"
243304
)
244305
)
245306
expect_identical(dim(params), c(6L, 11L))
246307

247-
out <- modelbased::estimate_contrasts(m, "island", by = "sex", comparison = ratio ~ inequality)
308+
out <- modelbased::estimate_contrasts(
309+
m,
310+
"island",
311+
by = "sex",
312+
comparison = ratio ~ inequality
313+
)
248314
expect_named(out, c("sex", "Mean_Ratio", "SE", "CI_low", "CI_high", "z", "p"))
249315
expect_identical(dim(out), c(2L, 7L))
250316

@@ -255,7 +321,7 @@ test_that("modelbased, tidiers work", {
255321
newdata = datagrid,
256322
hypothesis = ratio ~ pairwise | sex
257323
)
258-
out <- marginaleffects::hypotheses(out, hypothesis = ~I(mean(abs(x))) | sex)
324+
out <- marginaleffects::hypotheses(out, hypothesis = ~ I(mean(abs(x))) | sex)
259325
params <- model_parameters(out)
260326
expect_named(
261327
params,
@@ -291,13 +357,61 @@ test_that("predictions, using bayestestR #1063", {
291357
skip_if(is.null(m))
292358

293359
d <- insight::get_datagrid(m, by = "Days", include_random = TRUE)
294-
x <- marginaleffects::avg_predictions(m, newdata = d, by = "Days", allow_new_levels = TRUE)
360+
x <- marginaleffects::avg_predictions(
361+
m,
362+
newdata = d,
363+
by = "Days",
364+
allow_new_levels = TRUE
365+
)
295366
out <- model_parameters(x)
296367
expect_named(
297368
out,
298369
c(
299-
"Median", "CI", "CI_low", "CI_high", "pd", "ROPE_CI", "ROPE_low",
300-
"ROPE_high", "ROPE_Percentage", "Days"
370+
"Median",
371+
"CI",
372+
"CI_low",
373+
"CI_high",
374+
"pd",
375+
"ROPE_CI",
376+
"ROPE_low",
377+
"ROPE_high",
378+
"ROPE_Percentage",
379+
"Days"
380+
)
381+
)
382+
})
383+
384+
385+
test_that("predictions, don't remove Type column when it's a variable", {
386+
skip_on_cran()
387+
skip_if_not_installed("MASS")
388+
389+
data(housing, package = "MASS")
390+
m1 <- MASS::polr(Sat ~ Infl + Type + Cont, data = housing, weights = Freq)
391+
emm <- marginaleffects::avg_predictions(m1, by = "Type")
392+
out <- model_parameters(emm)
393+
394+
expect_named(
395+
out,
396+
c(
397+
"Predicted",
398+
"Type",
399+
"SE",
400+
"CI",
401+
"CI_low",
402+
"CI_high",
403+
"S",
404+
"Statistic",
405+
"df",
406+
"p",
407+
"group"
301408
)
302409
)
410+
411+
skip_if_not_installed("modelbased")
412+
out <- modelbased::estimate_means(m1, by = "Type")
413+
expect_named(
414+
out,
415+
c("Type", "Response", "Probability", "SE", "CI_low", "CI_high", "t", "df")
416+
)
303417
})

0 commit comments

Comments
 (0)