Worked examples of the standard multivariate methods in R, applied to survey data: PCA, factor analysis, clustering, MANOVA, discriminant analysis, and ordination.
Each method has a notebook that reads as a report. The shared cleaning, diagnostic, and plotting
code lives in R/ so the notebooks carry analysis rather than boilerplate.
| Notebook | Method | What it answers |
|---|---|---|
notebook/PCA.Rmd |
Principal components | Which dimensions carry the variance |
notebook/Factor Analysis.Rmd |
Factor analysis | Which latent factors explain the correlations |
notebook/Clustering.Rmd |
k means and hierarchical | Which respondents group together |
notebook/Manova.Rmd |
MANOVA and MANCOVA | Whether group means differ across several outcomes |
notebook/Discriminant.Rmd, Discriminant2.Rmd |
LDA and QDA | Which variables separate known groups |
notebook/Ordination Analysis.Rmd |
CA, CCA, NMDS | How rows and columns relate in low dimensions |
clustering/Clustering.Rmd is a longer version of the clustering analysis, and
notebook/Clustering_project/ holds a written up variant with its data and rendered pdf.
R/setup.R package groups, installing and loading, plus load_modules()
R/survey.R reading exports, dropping descriptors, filtering, mode imputation, typing
R/multiselect.R expanding select all that apply answers into indicator columns
R/diagnostics.R multivariate normality by block, outliers, correlation, factorability
R/pca.R fitting, variance explained, retention rules, loadings, plots
R/clustering.R distances, choosing k, k means, hierarchical, silhouette, profiles
scripts/run-pca.R the PCA pipeline end to end from the command line
tests/ base R checks that need no data
notebook/ the analyses, one per method
legacy/ the original PCA script, kept for reference
source("R/setup.R")
load_modules()
load_packages(install = TRUE)In a notebook that replaces the whole library block. From the command line:
Rscript scripts/run-pca.R "Travel Study 2.7.23.csv" outputTables and charts land in output/. The survey csv is not in this repository.
Rscript tests/test-functions.R20 checks over mode imputation, survey filtering, column dropping, type coercion, PCA variance accounting, and block splitting. Base R only, so they run without the modelling packages installed.
The analyses were unchanged in substance. The repetition around them was the problem.
| Before | After |
|---|---|
| The same eleven line library block in 10 files | load_packages() with named groups |
calc_mode defined 8 separate times |
once in R/survey.R |
| 3 near identical blocks expanding multi select answers | expand_all_multiselect() over a mapping |
7 hand written mvn() calls over column ranges |
normality_by_block() |
| Cleaning steps repeated per notebook | composable functions in R/survey.R |
670 KB of .RData and .Rhistory committed |
removed and ignored |
The old src/PCA.R also carried a stray df = re assignment and read an input.xml that the
rest of the script never used, both leftovers from an earlier draft. scripts/run-pca.R
replaces it, and the original is kept at legacy/pca-original.R.
Verified on R 4.6.1: every module parses, and the checks above pass.
Not reproduced end to end. The survey csv is not in the repository, so the fitting and plotting paths have not been run against real data. Treat the first run as a replication check.
The notebooks still contain their original inline code. They are the record of the analyses as written, so they were left intact rather than rewritten, and the modules are available to them whenever you want to trim one down.
See the repository. Original analyses 2023.