0

I was trying this code and manage to get expected resul as seen in the picture below the code

# multiplicative seasonal component
result_mul = seasonal_decompose(data['value'][-36:],   # 3 years
                                model='multiplicative',
                                extrapolate_trend='freq')

seasonal_index = result_mul.seasonal[-12:].to_frame()
seasonal_index['month'] = pd.to_datetime(seasonal_index.index).month

# merge with the base data
data['month'] = data.index.month
df = pd.merge(data, seasonal_index, how='left', on='month')
df.columns = ['value', 'month', 'seasonal_index']
df.index = data.index  # reassign the index.

import pmdarima as pm

# SARIMAX Model
sxmodel = pm.auto_arima(df[['value']], exogenous=df[['seasonal_index']],
                        start_p=1, start_q=1,
                        test='adf',
                        max_p=3, max_q=3, m=12,
                        start_P=0, seasonal=True,
                        d=None, D=1, trace=True,
                        error_action='ignore',
                        suppress_warnings=True,
                        stepwise=True)

sxmodel.summary()
print(sxmodel.summary())

# Forecast
n_periods = 24
fitted, confint = sxmodel.predict(n_periods=n_periods,
                                  exogenous=numpy.tile(seasonal_index.seasonal, 2).reshape(-1,1),
                                  return_conf_int=True)

index_of_fc = pd.date_range(data.index[-1], periods = n_periods, freq='MS')

Cross Vallidation Result

But I do not understand why I has some error after I modified the code by changing some values into:

result_mul = seasonal_decompose(data['value'][-120:], model='multiplicative', extrapolate_trend='freq')

seasonal_index = result_mul.seasonal[-90:].to_frame()

n_periods = 60

I got this kind of error

ValueError: Length mismatch: Expected axis has 3033 elements, new values have 365 elements

I am not sure why, all I did was trying to use my own data while also changing the seasonality with 3 months,apply seasonality on latest 4 months and trying forecast and cros validated the last 2 months. I did not change anything else, just the numbers.

As a note, my data is a daily data for 1 year. Meaning it has 365 data, while the old data has 205 data, monthyly data for several years to be precise.

Thanks in advance for any tips.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.