-
-
Notifications
You must be signed in to change notification settings - Fork 22
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
rempsyc
wants to merge
20
commits into
main
Choose a base branch
from
estimate_contrasts_cohens_d
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
31f1596
add effect size (cohen's d) to estimate_contrasts
rempsyc d439676
`estimate_contrasts`: now supports optional standardized effect sizes…
rempsyc eadb798
update docs [skip ci]
rempsyc 7d0c5bc
add method "marginal"
rempsyc 219935d
Buff details of methods [skip ci]
rempsyc 719f546
update docs [skip ci]
rempsyc 0c22e29
rewrite as "difference * (1- R2)/ sigma", rename as "partial_effect_s…
rempsyc a3aad0d
rewrite output column as "partial_d" when using emmeans [skip ci]
rempsyc 7ebdc3c
Merge branch 'main' into estimate_contrasts_cohens_d
strengejacke 2fbce7a
Merge branch 'main' into estimate_contrasts_cohens_d
strengejacke d1e0fad
descr
strengejacke e96d5eb
Merge branch 'main' into estimate_contrasts_cohens_d
strengejacke 683f2fb
fix
strengejacke 5f8f625
fix
strengejacke 0f6ee12
fix
strengejacke 9dbcbc6
use insight functions
strengejacke 0140e20
typos, document
rempsyc 7078a7e
Merge branch 'main' into estimate_contrasts_cohens_d
rempsyc f2d742e
Update R/estimate_contrasts_effecsize.R
rempsyc 4ff060f
Update R/estimate_contrasts_effecsize.R
rempsyc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,12 @@ Authors@R: | |
family = "Patil", | ||
role = "aut", | ||
email = "[email protected]", | ||
comment = c(ORCID = "0000-0003-1995-6531"))) | ||
comment = c(ORCID = "0000-0003-1995-6531")), | ||
person(given = "Rémi", | ||
family = "Thériault", | ||
role = c("aut"), | ||
email = "[email protected]", | ||
comment = c(ORCID = "0000-0003-4315-6788"))) | ||
Maintainer: Dominique Makowski <[email protected]> | ||
Description: Implements a general interface for model-based estimations | ||
for a wide variety of models, used in the computation of | ||
|
@@ -67,6 +72,7 @@ Suggests: | |
rmarkdown, | ||
rstanarm, | ||
rtdists, | ||
bootES, | ||
see (>= 0.8.4), | ||
testthat (>= 3.2.1) | ||
VignetteBuilder: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,49 @@ | |
#' comparisons. Can be one of `"holm"` (default), `"tukey"`, `"hochberg"`, | ||
#' `"hommel"`, `"bonferroni"`, `"BH"`, `"BY"`, `"fdr"` or `"none"`. See the | ||
#' p-value adjustment section in the `emmeans::test` documentation. | ||
#' @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 | ||
#' 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 <- difference * (1- R2)/ sigma`. This standardized | ||
#' 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 all(insight::check_if_installed(c("lme4", "emmeans", "rstanarm"), quietly = TRUE)) | ||
#' \dontrun{ | ||
#' # Basic usage | ||
|
@@ -78,6 +118,9 @@ estimate_contrasts <- function(model, | |
ci = 0.95, | ||
p_adjust = "holm", | ||
method = "pairwise", | ||
effectsize = "none", | ||
bootstraps = 200, | ||
bootES_type = "cohens.d", | ||
backend = "emmeans", | ||
...) { | ||
if (backend == "emmeans") { | ||
|
@@ -90,7 +133,7 @@ estimate_contrasts <- function(model, | |
adjust = p_adjust, | ||
... | ||
) | ||
out <- .format_emmeans_contrasts(model, estimated, ci, transform, p_adjust, ...) | ||
out <- .format_emmeans_contrasts(model, estimated, ci, transform, p_adjust, effectsize, ...) | ||
info <- attributes(estimated) | ||
} else { | ||
# Marginalmeans ------------------------------------------------------------ | ||
|
@@ -138,7 +181,7 @@ estimate_contrasts <- function(model, | |
# Table formatting emmeans ---------------------------------------------------- | ||
|
||
|
||
.format_emmeans_contrasts <- function(model, estimated, ci, transform, p_adjust, ...) { | ||
.format_emmeans_contrasts <- function(model, estimated, ci, transform, p_adjust, effectsize, ...) { | ||
# Summarize and clean | ||
if (insight::model_info(model)$is_bayesian) { | ||
out <- cbind(estimated@grid, bayestestR::describe_posterior(estimated, ci = ci, verbose = FALSE, ...)) | ||
|
@@ -171,6 +214,63 @@ estimate_contrasts <- function(model, | |
cbind(level_cols, out) | ||
} | ||
|
||
# Add standardized effect size | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("partial_d", "es_CI_low", "es_CI_high") | ||
contrasts <- cbind(contrasts, eff) | ||
|
||
} else if (effectsize == "marginal") { | ||
# Original: d_adj <- t * se_b / sigma * sqrt(1 - R2_cov) | ||
# d_adj <- contrasts$t * contrasts$SE / sigma(model) * sqrt(1 - R2) | ||
# New: d_adj <- difference * (1- R2)/ sigma | ||
R2 <- summary(model)$r.squared | ||
d_adj <- contrasts$Difference * (1 - R2) / sigma(model) | ||
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 marginal effects ------------------------------------------- | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if possible why not
There was a problem hiding this comment.
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