John
John

Reputation: 1947

How to perform linearity tests on OLS regression [statmodels]

I'm using the code following to test for linearity, however, somehow statsmodels thinks that my model is not linear. Input:

import numpy as np
import statsmodels.api as sm
from statsmodels.stats.diagnostic import linear_reset


x = np.random.randn(100)
y = np.random.randn(100)

regression = sm.OLS(y, x).fit_regularized()
linear_reset(regression, power = 2, test_type = "fitted")

Gives the error:

TypeError: result must come from a linear regression model

Do you know what I'm doing wrong?

Upvotes: 0

Views: 954

Answers (1)

LetteraUnica
LetteraUnica

Reputation: 11

linear_reset needs a RegressionResults object as first argument, while the object returned in line regression = sm.OLS(y, x).fit_regularized() by the fit_regularized() method is a RegularizedResultsWrapper.

If you want to use the linear_reset test you should use an unregularized OLS model by calling the .fit() method, this way the returned object is a RegressionResultsWrapper and can be passed to linear_reset.


import numpy as np
import statsmodels.api as sm
from statsmodels.stats.diagnostic import linear_reset


x = np.random.randn(100)
y = np.random.randn(100)

regression = sm.OLS(y, x)
result = regression.fit()

linear_reset(result, power = 2, test_type = "fitted")

This error makes sense, because one is trying to test the non-linearity of a regularized model, while the function assumes an un-regularized model.

In particular, the linear_reset test assumes that the given model is a un-regularized OLS and tries to prove the non-linearity of the relationship between x and y.
If one were to apply this test to a regularized model, the above assumption won't hold anymore and the test result is meaningless; for example it can report a missing non-linearity, while this can be caused by the regularization factor which brings the values of the parameters closer to 0.

Upvotes: 1

Related Questions