谁能给我解释一下 statsmodel.formula.api 中的 ols 和 statsmodel.api 中的 ols 之间的区别?
使用 ISLR 文本中的广告数据,我使用两者运行了一个 ols,得到了不同的结果。然后我与 scikit-learn 的 LinearRegression 进行了比较。
import numpy as np
import pandas as pd
import statsmodels.formula.api as smf
import statsmodels.api as sm
from sklearn.linear_model import LinearRegression
df = pd.read_csv("C:\...\Advertising.csv")
x1 = df.loc[:,['TV']]
y1 = df.loc[:,['Sales']]
print "Statsmodel.Formula.Api Method"
model1 = smf.ols(formula='Sales ~ TV', data=df).fit()
print model1.params
print "\nStatsmodel.Api Method"
model2 = sm.OLS(y1, x1)
results = model2.fit()
print results.params
print "\nSci-Kit Learn Method"
model3 = LinearRegression()
model3.fit(x1, y1)
print model3.coef_
print model3.intercept_
输出如下:
Statsmodel.Formula.Api Method
Intercept 7.032594
TV 0.047537
dtype: float64
Statsmodel.Api Method
TV 0.08325
dtype: float64
Sci-Kit Learn Method
[[ 0.04753664]]
[ 7.03259355]
statsmodel.api 方法从 statsmodel.formula.api 和 scikit-learn 方法返回一个不同的电视参数。
statsmodel.api 运行的是哪种 ols 算法会产生不同的结果?有没有人有指向可以帮助回答这个问题的文档的链接?
最佳答案
今天遇到这个问题,想详细说明@stellasia 的回答,因为 statsmodels 文档可能有点模棱两可。
除非您使用 actual R-style string-formulas 在实例化 OLS
时,您需要在 statsmodels.formulas.api
和普通 statsmodels.api 下添加一个常量(实际上是一列 1)
。 @Chetan 在这里使用 R 风格的格式 (formula='Sales ~ TV'
),所以他不会遇到这种微妙的情况,但对于具有一些 Python 知识但没有 R 背景的人来说,这可能非常令人困惑。
此外,您是否指定 hasconst
也无关紧要建立模型时的参数。 (这有点傻。)换句话说,除非您使用 R 风格的字符串公式,否则 hasconst
将被忽略,即使它应该是
[Indicate] whether the RHS includes a user-supplied constant
因为,在脚注中
No constant is added by the model unless you are using formulas.
下面的示例表明,如果不使用 R 风格的字符串公式,.formulas.api
和 .api
都需要用户添加的 1 列向量。
# Generate some relational data
np.random.seed(123)
nobs = 25
x = np.random.random((nobs, 2))
x_with_ones = sm.add_constant(x, prepend=False)
beta = [.1, .5, 1]
e = np.random.random(nobs)
y = np.dot(x_with_ones, beta) + e
现在将 x
和 y
放入 Excel 并运行 Data>Data Analysis>Regression,确保未选中“Constant is zero”。您将获得以下系数:
Intercept 1.497761024
X Variable 1 0.012073045
X Variable 2 0.623936056
现在,在 statsmodels.formula.api
或 statsmodels.api 中尝试在
,x
而不是 x_with_ones
上运行此回归hasconst
设置为 None
、True
或 False
。您会看到,在这 6 种情况中的每一种情况下,都没有返回拦截。 (只有2个参数。)
import statsmodels.formula.api as smf
import statsmodels.api as sm
print('smf models')
print('-' * 10)
for hc in [None, True, False]:
model = smf.OLS(endog=y, exog=x, hasconst=hc).fit()
print(model.params)
# smf models
# ----------
# [ 1.46852293 1.8558273 ]
# [ 1.46852293 1.8558273 ]
# [ 1.46852293 1.8558273 ]
现在可以通过将 1.0
添加到 x
的列向量正确运行。您可以在此处使用 smf
,但如果您不使用公式,则实际上没有必要。
print('sm models')
print('-' * 10)
for hc in [None, True, False]:
model = sm.OLS(endog=y, exog=x_with_ones, hasconst=hc).fit()
print(model.params)
# sm models
# ----------
# [ 0.01207304 0.62393606 1.49776102]
# [ 0.01207304 0.62393606 1.49776102]
# [ 0.01207304 0.62393606 1.49776102]
关于python - 使用 statsmodel.formula.api 与 statsmodel.api 的 OLS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30650257/