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.
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/rethinkingSPSS, 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,
statsmodelsfor the models,scipyfor the classical tests, andscikit-learnfor principal components and factor analysis:
pip install pandas numpy scipy statsmodels scikit-learnReproducibility
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.