Learn R Programming

GimmeMyStats (version 1.0.0)

post_hoc_chi2: Performs post hoc analysis for chi-squared or Fisher's exact test

Description

Identifies pairwise differences between categories following a chi-squared or Fisher's exact test.

Usage

post_hoc_chi2(
  x,
  method = "fisher",
  method_adjust = "BH",
  digits = 3,
  count = FALSE,
  ...
)

Value

A tibble with pairwise test results containing the following columns:

group1, group2

Character vectors specifying the pair of groups being compared.

n

Numeric vector specifying the total count or sample size for the comparison.

statistic

Numeric vector specifying the test statistic (for chi-squared tests only).

df

Numeric vector specifying the degrees of freedom (for chi-squared tests only).

p

Raw p-value for the pairwise comparison, formatted as numeric or character ("< 0.001" for very small p-values).

p.signif

Character vectors specifying the significance codes for raw p-values: 'ns' (not significant).

FDR

False Discovery Rate adjusted p-value using the specified method, formatted as numeric or character ("< 0.001" for very small values).

fdr.signif

Character vectors specifying the significance codes for FDR-adjusted p-values: 'ns' (not significant), '' (p < 0.05), '' (p < 0.01), '' (p < 0.001).

For Fisher's exact tests, the statistic and df columns are not included..

Arguments

x

Data frame, vector, or table. If numeric, treated as a contingency table and the names are considered as categories; otherwise, the levels of the factor or the characters are used.

method

Character specifying the statistical test: chisq for chi-squared or fisher for Fisher's exact test.

method_adjust

Character specifying the p-value adjustment method.

digits

Integer specifying the number of decimal places for the test statistic.

count

Logical specifying if x is a contingency table.

...

Additional arguments passed to chisq.test or fisher.test.

Details

If x is numeric, it is treated as a contingency table and the names are considered as categories; otherwise, the levels of the factor or the characters are used.

Examples

Run this code
x <- c(rep("A", 100), rep("B", 78), rep("C", 25))
post_hoc_chi2(x)

x <- data.frame(G1 = c(Yes = 100, No = 78), G2 = c(Yes = 75, No = 23))
post_hoc_chi2(x, count = TRUE, method = "chisq")

data("housetasks")
housetasks[, c("Wife", "Husband")] %>%
    t() %>%
    post_hoc_chi2(count = TRUE, workspace = 1e6)

x <- cbind(
    mapply(function(x, y) rep(x, y), letters[seq(3)], c(7, 5, 8)) %>% unlist(),
    mapply(function(x, y) rep(x, y), LETTERS[seq(3)], c(6, 6, 8)) %>% unlist()
)
post_hoc_chi2(x)

Run the code above in your browser using DataLab