【问题标题】:How to fit a restricted VAR model in Python (statsmodels)?如何在 Python(statsmodels)中拟合受限 VAR 模型?
【发布时间】:2018-02-18 18:07:40
【问题描述】:

问题是我有兴趣限制 VAR 模型中的非重要参数,比如 VAR(2)。我如何在 python 3.0x 中这样做?

问题已经针对 R 提出,但不是针对 python

How to fit a restricted VAR model in R?

你能帮我解决这个问题吗?

【问题讨论】:

    标签: python-3.x statistics time-series jupyter-notebook


    【解决方案1】:

    另一种方法是使用 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
    

    【讨论】:

      【解决方案2】:

      据我所知,我找不到能够执行这种限制的 python 包。但是,R 中有一个称为 vars 的包。因此,我不得不使用 rpy2 接口在 python 中扩充 R 包。

      这可能很容易完成,但是有一个应该发生的事件列表允许您从 python 调用 R 函数(甚至包)作为 vars 包。

      可以在link 中找到使用不同包的摘要

      但是,为了便于说明,这里是我的代码

      首先你必须将 vars 导入到 python 中

      # 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')
      

      然后需要在限制已知的 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 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-27
        • 1970-01-01
        • 1970-01-01
        • 2021-12-09
        • 2017-05-19
        • 2020-12-11
        • 2020-08-30
        • 1970-01-01
        相关资源
        最近更新 更多