Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add effect size (Cohen's d) to estimate_contrasts #227

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: modelbased
Title: Estimation of Model-Based Predictions, Contrasts and Means
Version: 0.8.6.3
Version: 0.8.6.4
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand All @@ -22,7 +22,12 @@ Authors@R:
family = "Patil",
role = "aut",
email = "[email protected]",
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")))
comment = c(ORCID = "0000-0003-1995-6531", Twitter = "@patilindrajeets")),
person(given = "Rémi",
family = "Thériault",
role = c("aut"),
email = "[email protected]",
comment = c(ORCID = "0000-0003-4315-6788", Twitter = "@rempsyc")))
Maintainer: Dominique Makowski <[email protected]>
Description: Implements a general interface for model-based estimations
for a wide variety of models (see list of supported models using the
Expand Down Expand Up @@ -63,12 +68,13 @@ Suggests:
rstanarm,
rtdists,
see (>= 0.7.4),
bootES,
testthat
VignetteBuilder:
knitr
Encoding: UTF-8
Language: en-US
RoxygenNote: 7.2.3.9000
RoxygenNote: 7.2.3
Config/testthat/edition: 3
Config/testthat/parallel: true
Roxygen: list(markdown = TRUE)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# modelbased (development version)

- `estimate_contrasts`: now supports optional standardized effect sizes, one of "none" (default), "emmeans", or "bootES" (#227, @rempsyc).

# modelbased 0.8.6

## Breaking Changes
Expand Down
98 changes: 98 additions & 0 deletions R/estimate_contrasts.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,49 @@
#' "bonferroni", "BH", "BY", "fdr" or "none". See the p-value adjustment
#' section in the `emmeans::test` documentation.
#' @param adjust Deprecated in favour of `p_adjust`.
#' @param effectsize Desired measure of standardized effect size, one of "none"
#' (default), "emmeans", "marginal", or "bootES".
#' @param bootES_type Specifies the type of effect-size measure to
Copy link
Member

@DominiqueMakowski DominiqueMakowski Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not document these in the main interface (to not alourdir the list of arguments) and allow their change via Kwargs, and mention them within the details section

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want this merged before submitting the paper?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible why not

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit crunched right now with faculty job applications, talks, and other deadlines but I will try

#' estimate when using `effectsize = "bootES"`. One of `c("unstandardized",
#' "cohens.d", "hedges.g", "cohens.d.sigma", "r", "akp.robust.d")`. See`
#' effect.type` argument of [bootES::bootES] for details.
#' @param bootstraps The number of bootstrap resamples to perform.
#'
#' @inherit estimate_slopes details
#'
#' @section Effect Size: By default, `estimate_contrasts` reports no
#' standardized effect size on purpose. Should one request one, some things
#' are to keep in mind. As the authors of `emmeans` write, "There is
#' substantial disagreement among practitioners on what is the appropriate
#' sigma to use in computing effect sizes; or, indeed, whether any effect-size
#' measure is appropriate for some situations. The user is completely
#' responsible for specifying appropriate parameters (or for failing to do
#' so)."
#'
#' In particular, effect size method `"bootES"` does not correct
#' for covariates in the model, so should probably only be used when there is
rempsyc marked this conversation as resolved.
Show resolved Hide resolved
#' just one categorical predictor (with however many levels). Some believe that
#' if there are multiple predictors or any covariates, it is important to
#' re-compute sigma adding back in the response variance associated with the
#' variables that aren't part of the contrast.
#'
#' `effectsize = "emmeans"` uses [emmeans::eff_size] with
rempsyc marked this conversation as resolved.
Show resolved Hide resolved
#' `sigma = stats::sigma(model)`, `edf = stats::df.residual(model)` and
#' `method = "identity")`. This standardizes using the MSE (sigma). Some believe
#' this works when the contrasts are the only predictors in the model, but not
#' when there are covariates. The response variance accounted for by the
#' covariates should not be removed from the SD used to standardize. Otherwise,
#' _d_ will be overestimated.
#'
#' `effectsize = "marginal"` uses the following formula to compute effect
#' size: `d_adj <- t * se_b / sigma * sqrt(1 - R2_cov)`. This standardized
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that t * se_b is equal to the raw difference - so this can be rewritten as difference * (1- R2)/ sigma, which I think is more clear.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also means that emmeans::eff_size() can be used here with sigma = stats::sigma(model) / (1 - R2_cov) (and some reasonable edf)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, one thing to note, is that previously I hardcoded sigma as as sigma = stats::sigma(model). Would it be preferable to add a new argument sigma with that as the default, but then people can specify a different one when using the emmeans method? And Should this argument also apply to Brenton's marginal method? (again, instead of hard coding it as sigma(model))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, in the emmeans method, edf is currently hardcoded as edf = stats::df.residual(model), and method as method = "identity". But I can add arguments for those as well (I'm always unsure whether the ellipsis three dots would work when they can be used in several functions with similar argument names, it would probably create conflict for method for example).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, in that case you're just making a wrapper around eff_size, yeah?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But I don't know what we should do (wrapper around eff_size with some defaults, or fixed values). Do you have a preference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing 😜🤷‍♂️

#' using the response SD with only the between-groups variance on the focal
#' factor/contrast removed. This allows for groups to be equated on their
#' covariates, but creates an appropriate scale for standardizing the response.
#'
#' `effectsize = "bootES"` uses bootstrapping (defaults to a low value of
#' 200) through [bootES::bootES]. Adjust for contrasts, but not for covariates.
#'
#' @examplesIf require("emmeans", quietly = TRUE)
#' # Basic usage
#' model <- lm(Sepal.Width ~ Species, data = iris)
Expand Down Expand Up @@ -83,6 +123,9 @@ estimate_contrasts <- function(model,
p_adjust = "holm",
method = "pairwise",
adjust = NULL,
effectsize = "none",
bootstraps = 200,
bootES_type = "cohens.d",
...) {
# Deprecation
if (!is.null(adjust)) {
Expand Down Expand Up @@ -135,6 +178,61 @@ estimate_contrasts <- function(model,
contrasts$contrast <- NULL
contrasts <- cbind(level_cols, contrasts)

# Add standardized effect size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we split that into its own separate internal function .estimate_contrasts_effecsize()? So that it's more encapsulated

if (!effectsize %in% c("none", "emmeans", "marginal", "bootES")) {
message("Unsupported effect size '", effectsize, "', returning none.")
}

if (effectsize == "emmeans") {
eff <- emmeans::eff_size(
estimated, sigma = stats::sigma(model),
edf = stats::df.residual(model), method = "identity")
eff <- as.data.frame(eff)
eff <- eff[c(2, 5:6)]
names(eff) <- c("effect_size", "es_CI_low", "es_CI_high")
contrasts <- cbind(contrasts, eff)

} else if (effectsize == "marginal") {
# d_adj <- t * se_b / sigma * sqrt(1 - R2_cov)
R2_cov <- summary(model)$r.squared
d_adj <- contrasts$t * contrasts$SE / sigma(model) * sqrt(1 - R2_cov)
contrasts <- cbind(contrasts, marginal_d = d_adj)

} else if (effectsize == "bootES") {
if (bootstraps < 500) {
message("Number of bootstraps probably too low. Consider increasing it.")
}

insight::check_if_installed("bootES")
dat <- insight::get_data(model)
resp <- insight::find_response(model)
group <- names([email protected]$xlev)
contrast <- estimated@misc$con.coef

contrast <- lapply(seq_len(nrow(contrast)), function(x) {
z <- contrast[x, ]
names(z) <- levels(as.factor(dat[[group]]))
z
})

es.lists <- lapply(contrast, function(x) {
y <- bootES::bootES(
data = stats::na.omit(dat),
R = bootstraps,
data.col = resp,
group.col = group,
contrast = x,
effect.type = bootES_type
)
y <- as.data.frame(summary(y))})

eff <- do.call(rbind, es.lists)
eff <- eff[1:3]
names(eff) <- c(bootES_type, paste0(bootES_type, "_CI_low"),
paste0(bootES_type, "es_CI_high"))

contrasts <- cbind(contrasts, eff)
}

# Table formatting
attr(contrasts, "table_title") <- c("Marginal Contrasts Analysis", "blue")
Expand Down
46 changes: 46 additions & 0 deletions man/estimate_contrasts.Rd

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

1 change: 1 addition & 0 deletions man/modelbased-package.Rd

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