Data Analysis
Run an end-to-end data analysis following sewage project conventions.
Input: $ARGUMENTS — a dataset path, analysis goal description, or specification to estimate.
Project-Specific Context
Analysis Organisation
Scripts in scripts/R/09_analysis/ by approach:
01_descriptive/ — Maps, scatter plots, Google Trends
02_hedonic/ — Cross-sectional hedonic regressions
03_repeat_sales/ — Repeat-transaction regressions
04_long_difference/ — 250m grid-level long differences
05_news/ — DiD and event studies with media coverage
06_upstream_downstream/ — Directional spillover
07_dry_spills/ — Dry spill analysis
Datasets
data/final/ — Analysis-ready datasets
data/processed/ — Intermediate pipeline outputs (parquet)
- All data loaded via
arrow::read_parquet() or arrow::open_dataset()
Output Destinations
- Tables:
output/tables/*.tex (modelsummary → LaTeX with tabularray)
- Figures:
output/figures/*.pdf or *.png
- Regression objects:
output/regs/*.rds
- HTML interactive:
output/html_plots/
Required R Conventions
here::here() for all paths
- Native pipe
|>
fixest::feols() for regressions with vcov = "hetero"
modelsummary for table output (tabularray format, [H] placement)
arrow for parquet I/O
snake_case naming
forcats::as_factor() for factors
Workflow
Step 1: Context Gathering
- Understand the analysis goal from
$ARGUMENTS
- Read existing analysis scripts in the relevant subdirectory for patterns
- Read
scripts/R/utils/spill_aggregation_utils.R if spill metrics are involved
- Check
data/final/ for available datasets
- Read the relevant manuscript section in
docs/overleaf/ if the analysis feeds into the paper
Step 2: Write Analysis Script
Follow the analysis script structure:
# ================================================================
# [Descriptive Title]
# Purpose: [What this script does]
# Inputs: [Data files]
# Outputs: [Figures, tables, RDS files]
# ================================================================
# === 1. Setup ============================================
library(tidyverse)
library(fixest)
library(modelsummary)
library(arrow)
library(here)
# === 2. Data Loading =====================================
df <- read_parquet(here("data", "final", "dataset.parquet"))
# === 3. Main Analysis ====================================
model <- feols(
log_price ~ spill_count | lsoa + year_quarter,
data = df,
vcov = "hetero"
)
# === 4. Tables and Figures ================================
modelsummary(
list("Main" = model),
output = here("output", "tables", "table_name.tex"),
fmt = 3
)
# === 5. Export ============================================
saveRDS(model, here("output", "regs", "model_name.rds"))
Step 3: Code Review
After writing the script, review it against the 9 categories from /review-r:
- Script structure, console hygiene, reproducibility
- Function design, figure quality, data persistence
- Comments, error handling, polish
Fix any Critical or Major issues before presenting.
Step 4: Run the Script
If the user wants execution:
cd /Users/jacopoolivieri/Library/CloudStorage/Dropbox/01_projects/sewage
Rscript scripts/R/09_analysis/[subdir]/[script_name].R
Step 5: Present Results
- Results summary — Key estimates with SEs and economic interpretation
- Script created — Path and description
- Output files — Tables and figures generated
- Code review notes — Any conventions to flag
- TODO items — Missing data, additional specifications needed
Principles
- Reproduce, don't guess. If a specific regression is requested, implement exactly that.
- Strategy alignment. If an analysis feeds into a manuscript section, the code must implement what the paper claims.
- Publication-ready output. Tables and figures should be directly includable in the paper.
- Follow existing patterns. Read neighbouring scripts in the same subdirectory for style consistency.
- Save everything. Every regression object saved as RDS, every table as LaTeX, every figure as PDF.
1---2name: data-analysis3description: End-to-end R data analysis for the sewage project. Writes analysis scripts following project conventions (here::here, arrow/parquet, fixest, modelsummary, native pipe), runs code review, and produces publication-ready tables and figures. This skill should be used when asked to "run an analysis", "estimate the model", "add a specification", or "write an R script".4---56# Data Analysis78Run an end-to-end data analysis following sewage project conventions.910**Input:** `$ARGUMENTS` — a dataset path, analysis goal description, or specification to estimate.1112---1314## Project-Specific Context1516### Analysis Organisation1718Scripts in `scripts/R/09_analysis/` by approach:19- `01_descriptive/` — Maps, scatter plots, Google Trends20- `02_hedonic/` — Cross-sectional hedonic regressions21- `03_repeat_sales/` — Repeat-transaction regressions22- `04_long_difference/` — 250m grid-level long differences23- `05_news/` — DiD and event studies with media coverage24- `06_upstream_downstream/` — Directional spillover25- `07_dry_spills/` — Dry spill analysis2627### Datasets2829- `data/final/` — Analysis-ready datasets30- `data/processed/` — Intermediate pipeline outputs (parquet)31- All data loaded via `arrow::read_parquet()` or `arrow::open_dataset()`3233### Output Destinations3435- Tables: `output/tables/*.tex` (modelsummary → LaTeX with tabularray)36- Figures: `output/figures/*.pdf` or `*.png`37- Regression objects: `output/regs/*.rds`38- HTML interactive: `output/html_plots/`3940### Required R Conventions4142- `here::here()` for all paths43- Native pipe `|>`44- `fixest::feols()` for regressions with `vcov = "hetero"`45- `modelsummary` for table output (tabularray format, `[H]` placement)46- `arrow` for parquet I/O47- `snake_case` naming48- `forcats::as_factor()` for factors4950---5152## Workflow5354### Step 1: Context Gathering55561. Understand the analysis goal from `$ARGUMENTS`572. Read existing analysis scripts in the relevant subdirectory for patterns583. Read `scripts/R/utils/spill_aggregation_utils.R` if spill metrics are involved594. Check `data/final/` for available datasets605. Read the relevant manuscript section in `docs/overleaf/` if the analysis feeds into the paper6162### Step 2: Write Analysis Script6364Follow the analysis script structure:6566```r67# ================================================================68# [Descriptive Title]69# Purpose: [What this script does]70# Inputs: [Data files]71# Outputs: [Figures, tables, RDS files]72# ================================================================7374# === 1. Setup ============================================7576library(tidyverse)77library(fixest)78library(modelsummary)79library(arrow)80library(here)8182# === 2. Data Loading =====================================8384df <- read_parquet(here("data", "final", "dataset.parquet"))8586# === 3. Main Analysis ====================================8788model <- feols(89 log_price ~ spill_count | lsoa + year_quarter,90 data = df,91 vcov = "hetero"92)9394# === 4. Tables and Figures ================================9596modelsummary(97 list("Main" = model),98 output = here("output", "tables", "table_name.tex"),99 fmt = 3100)101102# === 5. Export ============================================103104saveRDS(model, here("output", "regs", "model_name.rds"))105```106107### Step 3: Code Review108109After writing the script, review it against the 9 categories from `/review-r`:110- Script structure, console hygiene, reproducibility111- Function design, figure quality, data persistence112- Comments, error handling, polish113114Fix any Critical or Major issues before presenting.115116### Step 4: Run the Script117118If the user wants execution:119```bash120cd /Users/jacopoolivieri/Library/CloudStorage/Dropbox/01_projects/sewage121Rscript scripts/R/09_analysis/[subdir]/[script_name].R122```123124### Step 5: Present Results1251261. **Results summary** — Key estimates with SEs and economic interpretation1272. **Script created** — Path and description1283. **Output files** — Tables and figures generated1294. **Code review notes** — Any conventions to flag1305. **TODO items** — Missing data, additional specifications needed131132---133134## Principles135136- **Reproduce, don't guess.** If a specific regression is requested, implement exactly that.137- **Strategy alignment.** If an analysis feeds into a manuscript section, the code must implement what the paper claims.138- **Publication-ready output.** Tables and figures should be directly includable in the paper.139- **Follow existing patterns.** Read neighbouring scripts in the same subdirectory for style consistency.140- **Save everything.** Every regression object saved as RDS, every table as LaTeX, every figure as PDF.