从Matlab到R的ADF测试转换

时间:2016-05-27 15:38:29

标签: r matlab

在线,我找到了ADF测试的matlab代码以及SADF和DF测试的测试。现在,为了将这个matlab代码转换为R,第45行仍然倾向于给我一个错误。

    # if mflag=1 use ADF with constant and without trend
    # if mlag=2 use ADF with constant and trend
    # if mlag=3 use ADF without constant and trend

    #ADF  

    #Set data sample equal to y to start
    y=Data$OIL.P

    #Start ADF test 
    ADF_FL<- function(y,adflag,mflag)
    {
      t1=dim(y,1)-1
      const=matrix(1,t1,1)
      trend=t(seq(1,1,by=t1)) #or trend=t(c(seq(1,1,by=t)1)))

      y1=y(dim(y,1)-t1:dim(y,1)-1)
      dy=y(2:dim(y, 1))-y(1:dim(y,1)-1)
      dy0=dy(dim(dy,1)-t1+1:dim(dy,1))
      x=y1

      if(mflag==1){
      x=c(x,const)
      } else{
      x=c(x, const, trend)
      }

      x1=x

      t2=t1-adflag
      x2=x1(dim(x1,1)-t2+1:dim(x1,1),) #from k+1 to the end (including y1 and x)
      dy01=dy(dim(dy0,1)-t2+1:dim(dy0,1)) #from k+1 to the end (including dy0)

      if(adflag>0){
        j=1
        while(j<=adflag){
          x2=[x2 dy(dim(dy,1)-t2+1-j:dim(dy,1)-j)] #problem in this line
          j=j+1
        }
      }

      beta=((t(x2))*x2)^(-1)*(t(x2)*dy01)
      eps=dy01-x2*beta

      if(mflag==1){
        sign=sqrt(diag(t(eps)*eps/(t2-adflag-2)*(t(x2)*x2)^(-1)))
      } else{
        sign=sqrt(diag(t(eps)*eps/(t2-adflag-3)*(t(x2)*x2)^(-1)))
      }

      tvalue=beta/sign
      show(tvalue)
     }

原始matlab代码:

    function [estm]=ADF_FL(y,adflag,mflag)

      t1=size(y,1)-1;
      const=ones(t1,1);
      trend=1:1:t1;trend=trend';

      y1  = y(size(y,1)-t1:size(y,1)-1);
      dy  = y(2:size(y,1)) - y(1:size(y,1)-1);
      dy0 = dy(size(dy,1)-t1+1:size(dy,1));
      x=y1;

      if mflag==1;  x=[x const]; end;
      if mflag==2;  x=[x const trend]; end;
      % if mflag==3;  x=x; end;

      x1=x; 

      t2=t1-adflag;
      x2=x1(size(x1,1)-t2+1:size(x1,1),:);         % from k+1 to the end  (including y1 and x)-@
      dy01=dy0(size(dy0,1)-t2+1:size(dy0,1));      % from k+1 to the end   (including dy0)-@

      if adflag>0;
        j=1; 
        while j<=adflag; 
        x2=[x2 dy(size(dy,1)-t2+1-j:size(dy,1)-j)];     %including k lag variables of dy in x2-@
        j=j+1; 
      end;
    end;

       beta =(x2'*x2)^(-1)*(x2'*dy01);                       % model A-@
       eps  = dy01 - x2*beta;

       if mflag==1; sig = sqrt(diag(eps'*eps/(t2-adflag-2)*(x2'*x2)^(-1))); end;
       if mflag==2; sig = sqrt(diag(eps'*eps/(t2-adflag-3)*(x2'*x2)^(-1))); end;
       if mflag==3; sig = sqrt(diag(eps'*eps/(t2-adflag-1)*(x2'*x2)^(-1))); end;

       tvalue=beta./sig;

       estm=tvalue(1);

有人可以帮助我吗?

谢谢,

大卫

1 个答案:

答案 0 :(得分:1)

我有完整的代码,在这里用R编码。 通过这种编码,它假设y是一个向量,这就是为什么第一步将它转换成矩阵的原因。它可以很好地找到ADF统计数据,并将其与fUnitRoot adfTest进行比较,并得出相同的结果。

ADF_FL = function(y,adflag,mflag){
  y = as.matrix(y, nrow=1)
  t1 = nrow(y)-1
  constant = matrix(1,nrow=t1,ncol=1)
  trend = as.matrix(seq(1,t1,by=1),ncol=1)

  y1 = as.matrix(y[(nrow(y)-t1):(nrow(y)-1),],ncol=1)
  dy =as.matrix(y[-1,]- y[1:(nrow(y)-1),],ncol=1)
  dy0 = as.matrix(dy[(nrow(dy)-t1+1):nrow(dy)],ncol=1)
  x = y1

  if(mflag == 1){
    x = cbind(constant,x)
  }
  if(mflag == 2){
    x = cbin(constant,trend,x)
  } else{x = x}

  x1 = x

  t2 = t1 - adflag
  x2 = x1[(nrow(x1)-t2+1):nrow(x1),]       
  dy01 = as.matrix(dy0[(nrow(dy0)-t2+1):nrow(dy0),],ncol=1)

  if(adflag>0){
    j = 1
    while(j<=adflag){
      x2 = cbind(x2,dy[(nrow(dy)-t2+1-j):nrow(dy)-j])
      j = j+1
    }
  }

  beta = solve(t(x2)%*%x2)%*%(t(x2)%*%dy01) ## beta de la regresion de la prueba ADF
  eps = dy01-x2%*%beta ## errores de la regresion 
  if(mflag == 1){sig = sqrt(diag(as.numeric(t(eps)%*%(eps/(t2-adflag-2)))*solve(t(x2)%*%x2)))}
  if(mflag == 2){sig = sqrt(diag(as.numeric(t(eps)%*%(eps/(t2-adflag-3)))*solve(t(x2)%*%x2)))}
  if(mflag == 3){sig = sqrt(diag(as.numeric(t(eps)%*%(eps/(t2-adflag-1)))*solve(t(x2)%*%x2)))}

  tvalue = beta/sig
  return(tvalue[2])
}