问题是我有兴趣限制VAR模型中的非重要参数说VAR(2)。我怎么能在python 3.0x中这样做?
已经针对R提出了问题,但不是针对python
How to fit a restricted VAR model in R?
你能帮我解决这个问题吗?
答案 0 :(得分:0)
据我所知,我无法找到能够做到这种限制的python包。但是,R中有一个包称为vars。因此,我不得不使用rpy2接口在python中扩充R包。
这可以很容易地完成,但是有一个事件列表可以让你从python调用R函数(甚至包)作为vars包。
使用不同包的摘要可在此link
中找到但是,为了便于说明,这里是我的代码
首先,您必须将变量导入到python中
# Import vars
Rvars = importr("vars", lib_loc = "C:/Users/Rami Chehab/Documents/R/win-library/3.3")
然后需要将VAR模型拟合到您的数据(或Dataframe)中,并将data
称为
t=Rvars.VAR(data,p=2, type='const')
然后需要在restrict已知的vars包中应用不同的函数,这会删除不重要的参数
# Let us try restricting it
t1=Rvars.restrict(t,method = "ser")
允许您观察数据的最后步骤是调用R中的内置函数,称为摘要
# Calling built-in functions from R
Rsummary = robjects.r['summary']
现在将结果打印为
print(Rsummary(t1))
这将给出
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
答案 1 :(得分:0)
另一种方法是使用R函数
首先,您需要将包导入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
从python
将重要的包导入R中r('library("vars")')
然后,假设您在同一目录中有数据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)
输出如下:
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