Lab 10: Chi-Square Tests for Categorical Data Analysis

Author

Instructor Name

Published

November 19, 2025

Student Name:

Introduction: Chi-Square Tests for Categorical Data

This laboratory focuses on chi-square tests for analyzing relationships between categorical variables. Chi-square tests are essential statistical tools for examining whether observed frequency distributions differ from expected distributions or whether categorical variables are independent.

Learning Objectives

Upon completion of this laboratory, students will be able to:

  • Understand the concepts and applications of chi-square tests
  • Perform chi-square tests of independence for categorical variables
  • Conduct chi-square goodness-of-fit tests
  • Interpret chi-square test results and p-values
  • Check assumptions for chi-square tests
  • Handle sparse data situations with Fisher’s exact test
  • Apply chi-square tests to real-world datasets
  • Communicate statistical findings from categorical data analysis

Time Allocation (Total: 75 minutes)

  • Part 1: Chi-Square Test Concepts and Assumptions (25 minutes)
  • Part 2: Test of Independence Applications (30 minutes)
  • Part 3: Goodness-of-Fit Testing (20 minutes)

Part 1: Chi-Square Test Concepts and Assumptions

1.1 Chi-Square Test Fundamentals

Chi-square tests are used for categorical data analysis and come in two main forms:

  1. Test of Independence: Examines whether two categorical variables are related
  2. Goodness-of-Fit Test: Compares observed frequencies to expected frequencies

Key Concepts:

  • Contingency Table: A table showing frequency distributions of categorical variables
  • Expected Frequencies: The frequencies we would expect if the null hypothesis were true
  • Chi-Square Statistic: Measures the discrepancy between observed and expected frequencies
  • Degrees of Freedom: Determined by the dimensions of the contingency table

1.2 Assumptions and Conditions

For valid chi-square tests, the following assumptions must be met:

  1. Random Sampling: Data should come from a random sample
  2. Independence: Observations must be independent of each other
  3. Expected Counts: All expected frequencies should be ≥ 5
  4. Categorical Data: Variables must be categorical, not continuous

Expected Counts Condition:

  • When expected counts < 5, the chi-square approximation may be inaccurate
  • For 2×2 tables with small expected counts, use Fisher’s exact test
  • For larger tables, consider combining categories or using exact tests

1.3 Hypothesis Formulation

Test of Independence:

  • H0H_0: The two categorical variables are independent
  • H1H_1: The two categorical variables are dependent

Goodness-of-Fit Test:

  • H0H_0: The observed frequencies match the expected distribution
  • H1H_1: The observed frequencies do not match the expected distribution

1.4 Data Import and Preparation

# Load datasets for chi-square analysis
# Data URL: https://math214.netlify.app/data/Lab10/mall_customers.csv
mall_data <- read.csv("../data/Lab10/mall_customers.csv")
# Data URL: https://math214.netlify.app/data/Lab10/studentsurvey.csv
student_data <- read.csv("../data/Lab10/studentsurvey.csv")

# Create categorical variables for mall customers
mall_data$Income_Group <- cut(mall_data$Annual.Income..k..,
                              breaks = c(0, 30, 60, 90, 150),
                              labels = c("Low", "Medium-Low", "Medium", "High"))

mall_data$Spending_Group <- cut(mall_data$Spending.Score..1.100.,
                                breaks = c(0, 33, 66, 100),
                                labels = c("Low", "Medium", "High"))

# Create categorical variables for student survey
student_data$GPA_Group <- cut(student_data$GPA,
                              breaks = c(0, 2.5, 3.0, 3.5, 4.0),
                              labels = c("Below 2.5", "2.5-3.0", "3.0-3.5", "3.5+"))

# Display dataset
glimpse(mall_data)
Rows: 200
Columns: 7
$ CustomerID             <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, …
$ Genre                  <chr> "Male", "Male", "Female", "Female", "Female", "…
$ Age                    <int> 19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, 35,…
$ Annual.Income..k..     <int> 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, 19,…
$ Spending.Score..1.100. <int> 39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99, 15…
$ Income_Group           <fct> Low, Low, Low, Low, Low, Low, Low, Low, Low, Lo…
$ Spending_Group         <fct> Medium, High, Low, High, Medium, High, Low, Hig…

Part 2: Test of Independence Applications

2.1 Mall Customers Analysis

# Create contingency table: Gender vs Spending Group
gender_spending_table <- table(mall_data$Genre, mall_data$Spending_Group)
print("Gender vs Spending Group Contingency Table:")
[1] "Gender vs Spending Group Contingency Table:"
print(gender_spending_table)
        
         Low Medium High
  Female  25     54   33
  Male    24     40   24
# Perform chi-square test
chi_gender_spending <- chisq.test(gender_spending_table)
print("Expected Counts:")
[1] "Expected Counts:"
print(chi_gender_spending$expected)
        
           Low Medium  High
  Female 27.44  52.64 31.92
  Male   21.56  41.36 25.08
