PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/tsa/ex_dates.py

http://github.com/statsmodels/statsmodels
Python | 47 lines | 16 code | 13 blank | 18 comment | 0 complexity | aecaed14e53fab57646d3cf98e60cf2f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Using dates with timeseries models
  3. """
  4. import statsmodels.api as sm
  5. import numpy as np
  6. import pandas
  7. # Getting started
  8. # ---------------
  9. data = sm.datasets.sunspots.load()
  10. # Right now an annual date series must be datetimes at the end of the year.
  11. from datetime import datetime
  12. dates = sm.tsa.datetools.dates_from_range('1700', length=len(data.endog))
  13. # Using Pandas
  14. # ------------
  15. # Make a pandas TimeSeries or DataFrame
  16. endog = pandas.TimeSeries(data.endog, index=dates)
  17. # and instantiate the model
  18. ar_model = sm.tsa.AR(endog, freq='A')
  19. pandas_ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)
  20. # Let's do some out-of-sample prediction
  21. pred = pandas_ar_res.predict(start='2005', end='2015')
  22. print pred
  23. # Using explicit dates
  24. # --------------------
  25. ar_model = sm.tsa.AR(data.endog, dates=dates, freq='A')
  26. ar_res = ar_model.fit(maxlag=9, method='mle', disp=-1)
  27. pred = ar_res.predict(start='2005', end='2015')
  28. print pred
  29. # This just returns a regular array, but since the model has date information
  30. # attached, you can get the prediction dates in a roundabout way.
  31. print ar_res._data.predict_dates
  32. # This attribute only exists if predict has been called. It holds the dates
  33. # associated with the last call to predict.
  34. #..TODO: should this be attached to the results instance?