R/ges_partial_ss_mix.R

Defines functions ges.partial.SS.mix ges_partial_ss_mix

Documented in ges_partial_ss_mix ges.partial.SS.mix

#' \eqn{\eta^2_{G}} (Partial Generalized Eta-Squared) for
#' Mixed Design ANOVA from \eqn{F}
#'
#' This function displays partial generalized eta-squared
#' (\eqn{\eta^2_{G}}) from ANOVA analyses and its non-central
#' confidence interval based on the \eqn{F} distribution.
#' This formula works for mixed designs.
#'
#' To calculate partial generalized eta squared, first, the sum of
#' squares of the model, sum of squares of the subject
#' variance, sum of squares for the subject variance,
#' and the sum of squares for the error/residual/within are added together.
#'
#' \deqn{\eta^2_{G} = \frac{SS_M}{SS_M + SS_S + SS_E}}
#'
#' \href{https://www.aggieerin.com/shiny-server/tests/gesmixss.html}{Learn more on our example page.}
#'
#' **Note on function and output names:** This effect size is now implemented
#' with the snake_case function name `ges_partial_ss_mix()` to follow modern R
#' style guidelines. The original dotted version `ges.partial.SS.mix()` is
#' still available as a wrapper for backward compatibility, and both functions
#' return the same list. The returned object includes both the original
#' element names (e.g., `ges`, `geslow`, `geshigh`, `dfm`, `dfe`, `F`, `p`,
#' `estimate`, `statistic`) and newer snake_case aliases (e.g., `ges_value`,
#' `ges_lower_limit`, `ges_upper_limit`, `df_model`, `df_error`, `f_value`,
#' `p_value`). New code should prefer `ges_partial_ss_mix()` and the
#' snake_case output names, but existing code using the older names will
#' continue to work.
#'
#' @param dfm degrees of freedom for the model/IV/between
#' @param dfe degrees of freedom for the error/residual/within
#' @param ssm sum of squares for the model/IV/between
#' @param sss sum of squares subject variance
#' @param sse sum of squares for the error/residual/within
#' @param f_value F statistic
#' @param Fvalue Backward-compatible argument for the F statistic
#'   (deprecated; use `f_value` instead). If supplied, it overrides `f_value`.
#'   Included for users of the legacy `ges.partial.SS.mix()` API.
#' @param a significance level
#'
#' @return \describe{
#'   \item{ges}{\eqn{\eta^2_{G}} effect size}
#'   \item{geslow}{lower level confidence interval for \eqn{\eta^2_{G}}}
#'   \item{geshigh}{upper level confidence interval for \eqn{\eta^2_{G}}}
#'   \item{dfm}{degrees of freedom for the model/IV/between}
#'   \item{dfe}{degrees of freedom for the error/residual/within}
#'   \item{F}{\eqn{F}-statistic}
#'   \item{p}{p-value}
#'   \item{estimate}{the \eqn{\eta^2_{G}} statistic and confidence
#' interval in APA style for markdown printing}
#'   \item{statistic}{the \eqn{F}-statistic in APA style for markdown printing}
#' }
#'
#' @keywords effect size ges ANOVA
#' @import stats
#' @export
#' @examples
#'
#' # The following example is derived from the
#' # "mix2_data" dataset, included in the MOTE library.
#'
#' # Given previous research, we know that backward strength in free
#' # association tends to increase the ratings participants give when
#' # you ask them how many people out of 100 would say a word in
#' # response to a target word (like Family Feud). This result is
#' # tied to people’s overestimation of how well they think they know
#' # something, which is bad for studying. So, we gave people instructions
#' # on how to ignore the BSG.  Did it help? Is there an interaction
#' # between BSG and instructions given?
#'
#' # You would calculate one partial GES value for each F-statistic.
#' # Here's an example for the interaction using reported ANOVA values.
#' ges_partial_ss_mix(dfm = 1, dfe = 156,
#'                    ssm = 71.07608,
#'                    sss = 30936.498,
#'                    sse = 8657.094,
#'                    f_value = 1.280784, a = .05)
#'
#' # Backwards-compatible dotted name (deprecated)
#' ges.partial.SS.mix(dfm = 1, dfe = 156,
#'                    ssm = 71.07608,
#'                    sss = 30936.498,
#'                    sse = 8657.094,
#'                    Fvalue = 1.280784, a = .05)
#'
ges_partial_ss_mix <- function(dfm, dfe, ssm, sss,
                               sse, f_value, a = .05, Fvalue) { #nolint

  if (missing(dfm)) {
    stop("Be sure to include the degrees of freedom for the model (IV).")
  }

  if (missing(dfe)) {
    stop("Be sure to include the degrees of freedom for the error.")
  }

  if (missing(ssm)) {
    stop("Be sure to include the sum of squares for your model (IV).")
  }

  if (missing(sss)) {
    stop("Be sure to include the sum of squares for the subject variance.")
  }

  if (missing(sse)) {
    stop("Be sure to include the sum of squares for your error for the model.")
  }

  if (!missing(Fvalue)) {
    f_value <- Fvalue
  }

  if (missing(f_value)) {
    stop("Be sure to include the f_value from your ANOVA.")
  }

  if (a < 0 || a > 1) {
    stop("Alpha should be between 0 and 1.")
  }

  ges_value <- ssm / (ssm + sss + sse)

  limits <- ci_r2(
    r2 = ges_value,
    df1 = dfm,
    df2 = dfe,
    conf_level = (1 - a)
  )

  p_value <- pf(f_value, dfm, dfe, lower.tail = FALSE)

  if (p_value < .001) {
    report_p <- "< .001"
  } else {
    report_p <- paste("= ", apa(p_value, 3, FALSE), sep = "")
  }

  estimate <- paste(
    "$\\eta^2_{G}$ = ", apa(ges_value, 2, TRUE), ", ", (1 - a) * 100,
    "\\% CI [",
    apa(limits$lower_conf_limit_r2, 2, TRUE), ", ",
    apa(limits$upper_conf_limit_r2, 2, TRUE), "]",
    sep = ""
  )

  statistic <- paste(
    "$F$(", dfm, ", ", dfe, ") = ",
    apa(f_value, 2, TRUE), ", $p$ ",
    report_p,
    sep = ""
  )

  output <- list(
    # Legacy names
    ges       = ges_value,
    geslow    = limits$lower_conf_limit_r2,
    geshigh   = limits$upper_conf_limit_r2,
    dfm       = dfm,
    dfe       = dfe,
    F         = f_value,
    p         = p_value,
    estimate  = estimate,
    statistic = statistic,

    # Snake_case aliases
    ges_value        = ges_value,
    ges_lower_limit  = limits$lower_conf_limit_r2,
    ges_upper_limit  = limits$upper_conf_limit_r2,
    df_model         = dfm,
    df_error         = dfe,
    f_value          = f_value,
    p_value          = p_value
  )

  return(output)

}

# Backward compatibility wrapper
#' @rdname ges_partial_ss_mix
#' @export
ges.partial.SS.mix <- function(dfm, dfe, ssm, sss, sse, Fvalue, a = .05) { # nolint
  ges_partial_ss_mix(
    dfm    = dfm,
    dfe    = dfe,
    ssm    = ssm,
    sss    = sss,
    sse    = sse,
    f_value = Fvalue,
    a      = a
  )
}

Try the MOTE package in your browser

Any scripts or data that you put into this service are public.

MOTE documentation built on Dec. 15, 2025, 9:06 a.m.