From d4f40552b95c7cbe3abbb5538467ad09dc01e7c0 Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Thu, 9 Nov 2023 13:39:39 +0200 Subject: [PATCH 1/4] init --- NAMESPACE | 1 + NEWS.md | 2 +- R/sysdata.rda | Bin 1342 -> 1369 bytes R/vars_ratio.R | 147 ++++++++++++++++++++++++++++++++++++++ _pkgdown.yml | 1 + data-raw/es_info.R | 6 +- man/variance_ratio.Rd | 123 +++++++++++++++++++++++++++++++ tests/testthat/test-rov.R | 35 +++++++++ 8 files changed, 312 insertions(+), 3 deletions(-) create mode 100644 R/vars_ratio.R create mode 100644 man/variance_ratio.Rd create mode 100644 tests/testthat/test-rov.R diff --git a/NAMESPACE b/NAMESPACE index 233ea5b6..3f138851 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -238,6 +238,7 @@ export(t_to_w) export(tschuprows_t) export(v_to_t) export(v_to_w) +export(variance_ratio) export(vd_a) export(w_to_c) export(w_to_fei) diff --git a/NEWS.md b/NEWS.md index bfd8f3f0..0875f261 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,7 +6,7 @@ - `repeated_measures_d()` to compute standardized mean differences (SMD) for repeated measures data. - Also supported in `effectsize()` - +- `variance_ratio()` to compute variance ratios for independent or paired samples. ## Bug fixes - `nnt()` now properly accepts the `y` argument. diff --git a/R/sysdata.rda b/R/sysdata.rda index 50806d5790d96ab6b2573e8cdf00627330d48495..9c9957fc3bceab8d696b5d4afa00e1a7d4f3fa15 100644 GIT binary patch delta 1364 zcmV-a1*`hL3fT%BLRx4!F+o`-Q(3aPGeiIfcmMzY|Nm5Q=QsfW`;i?Oe_sFq0000F zO|epKD8osB0WmND000EQ0000002&Se0iXbA41fRupa3+`00001pa%c|&;T?BKmY*H z02*ij0002c0h+3S00w{n28{zC0000Q02u%P2dHoW4FCf`WB>pS00E|e0000D01A>J zOcPB^CWAs6X^6b<(^E|V9;f^NG^&KERY@b0P_;)&mj9ba z2H@GAL1iGSB}i$cmX#BcjiO0Dd=>>ti7iqN^P#9S!?(6w1f40fteU5gnHeZ(C z796G7t=n;%tgwkNR2&qOr<#_1PXjw`l{75zJZ$T;Kc0KKFNyKC+KSneCQO$xQ}~-q z*KRp%8ZhLj?iA3zEYCToDT6QRdp`E1)R?*#Z7?ud_coSREEavMr=@;+ubG)k(~i*6 z$%B_W6#(`mBq)TPe~O7>k|Or9As-DA4=o~9D5R<-DFT%uDvF?0Ng|~pDx_5>`1&mp zebob9K=%|#XrTHjRXu)I>yFsr;nQ4twNtDdbYhBk5gZ z8iiF+VvBoD3#O-Fq}X%Rux|7csP2ftPpPwOdYi9G-|2+FKS=Axr4mkm9GczvqM{ruO*jL zvT1#c7@KVcf0sBE#kPIs`+=#Qv|0P6?%TNVn%Om19m6J`_R->-fw;`cxLKX3sid*EWN4B} z!3|qbOpWGlKq4%aThQw6!J*Cqj(|*Wi2j9-SWJ#`aItr;XZ8 zaMsy1&zDQ8l#Cm;6tYD_slDi8Vljy{T_Wjqe1g(lQCv=b+;siPhJb>yO%u?TV+U z*5)p>b`ViUYq%R`q@Jxv`)W^{w;|@LdIlRlf&qJgfDTIVJF2RCA0BjpTpVFBHQq^@zQkS)_=6TFDeR){( WQmL|-Rbt%#;_gVN3KAAqCTM``K7@P# delta 1337 zcmV-91;+Z>3cd;*LRx4!F+o`-Q(4e?zyaH8e?I^K0000z zHV8;So`EqiCPqdkBLE4bCYb=hm;fUq6C+Gw8~_7A0MHo#00Te(X`lcA00Te{00E!? zXbgY=0iXah&;S4c0iXjHq^k0002c00000000lw4gdk50B8(=00E!?G|&J5 z00E!@Qb-^O&=mB_dJ_?&e?!zYJw|BKG|8qW#WN8PC~XX;ngBEYf2vhTl~qwAilnt2 zTD5=NhW^mo%@DGIRU)J`QcFsS=3!|PPUY()l&F%`B%TLTcGmXqi7`|Zab=2HK0p6x z6TMO&hMd<(5#jB$_iNeve7`05d2RQ~tnhQ{NKX;rJU#u^SAn(Le>XQD&1JUdiS)Vp zc4PPIVtlX=;fuI=Y^=knU;9pTPo&9^viS5ez(NZ(pJ_iSg~AS zr}8o~+}Pu1yNO3|rghm_T?UM0?0>D+eT^xDse>Z&^`BEB%E5Zi;<-Hgx=On%D(g5I z+8D2K&b>(_p3EW@e+d(>Q7lwMU26d!4I&RMB2_4)swF52REVl7l1ix&Qjrx+`o94Yv;tir}kNTGn08S#QY2=p7Cv%pE6n$g%dRBj;IS z`h`_dVvBo?>ytySQf(e`mJOXWs&_bk97Wh|x2V8puGebkxZ|u!6@Mv~r-Oa1r_Ux^9zkQk7+b~kfdj@yV zy@92evg3VBe;QA$-fy(CG8TO^W~a*W8rZbf`+`jzJ^ZJRZIf-8k+xZ#sNh`MHw?$% z-fy^^jVx56l$7OYDWjvk)_W>X%uPf1gzvCN|fs>H=9Lgnsf ztnV;J4Kiz$R8o$|1E4rjz+nG~_0o74SMHFdj z!!FJP2HUk}=+6aE6Yvb;d`) zc+Pg?-cnggS??H|45{`{2C2sAqHq}quGfm{=PDMneO$O14o+O*_(~Kh_y36JcsV_RE*g)EU!>uz*! z7sQ$INVzUjOA1VRFcH9VJSMQBM;;tY;nImLf4KO;b8;GwZJjYu)JHh=-Ia%uP1d8Z zWqx((=(FR<{ZAQqPsKIAvLggt$(W9nODi#5t+8CdTnl1%ST)kQI_WWQKcs}L&ZWqt;9eO zApqdJRN}y`fO!u96a^TGpl|zRhwg@9W)Q;)E|$*zwbJi<-7$BM_R1Hl`VR7fjF5&& v0|G(-W(G(TeJMdC4jzO8c;~@KPnq4U6LL@zog|4h7x=r9DZ+$?gU$oka7BD> diff --git a/R/vars_ratio.R b/R/vars_ratio.R new file mode 100644 index 00000000..9f70fffa --- /dev/null +++ b/R/vars_ratio.R @@ -0,0 +1,147 @@ +#' Ratio of Variances +#' +#' Computes the ratio of two variances of independent or paired samples. For +#' paired data, this can also be though of the ratio of marginal (squared) +#' scales of a bivariate normal distribution. For independent samples, this is a +#' convenient wrapper around [stats::var.test()]. +#' +#' @inheritParams cohens_d +#' @inheritParams means_ratio +#' +#' @details +#' +#' # Confidence (Compatibility) Intervals (CIs) +#' For independent samples, confidence intervals are estimated using +#' [stats::var.test()]. For paired samples, confidence intervals are estimated +#' using the standard parametric method (see Pitman, 1939; Morgan, 1929). +#' +#' @inheritSection effectsize_CIs CIs and Significance Tests +#' @inheritSection print.effectsize_table Plotting with `see` +#' +#' @return A data frame with the effect size (`Vars_ratio`) and its CIs +#' (`CI_low` and `CI_high`). +#' +#' @seealso [means_ratio()], [stats::var.test()] and the _Pitman-Morgan Test_. +#' +#' @references +#' - Morgan, W. A. (1939). A test for the significance of the difference between +#' the two variances in a sample from a normal bivariate population. Biometrika, +#' 31(1/2), 13-19. +#' - Pitman, E. J. G. (1939). A note on normal correlation. Biometrika, 31(1/2), 9-12. +#' +#' @examples +#' # Two Independent Samples ---------- +#' +#' x <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29) +#' y <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30) +#' +#' variance_ratio(x, y) +#' +#' # More options: +#' variance_ratio(x, y, alternative = "less") +#' variance_ratio(x, y, log = TRUE) +#' +#' # The ratio is scale invariant +#' variance_ratio(x * 3, y * 3) +#' +#' +#' +#' # Paired Samples ---------- +#' +#' sleep2 <- reshape(sleep, direction = "wide", +#' idvar = "ID", timevar = "group") +#' variance_ratio(Pair(extra.1, extra.2) ~ 1, data = sleep2) +#' +#' +#' @export +variance_ratio <- function(x, y = NULL, data = NULL, + paired = FALSE, log = FALSE, + ci = 0.95, alternative = "two.sided", + verbose = TRUE, ...) { + alternative <- .match.alt(alternative) + out <- .get_data_2_samples(x, y, data, paired = paired, verbose = verbose, ...) + x <- out[["x"]] + y <- out[["y"]] + paired <- out[["paired"]] + + if (is.null(y)) { + insight::format_error("'y' is missing.") + } + + if (paired) { + out <- .var_ratio_dep(x, y, ci, alternative) + } else { + out <- .var_ratio_ind(x, y, ci, alternative) + } + + if (log) { + i <- intersect(colnames(out), c("Vars_ratio", "CI_low", "CI_high")) + out[i] <- sapply(out[i], log) + colnames(out)[1] <- "log_Vars_ratio" + } + + out +} + +#' @keywords internal +.var_ratio_ind <- function(x, y, ci, alternative) { + conf.level <- if (is.null(ci)) 0.95 else ci + vt <- stats::var.test(x, y, alternative = alternative, conf.level = conf.level) + pars <- parameters::model_parameters(vt) + pars$CI <- ci + out <- pars[intersect(colnames(pars), c("Estimate", "CI", "CI_low", "CI_high"))] + if (is.null(ci)) { + out$CI_low <- out$CI_high <- NULL + } + colnames(out)[1] <- "Vars_ratio" + ci_method <- list(method = "F") + + class(out) <- c("effectsize_table", "see_effectsize_table", "data.frame") + .someattributes(out) <- .nlist( + paired = FALSE, ci, ci_method, alternative, + approximate = FALSE + ) + return(out) +} + +#' @keywords internal +.var_ratio_dep <- function(x, y, ci, alternative) { + v1 <- stats::var(x) + v2 <- stats::var(y) + w <- v1/v2 + out <- data.frame(Vars_ratio = w) + + if (.test_ci(ci)) { + # Add cis + out$CI <- ci + ci.level <- .adjust_ci(ci, alternative) + alpha <- 1 - ci.level + + # Or use https://www.jstor.org/stable/3701276?seq=3 ? + n <- length(x) + df <- n - 2 + r <- stats::cor(x, y) + qs <- qt(alpha / 2, df = n - 2) + K <- 1 + (2 * (1 - r ^ 2) * qs ^ 2) / (n - 2) + CONF <- w * (K + c(-1, 1) * sqrt(K^2 - 1)) + + out$CI_low <- CONF[1] + out$CI_high <- CONF[2] + ci_method <- list(method = "t") + out <- .limit_ci(out, alternative, 0, Inf) + } else { + ci_method <- alternative <- NULL + } + + + class(out) <- c("effectsize_table", "see_effectsize_table", "data.frame") + .someattributes(out) <- .nlist( + paired = TRUE, ci, ci_method, alternative, + approximate = FALSE + ) + return(out) +} + + + + diff --git a/_pkgdown.yml b/_pkgdown.yml index 4defa812..077e81fc 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -19,6 +19,7 @@ reference: - rank_biserial - p_superiority - means_ratio + - variance_ratio - subtitle: "For contingency tables" desc: > diff --git a/data-raw/es_info.R b/data-raw/es_info.R index 88044de6..4c5c413b 100644 --- a/data-raw/es_info.R +++ b/data-raw/es_info.R @@ -12,8 +12,10 @@ es_info <- tibble::tribble( "Mahalanobis_D", "Mahalanobis' D", NA, "onetail", 0, Inf, 0, "Means_ratio", "Means Ratio", NA, "twotail", 0, Inf, 1, "Means_ratio_adjusted", "Means Ratio (adj.)", NA, "twotail", 0, Inf, 1, - "log_Means_ratio", "log(Means Ratio)", NA, "twotail", 0, Inf, 1, - "log_Means_ratio_adjusted", "log(Means Ratio, adj.)", NA, "twotail", 0, Inf, 1, + "log_Means_ratio", "log(Means Ratio)", NA, "twotail", -Inf, Inf, 0, + "log_Means_ratio_adjusted", "log(Means Ratio, adj.)", NA, "twotail", -Inf, Inf, 0, + "Vars_ratio", "Variance Ratio", NA, "twotail", 0, Inf, 1, + "log_Vars_ratio", "log(Variance Ratio)", NA, "twotail", -Inf, Inf, 0, ## xtab cor "Cohens_w", "Cohen's w", NA, "onetail", 0, Inf, 0, diff --git a/man/variance_ratio.Rd b/man/variance_ratio.Rd new file mode 100644 index 00000000..92f4e1e5 --- /dev/null +++ b/man/variance_ratio.Rd @@ -0,0 +1,123 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/vars_ratio.R +\name{variance_ratio} +\alias{variance_ratio} +\title{Ratio of Variances} +\usage{ +variance_ratio( + x, + y = NULL, + data = NULL, + paired = FALSE, + log = FALSE, + ci = 0.95, + alternative = "two.sided", + verbose = TRUE, + ... +) +} +\arguments{ +\item{x, y}{A numeric vector, or a character name of one in \code{data}. +Any missing values (\code{NA}s) are dropped from the resulting vector. +\code{x} can also be a formula (see \code{\link[stats:t.test]{stats::t.test()}}), in which case \code{y} is +ignored.} + +\item{data}{An optional data frame containing the variables.} + +\item{paired}{If \code{TRUE}, the values of \code{x} and \code{y} are considered as paired. +This produces an effect size that is equivalent to the one-sample effect +size on \code{x - y}. See also \code{\link[=repeated_measures_d]{repeated_measures_d()}} for more options.} + +\item{log}{Should the log-ratio be returned? Defaults to \code{FALSE}. +Normally distributed and useful for meta-analysis.} + +\item{ci}{Confidence Interval (CI) level} + +\item{alternative}{a character string specifying the alternative hypothesis; +Controls the type of CI returned: \code{"two.sided"} (default, two-sided CI), +\code{"greater"} or \code{"less"} (one-sided CI). Partial matching is allowed (e.g., +\code{"g"}, \code{"l"}, \code{"two"}...). See \emph{One-Sided CIs} in \link{effectsize_CIs}.} + +\item{verbose}{Toggle warnings and messages on or off.} + +\item{...}{Arguments passed to or from other methods. When \code{x} is a formula, +these can be \code{subset} and \code{na.action}.} +} +\value{ +A data frame with the effect size (\code{Vars_ratio}) and its CIs +(\code{CI_low} and \code{CI_high}). +} +\description{ +Computes the ratio of two variances of independent or paired samples. For +paired data, this can also be though of the ratio of marginal (squared) +scales of a bivariate normal distribution. For independent samples, this is a +convenient wrapper around \code{\link[stats:var.test]{stats::var.test()}}. +} +\section{Confidence (Compatibility) Intervals (CIs)}{ +For independent samples, confidence intervals are estimated using +\code{\link[stats:var.test]{stats::var.test()}}. For paired samples, confidence intervals are estimated +using the standard parametric method (see Pitman, 1939; Morgan, 1929). +} + +\section{CIs and Significance Tests}{ +"Confidence intervals on measures of effect size convey all the information +in a hypothesis test, and more." (Steiger, 2004). Confidence (compatibility) +intervals and p values are complementary summaries of parameter uncertainty +given the observed data. A dichotomous hypothesis test could be performed +with either a CI or a p value. The 100 (1 - \eqn{\alpha})\% confidence +interval contains all of the parameter values for which \emph{p} > \eqn{\alpha} +for the current data and model. For example, a 95\% confidence interval +contains all of the values for which p > .05. +\cr\cr +Note that a confidence interval including 0 \emph{does not} indicate that the null +(no effect) is true. Rather, it suggests that the observed data together with +the model and its assumptions combined do not provided clear evidence against +a parameter value of 0 (same as with any other value in the interval), with +the level of this evidence defined by the chosen \eqn{\alpha} level (Rafi & +Greenland, 2020; Schweder & Hjort, 2016; Xie & Singh, 2013). To infer no +effect, additional judgments about what parameter values are "close enough" +to 0 to be negligible are needed ("equivalence testing"; Bauer & Kiesser, +1996). +} + +\section{Plotting with \code{see}}{ + +The \code{see} package contains relevant plotting functions. See the \href{https://easystats.github.io/see/articles/effectsize.html}{plotting vignette in the \code{see} package}. +} + +\examples{ +# Two Independent Samples ---------- + +x <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29) +y <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30) + +variance_ratio(x, y) + +# More options: +variance_ratio(x, y, alternative = "less") +variance_ratio(x, y, log = TRUE) + +# The ratio is scale invariant +variance_ratio(x * 3, y * 3) + + + +# Paired Samples ---------- + +sleep2 <- reshape(sleep, direction = "wide", + idvar = "ID", timevar = "group") +variance_ratio(Pair(extra.1, extra.2) ~ 1, data = sleep2) + + +} +\references{ +\itemize{ +\item Morgan, W. A. (1939). A test for the significance of the difference between +the two variances in a sample from a normal bivariate population. Biometrika, +31(1/2), 13-19. +\item Pitman, E. J. G. (1939). A note on normal correlation. Biometrika, 31(1/2), 9-12. +} +} +\seealso{ +\code{\link[=means_ratio]{means_ratio()}}, \code{\link[stats:var.test]{stats::var.test()}} and the \emph{Pitman-Morgan Test}. +} diff --git a/tests/testthat/test-rov.R b/tests/testthat/test-rov.R new file mode 100644 index 00000000..5ec46cab --- /dev/null +++ b/tests/testthat/test-rov.R @@ -0,0 +1,35 @@ +test_that("variance_ratio | independent", { + x <- c(0.878, 0.647, 0.598, 2.05, 1.06, 1.29, 1.06, 3.14, 1.29) + y <- c(1.83, 0.50, 1.62, 2.48, 1.68, 1.88, 1.55, 3.06, 1.30) + + rov1 <- variance_ratio(x, y) + rov2 <- variance_ratio(y, x) + + expect_equal(1/rov2[[1]], rov1[[1]]) + expect_equal(1/rov2$CI_low, rov1$CI_high) + expect_equal(1/rov2$CI_high, rov1$CI_low) + + lrov1 <- variance_ratio(x, y, log = TRUE) + lrov2 <- variance_ratio(y, x, log = TRUE) + + expect_equal(-lrov2[[1]], lrov1[[1]]) + expect_equal(-lrov2$CI_low, lrov1$CI_high) + expect_equal(-lrov2$CI_high, lrov1$CI_low) + + expect_error(variance_ratio(x), regexp = "missing") +}) + + +test_that("variance_ratio | paired", { + sleep2 <- reshape(sleep, direction = "wide", + idvar = "ID", timevar = "group") + pdat1 <- Pair(sleep2$extra.1, sleep2$extra.2) + pdat2 <- Pair(sleep2$extra.2, sleep2$extra.1) + + rov1 <- variance_ratio(pdat1) + rov2 <- variance_ratio(pdat2) + + expect_equal(1/rov2[[1]], rov1[[1]]) + expect_equal(1/rov2$CI_low, rov1$CI_high) + expect_equal(1/rov2$CI_high, rov1$CI_low) +}) From ec7a5db4a48546689a6cf5e44a0208048aafac14 Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Thu, 9 Nov 2023 13:50:05 +0200 Subject: [PATCH 2/4] styler --- R/vars_ratio.R | 25 ++++++++++++------------- man/variance_ratio.Rd | 15 +++++++++------ tests/testthat/test-rov.R | 18 ++++++++++-------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/R/vars_ratio.R b/R/vars_ratio.R index 9f70fffa..d3f7ffc9 100644 --- a/R/vars_ratio.R +++ b/R/vars_ratio.R @@ -1,9 +1,10 @@ #' Ratio of Variances #' #' Computes the ratio of two variances of independent or paired samples. For -#' paired data, this can also be though of the ratio of marginal (squared) +#' paired data, this can also be thought of as the ratio of marginal (squared) #' scales of a bivariate normal distribution. For independent samples, this is a -#' convenient wrapper around [stats::var.test()]. +#' convenient wrapper around [stats::var.test()] (for a paired version of this +#' test - the Pitman-Morgan test, see `PairedData::Var.test()`). #' #' @inheritParams cohens_d #' @inheritParams means_ratio @@ -21,7 +22,8 @@ #' @return A data frame with the effect size (`Vars_ratio`) and its CIs #' (`CI_low` and `CI_high`). #' -#' @seealso [means_ratio()], [stats::var.test()] and the _Pitman-Morgan Test_. +#' @seealso [means_ratio()], [stats::var.test()] and `PairedData::Var.test()` +#' for the _Pitman-Morgan Test_ for paired samples. #' #' @references #' - Morgan, W. A. (1939). A test for the significance of the difference between @@ -48,11 +50,12 @@ #' #' # Paired Samples ---------- #' -#' sleep2 <- reshape(sleep, direction = "wide", -#' idvar = "ID", timevar = "group") +#' sleep2 <- reshape(sleep, +#' direction = "wide", +#' idvar = "ID", timevar = "group" +#' ) #' variance_ratio(Pair(extra.1, extra.2) ~ 1, data = sleep2) #' -#' #' @export variance_ratio <- function(x, y = NULL, data = NULL, paired = FALSE, log = FALSE, @@ -108,7 +111,7 @@ variance_ratio <- function(x, y = NULL, data = NULL, .var_ratio_dep <- function(x, y, ci, alternative) { v1 <- stats::var(x) v2 <- stats::var(y) - w <- v1/v2 + w <- v1 / v2 out <- data.frame(Vars_ratio = w) if (.test_ci(ci)) { @@ -117,12 +120,12 @@ variance_ratio <- function(x, y = NULL, data = NULL, ci.level <- .adjust_ci(ci, alternative) alpha <- 1 - ci.level - # Or use https://www.jstor.org/stable/3701276?seq=3 ? + # Pitman 1939, pp 11 n <- length(x) df <- n - 2 r <- stats::cor(x, y) qs <- qt(alpha / 2, df = n - 2) - K <- 1 + (2 * (1 - r ^ 2) * qs ^ 2) / (n - 2) + K <- 1 + (2 * (1 - r^2) * qs^2) / (n - 2) CONF <- w * (K + c(-1, 1) * sqrt(K^2 - 1)) out$CI_low <- CONF[1] @@ -141,7 +144,3 @@ variance_ratio <- function(x, y = NULL, data = NULL, ) return(out) } - - - - diff --git a/man/variance_ratio.Rd b/man/variance_ratio.Rd index 92f4e1e5..66d1023f 100644 --- a/man/variance_ratio.Rd +++ b/man/variance_ratio.Rd @@ -49,9 +49,10 @@ A data frame with the effect size (\code{Vars_ratio}) and its CIs } \description{ Computes the ratio of two variances of independent or paired samples. For -paired data, this can also be though of the ratio of marginal (squared) +paired data, this can also be thought of as the ratio of marginal (squared) scales of a bivariate normal distribution. For independent samples, this is a -convenient wrapper around \code{\link[stats:var.test]{stats::var.test()}}. +convenient wrapper around \code{\link[stats:var.test]{stats::var.test()}} (for a paired version of this +test - the Pitman-Morgan test, see \code{PairedData::Var.test()}). } \section{Confidence (Compatibility) Intervals (CIs)}{ For independent samples, confidence intervals are estimated using @@ -104,11 +105,12 @@ variance_ratio(x * 3, y * 3) # Paired Samples ---------- -sleep2 <- reshape(sleep, direction = "wide", - idvar = "ID", timevar = "group") +sleep2 <- reshape(sleep, + direction = "wide", + idvar = "ID", timevar = "group" +) variance_ratio(Pair(extra.1, extra.2) ~ 1, data = sleep2) - } \references{ \itemize{ @@ -119,5 +121,6 @@ the two variances in a sample from a normal bivariate population. Biometrika, } } \seealso{ -\code{\link[=means_ratio]{means_ratio()}}, \code{\link[stats:var.test]{stats::var.test()}} and the \emph{Pitman-Morgan Test}. +\code{\link[=means_ratio]{means_ratio()}}, \code{\link[stats:var.test]{stats::var.test()}} and \code{PairedData::Var.test()} +for the \emph{Pitman-Morgan Test} for paired samples. } diff --git a/tests/testthat/test-rov.R b/tests/testthat/test-rov.R index 5ec46cab..27b758e0 100644 --- a/tests/testthat/test-rov.R +++ b/tests/testthat/test-rov.R @@ -5,9 +5,9 @@ test_that("variance_ratio | independent", { rov1 <- variance_ratio(x, y) rov2 <- variance_ratio(y, x) - expect_equal(1/rov2[[1]], rov1[[1]]) - expect_equal(1/rov2$CI_low, rov1$CI_high) - expect_equal(1/rov2$CI_high, rov1$CI_low) + expect_equal(1 / rov2[[1]], rov1[[1]]) + expect_equal(1 / rov2$CI_low, rov1$CI_high) + expect_equal(1 / rov2$CI_high, rov1$CI_low) lrov1 <- variance_ratio(x, y, log = TRUE) lrov2 <- variance_ratio(y, x, log = TRUE) @@ -21,15 +21,17 @@ test_that("variance_ratio | independent", { test_that("variance_ratio | paired", { - sleep2 <- reshape(sleep, direction = "wide", - idvar = "ID", timevar = "group") + sleep2 <- reshape(sleep, + direction = "wide", + idvar = "ID", timevar = "group" + ) pdat1 <- Pair(sleep2$extra.1, sleep2$extra.2) pdat2 <- Pair(sleep2$extra.2, sleep2$extra.1) rov1 <- variance_ratio(pdat1) rov2 <- variance_ratio(pdat2) - expect_equal(1/rov2[[1]], rov1[[1]]) - expect_equal(1/rov2$CI_low, rov1$CI_high) - expect_equal(1/rov2$CI_high, rov1$CI_low) + expect_equal(1 / rov2[[1]], rov1[[1]]) + expect_equal(1 / rov2$CI_low, rov1$CI_high) + expect_equal(1 / rov2$CI_high, rov1$CI_low) }) From 29e7188583f24177676f4f47cbb9b2999b84d04d Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Tue, 5 Dec 2023 13:08:28 +0200 Subject: [PATCH 3/4] fix error with missing values in rm_d --- DESCRIPTION | 2 +- R/utils_validate_input_data.R | 2 +- tests/testthat/test-rm_d.R | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 904a59c8..74feca8e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: effectsize Title: Indices of Effect Size -Version: 0.8.6.3 +Version: 0.8.6.4 Authors@R: c(person(given = "Mattan S.", family = "Ben-Shachar", diff --git a/R/utils_validate_input_data.R b/R/utils_validate_input_data.R index 3002ece0..0aa5e2f1 100644 --- a/R/utils_validate_input_data.R +++ b/R/utils_validate_input_data.R @@ -126,7 +126,7 @@ return(mf) } - if (verbose && any(tapply(mf[[1]], mf[3:2], length) > 1L)) { + if (verbose && any(tapply(mf[[1]], mf[3:2], length) > 1L, na.rm = TRUE)) { insight::format_alert( paste0("The ", method, " standardized difference requires paired data,"), "but data contains more than one observation per design cell.", diff --git a/tests/testthat/test-rm_d.R b/tests/testthat/test-rm_d.R index af94290d..86edf48e 100644 --- a/tests/testthat/test-rm_d.R +++ b/tests/testthat/test-rm_d.R @@ -27,6 +27,13 @@ test_that("rm_d | paired data", { regexp = "replications" ) + sleep[1, "extra"] <- NA + sleep2[1, "extra.1"] <- NA + + expect_no_error(d1NA <- rm_d(extra ~ group | ID, data = sleep)) + expect_no_error(d2NA <- rm_d(Pair(extra.1, extra.2) ~ 1, data = sleep2)) + expect_equal(d1NA, d2NA) + # equal with equal variance: dat <- data.frame( From 2b8c634a0147959f440779583ffa1bead350abaa Mon Sep 17 00:00:00 2001 From: "Mattan S. Ben-Shachar" Date: Tue, 5 Dec 2023 13:08:45 +0200 Subject: [PATCH 4/4] Revert "fix error with missing values in rm_d" This reverts commit 29e7188583f24177676f4f47cbb9b2999b84d04d. --- DESCRIPTION | 2 +- R/utils_validate_input_data.R | 2 +- tests/testthat/test-rm_d.R | 7 ------- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 74feca8e..904a59c8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: effectsize Title: Indices of Effect Size -Version: 0.8.6.4 +Version: 0.8.6.3 Authors@R: c(person(given = "Mattan S.", family = "Ben-Shachar", diff --git a/R/utils_validate_input_data.R b/R/utils_validate_input_data.R index 0aa5e2f1..3002ece0 100644 --- a/R/utils_validate_input_data.R +++ b/R/utils_validate_input_data.R @@ -126,7 +126,7 @@ return(mf) } - if (verbose && any(tapply(mf[[1]], mf[3:2], length) > 1L, na.rm = TRUE)) { + if (verbose && any(tapply(mf[[1]], mf[3:2], length) > 1L)) { insight::format_alert( paste0("The ", method, " standardized difference requires paired data,"), "but data contains more than one observation per design cell.", diff --git a/tests/testthat/test-rm_d.R b/tests/testthat/test-rm_d.R index 86edf48e..af94290d 100644 --- a/tests/testthat/test-rm_d.R +++ b/tests/testthat/test-rm_d.R @@ -27,13 +27,6 @@ test_that("rm_d | paired data", { regexp = "replications" ) - sleep[1, "extra"] <- NA - sleep2[1, "extra.1"] <- NA - - expect_no_error(d1NA <- rm_d(extra ~ group | ID, data = sleep)) - expect_no_error(d2NA <- rm_d(Pair(extra.1, extra.2) ~ 1, data = sleep2)) - expect_equal(d1NA, d2NA) - # equal with equal variance: dat <- data.frame(