AI Agents & Econometric Coding

Linear regression

Cook's distance

Cook's distance measures how much an OLS fit changes when one observation is removed.

The task

Task statement

Fit y on x1, x2, x3, x4, and x5 with an intercept. Compute Cook's distance for each row and report the top five row indices and values as rank1_obs_id, rank1_cooks_d, ..., rank5_obs_id, rank5_cooks_d. Use zero-based row indices.

Data

Columns: y, x1, x2, x3, x4, x5.

Files
input.csv

Required output

Scored estimates: rank1_obs_id, rank1_cooks_d, rank2_obs_id, rank2_cooks_d, rank3_obs_id, rank3_cooks_d, rank4_obs_id, rank4_cooks_d, rank5_obs_id, rank5_cooks_d.

Reference output

TermReference estimateSample size
rank1_obs_id190500
rank1_cooks_d0.028549159500
rank2_obs_id214500
rank2_cooks_d0.024652545500
rank3_obs_id377500
rank3_cooks_d0.020751336500
rank4_obs_id119500
rank4_cooks_d0.01747982500
rank5_obs_id323500
rank5_cooks_d0.017470236500

A run succeeds when every scored estimate lies within 1 percent or 0.005 of its reference value, whichever is larger. Sample sizes are not scored.

Task success

Agency conditionClaude Sonnet 4.6GPT-5.6 Luna
Chatbot96.7%96.7%
Self-repair100.0%100.0%
Constrained agent100.0%100.0%
All runs
98.9% of 360 runs
By prompt condition
Zero-shot 97.8%, One-shot 100.0%
By software
Python 100.0%, R 99.2%, Stata 97.5%

Influence combines two ingredients

An observation is influential when it has a large residual, high leverage, or both. Cook's distance summarizes its case-deletion effect (Cook, 1977).

$$ D_i=\frac{\hat u_i^2}{p\,MSE}\frac{h_{ii}}{(1-h_{ii})^2}. $$

Here $h_{ii}$ is leverage and $p$ is the number of fitted coefficients.

Same residual, different leverage

Let $p=2$, $MSE=1$, and $\hat u_i=2$.

Leverage $h_{ii}$Cook's distance
0.050.111
0.200.625
0.504.000

The denominator $(1-h_{ii})^2$ makes high-leverage cases especially sensitive.

Influence map with leverage on the horizontal axis, squared standardized residual on the vertical axis, and larger circles for larger Cook's distance.
Cook's distance increases when residual size and leverage combine. Point C is influential because both components are large.

Assumptions and interpretation

Cook's distance is a diagnostic for a declared fitted model. It is not meaningful before the outcome and regressors are specified. A large value signals sensitivity and calls for inspection. It does not automatically justify deletion.

Software can report distances using slightly different scale labels. Row identifiers must remain attached after sorting. A two-axis influence map should place leverage horizontally, squared residual vertically, and encode Cook's distance with contour lines.

Reading an influence measure

Cook's distance measures the combined change in fitted values after deleting case $i$. A large value marks sensitivity. It does not prove that the observation is wrong. High influence can arise from an unusual predictor value, a large residual, or both. The figure shows why equal residual magnitudes can yield different distances when leverage differs.

List influential cases with their response, predictors, leverage, studentized residual, and Cook's distance. Then refit the model without each flagged case and compare the coefficients that answer the research question. DFBETAs can show which coefficient moves. A data correction needs evidence from the source record, not a diagnostic threshold.

Diagnostic workflow and limitations

Compute influence measures from the exact fitted design matrix, including transformations, interactions, and fixed indicators. Check that row identifiers remain stable after missing values are removed. Common guides such as $4/n$ are screening conventions. They are not universal decision rules. Simulated envelopes or substantive knowledge can give more useful context.

Several influential observations can mask one another. Inspect joint deletions when cases share an extreme predictor pattern. A large Cook's distance can also reveal curvature, omitted interactions, or variance misspecification. Plot residuals against fitted values and each continuous predictor before treating the issue as a single case. Robust standard errors change reported uncertainty but do not reduce leverage or repair the conditional mean. If the model will predict new units near the influential case, deleting that region may weaken external validity. (Cook, 1977)

Further reading

Cook introduces the distance measure (Cook, 1977). Belsley, Kuh, and Welsch develop a broader framework for regression diagnostics (Belsley et al., 1980).

Sources and further reading

  1. R. Dennis Cook. 1977. “Detection of Influential Observation in Linear Regression.” Technometrics 19(1): 15–18. doi:10.1080/00401706.1977.10489493.
  2. David A. Belsley, Edwin Kuh, Roy E. Welsch. 1980. Regression Diagnostics: Identifying Influential Data and Sources of Collinearity. Wiley. doi:10.1002/0471725153.

Related reading

About this benchmark task

Method group
Linear regression
Software
Stata, R, and Python
Runs
360 runs, ten in each combination of model, software, prompt, and agency condition

Open every recorded run for this task.