Data Analysis Workflow
Run an end-to-end data analysis in R: load, explore, analyze, and produce publication-ready output.
Input: $ARGUMENTS — a dataset path (e.g., data/county_panel.csv) or a description of the analysis goal (e.g., "regress wages on education with state fixed effects using CPS data").
Constraints
- Follow R code conventions in
.claude/rules/r-code-conventions.md
- Save all scripts to
scripts/R/ with descriptive names
- Save all outputs (figures, tables, RDS) to
output/
- Use
saveRDS() for every computed object — Quarto slides may need them
- Use project theme for all figures (check for custom theme in
.claude/rules/)
- Run r-reviewer on the generated script before presenting results
Workflow Phases
Phase 1: Setup and Data Loading
- Read
.claude/rules/r-code-conventions.md for project standards
- Create R script with proper header (title, author, purpose, inputs, outputs)
- Load required packages at top (
library(), never require())
- Set seed once at top:
set.seed(42)
- Load and inspect the dataset
Phase 2: Exploratory Data Analysis
Generate diagnostic outputs:
- Summary statistics:
summary(), missingness rates, variable types
- Distributions: Histograms for key continuous variables
- Relationships: Scatter plots, correlation matrices
- Time patterns: If panel data, plot trends over time
- Group comparisons: If treatment/control, compare pre-treatment means
Save all diagnostic figures to output/diagnostics/.
Phase 3: Main Analysis
Based on the research question:
- Regression analysis: Use
fixest for panel data, lm/glm for cross-section
- Standard errors: Cluster at the appropriate level (document why)
- Multiple specifications: Start simple, progressively add controls
- Effect sizes: Report standardized effects alongside raw coefficients
Phase 4: Publication-Ready Output
Tables:
- Use
modelsummary for regression tables (preferred) or stargazer
- Include all standard elements: coefficients, SEs, significance stars, N, R-squared
- Export as
.tex for LaTeX inclusion and .html for quick viewing
Figures:
- Use
ggplot2 with project theme
- Set
bg = "transparent" for Beamer compatibility
- Include proper axis labels (sentence case, units)
- Export with explicit dimensions:
ggsave(width = X, height = Y)
- Save as both
.pdf and .png
Phase 5: Save and Review
saveRDS() for all key objects (regression results, summary tables, processed data)
- Create
output/ subdirectories as needed with dir.create(..., recursive = TRUE)
- Run the r-reviewer agent on the generated script:
Delegate to the r-reviewer agent:
"Review the script at scripts/R/[script_name].R"
- Address any Critical or High issues from the review.
Script Structure
Follow this template:
# ============================================================
# [Descriptive Title]
# Author: [from project context]
# Purpose: [What this script does]
# Inputs: [Data files]
# Outputs: [Figures, tables, RDS files]
# ============================================================
# 0. Setup ----
library(tidyverse)
library(fixest)
library(modelsummary)
set.seed(42)
dir.create("output/analysis", recursive = TRUE, showWarnings = FALSE)
# 1. Data Loading ----
# [Load and clean data]
# 2. Exploratory Analysis ----
# [Summary stats, diagnostic plots]
# 3. Main Analysis ----
# [Regressions, estimation]
# 4. Tables and Figures ----
# [Publication-ready output]
# 5. Export ----
# [saveRDS for all objects, ggsave for all figures]
Important
- Reproduce, don't guess. If the user specifies a regression, run exactly that.
- Show your work. Print summary statistics before jumping to regression.
- Check for issues. Look for multicollinearity, outliers, perfect prediction.
- Use relative paths. All paths relative to repository root.
- No hardcoded values. Use variables for sample restrictions, date ranges, etc.
1---
2name: data-analysis-3
3description: End-to-end R data analysis workflow from exploration through regression to publication-ready tables and figures
4argument-hint: "[dataset path or description of analysis goal]"
5allowed-tools: ["Read", "Grep", "Glob", "Write", "Edit", "Bash", "Task"]
6metadata:
7 source: https://skillmd.com/skills/brycewang-stanford/data-analysis-3
8---
9
10# Data Analysis Workflow
11
12Run an end-to-end data analysis in R: load, explore, analyze, and produce publication-ready output.
13
14**Input:** `$ARGUMENTS` — a dataset path (e.g., `data/county_panel.csv`) or a description of the analysis goal (e.g., "regress wages on education with state fixed effects using CPS data").
15
16---
17
18## Constraints
19
20- **Follow R code conventions** in `.claude/rules/r-code-conventions.md`
21- **Save all scripts** to `scripts/R/` with descriptive names
22- **Save all outputs** (figures, tables, RDS) to `output/`
23- **Use `saveRDS()`** for every computed object — Quarto slides may need them
24- **Use project theme** for all figures (check for custom theme in `.claude/rules/`)
25- **Run r-reviewer** on the generated script before presenting results
26
27---
28
29## Workflow Phases
30
31### Phase 1: Setup and Data Loading
32
331. Read `.claude/rules/r-code-conventions.md` for project standards
342. Create R script with proper header (title, author, purpose, inputs, outputs)
353. Load required packages at top (`library()`, never `require()`)
364. Set seed once at top: `set.seed(42)`
375. Load and inspect the dataset
38
39### Phase 2: Exploratory Data Analysis
40
41Generate diagnostic outputs:
42- **Summary statistics:** `summary()`, missingness rates, variable types
43- **Distributions:** Histograms for key continuous variables
44- **Relationships:** Scatter plots, correlation matrices
45- **Time patterns:** If panel data, plot trends over time
46- **Group comparisons:** If treatment/control, compare pre-treatment means
47
48Save all diagnostic figures to `output/diagnostics/`.
49
50### Phase 3: Main Analysis
51
52Based on the research question:
53- **Regression analysis:** Use `fixest` for panel data, `lm`/`glm` for cross-section
54- **Standard errors:** Cluster at the appropriate level (document why)
55- **Multiple specifications:** Start simple, progressively add controls
56- **Effect sizes:** Report standardized effects alongside raw coefficients
57
58### Phase 4: Publication-Ready Output
59
60**Tables:**
61- Use `modelsummary` for regression tables (preferred) or `stargazer`
62- Include all standard elements: coefficients, SEs, significance stars, N, R-squared
63- Export as `.tex` for LaTeX inclusion and `.html` for quick viewing
64
65**Figures:**
66- Use `ggplot2` with project theme
67- Set `bg = "transparent"` for Beamer compatibility
68- Include proper axis labels (sentence case, units)
69- Export with explicit dimensions: `ggsave(width = X, height = Y)`
70- Save as both `.pdf` and `.png`
71
72### Phase 5: Save and Review
73
741. `saveRDS()` for all key objects (regression results, summary tables, processed data)
752. Create `output/` subdirectories as needed with `dir.create(..., recursive = TRUE)`
763. Run the r-reviewer agent on the generated script:
77
78```
79Delegate to the r-reviewer agent:
80"Review the script at scripts/R/[script_name].R"
81```
82
834. Address any Critical or High issues from the review.
84
85---
86
87## Script Structure
88
89Follow this template:
90
91```r
92# ============================================================
93# [Descriptive Title]
94# Author: [from project context]
95# Purpose: [What this script does]
96# Inputs: [Data files]
97# Outputs: [Figures, tables, RDS files]
98# ============================================================
99
100# 0. Setup ----
101library(tidyverse)
102library(fixest)
103library(modelsummary)
104
105set.seed(42)
106
107dir.create("output/analysis", recursive = TRUE, showWarnings = FALSE)
108
109# 1. Data Loading ----
110# [Load and clean data]
111
112# 2. Exploratory Analysis ----
113# [Summary stats, diagnostic plots]
114
115# 3. Main Analysis ----
116# [Regressions, estimation]
117
118# 4. Tables and Figures ----
119# [Publication-ready output]
120
121# 5. Export ----
122# [saveRDS for all objects, ggsave for all figures]
123```
124
125---
126
127## Important
128
129- **Reproduce, don't guess.** If the user specifies a regression, run exactly that.
130- **Show your work.** Print summary statistics before jumping to regression.
131- **Check for issues.** Look for multicollinearity, outliers, perfect prediction.
132- **Use relative paths.** All paths relative to repository root.
133- **No hardcoded values.** Use variables for sample restrictions, date ranges, etc.