From afdd732097558ef519a5ebb2d806c8fdc3505a91 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 10 Jan 2025 16:21:52 +0100 Subject: [PATCH] Make functions pipe-friendly Fixes #304 --- NAMESPACE | 3 +++ R/estimate_contrasts.R | 46 +++++++++++++++++++++++++++++---------- man/estimate_contrasts.Rd | 9 +++++--- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index f1ed31172..27e35f23e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,9 @@ S3method(describe_nonlinear,data.frame) S3method(describe_nonlinear,estimate_predicted) S3method(describe_nonlinear,numeric) +S3method(estimate_contrasts,default) +S3method(estimate_contrasts,estimate_means) +S3method(estimate_contrasts,estimate_slopes) S3method(format,estimate_contrasts) S3method(format,estimate_grouplevel) S3method(format,estimate_means) diff --git a/R/estimate_contrasts.R b/R/estimate_contrasts.R index 6cbce4ea9..0ac17fa5b 100644 --- a/R/estimate_contrasts.R +++ b/R/estimate_contrasts.R @@ -98,17 +98,24 @@ #' #' @return A data frame of estimated contrasts. #' @export -estimate_contrasts <- function(model, - contrast = NULL, - by = NULL, - predict = NULL, - ci = 0.95, - p_adjust = "holm", - comparison = "pairwise", - backend = getOption("modelbased_backend", "emmeans"), - transform = NULL, - verbose = TRUE, - ...) { +estimate_contrasts <- function(model, ...) { + UseMethod("estimate_contrasts") +} + + +#' @export +#' @rdname estimate_contrasts +estimate_contrasts.default <- function(model, + contrast = NULL, + by = NULL, + predict = NULL, + ci = 0.95, + p_adjust = "holm", + comparison = "pairwise", + backend = getOption("modelbased_backend", "emmeans"), + transform = NULL, + verbose = TRUE, + ...) { ## TODO: remove deprecation warning later if (!is.null(transform)) { insight::format_warning("Argument `transform` is deprecated. Please use `predict` instead.") @@ -173,3 +180,20 @@ estimate_contrasts <- function(model, class(out) <- c("estimate_contrasts", "see_estimate_contrasts", class(out)) out } + + +#' @export +estimate_contrasts.estimate_means <- function(model, ...) { + x <- attributes(model)$model + contrast <- attributes(model)$by + # prepare dots - `by` must be removed + dot_args <- list(...) + dot_args$by <- NULL + # function arguments + fun_args <- c(list(x, contrast = contrast), dot_args) + # call contrasts function + do.call(estimate_contrasts, insight::compact_list(fun_args)) +} + +#' @export +estimate_contrasts.estimate_slopes <- estimate_contrasts.estimate_means diff --git a/man/estimate_contrasts.Rd b/man/estimate_contrasts.Rd index b0787df97..a74428201 100644 --- a/man/estimate_contrasts.Rd +++ b/man/estimate_contrasts.Rd @@ -2,9 +2,12 @@ % Please edit documentation in R/estimate_contrasts.R \name{estimate_contrasts} \alias{estimate_contrasts} +\alias{estimate_contrasts.default} \title{Estimate Marginal Contrasts} \usage{ -estimate_contrasts( +estimate_contrasts(model, ...) + +\method{estimate_contrasts}{default}( model, contrast = NULL, by = NULL, @@ -21,6 +24,8 @@ estimate_contrasts( \arguments{ \item{model}{A statistical model.} +\item{...}{Other arguments passed for instance to \code{\link[insight:get_datagrid]{insight::get_datagrid()}}.} + \item{contrast}{A character vector indicating the name of the variable(s) for which to compute the contrasts.} @@ -108,8 +113,6 @@ as default backend.} \item{transform}{Deprecated, please use \code{predict} instead.} \item{verbose}{Use \code{FALSE} to silence messages and warnings.} - -\item{...}{Other arguments passed for instance to \code{\link[insight:get_datagrid]{insight::get_datagrid()}}.} } \value{ A data frame of estimated contrasts.