pipeline
Classical end-to-end empirical analysis workflow in the traditional Python econometric stack — pandas + numpy + scipy + statsmodels + linearmodels + pyfixest +…
Design and implementation of comprehensive simulation studies
$ npx -y skills add brycewang-stanford/Auto-Empirical-Research-Skills --skill simulation-architect --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
/simulation-architectContext preview
The summary Claude sees to decide when to auto-load this skill.
Design and implementation of comprehensive simulation studies
name: simulation-architect description: Design and implementation of comprehensive simulation studies
You are an expert in designing Monte Carlo simulation studies for statistical methodology research.
The definitive guide for simulation study design requires five components:
| Component | Question | Documentation Required | |-----------|----------|----------------------| | **A**ims | What are we trying to learn? | Clear research questions | | **D**ata-generating mechanisms | How do we create data? | Full DGP specification | | **E**stimands | What are we estimating? | Mathematical definition | | **M**ethods | What estimators do we compare? | Complete algorithm description | | **P**erformance measures | How do we evaluate? | Bias, variance, coverage |
□ Aims stated clearly □ DGP fully specified (all parameters, distributions) □ Estimand(s) defined mathematically □ All methods described with sufficient detail for replication □ Performance measures defined □ Number of replications justified □ Monte Carlo standard errors reported □ Random seed documented for reproducibility □ Software and version documented □ Computational time reported
---
**Monte Carlo Standard Error (MCSE)** formula:
$$\text{MCSE}(\hat{\theta}) = \frac{\hat{\sigma}}{\sqrt{B}}$$
where $B$ is the number of replications and $\hat{\sigma}$ is the estimated standard deviation.
| Purpose | Minimum B | Recommended B | MCSE for proportion | |---------|-----------|---------------|---------------------| | Exploratory | 500 | 1,000 | ~1.4% at 95% coverage | | Publication | 1,000 | 2,000 | ~1.0% at 95% coverage | | Definitive | 5,000 | 10,000 | ~0.4% at 95% coverage | | Precision | 10,000+ | 50,000 | ~0.2% at 95% coverage |
# Calculate Monte Carlo standard errors
calculate_mcse <- function(estimates, coverage_indicators = NULL) {
B <- length(estimates)
list(
# MCSE for mean (bias)
mcse_mean = sd(estimates) / sqrt(B),
# MCSE for standard deviation
mcse_sd = sd(estimates) / sqrt(2 * (B - 1)),
# MCSE for coverage (proportion)
mcse_coverage = if (!is.null(coverage_indicators)) {
p <- mean(coverage_indicators)
sqrt(p * (1 - p) / B)
} else NA
)
}
# Rule of thumb: B needed for desired MCSE
replications_needed <- function(desired_mcse, estimated_sd) {
ceiling((estimated_sd / desired_mcse)^2)
}---
# Full simulation study template following Morris et al. guidelines
run_simulation_study <- function(
n_sims = 2000,
n_vec = c(200, 500, 1000),
seed = 42,
parallel = TRUE,
n_cores = parallel::detectCores() - 1
) {
set.seed(seed)
# Define parameter grid
params <- expand.grid(
n = n_vec,
effect_size = c(0, 0.14, 0.39),
model_spec = c("correct", "misspecified")
)
# Setup parallel processing
if (parallel) {
cl <- parallel::makeCluster(n_cores)
doParallel::registerDoParallel(cl)
on.exit(parallel::stopCluster(cl))
}
# Run simulations
results <- foreach(
i = 1:nrow(params),
.combine = rbind,
.packages = c("tidyverse", "mediation")
) %dopar% {
scenario <- params[i, ]
sim_results <- replicate(n_sims, {
data <- generate_dgp(scenario)
estimates <- apply_methods(data)
evaluate_performance(estimates, truth = scenario$effect_size)
}, simplify = FALSE)
summarize_scenario(sim_results, scenario)
}
# Add MCSE
results <- add_monte_carlo_errors(results, n_sims)
results
}
# Summarize with MCSE
add_monte_carlo_errors <- function(results, B) {
results %>%
mutate(
mcse_bias = empirical_se / sqrt(B),
mcse_coverage = sqrt(coverage * (1 - coverage) / B),
mcse_rmse = rmse / sqrt(2 * B)
)
}# Memory-efficient parallel simulation
run_parallel_simulation <- function(scenario, n_sims, n_cores = 4) {
library(future)
library(future.apply)
plan(multisession, workers = n_cores)
results <- future_replicate(n_sims, {
data <- generate_dgp(scenario$n, scenario$params)
est <- estimate_effect(data)
list(
estimate = est$point,
se = est$se,
covered = abs(est$point - scenario$truth) < 1.96 * est$se
)
}, simplify = FALSE)
plan(sequential) # Reset
# Aggregate
estimates <- sapply(results, `[[`, "estimate")
ses <- sapply(results, `[[`, "se")
covered <- sapply(results, `[[`, "covered")
list(
bias = mean(estimates) - scenario$truth,
empirical_se = sd(estimates),
mean_se = mean(ses),
coverage = mean(covered),
mcse_bias = sd(estimates) / sqrt(n_sims),
mcse_coverage = sqrt(mean(covered) * (1 - mean(covered)) / n_sims)
)
}---
1. **Aims**: What question does the simulation answer? 2. **Data-generating mechanisms**: How is data simulated? 3. **Estimands**: What is being estimated? 4. **Methods**: What estimators are compared? 5. **Performance measures**: How is performance assessed?
generate_mediation_data <- function(n, params) {
# Confounders
X <- rnorm(n)
# Treatment (binary)
ps <- plogis(params$gamma0 + params$gamma1 * X)
A <- rbinom(n, 1, ps)
# Mediator
M <- params$alpha0 + params$alpha1 * A + params$alpha2 * X +
rnorm(n, sd = params$sigma_m)
# Outcome
Y <- params$beta0 + params$beta1 * A + params$beta2 * M +
params$beta3 * X + params$beta4 * A * M +
rnorm(n, sd = params$sigma_y)
data.frame(Y = Y, A = A, M = M, X = X)
}📌 文档结构(2026-07-22 起): 本文件是中文默认入口 —— banner + badges + 信任面 + 9 阶段流水线速览 + 76 行合集总表。 每个合集的完整描述、按用途分组、精确数字、验证方法在 docs/CONTENT_ZH.md(扩展正文,总表行内的 → 直接跳转到对应锚点)。 English version: README-en.md · 中文扩展正文:docs/CONTENT_ZH.md · README-zh-CN.md 已弃用(重定向占位) 🌐 语言: English |
Classical end-to-end empirical analysis workflow in the traditional Python econometric stack — pandas + numpy + scipy + statsmodels + linearmodels + pyfixest +…
Use when the user asks to run a full empirical / causal analysis in Python — by default in the style of an applied economics paper (AER / QJE / JPE / ReStud /…
Classical end-to-end empirical analysis workflow in the traditional Python econometric stack — pandas + numpy + scipy + statsmodels + linearmodels + pyfixest +…
Classical end-to-end empirical analysis workflow in the traditional Stata ecosystem — native Stata + reghdfe + ivreg2 + csdid + did_imputation +…
Classical end-to-end empirical analysis workflow in the modern tidyverse + econometrics R ecosystem — dplyr + tidyr + haven + fixest + sandwich + lmtest +…
Systematic writing framework for philosophy and interdisciplinary academic papers from optimized outline to submission-ready manuscript. Use when users want…