🎲 Module 1: Why Chi-Square Matters

Understanding Categorical Data Analysis

Working guidelines

📚 Learning Objectives

By the end of this module, you will be able to:

🔍 Understanding Variable Types: The Foundation

Before we can understand Chi-square, we need to be crystal clear about what kind of data we're working with.

Categorical Variables (Also Called: Nominal, Discrete, Qualitative)

Categorical variables represent groups, categories, or classifications. They answer "what kind?" or "which category?"

Examples:

Continuous Variables (Also Called: Quantitative, Measurement)

Continuous variables represent measurements or counts. They answer "how much?" or "how many?"

Examples:

💡 Key Insight: Why This Matters

Different variable types require different statistical tests!

Chi-square is specifically designed for categorical data. If you try to use it on continuous data, you're using the wrong tool!

🎯 Interactive Activity: Categorize These Variables

Drag each variable into the correct category. This skill is essential for choosing the right statistical test!

Variable Bank (Drag these):

Neuron type (pyramidal, interneuron)
Firing rate (spikes/second)
Handedness (left, right)
Age (years)
Treatment outcome (improved, no change, worse)
Anxiety score (0-50 scale)
Maze arm chosen (left, right, center)
Time to find platform (seconds)

📊 Categorical Variables

Drop categorical variables here

📈 Continuous Variables

Drop continuous variables here

🎲 What Does Chi-Square Test?

Chi-square tests answer questions about frequencies and proportions in categorical data.

The core question Chi-square asks:

"Are the observed frequencies different from what we'd expect?"

Two Types of Chi-Square Tests:

1️⃣ Goodness-of-Fit Test

One categorical variable

Tests if observed distribution matches an expected distribution

Example questions:

  • Do rats choose maze arms equally?
  • Are births evenly distributed across weekdays?
  • Does a die land fairly on all sides?

2️⃣ Test of Independence

Two categorical variables

Tests if two variables are related or independent

Example questions:

  • Is treatment response related to sex?
  • Does species affect habitat preference?
  • Is diagnosis associated with treatment type?

📊 Visual Demo: Expected vs. Observed Frequencies

Let's see how Chi-square works with a simple example:

Research Question: Do rats have a left/right preference?

We test 60 rats in a T-maze where they must choose left or right. If there's no preference (null hypothesis), we'd expect 30 to go left and 30 to go right.

Expected (No Preference)

Direction Expected Count
Left 30
Right 30

Observed (Actual Data)

Direction Observed Count
Left 42
Right 18

The Chi-square test calculates: How different is the observed pattern from what we expected?

Interpretation: The observed frequencies (42 left, 18 right) are quite different from expected (30, 30). Chi-square will tell us if this difference is statistically significant or just random variation.

✅ When Should You Use Chi-Square?

Use Chi-square when ALL of these are true:

DO NOT use Chi-square when:

🎯 Interactive Activity: Choose the Right Test

For each research question below, decide if Chi-square is appropriate or if you should use a different test.

🤔 Check Your Understanding

Question 1: A researcher measures reaction time (in milliseconds) for participants who drank coffee vs. no coffee. Should they use Chi-square?

A) Yes, because there are two groups (coffee vs. no coffee)
B) No, because reaction time is continuous, not categorical
C) Yes, because we're comparing groups

Question 2: A researcher categorizes 100 patients as "improved," "no change," or "worse" after treatment. They want to test if the distribution differs from equal proportions (33%, 33%, 33%). Which Chi-square test should they use?

A) Goodness-of-fit test (one categorical variable)
B) Test of independence (two categorical variables)
C) Neither - should use ANOVA

Question 3: You want to know if males and females differ in their choice of three different habitats (forest, grassland, desert). Which test?

A) Goodness-of-fit test
B) Test of independence (Chi-square for two categorical variables)
C) Two-way ANOVA

📝 Module 1 Summary

Key Takeaways:

Next up: Module 2 will teach you how to run Chi-square goodness-of-fit tests in R and interpret the results!

See it in R

You have decided when chi-square applies. Here is what running one actually looks like — the same chisq.test() you will use all semester (Chapter 21). Run this in Posit Cloud.

