Skip to content

Commit

Permalink
styler
Browse files Browse the repository at this point in the history
  • Loading branch information
mattansb committed Nov 6, 2023
1 parent 1d857b5 commit 5c726a3
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 51 deletions.
6 changes: 4 additions & 2 deletions R/effectsize.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@
#' Tt <- t.test(1:10, y = c(7:20), alternative = "less")
#' effectsize(Tt)
#'
#' sleep2 <- reshape(sleep, direction = "wide",
#' idvar = "ID", timevar = "group")
#' sleep2 <- reshape(sleep,
#' direction = "wide",
#' idvar = "ID", timevar = "group"
#' )
#' Tt <- t.test(sleep2$extra.1, sleep2$extra.2, paired = TRUE)
#' effectsize(Tt, type = "rm_b")
#'
Expand Down
33 changes: 19 additions & 14 deletions R/repeated_measures_d.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@
#' # Paired data -------
#'
#' data("sleep")
#' sleep2 <- reshape(sleep, direction = "wide",
#' idvar = "ID", timevar = "group")
#' sleep2 <- reshape(sleep,
#' direction = "wide",
#' idvar = "ID", timevar = "group"
#' )
#'
#' repeated_measures_d(Pair(extra.1, extra.2) ~ 1, data = sleep2)
#'
Expand Down Expand Up @@ -204,9 +206,10 @@ repeated_measures_d <- function(x, y,

# rename column to method
colnames(out)[1] <- switch(method,
d = "Cohens_d",
b = "Beckers_d",
paste0("d_", method))
d = "Cohens_d",
b = "Beckers_d",
paste0("d_", method)
)

class(out) <- c("effectsize_difference", "effectsize_table", "see_effectsize_table", class(out))
.someattributes(out) <- .nlist(
Expand Down Expand Up @@ -237,7 +240,7 @@ rm_d <- repeated_measures_d
d <- (m - mu) / s

# Cooper et al., 2009, eq 12.21
se <- sqrt(((1 / n) + (d ^ 2) / (2 * n)) * f)
se <- sqrt(((1 / n) + (d^2) / (2 * n)) * f)
} else if (method == "av") {
s <- sqrt((stats::var(x) + stats::var(y)) / 2)
d <- (m - mu) / s
Expand All @@ -249,13 +252,13 @@ rm_d <- repeated_measures_d
d <- (m - mu) / s

# Hedges and Olkin, 1985, page 86, eq 14
se <- sqrt((1 / n) + (d ^ 2) / (2 * n))
se <- sqrt((1 / n) + (d^2) / (2 * n))
} else if (method == "b") {
s <- stats::sd(y)
d <- (m - mu) / s

# Becker 1988, eq. 6
se <- sqrt((2 * (1 - r) / n) + (d ^ 2) / (2 * n))
se <- sqrt((2 * (1 - r) / n) + (d^2) / (2 * n))
}

.nlist(d, se, df)
Expand All @@ -270,27 +273,29 @@ rm_d <- repeated_measures_d
}

mod <- suppressWarnings(
stats::aov(y ~ condition + Error(id / condition), data = data,
contrasts = list(condition = contr.treatment))
stats::aov(y ~ condition + Error(id / condition),
data = data,
contrasts = list(condition = contr.treatment)
)
)
m <- -unname(coef(mod[["id:condition"]]))
m_V <- unname(vcov(mod[["id:condition"]])[1])

pars <- parameters::model_parameters(mod)

if (method == "d") {
e <- as.data.frame(pars[pars$Parameter == "Residuals",])
e <- as.data.frame(pars[pars$Parameter == "Residuals", ])
} else if (method == "r") {
e <- as.data.frame(pars[pars$Group == "Within",])
e <- as.data.frame(pars[pars$Group == "Within", ])
}

s <- sqrt(sum(e[["Sum_Squares"]]) / sum(e[["df"]]))
df <- sum(e[["df"]])

