python - 如何在 statsmodel ols 回归中包含滞后变量

标签 python regression statsmodels

有没有办法在 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/

相关文章:

python - python 中的多处理,处理多个文件

python - 统计模型 ARIMA : how to get confidence/prediction interval?

python - 使用 smtplib 发送电子邮件不再起作用

python - 在子字符串上熔化和合并 - Python 和 Pandas

r - 如何使用R中的QR分解计算最小二乘估计量的方差?

r - 具有指定斜率的线性回归

tensorflow - 深度学习回归——巨大的均方误差和损失

python - 对 Dataframe 中的每一列应用 T 检验

python - Python 统计模型中的 GLM Gamma 回归

python - Pandas 按多列排名