Week 14: Course Review & Oral Exam Prep
2026-07-16
Part 1: Statistical Programming Foundations
Part 2: Working with Real World Data
Part 3: Advanced Topics & Summary (Not Examined!)
This course aims to support you in learning from data using statistical reasoning AND computational tools.
Statistical
Programming
Navigate
Create & remove
Tip
Windows uses cd for pwd, dir for ls, type for cat, and del for rm.
| Part | What it does |
|---|---|
function(arg1, arg2 = default) |
declares the function and its inputs |
{ ... } |
body — the code that runs |
return(...) |
what the function hands back (optional) |
Tip
If there is no return(), R returns the value of the last expression.
Test on known inputs, then stress-test
Warning
R coerces types silently — wrong answers with no error are worse than a crash.
Validate inputs explicitly
stopifnot() — quick but blunt:
if / stop() — custom message:
“you gave me THIS, but I need THAT”
DRY → DRRY
Outside-In vs Inside-Out
Tip
If you struggle to name a function, it’s probably doing too many things.
Outside in:
Name: What am I trying to do? What evokes the action
Inputs: What pieces of information do I need to provide?
Output: What are we returning?
Inside out:
Copy text into body
Identify complexity to manage
Abstract the complexity
{reprex} to format it[r]), Posit CommunityThree condition types
| Type | What to do | |
|---|---|---|
| 🚨 | Error | Must fix |
| ⚠️ | Warning | Inspect output |
| 💬 | Message | Read it |
Troubleshooting steps
Minimal
Reproducible
library() callsdput() for real dataset.seed() if randomness is involvedTip
Writing a reprex often finds the bug for you — making code self-contained reveals missing library() calls or stale variables.
| Goal | Tool |
|---|---|
| Locate where the error occurred | traceback(), rlang::last_trace() |
| Pause and inspect inside your function | browser() |
| Debug a package function without editing it | debugonce(fn), debug(fn) |
| Debug outside R (YAML, paths, render) | quarto render in terminal |
Set up & inspect
Stage & commit
The three areas
working directory → staging area → repository
(your files) (git add) (git commit)
Think of it as:
git add)git commit)init, add, commit — the core loop; each commit is a snapshot with a parent pointergit log) to browse and restore past versions.gitignoreIf you already have a local repo
-u sets origin/main as the upstream — after this, plain git push works.
Tip
git remote -v confirms the connection — you should see origin pointing to your GitHub URL.
git pull — fetch and merge any remote changes firstgit push — send your commits to GitHubTip
Make it a habit: pull first, then push. This avoids most “rejected push” errors.
Branches, stash & worktrees
git switch -c <name>git stash / git stash popgit worktree when you need two branches open at oncePull requests & conflicts
git push -u origin <name>, then open a PR on GitHub<<<<<<< markers, git add, then git commitTip
Pull at the start of every session, push at the end — this keeps conflicts small.
_quarto.yml — it renders all .qmd files and shares options across themquarto publish gh-pagescode-fold, code-line-numbers) under format: html: and execution options (echo, freeze) under execute: in _quarto.yml.qmd files_quarto.yml renders the whole repo as a website — proposal, report, and README in one placeusethis & devtools help to streamline package developmentmunichvisitors) distribute clean, documented datasets — a great first packageroxygen2 turns #' comments into .Rd help files — document as you write@param, @returns, @export, and @examplesdata = museum_visitors) when users may want to filter or swap itdata first, options last with sensible defaultsTip
Ask: if a colleague installed this tomorrow with no context, would the function names tell them what to call and when?
lubridate for dates; other specialist packages for geospatial (sf), time-of-day (hms), factors (forcats) etc.glimpse() for the whole table; str() for a single columnsummary() hidesas.*()), flag/replace out-of-range values, standardise encodingsdistinct()), reshape (pivot_*()), split/merge columns (separate(), unite())NA) — visible; find with is.na(), summary(), naniar::vis_miss()tidyr::complete() or tsibble::fill_gaps() to make implicit missingness explicitrenv::init() creates a project-local library and records versions in renv.lockrenv::snapshot() updates the lockfile; renv::restore() rebuilds the library from itrenv.lock to git; add renv/library/ to .gitignoredata-raw/YYYY-MM-DD dates, one value per cell, plain text files{infer}specify() → assume() → calculate() → visualize() / get_p_value()assume("t" / "z" / "Chisq" / "F") uses the same theoretical distributions as Stat 1/2generate() simulates the null without assuming a distributionbroom::tidy() to get a tidy data frame first{corrr} produces tidy correlation data frames ready to pipe into gt() or rplot()count() + pivot_wider() + gt() is the standard pipelineformattable colour tiles are one lightweight option{gt}gt workflow: wrangle first, then gt(), then stack formatting (fmt_*), column (cols_*), and annotation (tab_*) layersmm, g, %) — keep cells clean with cols_label() and html() / md() helperstab_spanner() groups related columns under a shared headertbl-cap in Quarto — write what a screen reader needs to understand the findingfig-alt chunk option) conveys the chart’s message — not its structure — for screen readersplotly, leaflet, DT) add interactivity to any Quarto HTML output — no server requiredggplotly() is the quickest path from a ggplot2 plot to an interactive one{sf} + geom_sf() for choropleth and boundary maps with proper projection handling{geofacet} for small-multiple comparisons arranged in a geographic layoutpandas, numpy, matplotlib/seaborn, scikit-learn[ ], dictionaries {k: v}, tuples ( ), sets { } — and indexing starts at 0df.groupby(...).mean()) instead of functions on data (aggregate(...))dplyr verbs and ggplot2 layers have direct pandas / seaborn counterparts — the concepts transfer, only the syntax changes{reticulate}py$x, R from Python via r.x; load functions with source_python() or import()group-reflection.qmd)| Topic | Potential Sub-topics |
|---|---|
| R programming | functions · debugging · R packages |
| General programming & workflow | git · CLI · renv · Quarto websites |
| Data preparation & analysis | IDA (glimpse) · hypothesis testing (infer) · open data principles |
| Statistical communication | tables (gt) · plots (ggplot2) · documenting data analyses |
| Project management | project organisation · collaborative practices · LLM usage |
munichvisitors/
├── DESCRIPTION
├── R/
│ └── plot_museums.R
├── data/
│ └── museum_visitors.rda
├── data-raw/
│ └── museum-visitors.R
└── README.Rmd
Course Content
data-raw/ and data/?.
├── CONTRIBUTING.md
├── README.md
├── _quarto.yml
├── code
│ ├── 01_download.R
│ ├── 02_clean.R
│ ├── 03_eda.R
│ └── 04_analysis.R
├── data
│ ├── processed
│ └── raw
│ └── LICENCE.txt
├── docs
├── index.qmd
├── our-statprog2-project.Rproj
└── proposal.qmd
Reflection
Cross-topic synthesis: