在Statsmodels中指定常数线性回归? [英] Specifying a Constant in Statsmodels Linear Regression?
问题描述
我想使用statsmodels.regression.linear_model.OLS 包进行预测,但具有指定的常数.
I want to use the statsmodels.regression.linear_model.OLS package to do a prediction, but with a specified constant.
当前,我可以使用参数指定常量的存在:
Currently, I can specify the presence of a constant with an argument:
(来自文档: http://statsmodels.sourceforge. net/devel/generation/statsmodels.regression.linear_model.OLS.html )
class statsmodels.regression.linear_model.OLS(endog,exog = None,missing ='none',hasconst = None),其中 hasconst 是布尔值.
class statsmodels.regression.linear_model.OLS(endog, exog=None, missing='none', hasconst=None), where hasconst is a boolean.
我想做的是明确指定一个常数C,然后围绕它拟合线性回归模型.通过使用该OLS,我想生成一个,然后访问所有属性(如resid等).
What I want to do is specify explicitly a constant C, and then fit a linear regression model around it. From using that OLS, I want to generate a and then access all the attributes like resid, etc.
当前的次优解决方法是指定没有常量的OLS,从Y值中减去常量,并创建一个自定义对象,该对象每次都想包装指定的常量和没有常量的OLS进行预测或拟合,首先要从Y变量中减去常数,然后再使用预测.
A current suboptimal work around would be to specify the OLS without a constant, subtract the constant from the Y-values, and create a custom object that wraps both the specified constant and OLS w/o constant, every time I want to do predict or fit, to first subtract the constant from the Y variables, and then use the prediction.
谢谢!
推荐答案
如果将formula
API用于statsmodels,则可以将其更简洁地指定为常量截距,作为Patsy设计矩阵规范的一部分.这仍然有点怪异-基本上只是表达您提出的解决方案的一种更简洁的方式-但至少它更短.例如:
If you use the formula
API for statsmodels, you can specify a constant intercept more concisely as part of a Patsy design matrix specification. This is still a bit hacky--it's basically just a cleaner way of expressing your proposed solution--but at least it's shorter. E.g.:
>>> import statsmodels.formula.api as smf
>>> import pandas as pd
>>> import numpy as np
>>> c = 3.1416
>>> df = pd.DataFrame(np.random.rand(10, 2), columns=['x', 'y'])
>>> ols = smf.ols('y - c ~ 0 + x', data=df)
>>> result = ols.fit()
>>> print result.summary()
...
==============================================================================
coef std err t P>|t| [95.0% Conf. Int.]
------------------------------------------------------------------------------
x 0.7404 0.230 3.220 0.010 0.220 1.261
==============================================================================
如您所见,截距没有系数,x
的最佳斜率不是1.
As you can see, there's no coefficient for the intercept, and the best slope for x
is not 1.
这篇关于在Statsmodels中指定常数线性回归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!