The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Comparing Higher-Lag AR Models

Comparing Higher-Lag AR Models

0
Votes
1
Answer

Comparing Higher-Lag AR Models

In [121]:

 

 

 
 

 

1

LLR_test(model_ar_2, model_ar_3)

 

 

 

 

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-121-be745ec553e3> in <module>
----> 1 LLR_test(model_ar_2, model_ar_3)

<ipython-input-120-3e315fe52fa6> in LLR_test(mod_1, mod_2, DF)
      1 def LLR_test(mod_1, mod_2, DF=1):
----> 2     L1 = mod_1.fit().llf
      3     L2 = mod_2.fit().llf
      4     LR = (2*(L2-L1))
      5     p = chi2.sf(LR, DF).round(3)

~\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in fit(self, start_params, trend, method, transparams, solver, maxiter, full_output, disp, callback, start_ar_lags, **kwargs)
    902         # (re)set trend and handle exogenous variables
    903         # always pass original exog
--> 904         k_trend, exog = _make_arma_exog(endog, self.exog, trend)
    905 
    906         # Check has something to estimate

~\Anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py in _make_arma_exog(endog, exog, trend)
    395         exog = np.ones((len(endog), 1))
    396     elif exog is not None and trend == 'c':  # constant plus exogenous
--> 397         exog = add_trend(exog, trend='c', prepend=True, has_constant='raise')
    398     elif exog is not None and trend == 'nc':
    399         # make sure it's not holding constant from last run

~\Anaconda3\lib\site-packages\statsmodels\tsa\tsatools.py in add_trend(x, trend, prepend, has_constant)
    106                 msg = "x contains a constant. Adding a constant with " \
    107                       "trend='{0}' is not allowed.".format(trend)
--> 108                 raise ValueError(msg)
    109             elif has_constant == 'skip':
    110                 columns = columns[1:]

ValueError: x contains a constant. Adding a constant with trend='c' is not allowed.
1 Answer

365 Team
0
Votes

Hey Davison, 
 
Thanks for reaching out!
The issue here comes from the release of a newer version of statsmodels than the one the code was initially created on. The simplest solution is to remove “.fit()” and use the “results” variables over the “model” variables when calling the LLR_test function. 

In other words, rewrite the LLR_test function to use:

  1. L1 = mod_1.llf
  2. L2 = mod_2.llf

rather than:

  1. L1 = mod_1.fit().llf
  2. L2 = mod_2.fit().llf

Then, when you actually call the function, you’d be using the “results” variables, so:

  1. LLR_test(results_ar_2, results_ar_3)

instead of:

  1. LLR_test(model_ar_2, model_ar_3)

Python returns an error when fitting the same model twice, so we work around this issue by using the fitted results in the LLR_test instead.
 
Hope this helps!
Best,
365 Vik

×
Ready to Learn
Data Science?
50% OFF