Skip to main content

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. The main running example is Wages1, and USArrests appears mostly in Test Yourself callouts and exercises. 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–17, 19–24, 29
state.region built in 50 US states in 1977 3, 12–14, 21, 23, 29
anscombe built in 11 points in each of four pairs, published in 1973 15
UCBAdmissions built in 24 cells of Berkeley graduate admissions in 1973 14, 21
LifeCycleSavings built in 50 countries, averaged over 1960–1970 3, 21
Wages1 Ecdat package 3294 workers in 1987 2–3, 5–8, 10–25, 29
wage2 wooldridge package 935 US working men in 1980 13, 29
finance-charts-apple.csv web 506 trading days from 2015–2017 26
tylervigen.csv web varies, yearly within 1996–2010 28

The help page ?Ecdat::Wages1 says 1976–1982, but those dates belong to Ecdat::Wages; the publisher’s note on the source data says 1987.

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')
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 in 1980 dollars
wage2 educ, wage Years of education, and monthly earnings
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, so sample statistics can be compared with a known truth. Chapters 5, 7, and 8 draw one random sample of 50 of these workers as their lab data. Chapters 18 and 19 do the same with school and wage stored in population_xy.
  • 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(), when to call one function with ::, and why datasets load with data().

Package What the book uses it for Chapters
Ecdat The Wages1 dataset 2–3, 5–8, 10–25, 29
wooldridge The wage2 dataset 13, 29
mvtnorm Draws from a multivariate normal distribution, with rmvnorm() 14, 18, 21
plotly Interactive figures, with plot_ly() 20, 22, 26
reactable Interactive tables 20
stargazer Formatted regression and summary tables 20, 27–28
psych Scatterplot matrices, with pairs.panels() 21
fixest Fixed-effects and instrumental-variable regressions, with feols() 23, 27–28
sf Vector spatial data, such as county borders 26
terra Raster spatial data, such as an elevation grid 26
units Attaching units such as kilometers to distances 26
knitr Placing images and tables on the page, in code the book hides 1, 17, 20
# 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), and DescTools for Cramer’s V (14). Chapter 25 points to strucchange for the Chow test and segmented for an estimated break point. Chapter 27 points to ivreg for instrumental-variable regression, and Appendix B points to car for variance inflation factors.

Chapter 21 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