d <- (m - mu) / s
se <- sqrt(
(df / (df - 2)) * (m_V / (s ^ 2)) +
(d ^ 2) * (8 * df ^ 2 - df + 2) / (16 * (df - 2) * ((df - 1) ^ 2))
(df / (df - 2)) * (m_V / (s^2)) +
(d^2) * (8 * df^2 - df + 2) / (16 * (df - 2) * ((df - 1)^2))
)

.nlist(d, se, df)
Expand Down
4 changes: 2 additions & 2 deletions R/utils_validate_input_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@
}

mf <- tapply(mf[[1]], mf[3:2], mean, na.rm = TRUE)
x <- mf[,1]
y <- mf[,2]
x <- mf[, 1]
y <- mf[, 2]
} else if (x[[2]][[1]] == as.name("Pair")) {
# is Pair (wide)
mf <- .resolve_formula(x, data, ...)
Expand Down
5 changes: 3 additions & 2 deletions data-raw/df data.R
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,11 @@ save(hardlyworking, file = "data/hardlyworking.rdata")
# rouder2016 --------------------------------------------------------------

rouder2016 <- read.table("data-raw/effectSizePuzzler.txt",
header = TRUE, stringsAsFactors = FALSE)
header = TRUE, stringsAsFactors = FALSE
)
rouder2016[["id"]] <- factor(rouder2016[["id"]])
rouder2016[["cond"]] <- factor(rouder2016[["cond"]])

save(rouder2016, file = "data/rouder2016.rdata")

