残留诊断图

时间:2019-11-05 21:34:45

标签: python time-series arima

因此,我正在尝试编写一个绘制残差诊断图的函数-图,直方图,qq和acf

我遵循python中的函数plot_diagnostic(),但是我想添加一些修饰

我不知道如何在直方图中添加KDE和正态分布N(0,1)-我可以寻求帮助吗?

COde跌倒了:


import seaborn as sns
import statsmodels.tsa.api as smt
import matplotlib.pyplot as plt
import statsmodels.graphics.gofplots as sgg
from scipy.stats import gaussian_kde, norm
def tsplot_resid(y, lags=None, title="Graf časové řady", legend = True, figsize=(14, 9), lw = 8):

    y = y.iloc[0:, 0]
    #Original source: https://tomaugspurger.github.io/modern-7-timeseries.html

    fig = plt.figure(figsize=figsize)
    layout = (2, 2)
    ts_ax   = plt.subplot2grid(layout, (0, 0))
    hist_ax = plt.subplot2grid(layout, (0, 1))
    acf_ax  = plt.subplot2grid(layout, (1, 1))
    qq_ax = plt.subplot2grid(layout, (1, 0))
    y.plot(ax=ts_ax, grid = True, legend = True)
    ts_ax.axhline(y=0, color='r', linewidth=3, alpha = 0.75) 
    ts_ax.set_title(title)
    # ts_ax.set_xlim([dt.datetime(1949, 1, 1), dt.datetime(1960, 1, 1)])
    ts_ax.set_xlabel("Roky")
    ts_ax.set_ylabel("Hodnoty")
    ts_ax.tick_params(axis = 'x', rotation = 45);
    ts_ax.tick_params(labelsize = 15);

    y.plot(ax=hist_ax, kind='hist', bins=8, grid = True, legend = True, edgecolor = "w");
    hist_ax.set_title('Histogram')
    hist_ax.set_xlabel("Hodnoty")
    hist_ax.set_ylabel("Frekvence")

    # ADD KDE AND NORMAL DISTRIBUTION


    smt.graphics.plot_acf(y.dropna(), lags=lags, ax=acf_ax, );
    sgg.qqplot(y.dropna(), ax=qq_ax, line = "s");

    acf_ax.set_title("ACF")
    acf_ax.grid()
    acf_ax.set_xlabel("Zpoždění")

    qq_ax.set_title("QQ-plot")
    qq_ax.grid()
    qq_ax.set_xlabel("Zpoždění")
    qq_ax.set_ylabel("")
    [ax.set_xlim(-1) 

     for ax in [acf_ax]]
    sns.despine()
    plt.tight_layout()
    return ts_ax, acf_ax, qq_ax

0 个答案:

没有答案