This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Bug in adfuller test and a suggested fix
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: third party
Dependencies: Superseder:
Assigned To: Nosy List: gyllila, ronaldoussoren
Priority: normal Keywords:

Created on 2020-11-06 12:44 by gyllila, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg380446 - (view) Author: Guo (gyllila) Date: 2020-11-06 12:44
from statsmodels.tsa.stattools import adfuller
adf = adfuller(x, regression=‘c’, autolag=‘t-stat’)

Sometimes comes error message:
UnboundLocalError: local variable ‘bestlag’ referenced before assignment 

I found the reason: when using t-stat, bestlag is only assigned, when the last lag becomes significant the first time, so if no lag has a significant t-value, then bestlag is never assigned

I fixed this bug this way: open the file stattools.py and find the lines:
elif method == “t-stat”:
  #stop = stats.norm.ppf(.95)
  stop = 1.6448536269514722
Then add here following two lines:
  bestlag = startlag
  icbest = np.abs(results[startlag].tvalues[-1])

This way, the code won’t crash again and t-stat simply uses no lag when there is no significant value
msg380447 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2020-11-06 12:54
Statsmodel is not part of the Python standard library. 

The issue tracker for this packages seems to be at: https://github.com/statsmodels/statsmodels/issues
History
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86442
2020-11-06 12:54:02ronaldoussorensetstatus: open -> closed

type: enhancement -> behavior

nosy: + ronaldoussoren
messages: + msg380447
resolution: third party
stage: resolved
2020-11-06 12:44:20gyllilacreate