Repository: statsmodels/statsmodels

Issue:
acorr_breusch_godfrey Value Error
When using statsmodels.stats.diagnostic.acorr_breusch_godfrey and passing a Results instance. I am getting the following ValueError

```
in acorr_breusch_godfrey(results, nlags, store)
    499         nlags = int(nlags)
    500
--> 501     x = np.concatenate((np.zeros(nlags), x))
    502
    503     #xdiff = np.diff(x)

ValueError: all the input arrays must have same number of dimensions
```

Looking at the error it looks like a bug to me, since the nlags will most likely be different than the length of the x (the residuals from the results instance). Thus, input arrays don't have the same number of dimensions


Discussion:
which model are you using, which results class?
What's the dimension of your x?

AFAIR, x should be 1-dim, and then concatenates without problems with a 1-dim `np.zeros(nlags)`.
I am using `statsmodels.tsa.api.VAR` to create my model.
```
from statsmodels.tsa.api import VAR
model = VAR(df)
results = model.fit(ic='bic')
```
x has the dimension of the dataframe which is multivariate.

Based on the comment above, it looks like I'm probably not passing the correct results instance. What class should I use? 
diagnositc functions like statsmodels.stats.diagnostic.acorr_breusch_godfrey where written for the univariate endog case.
VAR and similar are multivariate. We only have the diagnostics in tsa vector_ar and in statespace for those.

AFAIR, I never looked at a multivariate extension of acorr_breusch_godfrey, but don't remember if there is something similar already in tsa for the multivariate case
A quick google search shows that there is a literature for breush godfrey for VAR with implementation in R
https://stats.stackexchange.com/questions/301225/breusch-godfrey-lm-test-returning-different-results-in-vars-and-lmtest

for VAR we have https://www.statsmodels.org/dev/generated/statsmodels.tsa.vector_ar.var_model.VARResults.test_whiteness.html

Resolving patch:
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,7 +490,10 @@ def acorr_breusch_godfrey(results, nlags=None, store=False):
 
     '''
 
-    x = np.asarray(results.resid)
+    x = np.asarray(results.resid).squeeze()
+    if x.ndim != 1:
+        raise ValueError('Model resid must be a 1d array. Cannot be used on'
+                         ' multivariate models.')
     exog_old = results.model.exog
     nobs = x.shape[0]
     if nlags is None:


Test patch:
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
@@ -10,6 +10,7 @@
 
 """
 import os
+import json
 
 import numpy as np
 import pandas as pd
@@ -21,11 +22,9 @@
 from statsmodels.regression.linear_model import OLS
 from statsmodels.tools.tools import add_constant
 from statsmodels.datasets import macrodata
-
 import statsmodels.stats.sandwich_covariance as sw
 import statsmodels.stats.diagnostic as smsdia
-import json
-
+from statsmodels.tools.tools import Bunch
 import statsmodels.stats.outliers_influence as oi
 
 cur_dir = os.path.abspath(os.path.dirname(__file__))
@@ -305,6 +304,11 @@ def test_acorr_breusch_godfrey(self):
         bg3 = smsdia.acorr_breusch_godfrey(res, nlags=14)
         assert_almost_equal(bg2, bg3, decimal=13)
 
+    def test_acorr_breusch_godfrey_multidim(self):
+        res = Bunch(resid=np.empty((100,2)))
+        with pytest.raises(ValueError, match='Model resid must be a 1d array'):
+            smsdia.acorr_breusch_godfrey(res)
+
     def test_acorr_ljung_box(self):
 
         #unit-test which may be useful later