Python 3.x 如何在Python中适应受限的VAR模型(statsmodels)?
问题是我对限制VAR模型中的非重要参数感兴趣,比如VAR(2)。在python 3.0x中如何执行此操作 这个问题已经针对R提出了,但不是针对pythonPython 3.x 如何在Python中适应受限的VAR模型(statsmodels)?,python-3.x,statistics,time-series,jupyter-notebook,Python 3.x,Statistics,Time Series,Jupyter Notebook,问题是我对限制VAR模型中的非重要参数感兴趣,比如VAR(2)。在python 3.0x中如何执行此操作 这个问题已经针对R提出了,但不是针对python 你能帮我弄清楚这一点吗?据我所知,我找不到一个能够进行这种限制的python包。然而,R中有一个称为vars的包。因此,我必须使用rpy2接口在python中扩充R包 这可能很容易做到,但是有一个应该发生的事件列表,允许您从python调用R函数(甚至是包)作为vars包 使用不同软件包的摘要可在本文档中找到 然而,为了便于说明,这里是我的
你能帮我弄清楚这一点吗?据我所知,我找不到一个能够进行这种限制的python包。然而,R中有一个称为vars的包。因此,我必须使用rpy2接口在python中扩充R包 这可能很容易做到,但是有一个应该发生的事件列表,允许您从python调用R函数(甚至是包)作为vars包 使用不同软件包的摘要可在本文档中找到 然而,为了便于说明,这里是我的代码 首先,您必须将变量作为
# Import vars
Rvars = importr("vars", lib_loc = "C:/Users/Rami Chehab/Documents/R/win-library/3.3")
然后需要将VAR模型与您的数据(或数据框架)相匹配,比如说data
as
t=Rvars.VAR(data,p=2, type='const')
然后,需要在restrict已知的vars包中应用一个不同的函数,该函数删除不重要的参数
# Let us try restricting it
t1=Rvars.restrict(t,method = "ser")
允许您观察数据的最后一个步骤是调用R中的内置函数summary as
# 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
另一种方法是使用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")')
然后,假设在同一个名为的目录中有数据。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