Skip to content

Commit

Permalink
revert deprecation of margins tidiers
Browse files Browse the repository at this point in the history
  • Loading branch information
simonpcouch committed Sep 26, 2024
1 parent 8e04637 commit 4ce9797
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 79 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ Suggests:
lmtest (>= 0.9.38),
lsmeans,
maps,
margins,
MASS,
mclust,
mediation,
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# broom (development version)

* Reverted deprecation of tidiers for objects from the margins package
now that the package is back on CRAN (#1220).

* Addressed failure in `tidy.anova()` ahead of upcoming car
release (#1215).

Expand Down
127 changes: 54 additions & 73 deletions R/margins-tidiers.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' @templateVar class margins
#' @template title_desc_tidy
#'
#' @param x A `margins` object returned from `margins::margins()`.
#' @param x A `margins` object returned from [margins::margins()].
#' @template param_confint
#' @template param_unused_dots
#'
Expand All @@ -14,73 +14,63 @@
#' Similarly, an `augment.margins()` method is not currently supported, but
#' users can simply run the underlying model to obtain the same information.
#'
# examples no longer supplied, see #1200
# @examplesIf rlang::is_installed("margins")
#
# # load libraries for models and data
# library(margins)
#
# # example 1: logit model
# mod_log <- glm(am ~ cyl + hp + wt, data = mtcars, family = binomial)
#
# # get tidied "naive" model coefficients
# tidy(mod_log)
#
# # convert to marginal effects with margins()
# marg_log <- margins(mod_log)
#
# # get tidied marginal effects
# tidy(marg_log)
# tidy(marg_log, conf.int = TRUE)
#
# # requires running the underlying model again. quick for this example
# glance(marg_log)
#
# # augmenting `margins` outputs isn't supported, but
# # you can get the same info by running on the underlying model
# augment(mod_log)
#
# # example 2: threeway interaction terms
# mod_ie <- lm(mpg ~ wt * cyl * disp, data = mtcars)
#
# # get tidied "naive" model coefficients
# tidy(mod_ie)
#
# # convert to marginal effects with margins()
# marg_ie0 <- margins(mod_ie)
# # get tidied marginal effects
# tidy(marg_ie0)
# glance(marg_ie0)
#
# # marginal effects evaluated at specific values of a variable (here: cyl)
# marg_ie1 <- margins(mod_ie, at = list(cyl = c(4,6,8)))
#
# # summarize model fit with tidiers
# tidy(marg_ie1)
#
# # marginal effects of one interaction variable (here: wt), modulated at
# # specific values of the two other interaction variables (here: cyl and drat)
# marg_ie2 <- margins(mod_ie,
# variables = "wt",
# at = list(cyl = c(4,6,8), drat = c(3, 3.5, 4)))
#
# # summarize model fit with tidiers
# tidy(marg_ie2)
#
#' @examplesIf rlang::is_installed("margins")
#'
#' # load libraries for models and data
#' library(margins)
#'
#' # example 1: logit model
#' mod_log <- glm(am ~ cyl + hp + wt, data = mtcars, family = binomial)
#'
#' # get tidied "naive" model coefficients
#' tidy(mod_log)
#'
#' # convert to marginal effects with margins()
#' marg_log <- margins(mod_log)
#'
#' # get tidied marginal effects
#' tidy(marg_log)
#' tidy(marg_log, conf.int = TRUE)
#'
#' # requires running the underlying model again. quick for this example
#' glance(marg_log)
#'
#' # augmenting `margins` outputs isn't supported, but
#' # you can get the same info by running on the underlying model
#' augment(mod_log)
#'
#' # example 2: threeway interaction terms
#' mod_ie <- lm(mpg ~ wt * cyl * disp, data = mtcars)
#'
#' # get tidied "naive" model coefficients
#' tidy(mod_ie)
#'
#' # convert to marginal effects with margins()
#' marg_ie0 <- margins(mod_ie)
#' # get tidied marginal effects
#' tidy(marg_ie0)
#' glance(marg_ie0)
#'
#' # marginal effects evaluated at specific values of a variable (here: cyl)
#' marg_ie1 <- margins(mod_ie, at = list(cyl = c(4,6,8)))
#'
#' # summarize model fit with tidiers
#' tidy(marg_ie1)
#'
#' # marginal effects of one interaction variable (here: wt), modulated at
#' # specific values of the two other interaction variables (here: cyl and drat)
#' marg_ie2 <- margins(mod_ie,
#' variables = "wt",
#' at = list(cyl = c(4,6,8), drat = c(3, 3.5, 4)))
#'
#' # summarize model fit with tidiers
#' tidy(marg_ie2)
#'
#' @export
#' @aliases margins_tidiers
#' @family margins tidiers
#' @seealso [tidy()], `margins::margins()`
#' @seealso [tidy()], [margins::margins()]
tidy.margins <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
lifecycle::deprecate_soft(
"1.0.6",
I("tidying a `margins` object"),
details = c(
"i" = "margins was removed from CRAN in April 2024 and its tidiers are thus untested.",
"!" = "Please interpret output with caution."
)
)

check_ellipses("exponentiate", "tidy", "margins", ...)

ret <- as_tibble(summary(x, level = conf.level))
Expand Down Expand Up @@ -147,15 +137,6 @@ tidy.margins <- function(x, conf.int = FALSE, conf.level = 0.95, ...) {
#'
#' @export
glance.margins <- function(x, ...) {
lifecycle::deprecate_soft(
"1.0.6",
I("tidying a `margins` object"),
details = c(
"i" = "margins was removed from CRAN in April 2024 and its tidiers are thus untested.",
"!" = "Please interpret output with caution."
)
)

orig_mod_call <- attributes(x)$call
ret <- broom::glance(eval(orig_mod_call), ...)
return(ret)
Expand Down
56 changes: 55 additions & 1 deletion man/glance.margins.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 56 additions & 2 deletions man/tidy.margins.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions tests/testthat/test-margins.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ skip_on_cran()
skip_if_not_installed("modeltests")
library(modeltests)

skip("margins not available (#1200)")
# skip_if_not_installed("margins")
# library(margins)
skip_if_not_installed("margins")
library(margins)

fit1 <- glm(am ~ cyl + hp + wt, data = mtcars, family = binomial)
marg1 <- margins(fit1)
Expand Down

0 comments on commit 4ce9797

Please sign in to comment.