Repository: statsmodels/statsmodels

Issue:
AR model pvalues error with Pandas
Possibly related to #721 

After running: 

``` python
import statsmodels as sm
y=data.sentiment.dropna()
armodel=sm.tsa.ar_model.AR(y).fit(maxlags=15)
armodel.params
armodel.pvalues
```

armodel.params works just fine, but armodel.pvalues has the following traceback; 

`````` python
AttributeError                            Traceback (most recent call last)
C:\Users\Anna\Projects\honors\<ipython-input-18-e6db232ba3f4> in <module>()
      1 armodel.params
----> 2 armodel.pvalues

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\base\wrapper.pyc in __getattribute__(self, attr)
     33             pass
     34 
---> 35         obj = getattr(results, attr)
     36         data = results.model.data
     37         how = self._wrap_attrs.get(attr)

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tools\decorators.pyc in __get__(self, obj, type)
     93         if _cachedval is None:
     94             # Call the "fget" function

---> 95             _cachedval = self.fget(obj)
     96             # Set the attribute in obj

     97 #            print "Setting %s in cache to %s" % (name, _cachedval)


C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tsa\ar_model.pyc in pvalues(self)
    732     @cache_readonly
    733     def pvalues(self):
--> 734         return norm.sf(np.abs(self.tvalues))*2
    735 
    736     @cache_readonly

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tools\decorators.pyc in __get__(self, obj, type)
     93         if _cachedval is None:
     94             # Call the "fget" function

---> 95             _cachedval = self.fget(obj)
     96             # Set the attribute in obj

     97 #            print "Setting %s in cache to %s" % (name, _cachedval)


C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\base\model.pyc in tvalues(self)
    973         Return the t-statistic for a given parameter estimate.
    974         """
--> 975         return self.params / self.bse
    976 
    977     @cache_readonly

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tools\decorators.pyc in __get__(self, obj, type)
     93         if _cachedval is None:
     94             # Call the "fget" function

---> 95             _cachedval = self.fget(obj)
     96             # Set the attribute in obj

     97 #            print "Setting %s in cache to %s" % (name, _cachedval)


C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tsa\ar_model.pyc in bse(self)
    722     def bse(self): # allow user to specify?
    723         if self.model.method == "cmle": # uses different scale/sigma definition
--> 724             resid = self.resid
    725             ssr = np.dot(resid,resid)
    726             ols_scale = ssr/(self.nobs - self.k_ar - self.k_trend)

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tools\decorators.pyc in __get__(self, obj, type)
     93         if _cachedval is None:
     94             # Call the "fget" function

---> 95             _cachedval = self.fget(obj)
     96             # Set the attribute in obj

     97 #            print "Setting %s in cache to %s" % (name, _cachedval)


C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tsa\ar_model.pyc in resid(self)
    782         endog = model.endog.squeeze()
    783         if model.method == "cmle": # elimate pre-sample
--> 784             return endog[self.k_ar:] - self.fittedvalues
    785         else:
    786             return model.endog.squeeze() - self.fittedvalues

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tools\decorators.pyc in __get__(self, obj, type)
     93         if _cachedval is None:
     94             # Call the "fget" function

---> 95             _cachedval = self.fget(obj)
     96             # Set the attribute in obj

     97 #            print "Setting %s in cache to %s" % (name, _cachedval)


C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tsa\ar_model.pyc in fittedvalues(self)
    797     @cache_readonly
    798     def fittedvalues(self):
--> 799         return self.model.predict(self.params)
    800 
    801     def predict(self, start=None, end=None, dynamic=False):

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tsa\ar_model.pyc in predict(self, params, start, end, dynamic)
    191         # will return an index of a date

    192         start = self._get_predict_start(start, dynamic)
--> 193         end, out_of_sample = self._get_predict_end(end)
    194 
    195         if start - end > 1:

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tsa\base\tsa_model.pyc in _get_predict_end(self, end)
    187                                                 self.data.predict_start)
    188 
--> 189             self._make_predict_dates()
    190 
    191         elif isinstance(end, int):

C:\Python27\lib\site-packages\statsmodels-0.5.0-py2.7-win-amd64.egg\statsmodels\tsa\base\tsa_model.pyc in _make_predict_dates(self)
    203     def _make_predict_dates(self):
    204         data = self.data
--> 205         dtstart = data.predict_start
    206         dtend = data.predict_end
    207         freq = data.freq

AttributeError: 'PandasData' object has no attribute 'predict_start'
```python
``````



Discussion:
Thanks for reporting. I'm able to replicate this. 

```
dta = sm.datasets.macrodata.load_pandas().data["cpi"].diff().dropna()
dates = sm.tsa.datetools.dates_from_range("1959Q1", length=len(dta))
dta.index = dates
ar = sm.tsa.AR(dta).fit(maxlags=15)
ar.params
ar.pvalues
```

In the future, it's a tad easier for us if you could post a full replication.


Resolving patch:
diff --git a/statsmodels/tsa/ar_model.py b/statsmodels/tsa/ar_model.py
--- a/statsmodels/tsa/ar_model.py
+++ b/statsmodels/tsa/ar_model.py
@@ -153,6 +153,7 @@ def _get_predict_start(self, start, dynamic):
             start = _validate(start, k_ar, self.data.dates, method)
             start = super(AR, self)._get_predict_start(start)
         _check_ar_start(start, k_ar, method, dynamic)
+        self._set_predict_start_date(start)
         return start
 
     def predict(self, params, start=None, end=None, dynamic=False):


Test patch:
diff --git a/statsmodels/tsa/tests/test_ar.py b/statsmodels/tsa/tests/test_ar.py
--- a/statsmodels/tsa/tests/test_ar.py
+++ b/statsmodels/tsa/tests/test_ar.py
@@ -272,6 +272,14 @@ def test_ar_start_params():
     res = AR(data.endog).fit(maxlag=9, start_params=0.1*np.ones(10.),
                              method="mle", disp=-1)
 
+def test_ar_series():
+    # smoke test for 773
+    dta = sm.datasets.macrodata.load_pandas().data["cpi"].diff().dropna()
+    dates = sm.tsa.datetools.dates_from_range("1959Q1", length=len(dta))
+    dta.index = dates
+    ar = AR(dta).fit(maxlags=15)
+    ar.bse
+
 
 #TODO: likelihood for ARX model?
 #class TestAutolagARX(object):