Testing if Data Matches Expected Distributions
By the end of this module, you will be able to:
chisq.test() in R with specified expected proportionsA goodness-of-fit test answers the question:
"Does my observed data fit an expected pattern?"
Key characteristics:
Research Question: Do rats have equal preference for three maze arms?
Setup: Test 60 rats, each chooses one of three arms (Left, Center, Right)
Null Hypothesis (H₀): Rats choose arms equally (33.3% each)
Expected frequencies: 20 rats per arm (60 ÷ 3 = 20)
| Maze Arm | Observed | Expected |
|---|---|---|
| Left | 15 | 20 |
| Center | 18 | 20 |
| Right | 27 | 20 |
| Total | 60 | 60 |
Chi-square asks: Is the difference between observed (15, 18, 27) and expected (20, 20, 20) statistically significant, or just random variation?
Null Hypothesis (H₀): The observed distribution matches the expected distribution
Alternative Hypothesis (H₁): The observed distribution differs from the expected distribution
Unlike t-tests or ANOVA, Chi-square goodness-of-fit is non-directional. We don't predict which category will be higher - just that the pattern differs from expectation.
| Expected Pattern | When to Use | Example |
|---|---|---|
| Equal proportions | Testing if all categories are equally likely | Coin flips (50/50), die rolls (1/6 each) |
| Specified ratios | Testing Mendelian ratios, known distributions | Genetics (3:1 ratio), survey norms |
| Population proportions | Comparing sample to known population | Census data, disease prevalence |
Where:
Interpretation: Larger χ² values = bigger difference between observed and expected = more evidence against H₀
Calculate χ² by hand to understand how it works!
| Category | Observed (O) | Expected (E) | (O-E)²/E |
|---|---|---|---|
| Category 1 | - | ||
| Category 2 | - | ||
| Category 3 | - |
Now let's run this analysis in R! The function is chisq.test().
# Vector of observed frequencies
observed <- c(15, 18, 27)
# Give them meaningful names
names(observed) <- c("Left", "Center", "Right")
# Check your data
observed
# Test against equal proportions (default)
chisq.test(observed)
# OR specify custom expected proportions
# For equal: p = c(1/3, 1/3, 1/3)
chisq.test(observed, p = c(1/3, 1/3, 1/3))
Chi-squared test for given probabilities
data: observed
X-squared = 3.9, df = 2, p-value = 0.1423
What this output tells you:
Interpretation: p = 0.142 > 0.05, so we fail to reject H₀. The observed distribution doesn't significantly differ from equal proportions. Rats don't show a clear preference.
Question 1: Run the two blocks above in Posit Cloud. Your output should match ours exactly — these are fixed counts, not a simulation. Paste what you got, or note any difference.
Question 2: The test compared the rats' choices against an expected count of 20 per arm — but R never printed that number anywhere. Where does 20 come from? Show the arithmetic.
k = number of categories
Why k - 1? Once you know the frequencies in (k-1) categories and the total n, the last category is determined. That's one constraint, so we lose one degree of freedom.
Examples:
Always plot observed against expected — the test gives you one number, the plot tells you where that number came from.
This is ggplot2, the plotting system from Chapter 8. Because you already have the counts, use geom_col() (which draws bars at heights you supply) rather than geom_bar() (which counts rows for you).
library(ggplot2)
# ggplot2 wants a data frame with one row per bar
maze <- data.frame(
arm = rep(c("Left", "Center", "Right"), times = 2),
count = c(15, 18, 27, # observed
20, 20, 20), # expected
source = rep(c("Observed", "Expected"), each = 3)
)
# without this, ggplot2 puts the arms in alphabetical order
maze$arm <- factor(maze$arm, levels = c("Left", "Center", "Right"))
ggplot(maze, aes(x = arm, y = count, fill = source)) +
geom_col(position = "dodge") +
labs(title = "Rat Maze Arm Preferences",
x = "Maze arm", y = "Frequency", fill = NULL) +
theme_classic()
position = "dodge" puts the two bars side by side instead of stacking them — it is the grouped bar chart you want whenever you are comparing observed against expected.
Question 3: Make the plot. Which arm sits furthest from its expected count, and by how much?
Question 4: That gap is the biggest thing on the plot — and the test still returned p = .142. Explain, in your own words, how an obvious-looking gap can fail to be statistically significant.
Research Question: Are births equally distributed across days of the week?
Data: Hospital records show the following births:
| Day | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
| Births | 28 | 32 | 29 | 31 | 35 | 18 | 17 |
Total births: 190
Expected per day (if equal): 190 ÷ 7 = 27.14
# Your turn! Complete this R code:
# Step 1: Enter the observed frequencies
births <- c(28, 32, 29, 31, 35, 18, 17)
names(births) <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
# Step 2: Run Chi-square test
result <- chisq.test(births)
result
# Step 3: Visualize
library(ggplot2)
days <- c("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun")
births_df <- data.frame(
day = factor(days, levels = days), # keeps Mon..Sun in calendar order
births = as.numeric(births)
)
ggplot(births_df, aes(x = day, y = births)) +
geom_col(fill = "steelblue") +
labs(title = "Births by Day of Week",
x = "Day", y = "Number of births") +
theme_classic()
Question 5: Saturday and Sunday run 17–18 births against 28–35 on weekdays — roughly half. Before you reveal the output: will this test be significant at α = .05? Commit to an answer and say why.
Chi-squared test for given probabilities
data: births
X-squared = 10.716, df = 6, p-value = 0.09757
Interpretation: χ² = 10.72, p = .098. We fail to reject H₀ at α = .05. The weekend dip is clearly visible in the data — Sat/Sun run 17–18 births against 28–35 on weekdays — but with n = 190 the test cannot rule out chance. This is a useful pair to hold together: a real-looking pattern that this sample is too small to confirm.
Question 6: Was your prediction right? More importantly — does p = .098 mean births are evenly spread across the week? Answer carefully.
Research Question: Do flower colors follow Mendelian 3:1 ratio?
Background: According to Mendelian genetics, we expect 75% purple flowers and 25% white flowers.
Observed data: 152 purple, 48 white (n = 200 total)
# Test against 3:1 ratio (0.75 and 0.25 proportions)
flowers <- c(152, 48)
names(flowers) <- c("Purple", "White")
# Specify expected proportions
chisq.test(flowers, p = c(0.75, 0.25))
# Calculate expected frequencies manually
expected <- 200 * c(0.75, 0.25)
expected # Should be 150 and 50
Chi-squared test for given probabilities
data: flowers
X-squared = 0.107, df = 1, p-value = 0.744
Interpretation: χ² = 0.107, p = 0.744 >> 0.05. We fail to reject H₀. The observed data (152:48) fits the expected 3:1 Mendelian ratio very well. This supports Mendelian inheritance!
Question 7: Write the conclusion as one sentence you could put in a lab report. Include the statistic, its degrees of freedom, and the p-value in APA form.
Question 1: You test 100 animals for color preference. Observed: Red=35, Blue=40, Green=25. You expect equal preferences. What are the expected frequencies?
Question 2: You get χ² = 8.5, df = 3, p = 0.037. What do you conclude?
Question 3: A study has 4 categories. What are the degrees of freedom?
Mistake 1: Using raw data instead of frequencies
# WRONG - Don't use individual observations
responses <- c("A", "B", "A", "C", "B", "A")
chisq.test(responses) # Error!
# CORRECT - Use frequency counts
freq <- table(responses) # Converts to frequencies
chisq.test(freq)
Mistake 2: Forgetting that totals must match
Your expected frequencies must sum to the same total as your observed frequencies!
❌ Observed total = 100, Expected total = 120 → ERROR
✅ Observed total = 100, Expected total = 100 → CORRECT
Mistake 3: Misinterpreting non-significant results
If p > 0.05, we DON'T conclude "the distributions are the same." We say "we don't have evidence they differ" - absence of evidence isn't evidence of absence!
Mistake 4: Ignoring the assumption about expected frequencies
Chi-square requires expected frequencies ≥ 5 in all categories. We'll cover this more in Module 4!
Here's how to report Chi-square goodness-of-fit results in APA style:
Study: Rats choosing among three maze arms (n=60)
Results:
"A chi-square goodness-of-fit test was conducted to determine whether rats showed equal preference for three maze arms. The distribution of choices did not significantly differ from equal proportions, χ²(2) = 3.90, p = .142. Rats selected the left arm 15 times (25%), center arm 18 times (30%), and right arm 27 times (45%). Although there was a numerical preference for the right arm, this pattern could be due to chance variation."
Essential elements to include:
Research Question: Do neurons fire at uniform rates across four time periods?
Background: You record neural activity in 4 consecutive time periods and count spikes.
Observed spike counts:
| Time Period | 0-250ms | 250-500ms | 500-750ms | 750-1000ms |
|---|---|---|---|---|
| Spikes | 45 | 38 | 52 | 45 |
Your tasks:
# Complete this analysis:
# Step 1: Enter data
spikes <- c(45, 38, 52, 45)
names(spikes) <- c("0-250ms", "250-500ms", "500-750ms", "750-1000ms")
# Step 2: Calculate totals
total <- sum(spikes)
total # Should be 180
# Step 3: Expected frequency (if uniform)
expected_each <- total / 4
expected_each # Should be 45
# Step 4: Run chi-square
result <- chisq.test(spikes)
result
# Step 5: Visualize -- same observed-vs-expected pattern as before
library(ggplot2)
periods <- c("0-250ms", "250-500ms", "500-750ms", "750-1000ms")
spike_df <- data.frame(
period = rep(periods, times = 2),
count = c(spikes, rep(expected_each, 4)),
source = rep(c("Observed", "Expected"), each = 4)
)
spike_df$period <- factor(spike_df$period, levels = periods)
ggplot(spike_df, aes(x = period, y = count, fill = source)) +
geom_col(position = "dodge") +
labs(title = "Neural Spike Counts by Time Period",
x = "Time period", y = "Number of spikes", fill = NULL) +
theme_classic()
Question 8 (interpret): What does this result say about when the neuron fires? Be specific about what the test does and does not establish.
Question 9 (write it up): One APA sentence, with the statistic, df and p.
Chi-squared test for given probabilities
data: spikes
X-squared = 2.1778, df = 3, p-value = 0.5363
Calculations:
Interpretation: χ²(3) = 2.18, p = .536. We fail to reject H₀. The neural firing rate does not significantly differ from uniform across the four time periods. Although there is some variation (38 to 52 spikes), this could easily occur by chance.
Write-up:
"A chi-square goodness-of-fit test examined whether neural spike counts were uniformly distributed across four consecutive 250ms time periods. The distribution did not significantly differ from uniform, χ²(3) = 2.18, p = .536, suggesting consistent firing rates over time."
Follow these steps every time:
H₀: Distribution matches expected; H₁: Distribution differs from expected
• Categorical variable
• Independent observations
• Expected frequencies ≥ 5
Based on your null hypothesis (equal proportions, specific ratio, etc.)
chisq.test(observed, p = c(...))
Look at χ², df, and p-value. If p < .05, reject H₀
Create bar plot, write up results with all key statistics
Key Takeaways:
chisq.test(observed, p = c(...))Next up: Module 3 covers Chi-square Test of Independence - testing relationships between TWO categorical variables!
Three of the four tests you ran in this module were non-significant — the rats, the births, the neurons.
Question 10: A classmate looks at the births result and says "great, so births are evenly spread across the week." What is wrong with that sentence, and how would you say it correctly?
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.
chisq2_lastname1_lastname2.pdf and upload to the course site.