Marginal Effects and Hypothesis Tests via marginaleffects

PyFixest integrates with the excellent marginaleffects package. Among many other things, marginaleffects allows you to compute:

import numpy as np
from marginaleffects import avg_slopes, hypotheses

import pyfixest as pf

data = pf.get_data()
data["Y_bin"] = np.where(data["Y"] > 0, 1, 0)

fit = pf.feglm("Y_bin ~ X1 + X2 | f1", data=data, family="logit")
fit.tidy()
Estimate Std. Error t value Pr(>|t|) 2.5% 97.5%
Coefficient
X1 -1.016344 0.109323 -9.296712 0.000000e+00 -1.230613 -0.802075
X2 -0.165899 0.028449 -5.831409 5.496138e-09 -0.221659 -0.110140

Average marginal effects

To compute the average marginal effect of X1 on the response scale:

avg_slopes(fit, variables="X1")
shape: (1, 3)
termcontrastestimate
strstrf64
"X1""dY/dX"-1.016344

You can also report group-specific average marginal effects:

avg_slopes(fit, variables="X1", by="f1")
shape: (30, 4)
f1termcontrastestimate
f64strstrf64
0.0"X1""dY/dX"-1.016344
1.0"X1""dY/dX"-1.016344
2.0"X1""dY/dX"-1.016344
3.0"X1""dY/dX"-1.016344
4.0"X1""dY/dX"-1.016344
25.0"X1""dY/dX"-1.016344
26.0"X1""dY/dX"-1.016344
27.0"X1""dY/dX"-1.016344
28.0"X1""dY/dX"-1.016344
29.0"X1""dY/dX"-1.016344

Linear hypothesis tests

Suppose we want to test the linear restriction \(X_1 = X_2\). The hypotheses() function uses the model’s estimated covariance matrix, so it works naturally with PyFixest objects.

hypotheses(fit, "X1 - X2 = 0")
shape: (1, 8)
termestimatestd_errorstatisticp_values_valueconf_lowconf_high
strf64f64f64f64f64f64f64
"X1-X2=0"-0.8504450.1086-7.8309524.8850e-1547.540568-1.063297-0.637592

Non-linear hypothesis tests

For non-linear transformations, marginaleffects applies the delta method automatically. This is useful for ratio-style summaries or uplift calculations.

Here is a simple OLS example:

fit_ols = pf.feols("Y ~ X1 + X2", data=data)
hypotheses(fit_ols, "(X1 / Intercept - 1) * 100 = 0")
shape: (1, 8)
termestimatestd_errorstatisticp_values_valueconf_lowconf_high
strf64f64f64f64f64f64f64
"(X1/Intercept-1)*100=0"-211.7190678.478682-24.9707520.0inf-228.336978-195.101155

Notes

For broader functionality / the full capability of marginaleffects, see the marginaleffects book.