1

The question is that I am interested in restricting non significant parameters in a VAR model say VAR(2). How may I do so in python 3.0x?

The question is already raised for R but was not for python

How to fit a restricted VAR model in R?

Can you please help me figure this out?

rsc05
  • 3,626
  • 2
  • 36
  • 57

2 Answers2

3

Another way is to use R functions

First, you need to import packages into python

import rpy2.robjects as robjects, pandas as pd, numpy as np
from rpy2.robjects import r
from rpy2.robjects.numpy2ri import numpy2ri
from rpy2.robjects.packages import importr

Import important packages into R from python

r('library("vars")')

Then, suppose you have data in the same directory called here.Rdata

# Load the data in R through python this will create a variable B
r('load("here.Rdata")')

# Change the name of the variable to y
r('y=B')

# Run a normal VAR model
r("t=VAR(y, p=5, type='const')")

# Restrict it 
r('t1=restrict(t, method = "ser", thresh = 2.0, resmat = NULL)')

# Then find the summary statistics
r('s=summary(t1)')

# Save the output into text file call it myfile
r('capture.output(s, file = "myfile.txt")')


# Open it and print it in python
f = open('myfile.txt', 'r')
file_contents = f.read()
print (file_contents)

The output was as follows:

VAR Estimation Results:
========================= 
Endogenous variables: y1, y2 
Deterministic variables: const 
Sample size: 103 
Log Likelihood: 83.772 
Roots of the characteristic polynomial:
0.5334 0.3785 0.3785     0     0     0     0     0     0     0
Call:
VAR(y = y, p = 5, type = "const")


Estimation results for equation y1: 
=================================== 
y1 = y1.l1 + y2.l1 + y1.l2 

      Estimate Std. Error t value Pr(>|t|)   
y1.l1  0.26938    0.11306   2.383  0.01908 * 
y2.l1 -0.21767    0.07725  -2.818  0.00583 **
y1.l2  0.24068    0.10116   2.379  0.01925 * 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


Residual standard error: 0.146 on 100 degrees of freedom
Multiple R-Squared: 0.1047, Adjusted R-squared: 0.07786 
F-statistic: 3.899 on 3 and 100 DF,  p-value: 0.01111 


Estimation results for equation y2: 
=================================== 
y2 = y1.l1 + y2.l1 + const 

      Estimate Std. Error t value Pr(>|t|)    
y1.l1  0.73199    0.16065   4.557 1.47e-05 ***
y2.l1 -0.31753    0.10836  -2.930 0.004196 ** 
const  0.08039    0.02165   3.713 0.000338 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1


Residual standard error: 0.2082 on 100 degrees of freedom
Multiple R-Squared: 0.251,  Adjusted R-squared: 0.2286 
F-statistic: 11.17 on 3 and 100 DF,  p-value: 2.189e-06 



Covariance matrix of residuals:
        y1      y2
y1 0.02243 0.01573
y2 0.01573 0.04711

Correlation matrix of residuals:
       y1     y2
y1 1.0000 0.4838
y2 0.4838 1.0000
rsc05
  • 3,626
  • 2
  • 36
  • 57
1

To my knowledge, I was not able to find a python package that is able to do this kind of restriction. However, there is a package in R which is known as vars. Therefore, I had to augment the R package in python using the rpy2 interface.

This may be done easily but there is a list of events that should take place that allows you from python to call R functions (even packages) as vars package.

A summary using different package may be found in this link

However, for the sake of illustration here is my code

First you have to import vars to python as

# Import vars
Rvars = importr("vars", lib_loc = "C:/Users/Rami Chehab/Documents/R/win-library/3.3")

Then one need to fit a VAR model to your data (or Dataframe) say data as

t=Rvars.VAR(data,p=2, type='const')

then afterwards one needs to apply a different function in the vars packages known by restrict which removes nonsignificant parameters

# Let us try restricting it
t1=Rvars.restrict(t,method = "ser")

The final steps that will allow you to observe the data is to call a built-in function in R known as summary as

# Calling built-in functions from R
Rsummary = robjects.r['summary']

now print the outcome as

print(Rsummary(t1))

This will give

Estimation results for equation Ireland.Real.Bond: 

================================================== 

Ireland.Real.Bond = Ireland.Real.Bond.l1 + Ireland.Real.Equity.l1 + Ireland.Real.Bond.l2 



                       Estimate Std. Error t value Pr(>|t|)   

Ireland.Real.Bond.l1    0.26926    0.11139   2.417  0.01739 * 

Ireland.Real.Equity.l1 -0.21706    0.07618  -2.849  0.00529 **

Ireland.Real.Bond.l2    0.23929    0.09979   2.398  0.01829 * 

---

Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1





Residual standard error: 14.41 on 103 degrees of freedom

Multiple R-Squared: 0.1041, Adjusted R-squared: 0.07799 

F-statistic: 3.989 on 3 and 103 DF,  p-value: 0.009862 





Estimation results for equation Ireland.Real.Equity: 

==================================================== 

Ireland.Real.Equity = Ireland.Real.Bond.l1 + Ireland.Real.Equity.l1 + const 



                       Estimate Std. Error t value Pr(>|t|)    

Ireland.Real.Bond.l1     0.7253     0.1585   4.575 1.33e-05 ***

Ireland.Real.Equity.l1  -0.3112     0.1068  -2.914 0.004380 ** 

const                    7.7494     2.1057   3.680 0.000373 ***

---

Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1





Residual standard error: 20.58 on 103 degrees of freedom

Multiple R-Squared: 0.2462, Adjusted R-squared: 0.2243 

F-statistic: 11.21 on 3 and 103 DF,  p-value: 1.984e-06 
rsc05
  • 3,626
  • 2
  • 36
  • 57