Appendix E — Packages and Datasets


The book works from a small number of datasets on purpose, so that by the time you reach multiple regression the variables are familiar and only the method is new. This appendix lists each dataset, what its variables mean, and which chapters use it, followed by every package the book uses.

E.1 Datasets

Dataset Source Observations Chapters
USArrests built in 50 US states in 1973 2–3, 5–8, 10–11, 13–18, 20–25, 30
state.region built in 50 US states 3, 13, 22, 24, 30
anscombe built in 11 points in each of four pairs 15
UCBAdmissions built in 24 cells of Berkeley graduate admissions 16, 22
LifeCycleSavings built in 50 countries, averaged over 1960–1970 3, 22
Wages1 Ecdat package 3294 workers 2, 5–8, 11–15, 17–18, 20, 22, 26
wage2 wooldridge package 935 US working men in 1980 13, 30
countymurders wooldridge package 2197 US counties, yearly from 1980–1996 12
finance-charts-apple.csv web 506 trading days 27
tylervigen.csv web varies 29

Built-in datasets need no download: type the name to see the data, and ?USArrests for the help page. The others load as follows. The two CSV files need an internet connection and could change or move, which is itself a reproducibility lesson.

data(Wages1, package='Ecdat') # after install.packages('Ecdat')
data(wage2, package='wooldridge') # after install.packages('wooldridge')
data(countymurders, package='wooldridge')
stock <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
vigen_csv <- read.csv('https://raw.githubusercontent.com/the-mad-statter/whysospurious/master/data-raw/tylervigen.csv')

E.2 Variables

Dataset Variable Meaning
USArrests Murder, Assault, Rape Arrests per 100,000 residents
UrbanPop Percent of the state population living in urban areas
state.region (a factor) Census region: Northeast, South, North Central, or West
anscombe x1–x4, y1–y4 Four \(x\), \(y\) pairs with nearly identical means, variances, correlations, and regression lines, but very different scatterplots
UCBAdmissions Admit, Gender, Dept Admission outcome (2), gender (2), and department (6), for \(2\times2\times6=24\) cells
LifeCycleSavings sr Aggregate personal savings as a percent of disposable income
pop15, pop75 Percent of the population under age 15, and over age 75
dpi, ddpi Real per-capita disposable income in US dollars, and its percent growth rate
Wages1 exper, school Years of work experience, and years of schooling
sex male or female
wage Hourly wage
wage2 educ, wage Years of education, and monthly earnings
countymurders countyid, year County identifier, and year
murders, arrests Number of murders, and number of murder arrests
finance-charts-apple.csv Date, AAPL.Open, …, AAPL.Adjusted Daily Apple share prices, plus four columns (dn, mavg, up, direction) derived before the file was published
tylervigen.csv many Unrelated time series, any two of which tend to correlate strongly

A few features are worth knowing before you use these.

  • USArrests stores state names as row names rather than as a column, which is why the book indexes with USArrests[,'Murder'].
  • USArrests records arrest rates rather than offense rates, so differences across states reflect policing and reporting practice as well as crime.
  • state.region lists states in the same order as USArrests, so you can attach it directly without a merge.
  • Chapters 5–8 treat the 3294 workers in Wages1 as the whole population, and Chapters 15, 18, and 20 do the same with school and wage stored in population_xy, so sample statistics can be compared with a known truth.
  • With \(n = 3294\), Wages1 scatterplots need transparency to be readable, which is why Figures ties the alpha channel to sample size.

E.3 Packages

R starts with stats, graphics, grDevices, utils, and datasets already loaded, which is where lm(), plot(), rgb(), read.csv(), and USArrests come from. Every other package must be installed once and then loaded in each session, as explained in First Steps. Code Conventions explains when to load a package with library() and when to call one function with ::.

Package What the book uses it for Chapters
Ecdat The Wages1 dataset 2, 5–8, 11–15, 17–18, 20, 22, 26
wooldridge The wage2 and countymurders datasets 12–13, 30
mvtnorm Draws from a multivariate normal distribution, with rmvnorm() 14, 16, 19
plotly Interactive figures, with plot_ly() 15, 21, 23, 27
reactable Interactive tables 21
stargazer Formatted regression and summary tables 21, 28–29
psych Scatterplot matrices, with pairs.panels() 22
fixest Fixed-effects and instrumental-variable regressions, with feols() 24, 28–29
sf Vector spatial data, such as county borders 27
terra Raster spatial data, such as an elevation grid 27
units Attaching units such as kilometers to distances 27
knitr Placing images and tables on the page, in code the book hides 1, 18, 21
# Run once, in the console, not in your script
install.packages(c('Ecdat', 'wooldridge', 'mvtnorm', 'plotly', 'reactable',
    'stargazer', 'psych', 'fixest', 'sf', 'terra', 'units', 'knitr'))

On Linux, sf and terra also need the system libraries GDAL, GEOS, and PROJ. Some chapters also name a package as a pointer to a ready-made version of something they build by hand, but you do not need these to run the book’s code. They are spatstat.univar for weighted quantiles (Chapter 3), VGAM for the Dagum distribution (10), DescTools for Cramer’s V (14), strucchange for the Chow test and segmented for an estimated break point (26), ivreg for instrumental-variable regression (28), and car for variance inflation factors (Appendix B).

Chapter 22 draws a scatterplot matrix with psych::pairs.panels(). Restart R and run pairs.panels(USArrests) without the psych:: prefix. What happens, and what are two ways to fix it?

R reports that it cannot find the function pairs.panels, because psych is installed but not loaded. Either write psych::pairs.panels(USArrests), or run library(psych) once near the top of your script. If R instead says there is no package called psych, then it is not installed, so run install.packages('psych') in the console first.

Further Reading