print("Chi-Square Test Results:")
[1] "Chi-Square Test Results:"
print(chi_gender_spending)

    Pearson's Chi-squared test

data:  gender_spending_table
X-squared = 0.65601, df = 2, p-value = 0.7204
# Visualize the relationship
mosaicplot(gender_spending_table,
           main = "Spending Patterns by Gender",
           color = TRUE,
           las = 1,
           xlab = "Gender",
           ylab = "Spending Group")

Spending Patterns by Gender

# Create another table: Income Group vs Spending Group
income_spending_table <- table(mall_data$Income_Group, mall_data$Spending_Group)

print("Income Group vs Spending Group Contingency Table:")
[1] "Income Group vs Spending Group Contingency Table:"
print(income_spending_table)
            
             Low Medium High
  Low         12      6   14
  Medium-Low   5     56    5
  Medium      22     31   27
  High        10      1   11
chi_income_spending <- chisq.test(income_spending_table)
print("Expected Counts:")
[1] "Expected Counts:"
print(chi_income_spending$expected)
            
               Low Medium  High
  Low         7.84  15.04  9.12
  Medium-Low 16.17  31.02 18.81
  Medium     19.60  37.60 22.80
  High        5.39  10.34  6.27
print("Chi-Square Test Results:")
[1] "Chi-Square Test Results:"
print(chi_income_spending)

    Pearson's Chi-squared test

data:  income_spending_table
X-squared = 66.397, df = 6, p-value = 2.235e-12

2.2 Student Survey Analysis

# Create contingency table: Gender vs Smoking Status
gender_smoke_table <- table(student_data$Gender, student_data$Smoke)
print("Gender vs Smoking Status Contingency Table:")
[1] "Gender vs Smoking Status Contingency Table:"
print(gender_smoke_table)
   
     No Yes
  F 153  16
  M 166  27
# Perform chi-square test
chi_gender_smoke <- chisq.test(gender_smoke_table)
print("Expected Counts:")
[1] "Expected Counts:"
print(chi_gender_smoke$expected)
   
          No      Yes
  F 148.9254 20.07459
  M 170.0746 22.92541
print("Chi-Square Test Results:")
[1] "Chi-Square Test Results:"
print(chi_gender_smoke)

    Pearson's Chi-squared test with Yates' continuity correction

data:  gender_smoke_table
X-squared = 1.3548, df = 1, p-value = 0.2444
# Create table: Award Preference vs Higher SAT
award_sat_table <- table(student_data$Award, student_data$HigherSAT)
print("Award Preference vs Higher SAT Contingency Table:")
[1] "Award Preference vs Higher SAT Contingency Table:"
print(award_sat_table)
         
              Math Verbal
  Academy   0   21     10
  Nobel     2   68     79
  Olympic   5  116     61
chi_award_sat <- chisq.test(award_sat_table)
print("Expected Counts:")
[1] "Expected Counts:"
print(chi_award_sat$expected)
         
                         Math   Verbal
  Academy 0.5994475  17.55525 12.84530
  Nobel   2.8812155  84.37845 61.74033
  Olympic 3.5193370 103.06630 75.41436
print("Chi-Square Test Results:")
[1] "Chi-Square Test Results:"
print(chi_award_sat)

    Pearson's Chi-squared test

data:  award_sat_table
X-squared = 15.18, df = 4, p-value = 0.004341
# Visualize relationships
mosaicplot(gender_smoke_table,
           main = "Smoking Status by Gender",
           color = TRUE,
           las = 1)

Smoking Status by Gender

mosaicplot(award_sat_table,
           main = "Award Preference by SAT Strength",
           color = TRUE,
           las = 1)

Smoking Status by Gender

Part 3: Goodness-of-Fit Testing

3.1 Mall Customer Distribution Analysis

# Test if gender distribution is equal (50/50)
observed_gender <- table(mall_data$Genre)
print("Observed Gender Distribution:")
[1] "Observed Gender Distribution:"
print(observed_gender)

Female   Male 
   112     88 
# Expected distribution: equal proportions
chi_gender_fit <- chisq.test(observed_gender, p = c(0.5, 0.5))
print("Goodness-of-Fit Test for Gender Distribution:")
[1] "Goodness-of-Fit Test for Gender Distribution:"
print(chi_gender_fit)

    Chi-squared test for given probabilities

data:  observed_gender
X-squared = 2.88, df = 1, p-value = 0.08969
# Test if spending groups follow specific distribution
observed_spending <- table(mall_data$Spending_Group)
print("Observed Spending Group Distribution:")
[1] "Observed Spending Group Distribution:"
print(observed_spending)

   Low Medium   High 
    49     94     57 
# Expected distribution: 30% Low, 40% Medium, 30% High
expected_proportions <- c(0.3, 0.4, 0.3)
chi_spending_fit <- chisq.test(observed_spending, p = expected_proportions)
print("Goodness-of-Fit Test for Spending Groups:")
[1] "Goodness-of-Fit Test for Spending Groups:"
print(chi_spending_fit)

    Chi-squared test for given probabilities

