Chapter exercise: Spatial Descriptive Statistics
Where Is the Center of North Carolina’s Births?
This exercise goes with Chapter 17, Spatial Descriptive Statistics. It builds on the ESDA exercise from Chapter 16, which used the same county data, and on writing functions (Chapter 5). It should take you about one hour. Read this whole section before you start.
Download spatial-statistics-exercise.Rmd
Save it in a new folder called spatial-statistics-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 a mean center, a weighted mean center, a central feature, and a standard distance, by hand and in R.
- Match each spatial statistic to the ordinary statistic it extends (mean, weighted mean, median, standard deviation).
- Write your own functions in R, with inputs, a return value, and tests that check them.
- Get the x and y coordinates of an sf layer with
st_coordinates(), and make a new point withst_point(). - Explain why spatial statistics must be calculated in a projected coordinate system.
Knowledge you’ll gain. You’ll find the geographic center of North Carolina’s 100 counties and the center of its births in the 1970s, see how far apart they are and why, and check whether the center of births moved between 1974–78 and 1979–84.
Why this matters. “Where is the middle?” and “how spread out?” are the first questions to ask about any set of places: stores, crimes, cases of a disease, or the homes of your survey respondents. A shift in a weighted mean center is one of the simplest ways to show that something is moving, like the US center of population moving west over two centuries (Chapter 13). Writing these statistics as functions also gives you tools you can reuse on any layer.
Task
Work through Parts 1 to 6 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. Where you see# your code here, write the code yourself. - Run the tests under each function. If a test doesn’t give the value in its comment, fix the function before you go on.
- Answer each question (Q1, Q2, …) in a sentence or two of Markdown, where it says Your answer here.
| Part | What you’ll do | About |
|---|---|---|
| 1 | Set up your file and load the data | 4 min |
| 2 | Five stores, by hand and in R | 12 min |
| 3 | Write your own functions | 15 min |
| 4 | The centers of North Carolina | 14 min |
| 5 | Central feature and standard distance for North Carolina | 10 min |
| 6 | 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 and Q2.
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 every map and plot | Knits, but some chunks have errors | Doesn’t knit, or no HTML turned in |
| Hand calculations (2 pts) | Q1 and Q2 correct, with work shown | Some values wrong | Not done |
| Functions (5 pts) | All three functions written and pass their tests | Some functions fail a test | Not done |
| Questions (8 pts) | Q3 to Q10 answered with numbers, units, and reasons | Some answers missing or without units | Most answers missing |
| Map (2 pts) | Projected, titled, both centers shown and labeled | Missing parts | Not done |
| 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 the standard distance of the five stores tell you?
Weak answer: “3.58.”
Strong answer: “The standard distance is about 3.6 km: a typical store is about 3.6 km from the stores’ mean center at (4, 4). Drawn as a circle, it shows how spread out the stores are; a chain with all its stores downtown would have a much smaller one.”
The strong answer gives units, says what the number measures, and gives a comparison that makes it meaningful.
IMPORTANT! Save often. Before you knit, choose Run > Restart R and Run All Chunks to make sure every chunk works from a fresh start.
mean(temps).
The integrated development environment (IDE) where you write and run R.
R does the computing; RStudio is the workspace around it.
The average x coordinate and average y coordinate of a set of features.
Like the mean, outliers pull it.
A mean center in which each feature counts in proportion to an
attribute, such as population.
The existing feature with the smallest total distance to all the others:
the spatial counterpart of the median.
The typical distance of features from their mean center: the spatial
counterpart of the standard deviation, drawn as a circle.
An average in which some values count more than others. Total distance
divided by total air time weights each flight’s speed by its air time.
The middle value when values are sorted, so half are above and half
below. Unlike counts, medians can’t be added across areas.
A measure of how spread out values are around their mean, in the same
units as the values. sd() in R.
What a function gives back. In R, it is the value of the last line in
the function’s body.
“Simple features”: the R package for spatial vector data. An sf object
is a data frame with a geometry column holding each row’s point, line,
or polygon.
A method for flattening the curved graticule onto a flat map. Every
projection distorts shape, area, distance, or direction.
The rules that tie coordinates to places on the Earth, including how the
round Earth is flattened onto a map (the projection). Layers must share
a coordinate system before you can combine them.
The point where a flat map of the country would balance if every
resident weighed the same.
A block of R code inside an R Markdown file, starting with
```{r} and ending with three backticks.
A note for people that R ignores. In R, a comment starts with
#.
A simple way of formatting plain text: # for headings,
*italic*, **bold**, and - for
list items.
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 set of local projections for the US, with one or more zones per state,
in feet or meters. North Carolina’s zone is EPSG:32119.