My test data has two columns M0
and exog
(You can download the X13 software and test data from this link).
import statsmodels.api as sm
import pandas as pd
import numpy as np
import os
X13PATH = r'D:\data\X13'
os.chdir(X13PATH)
os.environ['X13PATH'] = X13PATH
print(os.environ['X13PATH'])
file_path = '../test_data/X13_test_data.xlsx'
sheet_name = 'Sheet1'
df = pd.read_excel(file_path, sheet_name=sheet_name, index_col='date')
print(df.head(5))
df.index = pd.to_datetime(df.index)
# df = df.rename(columns={'festival_factor': 'exog', 'M0': 'endog'})
res = sm.tsa.x13_arima_analysis(endog=df['M0'], exog=df['exog'], x12path=X13PATH, print_stdout=True)
# res = sm.tsa.x13_arima_analysis(endog=df['M0'], x12path=X13PATH, print_stdout=True)
print(res.seasadj)
print(res.plot)
Out:
M0 exog
date
2009-07-31 34239.30 0.000000
2009-08-31 34406.62 0.000000
2009-09-30 36787.89 0.333333
2009-10-31 35730.23 0.516129
2009-11-30 36343.86 0.000000
Traceback (most recent call last):
File "D:\data\X13_test.py", line 19, in <module>
res = sm.tsa.x13_arima_analysis(endog=df['M0'], exog=df['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 "D:\Program Files\anaconda3\lib\site-packages\statsmodels\tsa\x13.py", line 518, in x13_arima_analysis
_check_errors(errors)
File "D:\Program Files\anaconda3\lib\site-packages\statsmodels\tsa\x13.py", line 201, in _check_errors
raise X13Error(errors)
statsmodels.tools.sm_exceptions.X13Error: ERROR: Number of user-defined X elements= 166
not equal to a multiple of the number of columns= 4.
I use sm.tsa.x13_arima_analysis(endog=df['M0'], exog=df['exog'], x12path=X13PATH, print_stdout=True)
to perform X13 ARIMA on M0
For seasonal adjustment, the exog
column is used as an exogenous variable, but an error occurs: statsmodels.tools.sm_exceptions.X13Error: ERROR: Number of user-defined X elements= 166 not equal to a multiple of the number of columns= 4.
My analysis found that the main cause of the error is caused by exog=df['exog']
, because sm.tsa.x13_arima_analysis(endog=df['M0'], x12path=X13PATH, print_stdout=True)
can run normally.
How to deal with this error? Thanks.
Update:
res = sm.tsa.x13_arima_analysis(endog=df['M0'], exog=df['exog'].values, x12path=X13PATH, print_stdout=True)
Out:
M0 exog
date
2009-07-31 34239.30 0.000000
2009-08-31 34406.62 0.000000
2009-09-30 36787.89 0.333333
2009-10-31 35730.23 0.516129
2009-11-30 36343.86 0.000000
Traceback (most recent call last):
File ".\X13_test.py", line 34, in <module>
res = sm.tsa.x13_arima_analysis(endog=df['M0'], exog=df['exog'].values, 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 "D:\Program Files\anaconda3\lib\site-packages\statsmodels\tsa\x13.py", line 497, in x13_arima_analysis
spec += _make_regression_options(trading, exog)
File "D:\Program Files\anaconda3\lib\site-packages\statsmodels\tsa\x13.py", line 180, in _make_regression_options
var_names = _make_var_names(exog)
File "D:\Program Files\anaconda3\lib\site-packages\statsmodels\tsa\x13.py", line 159, in _make_var_names
raise ValueError("exog is not a Series or DataFrame or is unnamed.")
ValueError: exog is not a Series or DataFrame or is unnamed.