data:  observed_spending
X-squared = 4.6167, df = 2, p-value = 0.09943
# Visualize observed vs expected
spending_df <- tibble(
  Group = names(observed_spending),
  Observed = as.numeric(observed_spending),
  Expected = sum(observed_spending) * expected_proportions
)

ggplot(spending_df, aes(x = Group)) +
  geom_bar(aes(y = Observed, fill = "Observed"), stat = "identity", alpha = 0.7) +
  geom_point(aes(y = Expected, color = "Expected"), size = 3) +
  geom_line(aes(y = Expected, color = "Expected", group = 1), linetype = "dashed") +
  labs(title = "Spending Group Distribution: Observed vs Expected",
       y = "Frequency", x = "Spending Group") +
  scale_fill_manual("", values = "steelblue") +
  scale_color_manual("", values = "darkred") +
  theme_minimal(base_size = 10)

Spending Group Distribution: Observed vs Expected

3.2 Student Survey Distribution Analysis

# Test if award preferences are equally distributed
observed_awards <- table(student_data$Award)
print("Observed Award Preferences:")
[1] "Observed Award Preferences:"
print(observed_awards)

Academy   Nobel Olympic 
     31     149     182 
# Expected: equal distribution among three awards
expected_award_proportions <- c(1/3, 1/3, 1/3)
chi_award_fit <- chisq.test(observed_awards, p = expected_award_proportions)
print("Goodness-of-Fit Test for Award Preferences:")
[1] "Goodness-of-Fit Test for Award Preferences:"
print(chi_award_fit)

    Chi-squared test for given probabilities

data:  observed_awards
X-squared = 104.46, df = 2, p-value < 2.2e-16
# Test if smoking status follows population distribution
observed_smoking <- table(student_data$Smoke)
print("Observed Smoking Status:")
[1] "Observed Smoking Status:"
print(observed_smoking)

 No Yes 
319  43 
# Expected: 80% non-smokers, 20% smokers (based on general population)
# table() orders levels alphabetically as No, Yes
expected_smoking_proportions <- c(No = 0.8, Yes = 0.2)
chi_smoking_fit <- chisq.test(observed_smoking, p = expected_smoking_proportions)
print("Goodness-of-Fit Test for Smoking Status:")
[1] "Goodness-of-Fit Test for Smoking Status:"
print(chi_smoking_fit)

    Chi-squared test for given probabilities

data:  observed_smoking
X-squared = 14.923, df = 1, p-value = 0.000112

Assessment (Total: 50 points)

Section A: Formative Understanding (15 points)

A1. Explain the difference between chi-square test of independence and chi-square goodness-of-fit test. When would you use each type of test? Provide concrete examples for both. (4 points)

A2. Describe the key assumptions for chi-square tests and explain why the “expected counts ≥ 5” condition is important. What should you do when this condition is violated? (4 points)

A3. Write the null and alternative hypotheses for both a test of independence and a goodness-of-fit test. Explain how the interpretation differs between these two types of tests. (4 points)

A4. Discuss the role of contingency tables in chi-square analysis. How do you calculate expected frequencies, and what do they represent in the context of hypothesis testing? (3 points)

Section B: Application and Analysis (20 points)

B1. Test whether there is a relationship between gender and spending patterns in the mall customers dataset using α = 0.05. Create the appropriate contingency table, check assumptions, perform the chi-square test, and interpret your results. (5 points)

# Create contingency table

B2. Test whether income group and spending group are independent in the mall customers dataset using α = 0.05. Conduct the appropriate analysis and discuss whether the relationship makes practical sense. (5 points)

# Create contingency table

B3. Test whether gender and smoking status are independent in the student survey dataset using α = 0.05. Create the contingency table, check assumptions, and interpret your findings in the context of college student behavior. (5 points)

# Create contingency table

B4. Conduct a goodness-of-fit test to determine if award preferences in the student survey follow an equal distribution among the three options (Nobel Prize, Olympic Gold, Academy Award) using α = 0.05. Interpret your results. (5 points)

# Get observed frequencies

Section C: Statistical Synthesis (15 points)

C1. Create a comprehensive decision framework (200-250 words) for selecting appropriate categorical data analysis methods. Include guidance on when to use chi-square tests of independence, goodness-of-fit tests, and alternative methods like Fisher’s exact test. (7 points)

C2. Design a research study that would require chi-square analysis. Describe the categorical variables, research question, expected sample size, and how you would ensure assumptions are met. Include potential limitations and how you would address them. (4 points)

C3. Compare and contrast the mall customers dataset analysis with the student survey dataset analysis. Discuss the different types of relationships you might expect to find in these two contexts and how the interpretation of results would differ. (4 points)

Submission Guidelines:

  • Complete R Markdown document with all code and analysis
  • Proper execution of all chi-square tests following appropriate procedures
  • Comprehensive assumption checks with expected frequency analysis
  • Professional writing with clear analytical narrative and interpretation
  • Appropriate test justifications based on data characteristics
  • Knitted HTML document submitted via designated platform