Unit 01.04: Who does what: base stats, broom, infer, ggplot2
Four tools, four jobs. Confusing them is why results tables get assembled by copying numbers out of printed output.
Who does what
base stats runs the tests. t.test(), prop.test(), chisq.test(), lm(), glm() — they return rich objects and print a human-readable summary.
broom converts those objects into data frames. tidy() gives one row per term with the estimate, the statistic, the p-value and the confidence bounds as columns. That is what lets twenty tests become one table without anybody retyping anything.
infer expresses the same tests as a pipeline built from simulation, which makes the logic of a hypothesis test visible rather than hidden inside a function.
ggplot2 shows the data the test summarised. It is not an alternative to any of them.
This block runs one test and shows both the printed form and the tidied form.
suppressPackageStartupMessages({library(broom); library(ggplot2)})
set.seed(11)
control <- rnorm(40, 100, 12)
treated <- rnorm(40, 106, 12)
# base stats: runs the test, prints for a human
test <- t.test(treated, control)
cat("--- base stats print ---\n")
cat("t =", round(test$statistic, 3), " df =", round(test$parameter, 1),
" p =", signif(test$p.value, 3), "\n\n")
# broom: turns the same object into one row of a data frame
tidied <- tidy(test)
cat("--- broom::tidy gives a table you can bind, filter and write out ---\n")
print(tidied[, c("estimate", "statistic", "p.value", "conf.low", "conf.high")])
cat("\nColumns broom produced:", ncol(tidied), "\n")
cat("That row can go straight into a results table with 20 others.\n")
cat("ggplot2's job is separate: show the data the test summarised.\n")
The printed form gives t = 4.403, df = 74.8, p = 3.5e-05 — readable, and awkward to combine with anything. tidy() returns the same result as 10 columns of a data frame, with the estimated difference of 10.3 and its interval from 5.62 to 14.9 available as values. That row binds directly onto twenty others.
The mistake this prevents
The mistake is reading numbers off the printed output and typing them into a table. It is slow, it is where transcription errors come from, and the table does not update when the data does.
Takeaway
Run tests with base stats, tidy them with broom so results become data, and use ggplot2 to show the underlying values. Never retype a number that a function already returned.
