如何改善滞后功能?

发布于 2025-01-23 13:01:29 字数 2105 浏览 0 评论 0原文

I have the following table:

timecomsalesindsales
120.96127.3
221.4130
321.96132.7
421.52129.4
522.39135
622.76137.1
723.48141.2
823.66142.8
924.1145.5
1024.01145.3
1124.54148.3
1224.3146.4
1325150.2
1425.64153.1
1526.36157.3
160.716.9817.52
18164.2164.2
in27.78165.6 165.6
1928.24168.7
2028.78171.7

我在Python中实现滞后功能,并试图在下面使用以下代码

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm

dataset = pd.read_csv('Data/blaisdellcomp.csv', index_col=0)
y = dataset['comsales']
x = dataset['indsales']

X = sm.add_constant(x)
model = sm.OLS(y, X).fit()
print(model.summary())

residuals = model.resid
residuals = residuals.reset_index(drop=True)

lagged_residuals = residuals.rolling(1).sum()
lagged_residuals.drop(lagged_residuals.tail(1).index,inplace=True)
lagged_residuals.loc[-1] = lagged_residuals[0]  # adding a row
lagged_residuals.index = lagged_residuals.index + 1  # shifting index
lagged_residuals.sort_index(inplace=True)
lagged_residuals = lagged_residuals.reset_index(drop=True)

来实现以下代码,以便在我使用lag lag py the Rage lag 实施滞后。有没有更好的方法来实施残差?

I have the following table:

timecomsalesindsales
120.96127.3
221.4130
321.96132.7
421.52129.4
522.39135
622.76137.1
723.48141.2
823.66142.8
924.1145.5
1024.01145.3
1124.54148.3
1224.3146.4
1325150.2
1425.64153.1
1526.36157.3
1626.98160.7
1727.52164.2
1827.78165.6
1928.24168.7
2028.78171.7

I am trying to implement a LAG function in python with the following code below

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm

dataset = pd.read_csv('Data/blaisdellcomp.csv', index_col=0)
y = dataset['comsales']
x = dataset['indsales']

X = sm.add_constant(x)
model = sm.OLS(y, X).fit()
print(model.summary())

residuals = model.resid
residuals = residuals.reset_index(drop=True)

lagged_residuals = residuals.rolling(1).sum()
lagged_residuals.drop(lagged_residuals.tail(1).index,inplace=True)
lagged_residuals.loc[-1] = lagged_residuals[0]  # adding a row
lagged_residuals.index = lagged_residuals.index + 1  # shifting index
lagged_residuals.sort_index(inplace=True)
lagged_residuals = lagged_residuals.reset_index(drop=True)

To be able to make a lag in the residuals I use a lot of code to implement the lag. Is there a better way to implement the lag on the residuals?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久光 2025-01-30 13:01:29

看起来您正在寻找 pandas shift 方法:

lagged_residuals = ristuals.shift(1)

It looks like you're looking for the Pandas shift method:

lagged_residuals = residuals.shift(1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文