Understanding Categorical Data Analysis
By the end of this module, you will be able to:
chisq.test() in R and locate X-squared, df, and the p-value in its outputBefore we can understand Chi-square, we need to be crystal clear about what kind of data we're working with.
Categorical variables represent groups, categories, or classifications. They answer "what kind?" or "which category?"
Examples:
Continuous variables represent measurements or counts. They answer "how much?" or "how many?"
Examples:
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!
Drag each variable into the correct category. This skill is essential for choosing the right statistical test!
Drop categorical variables here
Drop continuous variables here
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?"
One categorical variable
Tests if observed distribution matches an expected distribution
Example questions:
Two categorical variables
Tests if two variables are related or independent
Example questions:
Let's see how Chi-square works with a simple example:
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.
| Direction | Expected Count |
|---|---|
| Left | 30 |
| Right | 30 |
| 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.
Use Chi-square when ALL of these are true:
DO NOT use Chi-square when:
For each research question below, decide if Chi-square is appropriate or if you should use a different test.
Question 1: A researcher measures reaction time (in milliseconds) for participants who drank coffee vs. no coffee. Should they use Chi-square?
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?
Question 3: You want to know if males and females differ in their choice of three different habitats (forest, grassland, desert). Which test?
Key Takeaways:
Next up: Module 2 will teach you how to run Chi-square goodness-of-fit tests in R and interpret the results!
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.
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.
| Statistic | Your 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.
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 = 60 | N = 6000 | |
|---|---|---|
| χ² | 4.53 | 453.33 |
| p | .209 | < .001 |
| Cohen's w | 0.27 | 0.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
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 =
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.
chisquare1_lastname1_lastname2.pdf and upload to the course site.