Skip to content

DAAF-Contribution-Community/daaf / .claude/skills/stata-r-translation

stata-r-translation

Verified

Stata-to-R translation for data analysis. Maps Stata commands (reghdfe, xtreg, ivregress, margins, esttab, svy:) to R equivalents (fixest, plm, survey, marginaleffects, modelsummary). Use when user has Stata background or requests Stata-equivalent code comments in R pipelines.

references/survey-spatial-ml.md

View on GitHub
# Survey, Spatial, and Machine Learning: Stata to R

For full R-side API details, load the dedicated skill: `survey-r`, `sf-terra`, or
`tidymodels`.

---

## Part A: Complex Surveys (Stata `svy:` to R `survey`)

R's `survey` package by Thomas Lumley is the gold standard for design-based
inference -- more mature and comprehensive than both Stata's `svy:` and Python's
`svy` package.

### Design Specification

```stata
svyset psu_id [pw=finalwgt], strata(stratum_var)
svy: mean income
svy: regress y x1 x2
```

```r
library(survey)

design <- svydesign(id = ~psu_id, strata = ~stratum_var,
                    weights = ~finalwgt, data = df)
svymean(~income, design)
svyglm(y ~ x1 + x2, design = design)
```

### Estimation Mapping

| Stata | R (survey) |
|-------|------------|
| `svy: mean income` | `svymean(~income, design)` |
| `svy: total population` | `svytotal(~population, design)` |
| `svy: proportion employed` | `svymean(~employed, design)` |
| `svy: regress y x1 x2` | `svyglm(y ~ x1 + x2, design)` |
| `svy: logit y x1 x2` | `svyglm(y ~ x1 + x2, design, family = quasibinomial)` |
| `svy, over(gender): mean income` | `svyby(~income, ~gender, design, svymean)` |
| `svy, subpop(female): mean income` | `svymean(~income, subset(design, female == 1))` |

### Key R Advantages Over Stata

- Broader model family (ordinal, multinomial, Cox survival via svycoxph)
- Post-stratification, raking, and GREG calibration via `calibrate()`
- Replicate weight support (BRR, jackknife, bootstrap) via `svrepdesign()`

---

## Part B: Spatial Analysis (sf/terra)

R's spatial ecosystem (sf + terra + spdep) is substantially more capable than
Stata's limited spatial tools.

| Stata | R |
|-------|---|
| `shp2dta using shapefile` | `st_read("shapefile.shp")` |
| `spmap var using coords.dta` | `ggplot() + geom_sf(aes(fill = var))` |
| `spmatrix create contiguity W` | `spdep::poly2nb(gdf)` |
| `spregress y x, gs2sls dvarlag(W)` | `spatialreg::lagsarlm(y ~ x, data, W)` |
| No equivalent | `st_join()`, `st_buffer()`, `st_intersection()` |
| No equivalent | Interactive maps via `leaflet` |

---

## Part C: Machine Learning (tidymodels)

R's tidymodels framework is dramatically more capable than Stata's limited ML.
That said, Python's scikit-learn ecosystem is deeper still — especially for model
interpretation, fairness auditing, and clustering diagnostics — so for ML-heavy
work it is reasonable to execute in Python (scikit-learn) even when the rest of
the pipeline is in R; the R-side interpretation/fairness tooling (iml,
fairmodels, vip) is covered in the `tidymodels` skill.

| Stata | R (tidymodels) |
|-------|----------------|
| `lasso linear y x1-x20` | `linear_reg(penalty, mixture = 1) \|> set_engine("glmnet")` |
| `rf y x1-x20` (via H2O) | `rand_forest() \|> set_engine("ranger")` |
| No equivalent | `recipe() \|> step_normalize() \|> step_dummy()` |
| No equivalent | `tune_grid()` for hyperparameter search |
| `pca x1 x2 x3` | `prcomp(X, scale. = TRUE)` or tidymodels recipe step |
| `cluster kmeans` | `kmeans(X, centers = k)` |

```r
library(tidymodels)

# Preprocessing + model pipeline
rec <- recipe(y ~ ., data = train) |>
  step_normalize(all_numeric_predictors()) |>
  step_dummy(all_nominal_predictors())

spec <- rand_forest(trees = 500) |>
  set_engine("ranger") |>
  set_mode("regression")

wf <- workflow() |> add_recipe(rec) |> add_model(spec)
fit <- wf |> fit(data = train)
```
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 322kUpdated Claude CodeCodexVerifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 119kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 118kUpdated Claude CodeCodexVerifiedInstall