Day 1
If your research involves any data, it likely also involves:
This means lots of code.
We can borrow some ideas from software development to
At a bare minimum, we want our code to be:
Nice to have, but not absolutely necessary
Through automated testing
Work through a problem together, learn tools and strategies
Share solutions/questions/responses on padlet:
https://leaflet.pub/ba70ae31-c031-4b23-ae9f-86e9e99637b5
Course website:
tidyverse vs data.table, we will focus on learning the general principles.
To get credit for the course, you must attend at least 80% of the course.
I encourage you to work on the exercises and take notes as we proceed.
Slides will occasionally allow you to interact with the code. Follow along with lectures and use it for short quizzes. Try it out:
A conditional probability model over text
\[P(\text{next token} \mid \text{text so far})\]
How it’s trained
Code is a strength
Code is abundant, structured, and correct by execution — a uniquely checkable signal in training data.
Hallucination
The model generates plausible text, not verified truth.
Reasoning models (o3, R1, extended thinking) trade compute for better multi-step work, but the same failure modes apply at higher stakes.
Important
Always verify
Every citation · every number every derivation · every API call
Treat model output as a hypothesis, not a result.
Warning
Quiet deskilling
Outsourcing judgement builds dependency, not expertise. Stay in the loop intentionally.
What the model “sees”
| Typical range | |
|---|---|
| Small / fast models | 8k–32k tokens |
| Standard assistants | 128k–200k tokens |
| Long-context leaders | 1M+ tokens |
A context window is not memory — each new conversation starts fresh.
What counts against it
How to work within limits
.md files persist context across sessionsCLAUDE.md project file that persistsTip
Practical rule
If the conversation is getting long, start fresh — older context degrades response quality before it truncates.
Move up the tiers as the task grows in scope and complexity.
Tier 1 Chat interfaces
ChatGPT · Claude · Gemini · Copilot Chat
Tier 2 In-IDE assistants
GitHub Copilot · RStudio Copilot ellmer · chattr · gander · chatlas
gander knows your data frame’s columns — context-aware suggestionsTier 3 Coding agents
Claude Code · Codex · Copilot · Pi.dev · NanoClaw
Tip
Suggested on-ramp: start with copilot in RStudio/vscode. It is the lowest-friction step up from copy-pasting to the chat window.
What makes agents different
DESCRIPTION, existing tests, READMEHuman-in-the-loop is not optional
Workflow
You: "Write a function coverage() that
computes CI coverage, with tests"
Agent: reads repo → writes function
→ writes tests → runs tests
→ iterates on failures
→ proposes diff
You: review diff → run tests yourself
→ commit if happy
Warning
The agent proposes; you decide. Never let an agent do something without a human reviewing.
Agents live in the terminal and work with plain text. A little fluency here pays outsized dividends.
Key skills
Navigate a project directory from the command line
Read and write plain text files (.md, .R, .py, .txt)
Understand what a project root looks like:
my-project/
├── R/ # functions
├── analysis/ # scripts
├── tests/
├── README.md # what is this project?
├── CLAUDE.md # instructions for the agent
└── renv.lock # reproducible packagesMarkdown files that guide agents
| File | Purpose |
|---|---|
README.md |
human-facing project overview |
CLAUDE.md / agents.md |
standing instructions for the AI agent |
skills.md |
reusable task templates |
Tip
A well-written CLAUDE.md is worth hours of re-prompting — the agent reads it at the start of every session.
The five operations you need
| Command | What it does |
|---|---|
git commit |
checkpoint — a snapshot you can return to |
git branch |
parallel work — experiment without risk |
git diff |
what changed — what you review before committing |
git revert |
undo a commit cleanly |
| pull request | propose changes for review on GitHub/GitLab |
Why git makes agents safe
An agent proposes work as diffs you review and commit — always visible, always reversible.
Workflow with an agent:
agent writes → you diff → you test
→ you commit → repeat
If anything looks wrong: git checkout -- . or git revert puts you back exactly where you were.
A test is a function that runs your code on a known input and asserts the expected output. It turns “I think this is right” into a pass/fail signal.
In R with testthat:
Test-driven development (TDD):
The agent iterates against your tests — without tests, it has no objective criterion.
For statisticians: what to test
Tip
Tests are not bureaucracy — they are the contract between you and the code.
Prompt quality determines output quality.
A rigorous prompt:
“Write an R function
coverage(truth, lower, upper)that computes the empirical coverage probability of a set of confidence intervals.truthis a scalar,lowerandupperare equal-length numeric vectors. Return the proportion of intervals that containtruth. Validate thatlowerandupperhave equal length and thatlower <= upperelement-wise. Return a scalar between 0 and 1.”
Generated output:
Important
Generated code is a hypothesis, not a result.
Read it · understand it · test it.
Would you submit a simulation result without checking the DGM?
What made this prompt good?
Vague prompt → vague output
Tests make the agent’s loop self-correcting.
library(testthat)
test_that("coverage() is correct", {
# Correctness — worked out by hand
expect_equal(coverage(0, c(-1, -1), c(1, 1)), 1) # all cover
expect_equal(coverage(0, c( 1, 2), c(3, 4)), 0) # none cover
expect_equal(coverage(0, c(-1, 1), c(1, 2)), 0.5) # half cover
# Boundary — closed endpoint counts
expect_equal(coverage(1, 1, 2), 1)
# Validity — bad input rejected
expect_error(coverage(0, c(1, 2), 3))
})The TDD loop with an agent:
Write tests → agent writes function
→ tests FAIL (Red)
→ agent revises
→ tests PASS (Green)
→ you review diff and commit
Tip
The test numbers are yours — you worked them out by hand. The agent cannot fake correctness against truth you define.