From 18787723d5ba6423f58a57b49fc21a1fd43c8f20 Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 9 Oct 2024 15:00:59 +0200 Subject: [PATCH] Digit arguments ignored for objects created with `marginaleffects::hypotheses()` Fixes #1024 --- NEWS.md | 5 +++++ R/methods_marginaleffects.R | 18 ++++++++++++++++++ tests/testthat/_snaps/marginaleffects.md | 11 +++++++++++ tests/testthat/test-marginaleffects.R | 10 ++++++++++ 4 files changed, 44 insertions(+) create mode 100644 tests/testthat/_snaps/marginaleffects.md diff --git a/NEWS.md b/NEWS.md index 1605c740e..7d5eaa314 100644 --- a/NEWS.md +++ b/NEWS.md @@ -34,6 +34,11 @@ * Improved printing for `simulate_parameters()` for models from packages *mclogit*. +## Bug fixes + +* Arguments like `digits` etc. were ignored in `model_parameters() for objects + from the *marginaleffects* package. + # parameters 0.22.2 ## New supported models diff --git a/R/methods_marginaleffects.R b/R/methods_marginaleffects.R index 803970fa1..8d21acbea 100644 --- a/R/methods_marginaleffects.R +++ b/R/methods_marginaleffects.R @@ -45,6 +45,15 @@ model_parameters.marginaleffects <- function(model, # exponentiate coefficients and SE/CI, if requested out <- .exponentiate_parameters(out, model = NULL, exponentiate) + # add further information as attributes + out <- .add_model_parameters_attributes( + out, + model = model, + ci = ci, + exponentiate = exponentiate, + ... + ) + class(out) <- c("parameters_model", "see_parameters_model", class(out)) out } @@ -97,6 +106,15 @@ model_parameters.predictions <- function(model, # exponentiate coefficients and SE/CI, if requested out <- .exponentiate_parameters(out, model = NULL, exponentiate) + # add further information as attributes + out <- .add_model_parameters_attributes( + out, + model = model, + ci = ci, + exponentiate = exponentiate, + ... + ) + class(out) <- c("parameters_model", "see_parameters_model", class(out)) out } diff --git a/tests/testthat/_snaps/marginaleffects.md b/tests/testthat/_snaps/marginaleffects.md new file mode 100644 index 000000000..816061db9 --- /dev/null +++ b/tests/testthat/_snaps/marginaleffects.md @@ -0,0 +1,11 @@ +# digits and ci_digits for marginaleffects + + Code + out + Output + # Fixed Effects + + Parameter | Coefficient | SE | Statistic | p | S | 95% CI + -------------------------------------------------------------------------- + 10*wt = 0 | -53.4 | 5.6 | -9.6 | < .001 | 69.5 | [-64.4, -42.5] + diff --git a/tests/testthat/test-marginaleffects.R b/tests/testthat/test-marginaleffects.R index 538e85737..ae1df673f 100644 --- a/tests/testthat/test-marginaleffects.R +++ b/tests/testthat/test-marginaleffects.R @@ -102,3 +102,13 @@ test_that("model_parameters defaults to FALSE: Issue #916", { out2 <- model_parameters(pred, exponentiate = FALSE) expect_equal(out1$Predicted, out2$Predicted, tolerance = 1e-4) }) + + +test_that("digits and ci_digits for marginaleffects", { + data(mtcars) + skip_if(getRversion() < "4.2.0") + out <- lm(mpg ~ wt, data = mtcars) |> + marginaleffects::hypotheses(hypothesis = "10*wt = 0") |> + model_parameters(digits = 1) + expect_snapshot(out) +})