2

I am trying to fit a time series to some data I have, and would like to forecast into the future. I have 2 questions: 1) is the model I use for this particular time series appropriate? and 2) Why, when I try to forecast, am I getting an error message?

The data more or less looks linear with some slight seasonality, so I thought statsmodels UnobservedCompoents model would give a good fit. The model appears to give a good fit as shown in the figures below. I have included the code I used to make these figures.

import matplotlib.pyplot as plt
import matplotlib
matplotlib.style.use('ggplot')
import numpy as np
import pandas as pd
from statsmodels.tsa.statespace.structural import UnobservedComponents

endog = pd.read_csv('file_name', parse_dates=True, index_col=0)

model=UnobservedComponents(endog, 'local level').fit()
fig = model.plot_components(figsize=(10, 10))

data and model fit

The dataframe, endog, looks like this:

data_frame

The model appears to fit the data quite well, but I am getting an error message when I try to forecast 100 steps into the future with the following command.

forecast=model.forecast(steps=100)

The full error is given below. Can anyone tell me why the .forecast method works with other statsmodels time series models, but is giving me an error here? Thanks for your help.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-c04a026facb5> in <module>()
----> 1 forecast=model.forecast(steps=100)

/Users/deeps/anaconda/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-    macosx-10.5-x86_64.egg/statsmodels/base/wrapper.py in wrapper(self, *args,  **kwargs)
     93             obj = data.wrap_output(func(results, *args, **kwargs),    how[0], how[1:])
     94         elif how:
---> 95             obj = data.wrap_output(func(results, *args, **kwargs), how)
     96         return obj
     97 

/Users/deeps/anaconda/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-   macosx-10.5-x86_64.egg/statsmodels/tsa/statespace/structural.py in forecast(self, steps, exog, **kwargs)
   1547         """
   1548         return super(UnobservedComponentsResults, self).forecast(
-> 1549             steps, exog=exog, **kwargs)
   1550 
   1551     def summary(self, alpha=.05, start=None):

/Users/deeps/anaconda/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-macosx-10.5-x86_64.egg/statsmodels/tsa/statespace/mlemodel.py in forecast(self, steps, **kwargs)
   2373         else:
   2374             end = steps
-> 2375         return self.predict(start=self.nobs, end=end, **kwargs)
   2376 
   2377     def simulate(self, nsimulations, measurement_shocks=None,

/Users/deeps/anaconda/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-macosx-10.5-x86_64.egg/statsmodels/tsa/statespace/structural.py in predict(self, start, end, exog, dynamic, **kwargs)
   1476 
   1477         # Handle end (e.g. date)
-> 1478         _start = self.model._get_predict_start(start)
   1479         _end, _out_of_sample = self.model._get_predict_end(end)
   1480 

/Users/deeps/anaconda/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-macosx-10.5-x86_64.egg/statsmodels/tsa/base/tsa_model.py in _get_predict_start(self, start)
    129                         (str(start), str(dtstart)))
    130 
--> 131         self._set_predict_start_date(start)
    132         return start
    133 

/Users/deeps/anaconda/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-macosx-10.5-x86_64.egg/statsmodels/tsa/base/tsa_model.py in _set_predict_start_date(self, start)
    104         if start == len(dates):
    105             self.data.predict_start =     datetools._date_from_idx(dates[-1],
--> 106                                                     1, self.data.freq)
    107         elif start < len(dates):
    108             self.data.predict_start = dates[start]

/Users/deeps/anaconda/lib/python3.5/site-packages/statsmodels-0.8.0-py3.5-macosx-10.5-x86_64.egg/statsmodels/tsa/base/datetools.py in _date_from_idx(d1, idx, freq)
     77     offset. For now, this needs to be taken care of before you get here.
     78     """
---> 79     return d1 + idx * _freq_to_pandas[freq]
     80 
     81 def _idx_from_dates(d1, d2, freq):

TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
Doug Rubin
  • 21
  • 3
  • my guess is that you need to either use a numpy array or provide a time index for the pandas Series. e.g. answer here http://stackoverflow.com/questions/35860357/fitting-arma-model-to-time-series-indexed-by-time-in-python/35860703#35860703 – Josef Mar 09 '16 at 16:58

0 Answers0