Funkcia pre výpočet autokorelácie

1.
auto = function (mat,dl,du)
{
  nr = nrow(mat)
  nc = ncol(mat)
  mt1 = mat[1:nr,2:nc]
  y1=mt1[1:nr,1]
  et=c()
  et2=c()
  ett=c()
  ett2=c()
  etet1=c()
  sumett2=0
  sumet2=0
  sumetet1=0
  d=0
  r=0
  mat1=mat[1:nr-1,1:nc-1]
 
  reg = function (Yt, mm)
  {
    regr = lm (Yt~mm)
    nr = nrow(mm)
    nc = ncol(mm)
    y = c()
    b = c()
    for (i in 1:nc)
    {
      b[i]=regr$coef[i]
    }    
    for (i in 1:nr)
    {
      ypom = 0;
      for (j in 2:nc)
      {
        ypom = ypom + b[j]*mm[i,(j-1)]
      }
      y[i]=b[1]+ypom
    }
    y   
  }
  yt1 = reg(y1,mt1)
  for (i in 1:nr)
    {
      et[i] = y1[i]-yt1[i]
    }    
  for (i in 1:nr)
    {
      et2[i] = et[i]*et[i]
      sumet2=sumet2+et2[i]
    }    
  for (i in 2:nr)
    {
      ett[i-1] = et[i]-et[i-1]
    }    
  for (i in 1:nr)
    {
      ett2[i] = ett[i]*ett[i]
      sumett2=sumett2+ett2[i]
    }
  for (i in 2:nr)
    {
      etet1[i-1] = et[i]*et[i-1]
      sumetet1 = sumetet1+etet1[i-1]
    }          
  d=sumett2/sumet2
  r = sumetet1 / sumet2
  if (d<dl){
    cat(
      " =======================================","\n",
      " Results:","\n\n",
      "---------------------------------------","\n",
      " d: ",d,"\n",
      " dl: ",dl,"\n",
      " du: ",du,"\n",
      " Positive autocorrelation","\n",
      "=======================================","\n\n"
      
    for (i in 1:nc){
      for (j in 1:nr){
        mat1[j,i]=mat[j+1,i]-r*mat[j,i]  
      }
    }
    et=c()
    et2=c()
    ett=c()
    ett2=c()
    sumett2=0
    sumet2=0
    for (i in 1:nr-1)
    {
      et[i] = y1[i]-yt1[i]
    }    
    for (i in 1:nr-1)
    {
      et2[i] = et[i]*et[i]
      sumet2=sumet2+et2[i]
    }    
    for (i in 2:nr-1)
    {
      ett[i-1] = et[i]-et[i-1]
    }    
    for (i in 1:nr-1)
    {
      ett2[i] = ett[i]*ett[i]
      sumett2=sumett2+ett2[i]
    }
    d=sumett2/sumet2
    cat(
      " =======================================","\n",
      " Results after trying to remove autocorrelation:","\n\n",
      "---------------------------------------","\n",
      " d: ",d,"\n",
      
      "=======================================","\n\n"
  }
  else if (d>du){
      cat(
      " =======================================","\n",
      " Results:","\n\n",
      "---------------------------------------","\n",
      " d: ",d,"\n",
      " dl: ",dl,"\n",
      " du: ",du,"\n",
      " Negative autocorrelation","\n",
      "=======================================","\n\n"
  }
  else if (((d>dl) && (d<dh)) || ((d>4-du) && (d<4-dl)){
      cat(
      " =======================================","\n",
      " Results:","\n\n",
      "---------------------------------------","\n",
      " d: ",d,"\n",
      " dl: ",dl,"\n",
      " du: ",du,"\n",
      " Not decide","\n",
      "=======================================","\n\n"
  }
  else {
      cat(
      " =======================================","\n",
      " Results:","\n\n",
      "---------------------------------------","\n",
      " d: ",d,"\n",
      " dl: ",dl,"\n",
      " du: ",du,"\n",
      " No autocorrelation","\n",
      "=======================================","\n\n"
  }  
}>

Comments are closed.