Chapter exercise: Comparing Groups: p-values and ANOVA
Does Income Differ by Region?
This exercise goes with Chapter 18, Comparing Groups: p-values and ANOVA. It uses joins (Chapter 15) and the summary statistics and boxplots from the ESDA exercise (Chapter 16). It should take you about one hour. Read this whole section before you start.
Save it in a new folder called anova-exercise inside your geog215 folder, open it in RStudio, and work through it from top to bottom. Everything on this page is also at the top of the file.
Purpose
Skills you’ll practice. By the end of this exercise you will be able to:
- Calculate group means, the sum of squares between and within groups, and the F statistic, by hand and with
aov(). - State a null hypothesis, and read a test result in three steps: the statistic, the p-value, and what it means for the question.
- Explore groups with summary statistics and boxplots before testing them.
- Follow a significant ANOVA with Tukey’s HSD to find which groups differ.
- Check ANOVA’s assumptions, and use
kruskal.test()when they fail. - Explain how outliers, the way regions are drawn, and neighboring places affect a test on spatial data.
Knowledge you’ll gain. You’ll test whether median income differs between the four Census regions of the United States, and find out how much the answer depends on one unusual place.
Why this matters. “Are these places really different, or is it chance?” is one of the most common questions in spatial data science, and in the news. Reading a p-value correctly, checking what’s behind it, and knowing when a test’s assumptions don’t hold will keep you from overclaiming in your final project, and help you judge the claims other people make with statistics.
Task
Work through Parts 1 to 7 in order, top to bottom. In each part:
- Read the short explanation, then do each step marked with a bullet (•).
- Wherever you see
___, replace it with the missing code before you run the chunk. - Answer each question (Q1, Q2, …) in a sentence or two of Markdown, where it says Your answer here.
- When you report a test, give the statistic, its degrees of freedom, and the p-value, and say what it means in words.
| Part | What you’ll do | About |
|---|---|---|
| 1 | Set up your file and load the data | 3 min |
| 2 | ANOVA by hand | 12 min |
| 3 | Explore income by region | 10 min |
| 4 | Run the ANOVA | 8 min |
| 5 | One unusual place | 10 min |
| 6 | Which regions differ? Check the assumptions | 12 min |
| 7 | Reflect, knit, and turn in | 5 min |
Turn in your .Rmd file and your knitted .html file on Canvas. You can do the Part 2 hand calculations on paper and type your answers into Q1.
Criteria for success
Use this checklist before you turn your work in. Each item matches a row of the rubric below.
| Criterion | Full credit | Partial credit | No credit |
|---|---|---|---|
| Knits (2 pts) | Knits to HTML with no errors | Knits, but some chunks have errors | Doesn’t knit, or no HTML turned in |
| Hand calculation (3 pts) | Means, SS between, SS within, and F correct | Some steps wrong | Not done |
| Code (4 pts) | Every blank filled in correctly | Most blanks correct | Many blanks missing or wrong |
| Reporting tests (4 pts) | Statistic, df, p-value, and plain-English meaning every time | Some parts missing | Results not interpreted |
| Questions (6 pts) | Q2 to Q11 answered correctly, with reasons | Some answers missing or wrong | Most answers missing |
| Reflection (1 pt) | All three answered | Missing |
What a good answer looks like. Here is a question, a weak answer, and a strong one.
Question: What does a p-value of 0.03 from an ANOVA mean?
Weak answer: “There’s a 3% chance the result is wrong.”
Strong answer: “If the groups really had the same mean, we’d see differences this large or larger only about 3% of the time by chance. That’s below 0.05, so we reject the null hypothesis that all the group means are equal. It doesn’t tell us which groups differ, or whether the difference is big enough to matter.”
The strong answer states what the p-value is a probability of, connects it to the null hypothesis, and says what the test doesn’t tell you.
IMPORTANT! Save often. Before you knit, choose Run > Restart R and Run All Chunks to make sure every chunk works from a fresh start.
GEOID with
left_join().
One number that describes many values, such as their mean, median,
maximum, or count. summarize() calculates them.
EDA that also asks how values vary across space, using maps, spatial
autocorrelation, and clustering.
The integrated development environment (IDE) where you write and run R.
R does the computing; RStudio is the workspace around it.
The squared distances of values from a mean, added up. ANOVA splits it
into between-group and within-group parts.
The claim a test starts from: that there is no difference or no
relationship. A small p-value is evidence against it.
Tukey’s honestly significant difference test: compares every pair of
groups after an ANOVA, with p-values adjusted for the number of
comparisons.
A value far from all the others. It may be real or an error, so check it
before you report it.
The middle value when values are sorted, so half are above and half
below. Unlike counts, medians can’t be added across areas.
The use of data that have a location attached to answer questions about
the world. It combines what we study (the spatial), how we
handle it (data skills), and how we ask and answer questions
(science).
A block of R code inside an R Markdown file, starting with
```{r} and ending with three backticks.
A simple way of formatting plain text: # for headings,
*italic*, **bold**, and - for
list items.
The number of values free to vary in a calculation. In ANOVA, k
− 1 between groups and N − k within them.
To build an R Markdown file into a finished document (HTML, PDF, or
Word). Knitting reruns every code chunk from the top in a fresh R
session.
A language, like R, that runs code one line at a time: an interpreter
reads each line, translates it, and runs it before moving on.