f_chisq_test | R Documentation |
Performs a chi-squared test chisq.test
, then automatically conducts post-hoc analysis if the test is significant. The function provides adjusted p-values for each cell in the contingency table using a specified correction method.
f_chisq_test(
x,
y,
p = NULL,
method = "bonferroni",
digits = 3,
alpha = 0.05,
force_posthoc = FALSE,
...
)
x |
A numeric vector (or factor), or a contingency table in matrix or table form. If a data frame is entered the function will try to convert it to a table. |
y |
A numeric vector; ignored if x is a matrix, table or data.frame. If x is a factor, y should be a factor of the same length. |
p |
A vector of probabilities of the same length as x. Default is |
method |
Character string specifying the adjustment method for p-values. Default is |
digits |
Integer specifying the number of decimal places for rounding. Default is |
alpha |
Numeric threshold for significance. Default is |
force_posthoc |
Logical indicating whether to perform post-hoc tests even if the chi-squared test is not significant. Default is |
... |
Additional arguments passed to |
The function first performs a chi-squared test using chisq.test
. If the test is
significant (p < alpha) or if force_posthoc = TRUE
, it conducts post-hoc analysis by examining
the standardized residuals. The p-values for these residuals are adjusted using the specified method
to control for multiple comparisons.
If the input is a data frame, the function attempts to convert it to a table and displays the resulting table for verification.
An object of class f_chisq_test containing:
chisq_test_output
: The output from chisq.test
.
adjusted_p_values
: Matrix of adjusted p-values (for table/matrix input).
observed_vs_adj_p_value
: Interleaved table of observed values and adjusted p-values.
stdres_vs_adj_p_value
: Interleaved table of standardized residuals and adjusted p-values.
adj_p_values
: Vector of adjusted p-values (for vector input).
posthoc_output_table
: Data frame with observed values, expected values,
standardized residuals, and adjusted p-values (for vector input).
Sander H. van Delden plantmind@proton.me
This function implements a post-hoc analysis for chi-squared tests inspired by the methodology in:
Beasley, T. M., & Schumacker, R. E. (1995). Multiple Regression Approach to Analyzing Contingency Tables: Post Hoc and Planned Comparison Procedures. The Journal of Experimental Education, 64(1), 79-93.
The implementation draws inspiration from the 'chisq.posthoc.test' package by Daniel Ebbert.
# Chi.square on independence: Association between two variables.
# Create a contingency table.
my_table <- as.table(rbind(c(100, 150, 50), c(120, 90, 40)))
dimnames(my_table) <- list(Gender = c("Male", "Female"),
Response = c("Agree", "Neutral", "Disagree"))
# Perform chi-squared test with post-hoc analysis.
f_chisq_test(my_table)
# Use a different adjustment method.
f_chisq_test(my_table, method = "holm")
# Other forms still work like Goodness-of-Fit: Match to theoretical distribution.
# Observed frequencies of rolling with a die 1 - 6.
observed <- c(2, 2, 10, 20, 15, 11)
# Expected probabilities under a fair die.
expected_probs <- rep(1/6, 6)
# Chi-Square Goodness-of-Fit Test.
f_chisq_test(x = observed, p = expected_probs)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.