[英]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.我正在使用以下代码来测试线性度,但是,不知何故 statsmodels 认为我的 model 不是线性的。 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?你知道我做错了什么吗?
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
. linear_reset
需要一个 RegressionResults object 作为第一个参数,而通过fit_regularized()
方法在线regression = sm.OLS(y, x).fit_regularized()
返回的 object 是一个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
.如果你想使用
linear_reset
测试,你应该通过调用.fit()
方法来使用非正则化的 OLS model,这样返回的 object 是一个RegressionResultsWrapper
并且可以传递给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.这个错误是有道理的,因为有人试图测试正则化 model 的非线性,而 function 假设一个非正则化 model。
In particular, 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.特别是,
linear_reset
测试假设给定的 model 是一个非正则化 OLS,并试图证明 x 和 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;如果将此测试应用于正则化 model,上述假设将不再成立,测试结果毫无意义; 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.
例如,它可以报告丢失的非线性,而这可能是由使参数值更接近 0 的正则化因子引起的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.