python-3.xpandasstatsmodelsx13seasonal-adjustment

statsmodels.tools.sm_exceptions.X13Error: ERROR: forecasts end date, [...], must end on or before user-defined regression variables end date, [...]


I use the following test data and code to test the exog parameters of sm.tsa.x13_arima_analysis from statsmodels, but an error occurs, how could I solve it? Thanks a lot.

import statsmodels.api as sm
import pandas as pd
import numpy as np
import os

datelist = pd.date_range(pd.to_datetime('2012-12-31'), periods=1500).tolist()
date_df = pd.DataFrame(datelist, columns=['dates'])
endog = pd.concat([date_df, pd.DataFrame(np.random.randint(0, 100, size=(1500, 1)), columns=list('a'))], axis=1)
exog = pd.concat([date_df, pd.DataFrame(np.random.randint(0, 100, size=(1500, 1)), columns=list('b'))], axis=1)
endog.index = endog.dates
endog = endog.drop(['dates'], axis=1)
exog.index = exog.dates
exog = exog.drop(['dates'], axis=1)
endog = endog.resample('M').mean()
exog = exog.resample('M').mean()

X13PATH = r'D:\EconomicDataSeasonalAdjustTool\X13\x13as'
# the link to download X13as model: https://www.census.gov/data/software/x13as.html
os.chdir(X13PATH)
os.environ['X13PATH'] = X13PATH
print(os.environ['X13PATH'])

res = sm.tsa.x13_arima_analysis(endog=endog, exog=exog, x12path=X13PATH, print_stdout=True)
print(res.seasadj)
print(res.plot)

Out:

Traceback (most recent call last):
  File "C:\Python\Python310\lib\site-packages\IPython\core\interactiveshell.py", line 3553, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-12-21406d809098>", line 12, in <cell line: 12>
    res = sm.tsa.x13_arima_analysis(endog=endog, exog=exog, x12path=X13PATH, print_stdout=True)
  File "C:\Users\LSTM\AppData\Roaming\Python\Python310\site-packages\pandas\util\_decorators.py", line 210, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\statsmodels\tsa\x13.py", line 518, in x13_arima_analysis
    _check_errors(errors)
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\statsmodels\tsa\x13.py", line 201, in _check_errors
    raise X13Error(errors)
statsmodels.tools.sm_exceptions.X13Error: ERROR: forecasts end date, 2018.Feb, must end on or before 
        user-defined regression variables end date, 2017.Feb.

Solution

  • Finally, the code below works after by adding forecast_years=0:

    import statsmodels.api as sm
    import pandas as pd
    import numpy as np
    import os
    import matplotlib.pyplot as plt
    
    datelist = pd.date_range(pd.to_datetime('2012-12-31'), periods=1500).tolist()
    date_df = pd.DataFrame(datelist, columns=['dates'])
    endog = pd.concat([date_df, pd.DataFrame(np.random.randint(0, 100, size=(1500, 1)), columns=list('a'))], axis=1)
    exog = pd.concat([date_df, pd.DataFrame(np.random.randint(0, 100, size=(1500, 1)), columns=list('b'))], axis=1)
    endog.index = endog.dates
    endog = endog.drop(['dates'], axis=1)
    exog.index = exog.dates
    exog = exog.drop(['dates'], axis=1)
    endog = endog.resample('M').mean()
    exog = exog.resample('M').mean()
    exog = exog.reindex(endog.index)
    
    X13PATH = r'D:\EconomicDataSeasonalAdjustTool\X13\x13as'
    os.chdir(X13PATH)
    os.environ['X13PATH'] = X13PATH
    print(os.environ['X13PATH'])
    
    # res = sm.tsa.x13_arima_analysis(endog=endog, exog=exog, x12path=X13PATH, print_stdout=True)
    res1 = sm.tsa.x13_arima_analysis(endog=endog, exog=exog, x12path=X13PATH, forecast_years=0, print_stdout=True)
    res2 = sm.tsa.x13_arima_analysis(endog=endog, x12path=X13PATH, forecast_years=0, print_stdout=True)
    print(res1.seasadj)
    print(res2.seasadj)
    

    Out:

    dates
    2012-12-31    48.846047
    2013-01-31    62.326384
    2013-02-28    54.322351
    2013-03-31    52.690840
    2013-04-30    47.551649
    2013-05-31    54.331202
    2013-06-30    50.865211
    2013-07-31    48.105752
    2013-08-31    48.062653
    2013-09-30    48.383561
    2013-10-31    57.290482
    2013-11-30    42.396296
    2013-12-31    50.871920
    2014-01-31    46.988795
    2014-02-28    54.463265
    2014-03-31    46.476195
    2014-04-30    50.067421
    2014-05-31    51.495410
    2014-06-30    55.851400
    2014-07-31    45.675858
    2014-08-31    51.647678
    2014-09-30    39.036736
    2014-10-31    65.639687
    2014-11-30    49.280582
    2014-12-31    56.242091
    2015-01-31    44.757873
    2015-02-28    42.837859
    2015-03-31    54.394856
    2015-04-30    50.421756
    2015-05-31    52.526607
    2015-06-30    49.770681
    2015-07-31    57.886844
    2015-08-31    35.762811
    2015-09-30    51.270080
    2015-10-31    45.920629
    2015-11-30    54.597159
    2015-12-31    42.607645
    2016-01-31    52.078086
    2016-02-29    56.904964
    2016-03-31    49.316221
    2016-04-30    54.000531
    2016-05-31    43.374732
    2016-06-30    45.848618
    2016-07-31    49.520627
    2016-08-31    49.724006
    2016-09-30    50.170454
    2016-10-31    41.949666
    2016-11-30    52.484160
    2016-12-31    50.035846
    2017-01-31    47.511667
    2017-02-28    41.287805
    Freq: M, Name: seasadj, dtype: float64
    dates
    2012-12-31    49.609184
    2013-01-31    62.017442
    2013-02-28    54.572534
    2013-03-31    52.747710
    2013-04-30    47.606578
    2013-05-31    54.457986
    2013-06-30    50.682544
    2013-07-31    48.048137
    2013-08-31    48.286644
    2013-09-30    47.993489
    2013-10-31    56.574971
    2013-11-30    42.398613
    2013-12-31    51.666707
    2014-01-31    46.755879
    2014-02-28    54.714097
    2014-03-31    46.526357
    2014-04-30    50.125257
    2014-05-31    51.615576
    2014-06-30    55.650827
    2014-07-31    45.621153
    2014-08-31    51.888377
    2014-09-30    38.722020
    2014-10-31    64.819902
    2014-11-30    49.283275
    2014-12-31    57.120778
    2015-01-31    44.536015
    2015-02-28    43.035150
    2015-03-31    54.453565
    2015-04-30    50.480001
    2015-05-31    52.649180
    2015-06-30    49.591945
    2015-07-31    57.817515
    2015-08-31    35.929480
    2015-09-30    50.856737
    2015-10-31    45.347119
    2015-11-30    54.600143
    2015-12-31    43.273317
    2016-01-31    51.819943
    2016-02-29    57.167042
    2016-03-31    49.369449
    2016-04-30    54.062910
    2016-05-31    43.475949
    2016-06-30    45.683967
    2016-07-31    49.461317
    2016-08-31    49.955740
    2016-09-30    49.765976
    2016-10-31    41.425749
    2016-11-30    52.487029
    2016-12-31    50.817571
    2017-01-31    47.276159
    2017-02-28    41.477957
    Freq: M, Name: seasadj, dtype: float64
    

    enter image description here