1Objectives¶
By the end of this lesson you will be able to:
Explain what R is and why an introductory statistics course would treat it as a real, gradable skill rather than an accessory.
Explain what “reproducible” means for a data analysis, and why it matters for your grade (if you’re taking a course) and for real research.
Predict and demonstrate the effect of
set.seed()on random results.Describe how this book, the coursebook, and the labs fit together.
2What is R?¶
R is a free, open-source programming language built specifically for statistics and data analysis. It was created by statisticians, for statisticians, which is why — unlike a general-purpose language — it comes with data analysis ideas baked in from the start: data frames (spreadsheet-like tables), built-in statistical tests, and a huge ecosystem of add-on packages (more in L05) written by the statistics community.
R is not a calculator app and not a menu-driven program like a spreadsheet. You write short lines of code that say exactly what you want done, R does it, and shows you the result. That extra step — writing the instructions down — is exactly what makes an analysis reproducible (more below). Some courses grade it directly: at CSUB, for example, R proficiency is a named learning outcome in MATH 2200 (Course Learning Outcome CLO8). Whether or not your own course numbers it that way, this is the skill this book teaches.
3What “reproducible” means¶
A data analysis is reproducible when someone else (including future you, a week from now) can run the same code on the same data and get the exact same answer — not a similar answer, the exact same numbers, plots, and conclusions.
Three ingredients make an R analysis reproducible:
The code — every step written down, in order, as a script or notebook (not clicks you can’t retrace).
The data — a specific file or dataset, not “whatever was in my spreadsheet that day.”
The random seed — if any step involves randomness (and several methods in this book do — see L08 and L09), the starting point of that randomness has to be fixed, or “random” results will be different every time you run the code.
That third ingredient is the one that surprises people, so let’s see it in action.
4Demonstration: why set.seed() matters¶
R can simulate randomness — for example, “roll a fair six-sided die” — with
sample():
sample(1:6, size = 1)[1] 1Run that exact same line again, with nothing else changed:
sample(1:6, size = 1)[1] 5Different number. That’s expected — it’s a random roll — but it means if you handed this code to a classmate, they could not check their answer against yours, and you could not check your own answer a second time. For homework, labs, and answer keys, that’s a problem.
The fix is set.seed(). It tells R exactly where to start its “randomness,”
so the sequence of “random” numbers that follows is the same every time:
set.seed(2200)
sample(1:6, size = 1)[1] 4Now reset the seed to the same value and roll again:
set.seed(2200)
sample(1:6, size = 1)[1] 4Same seed, same starting point, same “random” result — every time, on every computer, forever. That is what makes a simulation-based method (which this book uses heavily from L08 on) checkable: if an instructor’s answer key was produced with a fixed seed, it can be checked exactly.
5A first taste: R computing a real number from real data¶
R ships with some datasets built in, and this book adds the mosaic
package (introduced fully in L05), which supplies a very readable
way to ask for summaries. Here is the average length, in minutes, of an
eruption of Old Faithful geyser — a real, famous dataset that ships with R
itself:
set.seed(2200)
mean(~ eruptions, data = faithful)[1] 3.487783You will not fully understand the ~ eruptions syntax yet — that is
L05’s job — but notice what just happened: real code ran on a
real, documented dataset (?faithful in R tells you its source) and produced
a real number, printed right here. Every number in this book, and in every
lesson that follows, is produced exactly this way.
6How this book is organized¶
This is lesson 1 of 14. L02 and L03 get R and RStudio (or the CSUB JupyterHub) running and get you comfortable working in a notebook or script. L04 through L07 teach the language itself and the mosaic/BSDA toolkit for exploring data. From L08 on, the lessons follow the order most introductory statistics courses use — probability, sampling distributions, inference, ANOVA, regression — arriving exactly when a typical course needs each one. See the table of contents for the full map.
7Summary¶
R is a free, open-source language built for statistics; this book (and courses like MATH 2200, where it’s CLO8) treats it as a real skill, not an accessory.
A reproducible analysis needs fixed code, fixed data, and — whenever randomness is involved — a fixed seed (
set.seed()).set.seed(2200)before any random step is this book’s standing rule.Every number, table, and plot in this book was produced by R code you can see, run, and check yourself.