Skip to main content

Statistics for Demographic Data II

Course notes and R code for DEM 5283/7283

Author

Corey S. Sparks, PhD

Published

November 6, 2026

Preface

These are the course notes for DEM 5283/7283, Statistics for Demographic Data II, the second semester of the graduate statistics sequence in the Department of Demography at the University of Texas at San Antonio, as I’m teaching it in Spring 2027. Each chapter is one week of the course: the handout I talk through in class, with the code, the output and the homework.

The first course gave you the linear model and the logic of inference. This one takes those tools to the places demographic data actually live: complex surveys, binary and categorical outcomes, counts and rates, people nested inside places, and the same people measured over time. The organizing idea is that every method comes with a real data source and the design that produced it. So the survey design from week 2 doesn’t go away after week 2. It follows us into the logit models, the decompositions, the imputations and the multilevel models, because a model fit to survey data that ignores the design describes the sample, not the population, and its standard errors are usually too small.

How the book is organized

The chapters keep their week numbers, because the text refers to “week 2” or “next week” all the time. Week 8 is spring break, so its chapter is a short, optional review sheet, and week 16 is the final paper, so its chapter is a guide to the paper and the presentation.

Every chapter opens with a box that states the week’s question, what you should be able to do by the end, what you need from earlier weeks, and which sections are optional. Sections marked (optional) or (extension) go beyond what the homework requires. Each chapter also includes at least one example of a right and a wrong way to interpret a result, because the wrong ways are usually the tempting ones.

  • Workflow and survey data (weeks 1 and 2) sets up a reproducible project with Quarto, renv and Git, and then covers weights, strata, clusters and replicate weights.
  • Categorical and count outcomes (weeks 3 to 6) covers logit and probit models, comparing coefficients across nested logits, ordinal and multinomial models, and models for counts and rates, with predicted probabilities and average marginal effects throughout.
  • Decomposition and missing data (weeks 7 to 9) covers Kitagawa and Oaxaca-Blinder decomposition, an optional review over spring break, and multiple imputation that respects the survey design.
  • Longitudinal, nonlinear and multilevel models (weeks 10 to 13) covers GEE, fixed effects and within-between models, splines and GAMs, and two weeks of multilevel models, ending with growth curves and the age-period-cohort identification problem.
  • Causal inference and prediction (weeks 14 to 16) covers DAGs and propensity score weighting, tree-based machine learning with tidymodels, and a guide to the final paper.

Each chapter ends with that week’s homework. Each one starts with its purpose and what a satisfactory answer looks like. From week 2 on, the homework comes in two versions: one you do with the complex survey data you chose in week 2, and one with my data. Students in DEM 7283 do the version with their own data, and students in DEM 5283 can do either. The code chunks are left empty for you to fill in, and some have hints you can open.

Getting set up

Everything is done in R, and the Before the First Class page walks you through installing R, an editor and the course packages. The short version is one line in the R console:

source("https://raw.githubusercontent.com/coreysparks/DEM7283/master/install_first_7283.R")

Week 1 goes through the rest of the workflow (projects, here(), renv and Git) in detail.

Data

Most of the examples use the 2024 Behavioral Risk Factor Surveillance System SMART metropolitan and micropolitan area file (BRFSS MMSA) from the CDC (Centers for Disease Control and Prevention 2025). The other data sources come in where a method needs them:

Data Used in Source
BRFSS SMART MMSA, 2024 Most weeks CDC BRFSS SMART
DHS model dataset (women’s file, ZZIR62FL) Weeks 2, 4, 5 The DHS Program model datasets
ACS public use microdata, Texas, 2024 Week 2 Census Bureau, through tidycensus
IPUMS USA ACS extract, Texas Weeks 2, 7, 11 IPUMS USA (Ruggles et al. 2025)
Area Health Resources File, 2024-2025 Week 6 HRSA AHRF
Compressed Mortality File, Texas and California Weeks 6, 7 CDC WONDER
ACS 2019-2023 county estimates, summed to metro areas Weeks 12, 13 Census Bureau, through tidycensus
ECLS-K:2011, kindergarten to fifth grade Weeks 10, 13 NCES (National Center for Education Statistics 2019)
IPUMS NHIS, 2000-2014 adults Week 13 IPUMS Health Surveys

All of these are public, and the files the chapters use are in the data/ folder of the course repository, along with the scripts in R/ that build or read them. The DHS model data describe no real country, so anyone can use them without an application. IPUMS data come with citation requirements, and if you use the IPUMS extracts in your own work, you need to cite them the way IPUMS asks. The ACS examples need a free Census API key, and week 2 shows you how to set one up.

What changed from earlier versions

I last taught this course in 2022, from a set of R Markdown handouts and a three-chapter bookdown draft. Those are still in the repository, in code/ and archive/, but this book replaces them. The calendar is new. Principal components is gone, and event history models are out because they have their own course. Decomposition, missing data with survey designs, fixed effects and within-between models, splines and GAMs, causal inference and machine learning are new. Average marginal effects and predicted probabilities now sit next to odds ratios everywhere, and the BRFSS examples moved from the 2017 and 2020 files to 2024.

Some exercises also have Python versions (the *_python.qmd files in the repository). R is the primary tool in the course, and those versions aren’t part of this book.

Building the book yourself

The book is a Quarto book. Clone the repository, open the folder in Positron, run renv::restore() to get the package versions I used, and then run quarto render in the terminal. Several chapters take a while (the multiple imputation, the weighted multilevel models and the model tuning in week 15 especially), so Quarto saves the results in _freeze/ and only reruns a chapter when its source changes. The published version at https://coreysparks.github.io/DEM7283/ is rebuilt by GitHub Actions every time the master branch changes.