简体   繁体   English

Python - 每组观察的 ACF 和 PACF plot

[英]Python - ACF and PACF plot per group of observations

I have a dataset (Python dataframe) that looks like:我有一个看起来像的数据集(Python 数据框):

Date      Group  Value
Date1     Group1 Value1
Date2     Group1 Value2
Date3     Group1 Value3
Date1     Group2 Value1
Date2     Group2 Value2
Date3     Group3 Value3

I would like to run a ACF and PACF plot on this dataset, but I don't want it to use the lags of different groups.我想在此数据集上运行 ACF 和 PACF plot,但我不希望它使用不同组的滞后。 Ie the 1st lag of Date1 Group2 Value1 should not be the previous value which belongs to group1, ie I want it to use the correct lags within groups.Date1 Group2 Value1的第一个滞后不应该是属于 group1 的先前值,即我希望它在组内使用正确的滞后。

How can I do that?我怎样才能做到这一点?

It's a little convoluted, but it would be possible to get separate ACF/ PACF like this:这有点复杂,但可以像这样获得单独的 ACF/PACF:

dates = pd.date_range(df.index.min(), df.index.max(), freq="d") # assumes df.index is datetime

results_pacf = []
for group_name, df_group in df.groupby("group"):
        df_group = (pd.DataFrame(index=dates).merge(df_group, left_index=True, right_index=True, how="left").fillna(0))
        results_pacf.append({group_name: pacf(df_group.drop("group", axis=1))})`

results_pacf would be a list of dictionaries looking like [{group_name: [1, 0.22, -0.4, ...]}, {...}] results_pacf将是一个字典列表,看起来像[{group_name: [1, 0.22, -0.4, ...]}, {...}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM