python - 在 ARIMA 时间序列建模 python pandas 中为 Adfuller 测试(平稳性测试)提取 p 值
问题描述
df
Col1 Col2 Col3
12 10 3
3 5 2
100 12 10
等等.....
为时间序列中的 ARIMA 建模编写 adfuller 测试的代码。(将为数据框 df 的所有列计算 p 值)
import statsmodels.tsa.stattools as tsa
adf_results = {}
for col in df.columns.values:
adf_results[col] = tsa.adfuller(df[col])
使用此代码,我得到以下格式的输出:(输入 adf_result 时的输出)
[IN] adf_result
[OUT]
{'Col1': (-4.236149193618492,
0.0005719678593039654, #This is the second value for this column/p value
0,
37,
{'1%': -3.6209175221605827,
'5%': -2.9435394610388332,
'10%': -2.6104002410518627},
138.66116123406837),
'Col2': (-3.707023043984407,
0.004015446231411924, #This is the second value for this column/p value
0,
37,
{'1%': -3.6209175221605827,
'5%': -2.9435394610388332,
'10%': -2.6104002410518627},
144.6019873130419),
'Col3': (1.8083888603589304,
0.9983655107052215, #This is the second value for this column/p value
0,
37,
{'1%': -3.6209175221605827,
'5%': -2.9435394610388332,
'10%': -2.6104002410518627},
-74.4384052778039)}
等等。
在这个问题中,第二个值/p 值是
0.0005719678593039654, 0.004015446231411924 and 0.9983655107052215 for the 3 columns taken.
我需要一个列表中第二个值 >0.05 的列和另一个列表中 p 值 <0.05 的列
因此,一个列表将是 col1 和 col2(第二个值/p 值<0.05),另一个列表将是 col3(第二个值/p 值<0.05)
解决方案
import pandas as pd
from io import StringIO
data = StringIO("""
Col1 Col2 Col3
12 10 3
3 5 2
100 12 10
13 4 1
""")
# load data into data frame
df = pd.read_csv(data, sep=' ')
import statsmodels.tsa.stattools as tsa
adf_results = {}
for col in df.columns.values:
adf_results[col] = tsa.adfuller(df[col])
# loop over dictionary data
columns_big = []
columns_small = []
for key, value in adf_results.items():
if value[1] > 0.05:
columns_big.append(key)
else:
columns_small.append(key)
输出:
columns_big = ['Col1', 'Col3']
columns_small = ['Col2']
推荐阅读
- asp.net-core - 为什么 Ctrl+C 不停止通用主机?
- c# - 从动态队列 RabbitMQ 读取数据
- python - 在python中使用selenium搜索电子邮件地址并在没有结果时返回信息失败
- docker - 主体反序列化失败(提供的身份无效:x509:证书已过期或尚未生效)
- ios - 在Objective C中添加二维数组的对角线
- python - 具有(单个)最大分区大小的星形和条形
- javascript - 道具更改时反应组件不重新渲染
- python-2.7 - P4Python 在删除客户端时遇到问题 - [错误]:要删除客户端,请先删除所有挂起的更改
- python - 如何将代码中的值与单独文本文档中的值进行比较?
- java - Map.merge .. 不能从静态上下文中引用非静态方法