赞
踩
- import matplotlib.pyplot as plt
- from statsmodels.tsa.statespace.sarimax import SARIMAX
- import numpy as np
- from pmdarima.arima import auto_arima
- import statsmodels.api as sm
- import pandas as pd
- raw_data=pd.read_excel(r"C:\Users\附件4、土壤蒸发量2012—2022年.xls")
- data=raw_data["土壤蒸发量(mm)"].values
- index = pd.date_range(start='2012-01-01', periods=len(data), freq='M')
- series=pd.Series(data,index=index)
-
- diff1=series.diff(1)
- diff2=series.diff(2)
- print(u'原始序列的ADF检验结果为:', ADF(series))
- print(u'一阶差分序列的ADF检验结果为:', ADF(diff1[1:]))
- # 原始序列的ADF检验结果为: (-1.5045801917212067, 0.5312443790121629, 11, 111, {'1%': -3.490683082754047, '5%': -2.8879516565798817, '10%': -2.5808574442009578}, 642.5500224639288)
- # 一阶差分序列的ADF检验结果为: (-10.061376456523105, 1.3307651257364255e-17, 10, 111, {'1%': -3.490683082754047, '5%': -2.8879516565798817, '10%': -2.5808574442009578}, 637.9083146867483)
- #看输出的第二项,<=0.05则符合要求
-
-
- #分解周期信息,得到周期
- # decomposition = sm.tsa.seasonal_decompose(series, model='additive', extrapolate_trend='freq')
- # fig = decomposition.plot()
- # plt.show()
-
- #自动求解参数
- # model = auto_arima(series, seasonal=True, m=12, stepwise=True, trace=True,
- # suppress_warnings=True, error_action="ignore", max_order=None)
- # print(model.summary())
- #SARIMAX(0, 0, 3)x(1, 0, [1], 12)
-
- model = SARIMAX(series, order=(1,1,3), seasonal_order=(1, 0, 1, 12))
- ar_result = model.fit()
- # 打印模型的回归系数
- # print("AR 模型的回归系数:")
- # print(ar_result.params)
- forecast_steps = 12 # 预测未来 3 个时间步
- forecast = ar_result.get_prediction(start=len(data), end=len(data) + forecast_steps - 1)
- forecast_mean = forecast.predicted_mean
- forecast_ci = forecast.conf_int()
- plt.figure(figsize=(10, 6))
- plt.rcParams['font.sans-serif'] = ['Kaitt', 'SimHei']
- plt.plot(series,label="观测值")
- plt.plot(forecast_mean,linestyle='--',label="预测值")
- plt.fill_between(forecast_ci.index, forecast_ci.iloc[:, 0], forecast_ci.iloc[:, 1], color='orange', alpha=0.2)
- plt.title("时序预测图")
- plt.legend()
- plt.show()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。