Setup and Required Packages

To run the live code in this book you need R and a small handful of packages. To reproduce the walkthrough examples - the ones shown but not executed here, because they need heavier tools - you need a few more. This page lists both, with one-line installers, so you can pull the requirements immediately.

To run the book’s live code

The executable code throughout the book is written in the tidyverse, plus a couple of publishing packages:

install.packages(c("tidyverse", "broom", "plotly", "knitr", "rmarkdown"))

tidyverse pulls in everything the chapters use for handling data: dplyr for wrangling, ggplot2 for every figure, purrr for iteration, tibble, tidyr, readr, stringr, and forcats. broom is the companion that turns a fitted model into a tidy data frame, which is how the book prints its results.

The statistical engines themselves are still base R - lm(), aov(), glm(), factanal(), prcomp(), power.t.test() - and so is every from-scratch derivation: reliability computed by hand, the Rasch estimator, the EM algorithm for latent classes, the bootstrap and permutation tests. That is deliberate. The tidyverse is how we handle and display the data; the statistics are demonstrated with tools that ship with R, so nothing important is hidden inside a package.

The book’s shared helpers

Every chapter starts by sourcing _common.R, a short file of three helpers that keep the numbers consistent. Here it is in full, so nothing in the chapters is a black box:

# House rule: every number we print is rounded to two decimal places.
round2 <- function(x, digits = 2) {
  dplyr::mutate(x, dplyr::across(
    tidyselect::where(is.numeric),
    \(v) round(v, digits)
  ))
}

# p-values are the exception. Rounded to two decimals a p of .0000003 would
# print as 0.00, which reads as "exactly zero" - and no p-value is ever exactly
# zero. So p gets three decimals and a floor, the way APA style reports it.
fmt_p <- function(p) {
  dplyr::if_else(p < .001, "< .001", sprintf("%.3f", p))
}

# tidy2() is the one used most: fit a model, get a tidy table back with the
# estimates rounded and the p-values formatted. One call, house style applied.
tidy2 <- function(model, ...) {
  broom::tidy(model, ...) |>
    dplyr::mutate(
      dplyr::across(
        tidyselect::where(is.numeric) & !dplyr::any_of("p.value"),
        \(v) round(v, 2)
      ),
      dplyr::across(dplyr::any_of("p.value"), fmt_p)
    )
}

One chapter deliberately breaks the two-decimal rule: Tables, which is about choosing how many digits to show, sets its own precision by hand.

To reproduce the walkthrough examples

Several chapters show the production tool for a job as static code (for example, lavaan for SEM, mirt for IRT, brms for Bayesian models). These are the packages the book actually names; all are on CRAN, so a single call installs them:

install.packages(c(
  "psych",    # reliability (alpha, omega), EFA, describe()
  "lavaan",   # confirmatory factor analysis, SEM, measurement invariance
  "mirt",     # item response theory (2PL, graded response)
  "poLCA",    # latent class analysis
  "mice",     # multiple imputation for missing data
  "dagitty",  # directed acyclic graphs (adjustment sets, implied independencies)
  "WRS2",     # Wilcox robust methods (trimmed-means tests)
  "MASS"      # robust regression (rlm); ships with R, listed here for completeness
))

For the Bayesian chapters, brms and bayestestR are on CRAN, but McElreath’s rethinking is not - it is distributed on GitHub. Both brms and rethinking compile models to Stan, which needs a working C++ toolchain:

install.packages(c("brms", "bayestestR"))   # on CRAN

# rethinking is GitHub-only, so install it from there:
install.packages("remotes")
remotes::install_github("rmcelreath/rethinking")
# Stan toolchain setup: https://github.com/rmcelreath/rethinking

SPSS, Julia, and Python

Every procedure chapter shows its analysis in four languages. Only the R tab runs here; the other three are there so you can do the same work in whatever you already use.

  • SPSS is commercial software. If your institution provides it, the code-tabs appendix gives the syntax for the book’s core procedures.
  • Julia is free and open source (julialang.org). Its statistics live in packages you add once:
using Pkg
Pkg.add(["DataFrames", "Statistics", "GLM", "HypothesisTests", "MultivariateStats", "MixedModels"])
  • Python is free and open source (python.org). The book’s Python tabs use pandas for data, statsmodels for the models, scipy for the classical tests, and scikit-learn for principal components and factor analysis:
pip install pandas numpy scipy statsmodels scikit-learn

Reproducibility

Every running example in this book sets a random seed, so re-running the same code returns the same numbers. If a result ever differs, check your package versions first; sessionInfo() in R prints exactly what you have loaded.