Learn R Programming

mariposa (version 0.7.3)

chi_square: Test If Two Categories Are Related

Description

chi_square() helps you discover if two categorical variables are related or independent. For example, is education level related to voting preference? Or are they independent of each other?

The test tells you:

  • Whether the relationship is statistically significant

  • How strong the relationship is (effect sizes)

  • What patterns exist in your data

Usage

chi_square(data, ..., weights = NULL, correct = FALSE)

Value

Test results showing whether the variables are related, including:

  • Chi-squared statistic and p-value

  • Observed vs expected frequencies

  • Effect sizes to measure relationship strength Use summary() for the full SPSS-style output with toggleable sections.

Arguments

data

Your survey data (a data frame or tibble)

...

Two categorical variables to test (e.g., gender, region)

weights

Optional survey weights for population-representative results

correct

Apply continuity correction for small samples? (Default: FALSE)

Details

Understanding the Results

P-value: If p < 0.05, the variables are likely related (not independent)

  • p < 0.001: Very strong evidence of relationship

  • p < 0.01: Strong evidence of relationship

  • p < 0.05: Moderate evidence of relationship

  • p >= 0.05: No significant relationship found

Effect Sizes (How strong is the relationship?):

  • Cramer's V: Works for any table size (0 = no relationship, 1 = perfect relationship)

    • < 0.1: Negligible relationship

    • 0.1-0.3: Small relationship

    • 0.3-0.5: Medium relationship

    • 0.5 or higher: Large relationship

  • Phi: Only for 2x2 tables (similar interpretation as Cramer's V)

  • Gamma: For ordinal data (-1 to +1, shows direction of relationship)

When to Use This

Use chi-squared test when:

  • Both variables are categorical (gender, region, education level, etc.)

  • You want to know if they're related or independent

  • You have at least 5 observations in most cells

Reading the Frequency Tables

  • Observed: What you actually found in your data

  • Expected: What you'd expect if variables were independent

  • Large differences suggest a relationship exists

Tips for Success

  • Check that most cells have at least 5 observations

  • Use weights for population estimates

  • Look at both significance (p-value) and strength (effect sizes)

  • Consider using crosstab() for detailed percentage breakdowns

References

Pearson, K. (1900). On the criterion that a given system of deviations from the probable in the case of a correlated system of variables is such that it can be reasonably supposed to have arisen from random sampling. Philosophical Magazine, 50(302), 157--175.

Cramer, H. (1946). Mathematical Methods of Statistics. Princeton University Press.

IBM Corp. (2023). IBM SPSS Statistics 29 Algorithms. IBM Corporation.

See Also

chisq.test for the base R chi-squared test.

crosstab for detailed cross-tabulation tables.

frequency for single-variable frequency tables.

summary.chi_square for detailed output with toggleable sections.

Other hypothesis_tests: ancova(), binomial_test(), chisq_gof(), factorial_anova(), fisher_test(), friedman_test(), kruskal_wallis(), mann_whitney(), mcnemar_test(), oneway_anova(), t_test(), wilcoxon_test()

Examples

Run this code
# Load required packages and data
library(dplyr)
data(survey_data)

# Basic chi-squared test for independence
survey_data %>% chi_square(gender, region)

# With weights
survey_data %>% chi_square(gender, education, weights = sampling_weight)

# Grouped analysis
survey_data %>% 
  group_by(region) %>% 
  chi_square(gender, employment)

# With continuity correction
survey_data %>% chi_square(gender, region, correct = TRUE)

# --- Three-layer output ---
result <- chi_square(survey_data, gender, education)
result              # compact one-line overview
summary(result)     # full detailed output with all sections
summary(result, cross_tabulation = FALSE)  # hide cross-tabulation

Run the code above in your browser using DataLab