Skip to content

Commit de84369

Browse files
authored
Merge pull request #1562 from exploratory-io/codex/issue-37226-r
Add adjusted chi-square residual output
2 parents 94c5ef4 + a2b4db9 commit de84369

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Package: exploratory
22
Type: Package
33
Title: R package for Exploratory
4-
Version: 16.0.12
5-
Date: 2026-07-23
4+
Version: 16.0.13
5+
Date: 2026-07-24
66
Authors@R: c(person("Hideaki", "Hayashi", email = "hideaki@exploratory.io", role = c("aut", "cre")), person("Hide", "Kojima", email = "hide@exploratory.io", role = c("aut")), person("Kan", "Nishida", email = "kan@exploratory.io", role = c("aut")), person("Kei", "Saito", email = "kei@exploratory.io", role = c("aut")), person("Yosuke", "Yasuda", email = "double.y.919.quick@gmail.com", role = c("aut")))
77
URL: https://github.com/exploratory-io/exploratory_func
88
Description: Functions for Exploratory

R/test_wrapper.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,12 @@ tidy.chisq_exploratory <- function(x, type = "observed") {
534534
resid_df <- resid_df %>% tibble::rownames_to_column(var = x$var1)
535535
resid_df <- resid_df %>% tidyr::gather(!!rlang::sym(x$var2), "residual", -!!rlang::sym(x$var1))
536536

537+
# Keep the cell statistic and multiplicity correction aligned with the
538+
# Correspondence Analysis report.
539+
adjusted_resid_df <- as.data.frame(x$stdres)
540+
adjusted_resid_df <- adjusted_resid_df %>% tibble::rownames_to_column(var = x$var1)
541+
adjusted_resid_df <- adjusted_resid_df %>% tidyr::gather(!!rlang::sym(x$var2), "adjusted_standardized_residual", -!!rlang::sym(x$var1))
542+
537543
resid_raw_df <- as.data.frame(x$observed - x$expected) # x$residual is standardized, but here, take raw difference between observed and expected.
538544
resid_raw_df <- resid_raw_df %>% tibble::rownames_to_column(var = x$var1)
539545
resid_raw_df <- resid_raw_df %>% tidyr::gather(!!rlang::sym(x$var2), "residual_raw", -!!rlang::sym(x$var1))
@@ -544,6 +550,7 @@ tidy.chisq_exploratory <- function(x, type = "observed") {
544550

545551
ret <- obs_df %>% left_join(expected_df, by=c(x$var1, x$var2)) # join expected column
546552
ret <- ret %>% left_join(resid_df, by=c(x$var1, x$var2)) # join expected column
553+
ret <- ret %>% left_join(adjusted_resid_df, by=c(x$var1, x$var2))
547554
ret <- ret %>% left_join(resid_raw_df, by=c(x$var1, x$var2)) # join residual_raw column
548555
ret <- ret %>% left_join(resid_ratio_df, by=c(x$var1, x$var2)) # join residual_ratio column
549556
if (is.nan(x$statistic) || x$statistic <= 0) {
@@ -552,6 +559,11 @@ tidy.chisq_exploratory <- function(x, type = "observed") {
552559
else {
553560
ret <- ret %>% mutate(contrib = 100*residual^2/(!!(x$statistic))) # add percent contribution too.
554561
}
562+
ret <- ret %>%
563+
mutate(
564+
adjusted_p_value = 2 * stats::pnorm(abs(adjusted_standardized_residual), lower.tail = FALSE),
565+
adjusted_p_value = stats::p.adjust(adjusted_p_value, method = "holm")
566+
)
555567

556568
if (!is.null(x$var1_levels)) {
557569
ret[[x$var1]] <- factor(ret[[x$var1]], levels=x$var1_levels)

exploratory_16.0.8.tgz

-2.46 MB
Binary file not shown.

tests/testthat/test_test_wrapper_1.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,17 @@ test_that("test exp_chisq", {
291291
observed <- ret %>% tidy_rowwise(model, type="observed")
292292
summary <- ret %>% glance_rowwise(model)
293293
residuals <- ret %>% tidy_rowwise(model, type="residuals")
294+
expect_true(all(c("adjusted_standardized_residual", "adjusted_p_value") %in% colnames(residuals)))
295+
expect_equal(
296+
residuals$adjusted_standardized_residual,
297+
as.vector(ret$model[[1]]$stdres),
298+
tolerance = 1e-12
299+
)
300+
expect_equal(
301+
residuals$adjusted_p_value,
302+
p.adjust(2 * pnorm(abs(residuals$adjusted_standardized_residual), lower.tail = FALSE), method = "holm"),
303+
tolerance = 1e-12
304+
)
294305
expect_true(all(c("Cramer's V","Chi-Square","DF","P Value","Cohen's W",
295306
"Power", "Type 2 Error","Rows") %in% colnames(summary)
296307
))
@@ -1085,4 +1096,4 @@ test_that("1-way ANOVA with complex column names works for type='emmeans' and ty
10851096
expect_true(is.data.frame(ret_emmeans))
10861097
ret_pairs <- model_df %>% tidy_rowwise(model, type = "pairs", pairs_adjust = "tukey")
10871098
expect_true(is.data.frame(ret_pairs))
1088-
})
1099+
})

0 commit comments

Comments
 (0)