Skip to content

Commit

Permalink
linter, simplify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rempsyc committed Sep 30, 2023
1 parent 9a0c4df commit 88102ca
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 116 deletions.
16 changes: 9 additions & 7 deletions R/report.htest.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,10 @@ report_statistics.htest <- function(x, table = NULL, ...) {
text <- NULL

# Estimate
candidates <- c("rho", "r", "tau", "Difference", "r_rank_biserial",
"Chi2", "Odds Ratio")
candidates <- c(
"rho", "r", "tau", "Difference", "r_rank_biserial",
"Chi2", "Odds Ratio"
)
estimate <- candidates[candidates %in% names(table)][1]
if (!is.null(estimate) && !is.na(estimate)) {
text <- paste0(tolower(estimate), " = ", insight::format_value(table[[estimate]]))
Expand Down Expand Up @@ -262,11 +264,11 @@ report_parameters.htest <- function(x, table = NULL, ...) {
out <- .report_parameters_friedman(table, stats, effsize, ...)
# chi2
} else if (model_info$is_chi2test) {
if (chi2_type(x) == "fisher") {
out <- .report_parameters_fisher(table, stats, effsize, ...)
} else {
out <- .report_parameters_chi2(table, stats, effsize, ...)
}
if (chi2_type(x) == "fisher") {
out <- .report_parameters_fisher(table, stats, effsize, ...)
} else {
out <- .report_parameters_chi2(table, stats, effsize, ...)
}
} else {
# TODO: default, same as t-test?
out <- .report_parameters_htest_default(table, stats, effsize, ...)
Expand Down
19 changes: 9 additions & 10 deletions R/report_htest_fisher.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
# report_effectsize ---------------------

.report_effectsize_fisher <- function(x, table, dot_args, rules = "funder2019") {
args <- c(list(x), dot_args)
table <- do.call(effectsize::effectsize, args)
ci <- attributes(table)$ci
estimate <- names(table)[1]
rules <- ifelse(is.null(dot_args$rules), rules, dot_args$rules)

args <- c(list(x), dot_args)
table <- do.call(effectsize::effectsize, args)
ci <- attributes(table)$ci
estimate <- names(table)[1]
rules <- ifelse(is.null(dot_args$rules), rules, dot_args$rules)
args <- list(table, rules = rules, dot_args)
interpretation <- do.call(effectsize::interpret, args)$Interpretation
rules <- .text_effectsize(attr(attr(interpretation, "rules"), "rule_name"))

args <- list(table, rules = rules, dot_args)
interpretation <- do.call(effectsize::interpret, args)$Interpretation
rules <- .text_effectsize(attr(attr(interpretation, "rules"), "rule_name"))

if (estimate == "Cramers_v_adjusted") {
if (estimate == "Cramers_v_adjusted") {
main <- paste0("Adjusted Cramer's v = ", insight::format_value(table[[estimate]]))
} else if (estimate == "Tschuprows_t") {
main <- paste0("Tschuprow's t = ", insight::format_value(table[[estimate]]))
Expand Down
95 changes: 0 additions & 95 deletions tests/testthat/test-report.htest-chi2.R
Original file line number Diff line number Diff line change
@@ -1,98 +1,3 @@
test_that("report.htest-chi2 report_effectsize", {
m <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))
dimnames(m) <- list(gender = c("F", "M"), party = c("Democrat", "Independent", "Republican"))
x <- chisq.test(m)

expect_snapshot(
variant = "windows",
report_effectsize(x)
)

# Rules
expect_snapshot(
variant = "windows",
report_effectsize(x, rules = "funder2019")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, rules = "gignac2016")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, rules = "cohen1988")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, rules = "evans1996")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, rules = "lovakov2021")
)

# Types
expect_snapshot(
variant = "windows",
report_effectsize(x, type = "cramers_v")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, type = "pearsons_c")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, type = "tschuprows_t", adjust = FALSE)
)

expect_snapshot(
variant = "windows",
report_effectsize(x, type = "tschuprows_t")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, type = "cohens_w")
)

# Change dataset for "Error: Phi is not appropriate for non-2x2 tables."
dat <- structure(
c(71, 50, 30, 100),
dim = c(2L, 2L), dimnames = list(
Diagnosis = c("Sick", "Recovered"),
Group = c("Treatment", "Control")
),
class = "table"
)
x <- chisq.test(dat)

expect_snapshot(
variant = "windows",
report_effectsize(x, type = "phi")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, type = "cohens_h", rules = "sawilowsky2009")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, type = "oddsratio", rules = "chen2010")
)

expect_snapshot(
variant = "windows",
report_effectsize(x, type = "riskratio")
) # riskratio has no interpretation in effectsize
# Watch carefully in case effectsize adds support
})

test_that("report.htest-chi2 report", {
m <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))
dimnames(m) <- list(gender = c("F", "M"), party = c("Democrat", "Independent", "Republican"))
Expand Down
10 changes: 6 additions & 4 deletions tests/testthat/test-report.htest-fisher.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
test_that("report.htest-fisher report", {
TeaTasting <<-
matrix(c(3, 1, 1, 3),
nrow = 2,
dimnames = list(Guess = c("Milk", "Tea"),
Truth = c("Milk", "Tea")))
nrow = 2,
dimnames = list(
Guess = c("Milk", "Tea"),
Truth = c("Milk", "Tea")
)
)
x <- fisher.test(TeaTasting, alternative = "greater")

expect_snapshot(
variant = "windows",
report(x)
)

})

0 comments on commit 88102ca

Please sign in to comment.