Repository: statsmodels/statsmodels

Issue:
acorr_breush_godfrey  undefined nlags
acorr_breush_godfrey uses nlags before the default None is overwritten



Discussion:
related: need minimum default nlags for very small samples, or minimum nobs, see #347


Resolving patch:
diff --git a/statsmodels/nonparametric/smoothers_lowess.py b/statsmodels/nonparametric/smoothers_lowess.py
--- a/statsmodels/nonparametric/smoothers_lowess.py
+++ b/statsmodels/nonparametric/smoothers_lowess.py
@@ -165,7 +165,7 @@ def lowess(endog, exog, frac=2.0/3.0, it=3, delta=0.0, is_sorted=False,
     res = _lowess(y, x, frac=frac, it=it, delta=delta)
     _, yfitted = res.T
 
-    if return_sorted or (all_valid and is_sorted):
+    if return_sorted:
         return res
     else:
         # rebuild yfitted with original indices
@@ -175,6 +175,8 @@ def lowess(endog, exog, frac=2.0/3.0, it=3, delta=0.0, is_sorted=False,
             yfitted_.fill(np.nan)
             yfitted_[sort_index] = yfitted
             yfitted = yfitted_
+        else:
+            yfitted = yfitted
 
         if not all_valid:
             yfitted_ = np.empty_like(endog)
diff --git a/statsmodels/sandbox/stats/diagnostic.py b/statsmodels/sandbox/stats/diagnostic.py
--- a/statsmodels/sandbox/stats/diagnostic.py
+++ b/statsmodels/sandbox/stats/diagnostic.py
@@ -490,14 +490,14 @@ def acorr_breush_godfrey(results, nlags=None, store=False):
 
     '''
 
-    x = np.concatenate((np.zeros(nlags), results.resid))
+    x = np.asarray(results.resid)
     exog_old = results.model.exog
-    x = np.asarray(x)
     nobs = x.shape[0]
     if nlags is None:
         #for adf from Greene referencing Schwert 1989
-        nlags = 12. * np.power(nobs/100., 1/4.)#nobs//4  #TODO: check default, or do AIC/BIC
+        nlags = np.trunc(12. * np.power(nobs/100., 1/4.))#nobs//4  #TODO: check default, or do AIC/BIC
 
+    x = np.concatenate((np.zeros(nlags), x))
 
     #xdiff = np.diff(x)
     #


Test patch:
diff --git a/statsmodels/nonparametric/tests/test_lowess.py b/statsmodels/nonparametric/tests/test_lowess.py
--- a/statsmodels/nonparametric/tests/test_lowess.py
+++ b/statsmodels/nonparametric/tests/test_lowess.py
@@ -112,6 +112,11 @@ def test_options(self):
         actual_lowess = lowess(y[::-1], x[::-1], return_sorted=False)
         assert_almost_equal(actual_lowess, actual_lowess1[::-1, 1], decimal=13)
 
+        # check returns yfitted only
+        actual_lowess = lowess(y, x, return_sorted=False, missing='none',
+                               is_sorted=True)
+        assert_almost_equal(actual_lowess, actual_lowess1[:, 1], decimal=13)
+
         # check integer input
         actual_lowess = lowess(np.round(y).astype(int), x, is_sorted=True)
         actual_lowess1 = lowess(np.round(y), x, is_sorted=True)
diff --git a/statsmodels/stats/tests/test_diagnostic.py b/statsmodels/stats/tests/test_diagnostic.py
--- a/statsmodels/stats/tests/test_diagnostic.py
+++ b/statsmodels/stats/tests/test_diagnostic.py
@@ -294,6 +294,11 @@ def test_acorr_breush_godfrey(self):
                 breushgodfrey_f['statistic'], breushgodfrey_f['pvalue']]
         assert_almost_equal(bg, bg_r, decimal=13)
 
+        # check that lag choice works
+        bg2 = smsdia.acorr_breush_godfrey(res, nlags=None)
+        bg3 = smsdia.acorr_breush_godfrey(res, nlags=14)
+        assert_almost_equal(bg2, bg3, decimal=13)
+
     def test_acorr_ljung_box(self):
         res = self.res