3

In R, the car::linearHypothesis function can be used to test the hypothesis that two coefficients are equal (that their difference differs significantly from zero). Here's an example from its documentation:

linearHypothesis(mod.duncan, "income = education")

Per this CrossValidated answer this is also available in MATLAB as linhyptest.

Is there an equivalent for Python statsmodels regression models?

Max Ghenis
  • 14,783
  • 16
  • 84
  • 132
  • 1
    I don't know if there is something built-in or not, but if you can retrieve the values of the two parameters and the subset of the variance-covariance matrix (i.e., a two-row, two-column submatrix) pertaining to these two parameters, we can tell you how to implement it ... – Ben Bolker Nov 05 '18 at 22:26
  • probably start here: https://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLSResults.cov_params.html – Ben Bolker Nov 05 '18 at 22:27

1 Answers1

3

The results classes of most models have several methods for Wald tests.

t_test is vectorized for single hypothesis.
wald_test is for joint hypothesis.
wald_test_terms automatically tests that "terms", i.e. subset of coefficients are jointly zero, similar to a type 3 ANOVA table based on Wald tests.

See for example the docstring for t_test after OLS, but all models inherit the same method and work in the same way (*). https://www.statsmodels.org/dev/generated/statsmodels.regression.linear_model.OLSResults.t_test.html

for example

>>> t_test = results.t_test("income = education")
>>> print(t_test)

(*) There are a few models that do not follow the standard pattern where these wald tests are not yet available.

The t_test use either the normal or the t distribution, the other two wald tests use either chisquare or F distribution. The distribution can be selected using the use_t keyword in model.fit.
If use_t=True then t and F distributions are used. if it is False, then the normal and chisquare distributions are used. The default is t and F for linear regression models and normal and chisquare for all other models.

Josef
  • 21,998
  • 3
  • 54
  • 67
  • Is there any way to use this on IV2SLS? – Jameson Oct 17 '21 at 20:36
  • 1
    IV2SLS in statsmodels inherits the same wald test methods as other models. However, I'm not sure how well formula support works. Without formulas, the restriction matrix needs to be supplied directly, instead of as strings. – Josef Oct 17 '21 at 21:19
  • Is there any chance you could show me how to extract the restriction matrix needed? I have an open question on this here: https://stackoverflow.com/questions/69595896/how-to-do-test-of-equality-of-coefficient-for-2sls-in-statsmodels-or-linearmodel – Jameson Oct 18 '21 at 09:18