有没有办法在 statsmodel ols 回归中指定滞后自变量?下面是一个示例数据框和 ols 模型规范。我想在模型中包含一个滞后变量。
df = pd.DataFrame({
"y": [2,3,7,8,1],
"x": [8,6,2,1,9],
"v": [4,3,1,3,8]
})
Current model:
model = sm.ols(formula = 'y ~ x + v', data=df).fit()
Desired model:
model_lag = sm.ols(formula = 'y ~ (x-1) + v', data=df).fit()
最佳答案
我认为你不能在公式中即时调用它。也许使用 shift方法?如果这不是您需要的,请澄清
import statsmodels.api as sm
df['xlag'] = df['x'].shift()
df
y x v xlag
0 2 8 4 NaN
1 3 6 3 8.0
2 7 2 1 6.0
3 8 1 3 2.0
4 1 9 8 1.0
sm.formula.ols(formula = 'y ~ xlag + v', data=df).fit()
关于python - 如何在 statsmodel ols 回归中包含滞后变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64330874/