使用 statsmodels.formula.api 的多项式回归
Polynomial Regression Using statsmodels.formula.api
请原谅我的无知。我想要做的就是在我的回归中添加一个平方项,而无需经历在我的数据框中定义新列的麻烦。我正在使用 statsmodels.formula.api(作为统计数据),因为格式类似于我更熟悉的 R。
hours_model = stats.ols(formula='act_hours ~ h_hours + C(month) + trend', data = df).fit()
以上按预期工作。
hours_model = stats.ols(formula='act_hours ~ h_hours + h_hours**2 + C(month) + trend', data = df).fit()
这会省略 h_hours**2 和 returns 与上面一行相同的输出。
我也尝试过:h_hours^2、math.pow(h_hours,2) 和 poly(h_hours,2)
都抛出错误。
如有任何帮助,我们将不胜感激。
你可以尝试像在 R:
中那样使用 I()
import statsmodels.formula.api as smf
np.random.seed(0)
df = pd.DataFrame({'act_hours':np.random.uniform(1,4,100),'h_hours':np.random.uniform(1,4,100),
'month':np.random.randint(0,3,100),'trend':np.random.uniform(0,2,100)})
model = 'act_hours ~ h_hours + I(h_hours**2)'
hours_model = smf.ols(formula = model, data = df)
hours_model.exog[:5,]
array([[ 1. , 3.03344961, 9.20181654],
[ 1. , 1.81002392, 3.27618659],
[ 1. , 3.20558207, 10.27575638],
[ 1. , 3.88656564, 15.10539244],
[ 1. , 1.74625943, 3.049422 ]])
目前,虽然 statsmodels 公式 API(实际上是 Patsy 库)不支持 R 中的 poly(variable, degree)
函数,但 NumPy 的 vander(variable, degree+1)
可以完成这项工作。但是,注意 np.vander()
会生成 Vandermonde 矩阵,这意味着您也可以获得截距列!让我们在示例中查看此函数:
>> x = np.array([1, 2, 3, 5])
>> np.vander(x, 4, increasing=True)
array([[ 1, 1, 1, 1],
[ 1, 2, 4, 8],
[ 1, 3, 9, 27],
[ 1, 5, 25, 125]])
因此,您需要通过在公式中添加 -1
来删除 Patsy 的内部截距:
hours_model = stats.ols(formula='act_hours ~ np.vander(h_hours, 3, increasing=True) - 1', data = df).fit()
注意需要传your_desired_degree + 1
因为第一列是x^0=1.
请原谅我的无知。我想要做的就是在我的回归中添加一个平方项,而无需经历在我的数据框中定义新列的麻烦。我正在使用 statsmodels.formula.api(作为统计数据),因为格式类似于我更熟悉的 R。
hours_model = stats.ols(formula='act_hours ~ h_hours + C(month) + trend', data = df).fit()
以上按预期工作。
hours_model = stats.ols(formula='act_hours ~ h_hours + h_hours**2 + C(month) + trend', data = df).fit()
这会省略 h_hours**2 和 returns 与上面一行相同的输出。
我也尝试过:h_hours^2、math.pow(h_hours,2) 和 poly(h_hours,2) 都抛出错误。
如有任何帮助,我们将不胜感激。
你可以尝试像在 R:
中那样使用I()
import statsmodels.formula.api as smf
np.random.seed(0)
df = pd.DataFrame({'act_hours':np.random.uniform(1,4,100),'h_hours':np.random.uniform(1,4,100),
'month':np.random.randint(0,3,100),'trend':np.random.uniform(0,2,100)})
model = 'act_hours ~ h_hours + I(h_hours**2)'
hours_model = smf.ols(formula = model, data = df)
hours_model.exog[:5,]
array([[ 1. , 3.03344961, 9.20181654],
[ 1. , 1.81002392, 3.27618659],
[ 1. , 3.20558207, 10.27575638],
[ 1. , 3.88656564, 15.10539244],
[ 1. , 1.74625943, 3.049422 ]])
目前,虽然 statsmodels 公式 API(实际上是 Patsy 库)不支持 R 中的 poly(variable, degree)
函数,但 NumPy 的 vander(variable, degree+1)
可以完成这项工作。但是,注意 np.vander()
会生成 Vandermonde 矩阵,这意味着您也可以获得截距列!让我们在示例中查看此函数:
>> x = np.array([1, 2, 3, 5])
>> np.vander(x, 4, increasing=True)
array([[ 1, 1, 1, 1],
[ 1, 2, 4, 8],
[ 1, 3, 9, 27],
[ 1, 5, 25, 125]])
因此,您需要通过在公式中添加 -1
来删除 Patsy 的内部截距:
hours_model = stats.ols(formula='act_hours ~ np.vander(h_hours, 3, increasing=True) - 1', data = df).fit()
注意需要传your_desired_degree + 1
因为第一列是x^0=1.