Partial Effects in Python

ECON 3209 · Week 10, Lecture 3 · Kerala Agricultural University

Department of Development Economics, KAU

Autumn 2026

Learning Outcomes

By the end of this lecture, you will be able to:

  1. estimate multiple regression models with statsmodels formula syntax
  2. read the main sections of a regression summary table
  3. extract coefficients, confidence intervals, and fitted values in Python
  4. visualize partial effects with prediction grids
  5. compute scenario-based marginal effects for Kerala policy examples

The Python Workflow for MLR

  1. Build a tidy pandas DataFrame.
  2. Write a model formula such as yield_ ~ fertilizer + rainfall + irrigation.
  3. Fit with smf.ols(...).fit().
  4. Read the summary table.
  5. Use fitted values and prediction grids for interpretation.

For a linear model without interactions or logs, the marginal effect of a regressor equals its estimated coefficient everywhere.

What Does the Summary Table Contain?

Model fit panel

  • number of observations
  • \(R^2\) and adjusted \(R^2\)
  • F-statistic for joint significance
  • residual standard error proxy (sqrt(mse_resid))

Coefficient panel

  • estimate (coef)
  • standard error (std err)
  • t-statistic and p-value
  • confidence interval

Python Demo: Fit and Print the Full Summary

Reading the Coefficient Panel

  • coef gives the estimated partial effect.
  • std err shows sampling uncertainty.
  • P>|t| helps test \(H_0: \beta_j=0\).
  • The 95% interval shows a plausible range for the true parameter.

\[t_j = \frac{\hat\beta_j - 0}{se(\hat\beta_j)}\]

If the model is linear in variables, the estimated partial effect of fertilizer is simply \(\hat\beta_{fertilizer}\).

Python Demo: Extract a Custom Results Table

Fitted Values and Residuals

After estimation we can compute

\[\hat{Y}_i = X_i'\hat{\beta}, \qquad \hat{\varepsilon}_i = Y_i - \hat{Y}_i\]

These are useful for: - prediction - residual plots - model checking - scenario analysis

Python Demo: Plot a Partial Effect

When Marginal Effects Need More Care

  • In a plain linear model, the marginal effect is constant.
  • With interactions, the effect depends on another variable.
  • With logs, the effect can be a percentage effect or elasticity.
  • With quadratic terms, the effect changes with the level of \(X\).

Later weeks extend this idea to interactions, heteroscedasticity-robust inference, and nonlinear models.

Python Demo: Scenario-Based Effects

A Good Reporting Sentence

“Holding rainfall, irrigation, and soil quality constant, an additional kilogram of fertilizer per acre is associated with an increase of \(\hat\beta_1\) units in expected crop yield.”

This sentence contains: - the conditioning variables - the unit of change in \(X\) - the unit of change in \(Y\) - the direction of the effect

🏋️ Exercise

  1. Simulate data on crop yield, fertilizer, rainfall, irrigation, and soil score.
  2. Estimate an MLR with statsmodels.
  3. Extract a compact coefficient table and identify the most precisely estimated regressor.
  4. Create a prediction grid for fertilizer and plot the partial effect.

Summary

statsmodels makes it easy to estimate, summarize, and visualize MLR models.

✅ The summary table reports fit statistics, coefficient estimates, standard errors, tests, and confidence intervals.

✅ In a linear MLR without interactions, the marginal effect equals the coefficient everywhere.

✅ Prediction grids convert regression output into interpretable visuals and scenarios.

✅ Clear interpretation requires both statistical precision and economic language.

Next Lecture

Week 11 — Multicollinearity

We will cover: - what multicollinearity is - how to detect it - why it inflates standard errors - how to diagnose and remedy it in Python