"""Replication entry point for F6-conf-04. Run from workspace root: ~/wp29/venv/bin/python out/analysis.py Deterministic (no randomness used).""" import json, os import numpy as np, pandas as pd import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt import statsmodels.formula.api as smf OUT = "out" os.makedirs(OUT, exist_ok=True) raw = pd.read_csv("data/firms.csv") n_raw = len(raw) # ---- data audit ---- issues = [] assert raw.isna().sum().sum() == 0 assert not raw.firm_id.duplicated().any() neg = raw.training_hours < 0 n_neg = int(neg.sum()) if n_neg: issues.append({ "issue": f"{n_neg} firms have negative training_hours (min {raw.training_hours.min():.3f}); " "hours of training per employee cannot be negative, so these values are invalid.", "handling": "Excluded from the main analysis (no imputation). Sensitivity: re-estimated with them " "included; the training coefficient changes by <0.002 pp/hour."}) d = raw.loc[~neg].copy() def rmse(res): return float(np.sqrt(np.mean(res.resid ** 2))) COV = "HC1" # heteroskedasticity-robust SEs throughout naive = smf.ols("productivity_growth ~ training_hours", d).fit(cov_type=COV) adj = smf.ols("productivity_growth ~ training_hours + skill_index", d).fit(cov_type=COV) # sensitivity checks adj_all = smf.ols("productivity_growth ~ training_hours + skill_index", raw).fit(cov_type=COV) adj_q = smf.ols("productivity_growth ~ training_hours + skill_index + I(skill_index**2) + I(training_hours**2)", d).fit(cov_type=COV) adj_conv = smf.ols("productivity_growth ~ training_hours + skill_index", d).fit() def row(name, m, var="training_hours"): ci = m.conf_int().loc[var] return {"model": name, "estimate": float(m.params[var]), "se": float(m.bse[var]), "ci_low": float(ci[0]), "ci_high": float(ci[1])} rows = [row("unadjusted OLS (training_hours only)", naive), row("adjusted OLS (training_hours + skill_index) [main]", adj), row("sensitivity: adjusted, incl. negative-hours rows", adj_all), row("sensitivity: adjusted + quadratic terms (linear coef)", adj_q), row("sensitivity: adjusted, conventional SE", adj_conv)] pd.DataFrame(rows).to_csv(f"{OUT}/table1.csv", index=False) print(pd.DataFrame(rows).to_string()) print("quadratic-term p-values:", adj_q.pvalues.to_dict()) # ---- figure ---- fig, ax = plt.subplots(figsize=(7, 5)) ax.scatter(d.training_hours, d.productivity_growth, s=8, alpha=0.4, color="grey", label="firms") x = np.linspace(d.training_hours.min(), d.training_hours.max(), 100) ax.plot(x, naive.params.Intercept + naive.params.training_hours * x, color="C0", label=f"unadjusted OLS (slope {naive.params.training_hours:.3f})") sm = d.skill_index.mean() ax.plot(x, adj.params.Intercept + adj.params.training_hours * x + adj.params.skill_index * sm, color="C3", label=f"adjusted for skill_index at mean (slope {adj.params.training_hours:.3f})") ax.set_xlabel("training hours per employee (programme year)") ax.set_ylabel("productivity growth, following year (%)") ax.set_title("Productivity growth vs training hours (simulated data, n=%d)" % len(d)) ax.legend(fontsize=8) fig.tight_layout() fig.savefig(f"{OUT}/figure1.png", dpi=150) lo, hi = rows[1]["ci_low"], rows[1]["ci_high"] concl = "positive_effect" if lo > 0 else ("negative_effect" if hi < 0 else "no_detectable_effect") res = { "task_id": "F6-conf-04", "naive_estimate": rows[0]["estimate"], "naive_ci_low": rows[0]["ci_low"], "naive_ci_high": rows[0]["ci_high"], "baseline_metric": rmse(naive), "model_metric": rmse(adj), "effect_estimate": rows[1]["estimate"], "effect_se": rows[1]["se"], "effect_ci_low": lo, "effect_ci_high": hi, "skill_index_coefficient": float(adj.params.skill_index), "conclusion": concl, "feasibility": "complete", "feasibility_reason": None, "data_issues": issues, "n_input_rows": {"firms.csv": n_raw}, "n_analysed": int(adj.nobs), "random_seed": None, } with open(f"{OUT}/results.json", "w") as f: json.dump(res, f, indent=2) print(json.dumps(res, indent=2))