1

Anyone has an idea on how to model a VECM in python? I can't find it in the statsmodels package.

BigChief
  • 1,413
  • 4
  • 24
  • 37
  • 1
    It is pretty much finished but needs more review before merging https://github.com/statsmodels/statsmodels/pull/3246 – Josef Mar 13 '17 at 22:35
  • @user333700 hmmzz still a bit lagging with R I wonder if it's possible to fit a VECM with the current VAR functions (how??) – BigChief Mar 14 '17 at 18:16
  • 2
    R has a large head start, but the catchup targets are Luetkepohl's package, Stata, Eviews and PcGive. No, current VAR doesn't work for VECM, it doesn't have an estimator for the cointegrating relationship, and even if that is given it doesn't allow for extra explanatory variables in the VAR in differences, i.e. the current version has no X in VAR. – Josef Mar 14 '17 at 19:44
  • 1
    When I get the latest version of statsmodels with conda command, VECM is not downloaded into the statsmodels library. However, it is now obviously in the vector-ar sub-library (https://github.com/josef-pkt/statsmodels/tree/master/statsmodels/tsa/vector_ar). What I ended up doing was to copy and paste the vecm.py file manually. – Saeed Dec 30 '17 at 00:06

1 Answers1

3

https://gist.github.com/yogabonito/5461b26bed335cad6907aa4e613acb99

In this git link they implement a model using VECM in python

Some important parts of code are here

%matplotlib inline
import pandas
from statsmodels.tsa.vecm.vecm import VECM, select_order
import data as dta
iidata = dta.load_pandas();
mdata = iidata.data
dates = mdata[['year', 'quarter']].astype(int).astype(str)
quarterly = dates["year"] + "Q" + dates["quarter"]
from statsmodels.tsa.base.datetools import dates_from_str
quarterly = dates_from_str(quarterly)
mdata = mdata[dta.variable_names]
mdata.index = pandas.DatetimeIndex(quarterly)
data = mdata
model = VECM(data, diff_lags=3, coint_rank=1)
vecm_res = model.fit()
vecm_res.gamma.round(4)
vecm_res.summary()
[![vecm_res.predict(steps=5)
forecast, lower, upper = vecm_res.predict(5, 0.05)
print("lower bounds of confidence intervals:")
print(lower.round(3))
print("\npoint forecasts:")
print(forecast.round(3))
print("\nupper bounds of confidence intervals:")
print(upper.round(3))
vecm_res.plot_forecast(steps=10)][1]][1]

Output forecast shown here:

Output of forecast with inputom/1BVSA.png

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/21038033) – Rahul Oct 04 '18 at 11:10
  • 1
    Thank you for your suggestion @RahulMeshram I added the code and screenshot – saravanan saminathan Oct 04 '18 at 12:20