Residual Plot in Python seasonal_decompose function not displaying properly

1.1k Views Asked by At

The residual plot is not displaying properly in my plot. I cannot understand what can the issue be. Please need help withenter image description here this. There is some issue with the axis. I am pulling Data of COVID 19 and I am plotting first-order data (made stationary set). I have removed all nan values.

format of data is date value_diff 268 2020-10-16 745.0 269 2020-10-17 428.0 270 2020-10-18 465.0

ecomposition = seasonal_decompose(data_set_3, model='additive', period=7)

    trend = decomposition.trend
    seasonal = decomposition.seasonal
    residual = decomposition.resid

    plt.subplot(411)
    plt.plot(data_set_3, label='Original')
    plt.legend(loc='best')

    plt.subplot(412)
    plt.plot(trend, label='Trend')
    plt.legend(loc='best')

    plt.subplot(413)
    plt.plot(seasonal, label='Seasonality')
    plt.legend(loc='best')

    plt.subplot(414)
    plt.plot(residual, label='Residuals')
    plt.legend(loc='best')

    plt.tight_layout()

1

There are 1 best solutions below

0
On

add freq and periods to your seasonal_decomposition for smoothing. it will work.

from pandas_datareader import data as pdr
from statsmodels.graphics import tsaplots
import statsmodels.api as sm

current_date=datetime.datetime.now()
start_date=datetime.datetime(current_date.year,1,1)
df = pdr.get_data_yahoo("MSFT",start_date,current_date).reset_index()

decomposition=sm.tsa.seasonal_decompose(x=df['High'],model='additive',         extrapolate_trend='freq', period=30)
decomposition.plot()
plt.show()

decomposition_trend=decomposition.trend
ax= decomposition_trend.plot(figsize=(14,2))
ax.set_xlabel('Date')
ax.set_ylabel('Trend of time series')
ax.set_title('Trend values of the time series')
plt.show()

decomposition_residual=decomposition.resid
ax= decomposition_residual.plot(figsize=(14,2))
ax.set_xlabel('Date')
ax.set_ylabel('Residual of time series')
ax.set_title('Residual values of the time series')
plt.show()

decomposition_trend=decomposition.trend
ax= decomposition_trend.plot(figsize=(14,2))
ax.set_xlabel('Date')
ax.set_ylabel('Trend of time series')
ax.set_title('Trend values of the time series')
plt.show()