Data Analysis
Run an end-to-end data analysis in R through 5 phases.
Phase 1: Setup and Data Loading
- Read R code conventions from
.claude/rules/r-code-conventions.md
- Create script with proper header (title, author, purpose, inputs, outputs)
- Load packages with
library() (never require())
- Set
set.seed() if stochastic elements are involved
- Load data using relative paths
Phase 2: Exploratory Data Analysis
- Summary statistics (mean, sd, min, max, N)
- Missingness patterns
- Distributions of key variables
- Bivariate relationships
- Time patterns (if panel data)
- Group comparisons
- Save diagnostics to
output/diagnostics/
Phase 3: Main Analysis
- Use
fixest for panel data with fixed effects (see /econometrics-r for detailed patterns)
- Use
lm/glm for cross-section
- Cluster standard errors appropriately
- Run multiple specifications (simple to complex)
- Report both statistical and economic significance
- For causal inference (IV, DiD, RDD), see
/causal-inference-r for diagnostics and modern estimators
- For spatial econometrics, see
/econometrics-r for spdep/spatialreg patterns
Phase 4: Publication-Ready Output
- Tables:
modelsummary (preferred) or stargazer, export as .tex and .html
- Figures:
ggplot2 with project theme, explicit dimensions, white background, 300 DPI
- Export figures as
.pdf and .png
Phase 5: Save and Review
saveRDS() for all computed objects
- Run r-reviewer agent on generated script
- Address Critical/High issues
Script Structure Template
# ==============================================================================
# Title: [Analysis Name]
# Author: [Name]
# Date: [YYYY-MM-DD]
# Purpose: [What this script does]
# Inputs: [Data files]
# Outputs: [Tables, figures, RDS files]
# ==============================================================================
# 0. Setup ----
library(tidyverse)
library(fixest)
library(modelsummary)
# 1. Data Loading ----
# 2. Exploratory Analysis ----
# 3. Main Analysis ----
# 4. Tables and Figures ----
# 5. Export ----
All scripts saved to scripts/R/, all outputs to output/.
1---
2name: data-analysis-4
3description: End-to-end R data analysis workflow from exploration through regression to publication-ready tables and figures.
4disable-model-invocation: true
5argument-hint: "[dataset path or description of analysis goal]"
6allowed-tools: ["Read", "Grep", "Glob", "Write", "Edit", "Bash", "Task"]
7metadata:
8 source: https://skillmd.com/skills/brycewang-stanford/data-analysis-4
9---
10
11## Data Analysis
12
13Run an end-to-end data analysis in R through 5 phases.
14
15### Phase 1: Setup and Data Loading
16- Read R code conventions from `.claude/rules/r-code-conventions.md`
17- Create script with proper header (title, author, purpose, inputs, outputs)
18- Load packages with `library()` (never `require()`)
19- Set `set.seed()` if stochastic elements are involved
20- Load data using relative paths
21
22### Phase 2: Exploratory Data Analysis
23- Summary statistics (mean, sd, min, max, N)
24- Missingness patterns
25- Distributions of key variables
26- Bivariate relationships
27- Time patterns (if panel data)
28- Group comparisons
29- Save diagnostics to `output/diagnostics/`
30
31### Phase 3: Main Analysis
32- Use `fixest` for panel data with fixed effects (see `/econometrics-r` for detailed patterns)
33- Use `lm`/`glm` for cross-section
34- Cluster standard errors appropriately
35- Run multiple specifications (simple to complex)
36- Report both statistical and economic significance
37- For causal inference (IV, DiD, RDD), see `/causal-inference-r` for diagnostics and modern estimators
38- For spatial econometrics, see `/econometrics-r` for `spdep`/`spatialreg` patterns
39
40### Phase 4: Publication-Ready Output
41- **Tables:** `modelsummary` (preferred) or `stargazer`, export as `.tex` and `.html`
42- **Figures:** `ggplot2` with project theme, explicit dimensions, white background, 300 DPI
43- Export figures as `.pdf` and `.png`
44
45### Phase 5: Save and Review
46- `saveRDS()` for all computed objects
47- Run r-reviewer agent on generated script
48- Address Critical/High issues
49
50### Script Structure Template
51```r
52# ==============================================================================
53# Title: [Analysis Name]
54# Author: [Name]
55# Date: [YYYY-MM-DD]
56# Purpose: [What this script does]
57# Inputs: [Data files]
58# Outputs: [Tables, figures, RDS files]
59# ==============================================================================
60
61# 0. Setup ----
62library(tidyverse)
63library(fixest)
64library(modelsummary)
65
66# 1. Data Loading ----
67
68# 2. Exploratory Analysis ----
69
70# 3. Main Analysis ----
71
72# 4. Tables and Figures ----
73
74# 5. Export ----
75```
76
77All scripts saved to `scripts/R/`, all outputs to `output/`.