The scenario. You record which of four maze arms a rat enters first, across 60 trials. If the rat has no preference, each arm should get a quarter of the entries.

# Observed counts in the four arms
observed <- c(12, 18, 10, 20)

# Expected proportions under "no preference" (must sum to 1)
expected <- rep(0.25, 4)

chisq.test(x = observed, p = expected)

Predict first. The counts are 12, 18, 10, 20 rather than 15, 15, 15, 15. Is that enough of a departure to call a real preference? Commit to an answer before you run it.

Check your output — open this after you have run the code yourself

What R returns:

	Chi-squared test for given probabilities

data:  observed
X-squared = 4.5333, df = 3, p-value = 0.2093

You can also ask R what it expected, and where the biggest departures were:

result <- chisq.test(x = observed, p = expected)
result$expected       # what you'd expect under H0
result$residuals      # standardized residuals

# expected
[1] 15 15 15 15

# residuals
[1] -0.775  0.775 -1.291  1.291

Question C1. Find X-squared, df, and the p-value in that output and record them.

StatisticYour value
X²
df
p

Question C2. Where did df = 3 come from? You had four categories.

Question C3. The residuals are −0.775, 0.775, −1.291, 1.291. Which arm was entered most above expectation, and how do you know from that vector alone?

Question C4. Was your prediction right? Write one sentence stating what you can and cannot conclude about the rat's preference from p = .209.

Before you go: significance is not strength

Chapter 21 is blunt about this. Chi-square is sensitive to sample size: a tiny, practically meaningless association can come out significant when N is large enough. A significant χ² tells you the association is probably not zero — not how large it is.

That is why the size question needs its own statistic. For tests of independence this course reports Cramér's V, and it is reported regardless of whether the test was significant. You will compute it in a later module.

For a goodness-of-fit test like the maze study, the effect size is Cohen's w:

w = √(χ² / N)

For the maze data, χ² = 4.5333 and N = 60, so w = √(4.5333 / 60) = 0.27. By convention 0.1 is small, 0.3 medium, and 0.5 large — but read those the way Chapter 27 tells you to read Cohen's benchmarks: as rough conventions, not thresholds that decide whether a finding matters.

Now watch what w does that p cannot. Keep the rat's preference exactly as it is and just run 100× more trials — the same proportions, 1200 / 1800 / 1000 / 2000 instead of 12 / 18 / 10 / 20:

chisq.test(x = c(1200, 1800, 1000, 2000), p = rep(0.25, 4))

X-squared = 453.33, df = 3, p-value < 2.2e-16
N = 60N = 6000
χ²4.53453.33
p.209< .001
Cohen's w0.270.27

The preference never changed, and w never moved. χ² grew a hundredfold and p fell off a cliff — both of those track sample size. w tracks the effect. That is the whole reason this course asks for an effect size on every result, significant or not.

Question C5. Suppose you re-ran the maze study with 6,000 trials instead of 60, and the proportions came out exactly the same. Would you expect the p-value to go up, go down, or stay put? Would the strength of the preference have changed?

Now let R compute w. There is no assocstats() shortcut for a goodness-of-fit test — you build w straight from the test object. This is the Chapter 21 code:

result <- chisq.test(x = observed, p = expected)

w <- sqrt(result$statistic / sum(observed))
w
Check your output — open this after you have run the code yourself
X-squared
0.2748737

R carries the X-squared label over from result$statistic. Ignore the label — the number is w, not a chi-square. Rounded, w = 0.27.

Question C6. Record w, then write one sentence saying what it tells you about the rat's preference that p = .209 does not.

w =

Before you submit

Grading: Credit/no-credit, not scored for correctness — but credit requires a genuine attempt; blank or clearly-unattempted answers earn no credit. Work through it with your partner and say what you actually think; a wrong idea you reasoned your way to is worth more here than a right one you copied.

How to submit

  1. Click Save progress.
  2. Print: Ctrl+P (Windows) or Cmd+P (Mac).
  3. Choose "Save as PDF".
  4. Save as chisquare1_lastname1_lastname2.pdf and upload to the course site.