Run the Numbers

The R Companion to the Margin of Error Series

Author
Published

January 2027

Run the Numbers

The R Companion to the Margin of Error Series

The free R companion to the Margin of Error series. It teaches the R and tidyverse that the books use, from installing R to reading code a model wrote. It also contains the code behind every figure in Make It Visible.

What This Is

The books in the Margin of Error series show their analyses in R. Margin of Error runs its statistics in R. Make It Visible draws every figure in R and ggplot2. Before the Numbers and Beyond the Numbers analyze their survey and interview data in R. None of the four teaches R. Explaining a filter() call inside a statistics chapter would interrupt the statistics. This companion holds that material.

It is free and online only. Read it from the start if you are new to R. Open it at the chapter you need if you are reading one of the books and want to see how a result was produced.

Why Learn R When a Model Will Write It

A language model will write R on request, faster than you can type. The skill that matters most is now reading what it wrote.

A first-year analyst at a regional retailer asked a language model for code that flagged customers likely to leave. The code ran. The reported accuracy was 95 percent. Her director asked which customers to call, and the list came back empty. The code had reached 95 percent by predicting that nobody would leave, since about 5 percent of customers leave in a typical quarter. The analyst had not read the code closely enough to see that it predicted no departures.

Fewer analysts can read code than can prompt for it. A model turns a description into code that runs. It does not tell you whether the code does the right thing for your data, or whether the output means what it appears to mean. Every chapter here pairs the R with a check. Chapter 16 is about the checks alone.

How the Companion Is Organized

Part Chapters What it covers
Getting started 1 to 3 What R is, installing it, finding your way around, and the datasets the series uses
Working with data 4 to 8 The tidyverse, reading data in, and the dplyr and tidyr verbs used most often
Summarizing and plotting 9 to 11 Summary statistics and ggplot2, from a first plot to a finished one
Statistics 12 and 13 The tests and models that Margin of Error uses, in R
Reports 14 and 15 Quarto and R Markdown, so the analysis and the writing are in one file
Code a model wrote 16 Reading, running, and checking model-written R
Reproduce the figures 12 pages The code behind every figure in Make It Visible, one page per chapter
Appendix A to D Strings, factors, dates, and iteration, for when a chapter needs them

Each chapter opens with a problem an analyst has to solve and shows the R that solves it. It ends with an AI Pitfall. That box shows model-written code for the problem that runs but gives a wrong result, and the check that exposes it.

What You Need

R and an editor. Both are free. RStudio is the long-standing choice, and Positron is Posit’s newer editor built on VS Code. Either works, and the code here does not depend on which you use. Chapter 2 covers the installation.

The Series

Margin of Error, on statistics, at stats.marginoferrormedia.com. Make It Visible, on charts and data storytelling, at viz.marginoferrormedia.com. Before the Numbers, on quantitative research design, and Beyond the Numbers, on qualitative research. All from Margin of Error Media.

Corrections

Errors and suggestions go to github.com/patilv/run-the-numbers-feedback or to patilv@gmail.com.

A Note on AI as Research Partner

This book was researched, written, and produced with AI as a research partner. I used Claude, developed by Anthropic, throughout the process: to explore ideas, pressure-test reasoning, draft and revise passages, identify gaps in coverage, and build the technical infrastructure behind the companion website, interactive tools, and publishing pipeline. Claude Code helped construct the Quarto projects, the Shiny applications, and the automation that made a project of this scope feasible for a single author.

I want to be direct about this because I think readers deserve honesty, not theater. AI did not write this book in the way that matters. Every claim has been verified against primary sources. Every analytical position reflects my own judgment, shaped by two decades of teaching and research. Every sentence has been read, reconsidered, and revised by a human who cares whether it is right. The responsibility for what appears here, including any errors, is entirely mine.

The content will improve iteratively. If you find something that needs correcting, or if you have suggestions, I welcome them. Each book has its own feedback page (linked on the companion site and in the preface), or you can email me directly at patilv@gmail.com.

I mention this not as a disclaimer but as a matter of principle. The norms around AI use in scholarly work are being negotiated in real time. I would rather be transparent about my process than pretend the tools I used do not exist. If this book helps you think more clearly about data, the fact that an AI helped me write it does not make the thinking less clear. And if something in this book is wrong, the fact that an AI helped me write it does not make it less my fault.