# styler: on
# styler: on
2 changes: 0 additions & 2 deletions data-raw/es_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ es_info <- tibble::tribble(
"Cohens_d", "Cohen's d", NA, "twotail", -Inf, Inf, 0,
"Hedges_g", "Hedges' g", NA, "twotail", -Inf, Inf, 0,
"Glass_delta", "Glass' delta", "Glass' \u0394", "twotail", -Inf, Inf, 0,

"d_rm", "d (rm)", "d\u1D63\u2098", "twotail", -Inf, Inf, 0,
"d_av", "d (av)", "d\u2090\u1D65", "twotail", -Inf, Inf, 0,
"d_z", "d (z)", NA, "twotail", -Inf, Inf, 0,
"Beckers_d", "Becker's d", NA, "twotail", -Inf, Inf, 0,
"d_r", "d (r)", "d\u1D63", "twotail", -Inf, Inf, 0,

"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,
Expand Down
6 changes: 4 additions & 2 deletions man/effectsize.Rd

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

6 changes: 4 additions & 2 deletions man/repeated_measures_d.Rd

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

6 changes: 4 additions & 2 deletions tests/testthat/test-effectsize.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ test_that("t-test", {
model <- t.test(x, y, paired = TRUE)
expect_equal(effectsize(model, verbose = FALSE), cohens_d(x, y, paired = TRUE, verbose = FALSE), ignore_attr = TRUE)

sleep2 <<- reshape(sleep, direction = "wide",
idvar = "ID", timevar = "group")
sleep2 <<- reshape(sleep,
direction = "wide",
idvar = "ID", timevar = "group"
)
tt <- t.test(sleep2$extra.1, sleep2$extra.2, paired = TRUE)

es1 <- effectsize(tt, type = "rm_b")
Expand Down
61 changes: 38 additions & 23 deletions tests/testthat/test-rm_d.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
test_that("rm_d | paired data", {

data("sleep")
sleep2 <- reshape(sleep, direction = "wide",
idvar = "ID", timevar = "group")
sleep2 <- reshape(sleep,
direction = "wide",
idvar = "ID", timevar = "group"
)

d1 <- rm_d(sleep$extra[sleep$group==1],
sleep$extra[sleep$group==2])
d1 <- rm_d(
sleep$extra[sleep$group == 1],
sleep$extra[sleep$group == 2]
)
d2 <- rm_d(Pair(extra.1, extra.2) ~ 1, data = sleep2)
expect_no_message(d3 <- rm_d(extra ~ group | ID, data = sleep))

expect_equal(d1, d2)
expect_equal(d1, d3)

d_z <- rm_d(Pair(extra.1, extra.2) ~ 1, data = sleep2, method = "z")
d_z2 <- hedges_g(sleep$extra[sleep$group==1] - sleep$extra[sleep$group==2])
d_z2 <- hedges_g(sleep$extra[sleep$group == 1] - sleep$extra[sleep$group == 2])

expect_equal(d_z[[1]], d_z2[[1]])
expect_equal(d_z$CI_low, d_z2$CI_low, tolerance = 0.01)
Expand All @@ -27,14 +30,18 @@ test_that("rm_d | paired data", {

# equal with equal variance:
dat <- data.frame(
V1 = c(-0.32150435528124, -4.02978032779713, 0.159645811226589,
1.95179927058772, 0.168527299289471, 3.4499229496418,
-1.87888939495506, 0.431333839911973, -0.26004200470096,
0.328986912076835),
V2 = c(2.50107579495566, -0.32926747002329, 4.01118960037018,
1.44969284040984, -1.46241877172319, 1.25068499614667,
0.028928469929524, 3.05532100575796, -1.67014771817319,
3.16494125234984)
V1 = c(
-0.32150435528124, -4.02978032779713, 0.159645811226589,
1.95179927058772, 0.168527299289471, 3.4499229496418,
-1.87888939495506, 0.431333839911973, -0.26004200470096,
0.328986912076835
),
V2 = c(
2.50107579495566, -0.32926747002329, 4.01118960037018,
1.44969284040984, -1.46241877172319, 1.25068499614667,
0.028928469929524, 3.05532100575796, -1.67014771817319,
3.16494125234984
)
)

d_rm <- rm_d("V1", "V2", data = dat, adjust = FALSE)
Expand All @@ -61,21 +68,29 @@ test_that("rm_d | replication data", {
rm_d(rt ~ cond | id, data = rouder2016, method = "d"),
)

d_av <- rm_d(rt ~ cond | id, data = rouder2016, method = "av",
adjust = FALSE, verbose = FALSE)
d_z <- rm_d(rt ~ cond | id, data = rouder2016, method = "z",
adjust = FALSE, verbose = FALSE)
d_d <- rm_d(rt ~ cond | id, data = rouder2016, method = "d",
adjust = FALSE)
d_r <- rm_d(rt ~ cond | id, data = rouder2016, method = "r",
adjust = FALSE)
d_av <- rm_d(rt ~ cond | id,
data = rouder2016, method = "av",
adjust = FALSE, verbose = FALSE
)
d_z <- rm_d(rt ~ cond | id,
data = rouder2016, method = "z",
adjust = FALSE, verbose = FALSE
)
d_d <- rm_d(rt ~ cond | id,
data = rouder2016, method = "d",
adjust = FALSE
)
d_r <- rm_d(rt ~ cond | id,
data = rouder2016, method = "r",
adjust = FALSE
)

# values takes from
# https://jakewestfall.org/blog/index.php/2016/03/25/five-different-cohens-d-statistics-for-within-subject-designs/
expect_equal(d_av[[1]], -0.8357347, tolerance = 0.001)
expect_equal(d_z[[1]], -1.353713, tolerance = 0.001)
expect_equal(d_d[[1]], -0.2497971, tolerance = 0.001)
expect_equal(d_r[[1]], -0.052298/0.20195, tolerance = 0.001)
expect_equal(d_r[[1]], -0.052298 / 0.20195, tolerance = 0.001)

# also:
expect_equal(d_d[[1]], cohens_d(rt ~ cond, data = rouder2016)[[1]], tolerance = 0.001)
Expand Down

0 comments on commit 5c726a3

Please sign in to comment.