我一直在尝试在Python中指定OLS/GLS回归中预定义的系数的方法。我可以使用R中的offset
来实现这一点,但是在Python中似乎没有类似的功能。
R版本:
model=lm(y~x+offset(0.2*z))
在这个例子中,x和z是我们的自变量,模型预测了x,但我们指定了z的影响为0.2。
我一直在尝试在Python中指定OLS/GLS回归中预定义的系数的方法。我可以使用R中的offset
来实现这一点,但是在Python中似乎没有类似的功能。
R版本:
model=lm(y~x+offset(0.2*z))
offset
。一个示例是GLM。import statsmodels.formula.api as smf
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(8, 3), columns=list('yxz'))
df
# y x z
# 0 0.091761 -1.987569 -0.219672
# 1 0.357113 1.477894 -0.518270
# 2 -0.808494 -0.501757 0.915402
# 3 0.328751 -0.529760 0.513267
# 4 0.097078 0.968645 -0.702053
# 5 -0.327662 -0.392108 -1.463515
# 6 0.296120 0.261055 0.005113
# 7 -0.234587 -1.415371 -0.420645
known = 0.2
res1 = smf.glm('y ~ x', data = df, offset=known*df['z']).fit()
print(res1.summary())
# ==============================================================================
# Dep. Variable: y No. Observations: 8
# Model: GLM Df Residuals: 6
# .
# .
# ==============================================================================
# coef std err z P>|z| [0.025 0.975]
# ------------------------------------------------------------------------------
# Intercept 0.0614 0.165 0.373 0.709 -0.261 0.384
# x 0.1478 0.148 0.995 0.320 -0.143 0.439
# ==============================================================================
您也可以通过手动执行相同的操作进行一次检查。您可以像这样创建一个偏移量:
offset = known*df['z']
y_offset = df['y']-offset
df2 = pd.concat([pd.Series(y_diff), df['x']], axis = 1)
df2.columns = ['y_diff', 'x']
res2 = smf.glm('y_offset ~ x', data = df2).fit()
print(res2.summary())
# ==============================================================================
# Dep. Variable: y_offset No. Observations: 8
# Model: GLM Df Residuals: 6
# .
# .
# ==============================================================================
# coef std err z P>|z| [0.025 0.975]
# ------------------------------------------------------------------------------
# Intercept 0.0614 0.165 0.373 0.709 -0.261 0.384
# x 0.1478 0.148 0.995 0.320 -0.143 0.439
# ==============================================================================