Skip to content

Commit

Permalink
Fix #978
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 9, 2024
1 parent 22c1d85 commit e7d3ead
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 75 deletions.
17 changes: 12 additions & 5 deletions R/methods_BayesFactor.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#' # Bayesian t-test
#' model <- BayesFactor::ttestBF(x = rnorm(100, 1, 1))
#' model_parameters(model)
#' model_parameters(model, effectsize_type = "cohens_d", ci = 0.9)
#' model_parameters(model, es_type = "cohens_d", ci = 0.9)
#'
#' # Bayesian contingency table analysis
#' data(raceDolls)
Expand All @@ -46,7 +46,7 @@
#' centrality = "mean",
#' dispersion = TRUE,
#' verbose = FALSE,
#' effectsize_type = "cramers_v"
#' es_type = "cramers_v"
#' )
#' }
#' @return A data frame of indices related to the model's parameters.
Expand All @@ -60,9 +60,10 @@ model_parameters.BFBayesFactor <- function(model,
rope_range = "default",
rope_ci = 0.95,
priors = TRUE,
effectsize_type = NULL,
es_type = NULL,
include_proportions = FALSE,
verbose = TRUE,
effectsize_type = NULL,
...) {
insight::check_if_installed("BayesFactor")

Expand All @@ -83,6 +84,12 @@ model_parameters.BFBayesFactor <- function(model,
return(NULL)
}

## TODO: remove deprecation warning later
if (!is.null(effectsize_type)) {
insight::format_warning("Argument `effectsize_type` is deprecated. Use `es_type` instead.")
es_type <- effectsize_type
}

out <- bayestestR::describe_posterior(
model,
centrality = centrality,
Expand Down Expand Up @@ -132,7 +139,7 @@ model_parameters.BFBayesFactor <- function(model,
}

# Effect size?
if (!is.null(effectsize_type)) {
if (!is.null(es_type)) {
# needs {effectsize} to be installed
insight::check_if_installed("effectsize")

Expand All @@ -144,7 +151,7 @@ model_parameters.BFBayesFactor <- function(model,
dispersion = dispersion,
ci_method = ci_method,
rope_ci = rope_ci,
type = effectsize_type,
type = es_type,
...
)

Expand Down
34 changes: 21 additions & 13 deletions R/methods_aov.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param model Object of class [aov()], [anova()],
#' `aovlist`, `Gam`, [manova()], `Anova.mlm`,
#' `afex_aov` or `maov`.
#' @param effectsize_type The effect size of interest. Not that possibly not all
#' @param es_type The effect size of interest. Not that possibly not all
#' effect sizes are applicable to the model object. See 'Details'. For Anova
#' models, can also be a character vector with multiple effect size names.
#' @param df_error Denominator degrees of freedom (or degrees of freedom of the
Expand All @@ -18,7 +18,7 @@
#' ANOVA-tables using `car::Anova()` will be returned. (Ignored for
#' `afex_aov`.)
#' @param ci Confidence Interval (CI) level for effect sizes specified in
#' `effectsize_type`. The default, `NULL`, will compute no confidence
#' `es_type`. The default, `NULL`, will compute no confidence
#' intervals. `ci` should be a scalar between 0 and 1.
#' @param test String, indicating the type of test for `Anova.mlm` to be
#' returned. If `"multivariate"` (or `NULL`), returns the summary of
Expand All @@ -35,6 +35,7 @@
#' (e.g., `"g"`, `"l"`, `"two"`...). See section *One-Sided CIs* in
#' the [effectsize_CIs vignette](https://easystats.github.io/effectsize/).
#' @inheritParams model_parameters.default
#' @param effectsize_type Deprecated. Use `es_type` instead.
#' @param ... Arguments passed to [`effectsize::effectsize()`]. For example,
#' to calculate _partial_ effect sizes types, use `partial = TRUE`. For objects
#' of class `htest` or `BFBayesFactor`, `adjust = TRUE` can be used to return
Expand Down Expand Up @@ -65,13 +66,13 @@
#' model <- aov(Sepal.Length ~ Sepal.Big, data = df)
#' model_parameters(model)
#'
#' model_parameters(model, effectsize_type = c("omega", "eta"), ci = 0.9)
#' model_parameters(model, es_type = c("omega", "eta"), ci = 0.9)
#'
#' model <- anova(lm(Sepal.Length ~ Sepal.Big, data = df))
#' model_parameters(model)
#' model_parameters(
#' model,
#' effectsize_type = c("omega", "eta", "epsilon"),
#' es_type = c("omega", "eta", "epsilon"),
#' alternative = "greater"
#' )
#'
Expand All @@ -91,7 +92,7 @@
#' # parameters table including effect sizes
#' model_parameters(
#' model,
#' effectsize_type = "eta",
#' es_type = "eta",
#' ci = 0.9,
#' df_error = dof_satterthwaite(mm)[2:3]
#' )
Expand All @@ -104,16 +105,23 @@ model_parameters.aov <- function(model,
alternative = NULL,
test = NULL,
power = FALSE,
effectsize_type = NULL,
es_type = NULL,
keep = NULL,
drop = NULL,
table_wide = FALSE,
verbose = TRUE,
effectsize_type = NULL,
...) {
# save model object, for later checks
original_model <- model
object_name <- insight::safe_deparse_symbol(substitute(model))

## TODO: remove deprecation warning later
if (!is.null(effectsize_type)) {
insight::format_warning("Argument `effectsize_type` is deprecated. Use `es_type` instead.")
es_type <- effectsize_type
}

if (inherits(model, "aov") && !is.null(type) && type > 1) {
if (requireNamespace("car", quietly = TRUE)) {
model <- car::Anova(model, type = type)
Expand Down Expand Up @@ -144,7 +152,7 @@ model_parameters.aov <- function(model,
params <- .effectsizes_for_aov(
model,
params = params,
effectsize_type = effectsize_type,
es_type = es_type,
df_error = df_error,
ci = ci,
alternative = alternative,
Expand Down Expand Up @@ -250,7 +258,7 @@ model_parameters.aovlist <- model_parameters.aov
#' @rdname model_parameters.aov
#' @export
model_parameters.afex_aov <- function(model,
effectsize_type = NULL,
es_type = NULL,
df_error = NULL,
type = NULL,
keep = NULL,
Expand All @@ -270,7 +278,7 @@ model_parameters.afex_aov <- function(model,
out <- .effectsizes_for_aov(
model,
params = out,
effectsize_type = effectsize_type,
es_type = es_type,
df_error = df_error,
verbose = verbose,
...
Expand Down Expand Up @@ -432,19 +440,19 @@ model_parameters.maov <- model_parameters.aov

.effectsizes_for_aov <- function(model,
params,
effectsize_type = NULL,
es_type = NULL,
df_error = NULL,
ci = NULL,
alternative = NULL,
verbose = TRUE,
...) {
# user actually does not want to compute effect sizes
if (is.null(effectsize_type)) {
if (is.null(es_type)) {
return(params)
}

# is valid effect size?
if (!all(effectsize_type %in% c("eta", "omega", "epsilon", "f", "f2"))) {
if (!all(es_type %in% c("eta", "omega", "epsilon", "f", "f2"))) {
return(params)
}

Expand All @@ -462,7 +470,7 @@ model_parameters.maov <- model_parameters.aov
}

# multiple effect sizes possible
for (es in effectsize_type) {
for (es in es_type) {
fx <- effectsize::effectsize(
model,
type = es,
Expand Down
37 changes: 22 additions & 15 deletions R/methods_htest.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
#' model_parameters(model)
#'
#' model <- t.test(iris$Sepal.Width, iris$Sepal.Length)
#' model_parameters(model, effectsize_type = "hedges_g")
#' model_parameters(model, es_type = "hedges_g")
#'
#' model <- t.test(mtcars$mpg ~ mtcars$vs)
#' model_parameters(model, effectsize_type = "hedges_g")
#' model_parameters(model, es_type = "hedges_g")
#'
#' model <- t.test(iris$Sepal.Width, mu = 1)
#' model_parameters(model, effectsize_type = "cohens_d")
#' model_parameters(model, es_type = "cohens_d")
#'
#' data(airquality)
#' airquality$Month <- factor(airquality$Month, labels = month.abb[5:9])
Expand All @@ -35,7 +35,7 @@
#' model_parameters(model)
#'
#' model <- suppressWarnings(chisq.test(table(mtcars$am, mtcars$cyl)))
#' model_parameters(model, effectsize_type = "cramers_v")
#' model_parameters(model, es_type = "cramers_v")
#'
#' @return A data frame of indices related to the model's parameters.
#'
Expand All @@ -44,15 +44,22 @@ model_parameters.htest <- function(model,
ci = 0.95,
alternative = NULL,
bootstrap = FALSE,
effectsize_type = NULL,
es_type = NULL,
verbose = TRUE,
effectsize_type = NULL,
...) {
## TODO: remove deprecation warning later
if (!is.null(effectsize_type)) {
insight::format_warning("Argument `effectsize_type` is deprecated. Use `es_type` instead.")
es_type <- effectsize_type
}

if (bootstrap) {
insight::format_error("Bootstrapped h-tests are not yet implemented.")
} else {
parameters <- .extract_parameters_htest(
model,
effectsize_type = effectsize_type,
es_type = es_type,
ci = ci,
alternative = alternative,
verbose = verbose,
Expand Down Expand Up @@ -121,7 +128,7 @@ model_parameters.svytable <- function(model, verbose = TRUE, ...) {

#' @keywords internal
.extract_parameters_htest <- function(model,
effectsize_type = NULL,
es_type = NULL,
ci = 0.95,
alternative = NULL,
verbose = TRUE,
Expand Down Expand Up @@ -164,7 +171,7 @@ model_parameters.svytable <- function(model, verbose = TRUE, ...) {

out <- .add_effectsize_htest(model,
out,
effectsize_type = effectsize_type,
es_type = es_type,
ci = ci,
alternative = alternative,
verbose = verbose,
Expand Down Expand Up @@ -568,20 +575,20 @@ model_parameters.svytable <- function(model, verbose = TRUE, ...) {

.add_effectsize_htest <- function(model,
out,
effectsize_type = NULL,
es_type = NULL,
ci = 0.95,
alternative = NULL,
verbose = TRUE,
...) {
# check if effect sizes are requested
if (!requireNamespace("effectsize", quietly = TRUE) || is.null(effectsize_type)) {
if (!requireNamespace("effectsize", quietly = TRUE) || is.null(es_type)) {
return(out)
}

# return on invalid options. We may have partial matching with argument
# `effects` for `effectsize_type`, and thus all "effects" options should be
# `effects` for `es_type`, and thus all "effects" options should be
# ignored.
if (effectsize_type %in% c("fixed", "random", "all")) {
if (es_type %in% c("fixed", "random", "all")) {
return(out)
}

Expand All @@ -590,7 +597,7 @@ model_parameters.svytable <- function(model, verbose = TRUE, ...) {
{
effectsize::effectsize(
model,
type = effectsize_type,
type = es_type,
ci = ci,
alternative = alternative,
verbose = verbose,
Expand All @@ -600,7 +607,7 @@ model_parameters.svytable <- function(model, verbose = TRUE, ...) {
error = function(e) {
if (verbose) {
msg <- c(
paste0("Could not compute effectsize ", effectsize::get_effectsize_label(effectsize_type), "."),
paste0("Could not compute effectsize ", effectsize::get_effectsize_label(es_type), "."),
paste0("Possible reason: ", e$message)
)
insight::format_alert(msg)
Expand All @@ -616,7 +623,7 @@ model_parameters.svytable <- function(model, verbose = TRUE, ...) {
## TODO: check if effectsize prefixes are correct @mattansb

# Find prefix for CI-columns
prefix <- switch(effectsize_type,
prefix <- switch(es_type,
cohens_g = "Cohens_",
cramers_v = "Cramers_",
phi = "phi_",
Expand Down
11 changes: 9 additions & 2 deletions R/methods_other.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@ degrees_of_freedom.complmrob <- function(model, method = "wald", ...) {
#' @inheritParams model_parameters.aov
#' @export
model_parameters.Gam <- function(model,
effectsize_type = NULL,
es_type = NULL,
df_error = NULL,
type = NULL,
table_wide = FALSE,
verbose = TRUE,
effectsize_type = NULL,
...) {
## TODO: remove deprecation warning later
if (!is.null(effectsize_type)) {
insight::format_warning("Argument `effectsize_type` is deprecated. Use `es_type` instead.")
es_type <- effectsize_type
}

model_parameters(
summary(model)$parametric.anova,
effectsize_type = effectsize_type,
es_type = es_type,
df_error = df_error,
type = type,
table_wide = table_wide,
Expand Down
11 changes: 7 additions & 4 deletions man/model_parameters.BFBayesFactor.Rd

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

Loading

0 comments on commit e7d3ead

Please sign in to comment.