LAB 1 — TUTORIAL

Measurement, Indexing & Multi-Scale Mapping

Download the tutorial file, knit it to see the complete analysis, then run it chunk by chunk to understand each step.

What You Will Build

In this lab you will construct a composite Economic Hardship Index from five US Census variables, evaluate its reliability using Cronbach's alpha, and map the results at three geographic scales — US counties (national), Arizona census tracts (state), and Maricopa County block groups (neighborhood).

The tutorial walks through every step with fully working code. Knit it first to see the finished product, then go back and run chunk by chunk to understand how it works.

Learning Objectives
  • Retrieve Census variables using tidycensus::get_acs()
  • Compute derived rates (poverty rate, unemployment rate, etc.) from numerator/denominator pairs
  • Standardize variables to z-scores and build a composite index
  • Assess internal consistency using psych::alpha()
  • Map the index at three geographic scales using ggplot2 + patchwork

Before You Begin

1 — File and Folder Setup

Create this folder structure before downloading anything. Move the downloaded .qmd into PAF516/Lab1/ before opening it in RStudio.

PAF516/
  Lab1/
    Lab1_Tutorial.qmd            ← downloaded tutorial (do not edit)
    Lab1_Tutorial.html           ← auto-generated when you knit
    Lab1_Assignment_Howell.qmd   ← your renamed assignment copy
    Lab1_Assignment_Howell.html  ← submit this to Canvas
  Lab2/  Lab3/  ...

2 — Install System Build Tools (Windows/Mac — Run Once)

⚠ One-time setup for the entire course

R needs a system-level compiler to install certain packages used in this course. You only need to do this once — it applies to all future labs.

Windows: Run these two lines in your RStudio Console:

install.packages("installr")
installr::install.Rtools()

Mac: Run this line in your Terminal (Applications → Utilities → Terminal):

xcode-select --install

Once complete, restart RStudio before proceeding.

3 — Install Packages (Run Once)

⚠ Do this before knitting
  1. Open Lab1_Tutorial.qmd in RStudio
  2. Click inside the packages chunk (labeled #| label: packages)
  3. Press Cmd+Return (Mac) or Ctrl+Enter (PC) to run just that chunk
  4. Wait — first run downloads and installs packages and may take 5–10 minutes
  5. After it finishes, future knits will be fast — the renv block only runs once automatically
What is renv?
renv records the exact version of every package used so that code that works today still works next semester.

4 — Census API Key

The lab pulls live data from the Census Bureau. If you don't have a key yet:

  1. Register free at api.census.gov/data/key_signup.html
  2. Check email for your key (arrives within a few minutes)
  3. In the RStudio console run: census_api_key("YOUR_KEY_HERE", install = TRUE)
  4. Restart R: Session → Restart R

Running the Tutorial

Option A — Knit First (Recommended)

Click the Render button (blue arrow, top of editor) or press Cmd+Shift+K / Ctrl+Shift+K. This produces Lab1_Tutorial.html with all results — maps, tables, and the correlation matrix. Review the output to see what the completed analysis looks like.

Option B — Run Chunk by Chunk (Best for Learning)

Place your cursor inside any code chunk and press the run shortcut. Output appears inline. Fix any errors before moving on. This is how you understand what each step does.

ActionMacPC
Run current chunkCmd+ReturnCtrl+Enter
Run all chunks aboveCmd+Option+PCtrl+Alt+P
Knit / RenderCmd+Shift+KCtrl+Shift+K

What's Inside

StepWhat It Does
Step 1Retrieve Census variables with get_acs() — Arizona census tracts
Step 2Reshape to wide format and compute poverty, unemployment, education, and insurance rates
Step 3Run psych::alpha() — assess reliability of the 5-item index
Step 4Build the composite index (z-score row mean) and visualize its distribution
Step 5Pull county-level data for the entire US and map national hardship patterns
Step 6Map the index across Arizona census tracts
Step 7Pull Maricopa County block groups and map neighborhood-level variation
Step 8Combine all three maps side-by-side using patchwork

Download

Lab 1 Tutorial File

⬇ Download Lab1_Tutorial.qmd

Right-click → Save Link As. Save directly to your PAF516/Lab1/ folder. Do not open in the browser.