| quick_chisq | R Documentation |
Perform chi-square test of independence or Fisher's exact test (automatically selected based on expected frequencies) with publication-ready visualization. Designed for analyzing the association between two categorical variables.
quick_chisq(
data,
var1,
var2,
method = c("auto", "chisq", "fisher", "mcnemar"),
correct = NULL,
conf.level = 0.95,
plot_type = c("bar_grouped", "bar_stacked", "heatmap"),
show_p_value = TRUE,
p_label = c("p.format", "p.signif"),
palette = "qual_vivid",
verbose = TRUE,
...
)
data |
A data frame containing the variables. |
var1 |
Column name for the first categorical variable (row variable). Supports both quoted and unquoted names via NSE. |
var2 |
Column name for the second categorical variable (column variable). Supports both quoted and unquoted names via NSE. |
method |
Character. Test method: "auto" (default), "chisq", "fisher", or "mcnemar". When "auto", the function intelligently selects based on expected frequencies and table size. WARNING: "mcnemar" is ONLY for paired/matched data (e.g., before-after measurements on the same subjects). It tests marginal homogeneity, NOT independence. Do NOT use McNemar's test for independent samples - use "chisq" or "fisher" instead. |
correct |
Logical or |
conf.level |
Numeric. Confidence level for the interval. Default is 0.95. |
plot_type |
Character. Type of plot: "bar_grouped" (default), "bar_stacked", or "heatmap". |
show_p_value |
Logical. Display p-value on the plot? Default is |
p_label |
Character. P-value label format: "p.format" (numeric p-value, default) or "p.signif" (stars). |
palette |
Character. Color palette name from evanverse palettes.
Default is "qual_vivid". Set to |
verbose |
Logical. Print diagnostic messages? Default is |
... |
Additional arguments (currently unused, reserved for future extensions). |
"Quick" means easy to use, not simplified or inaccurate.
This function performs full statistical testing with proper assumption checking:
The function uses an intelligent algorithm based on expected frequencies:
All expected frequencies >= 5: Standard chi-square test
2x2 table with any expected frequency < 5: Fisher's exact test
Larger table with expected frequency < 5: Chi-square with warning
2x2 table with 5 <= expected frequency < 10: Chi-square with Yates' correction
Cramer's V is calculated as a measure of effect size:
Small effect: V = 0.1
Medium effect: V = 0.3
Large effect: V = 0.5
Pearson residuals are calculated for each cell as (observed - expected) / sqrt(expected):
Values > |2| indicate significant deviation from independence
Values > |3| indicate very significant deviation
bar_grouped: Grouped bar chart (default)
bar_stacked: Stacked bar chart (100\
heatmap: Heatmap of Pearson residuals
An object of class quick_chisq_result containing:
A ggplot object with the association visualization
The htest object from chisq.test() or fisher.test()
Character string of the test method used
The contingency table (counts)
Matrix of expected frequencies
Pearson residuals for each cell
Cramer's V effect size measure
Data frame with frequencies and proportions
Details about automatic method selection
POSIXct timestamp of analysis
Categorical variables: Both variables must be categorical or will be coerced to factors.
Sample size: Fisher's exact test may be computationally intensive for large tables.
Missing values: Automatically removed with a warning.
Low frequencies: Cells with expected frequency < 5 may lead to unreliable results.
chisq.test, fisher.test,
quick_ttest, quick_anova
# Example 1: Basic usage with automatic method selection
set.seed(123)
data <- data.frame(
treatment = sample(c("A", "B", "C"), 100, replace = TRUE),
response = sample(c("Success", "Failure"), 100, replace = TRUE,
prob = c(0.6, 0.4))
)
result <- quick_chisq(data, var1 = treatment, var2 = response)
print(result)
# Example 2: 2x2 table
data_2x2 <- data.frame(
gender = rep(c("Male", "Female"), each = 50),
disease = sample(c("Yes", "No"), 100, replace = TRUE)
)
result <- quick_chisq(data_2x2, var1 = gender, var2 = disease)
# Example 3: Customize visualization
result <- quick_chisq(data,
var1 = treatment,
var2 = response,
plot_type = "bar_grouped",
palette = "qual_balanced")
# Example 4: Manual method selection
result <- quick_chisq(data,
var1 = treatment,
var2 = response,
method = "chisq",
correct = FALSE)
# Access components
result$plot # ggplot object
result$test_result # htest object
result$contingency_table # Contingency table
result$pearson_residuals # Pearson residuals
summary(result) # Detailed summary
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.