s-news
[Top] [All Lists]

Re: Causality Test

To: "Olanrewaju Sanni" <o_sanni@yahoo.com>, <s-news@lists.biostat.wustl.edu>
Subject: Re: Causality Test
From: "Guy Yollin" <guy.yollin@rotellacapital.com>
Date: Mon, 7 Apr 2008 10:04:14 -0400
In-reply-to: <357514.93912.qm@web62303.mail.re1.yahoo.com>
References: <357514.93912.qm@web62303.mail.re1.yahoo.com>
Thread-index: AciYo+PX0oHLuiQYSOiqFkA/p7gURgAEgR8g
Thread-topic: [S] Causality Test

Lanre,

 

You really don’t need FinMetrics to perform the Granger causality test since it just involves linear regression.  The following function can be used; setting verbose=T will print out step-by-step description of the test.  The reference I used was Econometric Models and Economic Forecasts by Pindyck & Rubinfeld.

 

Best,

 

-- G

 

 

 

grangertest <- function(X,Y,p=0.95,verbose=F) {

  if( verbose ) {

    cat("\nTest that X causes Y\n")

    cat("Step 1 - null hypothesis: X does NOT cause Y (hoping to reject this hypothesis)\n")

  }

  y.lag.1 <- Y[-length(Y)]

  x.lag.1 <- X[-length(X)]

  y <- Y[-1]

  x <- X[-1]

  if( verbose )

    cat("  Unrestricted regression: regress Y against lagged values of Y and lagged values of X\n")

  mod1.UR <- lm(y ~ y.lag.1 + x.lag.1)

  if( verbose )

    cat("  Restricted regression: regress Y against only lagged values of Y\n")

  mod1.R <- lm(y ~ y.lag.1)

  if( verbose )

    cat("  Test null hypothesis with an Ftest\n")

  N<-length(y)

  q<-1

  k<-3

  cv<-qf(p,q,N-k)

  pcv<-1-p

  ESS1.R <- sum(mod1.R$resid^2)

  ESS1.UR <- sum(mod1.UR$resid^2)

  Fstat1 <- ((N-k) / q) * ((ESS1.R - ESS1.UR) / ESS1.UR)

  pv1<-pf(Fstat1, q, N-k)

  if( verbose ) {

    cat("    F-stat =",Fstat1," (",1-pv1,")\n")

    cat("    Critial Value =",cv," (",pcv,")\n")

  }

  if( Fstat1 < cv ) {

    if( verbose ) {

      cat("    Since F-stat is < Critial Value, we CAN NOT reject the null-hypothesis\n")

      cat("    Hence, we CAN NOT conclude that X causes Y\n")

    }

    step1<-F

  } else {

    if( verbose ) {

      cat("    Since F-stat is > Critial Value, we CAN reject the null-hypothesis\n")

      cat("    Hence, we may be able to conclude that X actually does causes Y\n")

    }

    step1<-T

  }

  if( verbose ) {

    cat("Step 2 - null hypothesis: Y does NOT cause X (hoping to accept this hypothesis)\n")

    cat("  Unrestricted regression: regress X against lagged values of X and lagged values of Y\n")

  }

  mod2.UR <- lm(x ~ x.lag.1 + y.lag.1)

  if( verbose )

    cat("  Restricted regression: regress X against only lagged values of X\n")

  mod2.R <- lm(x ~ x.lag.1)

  if( verbose )

    cat("  Test null hypothesis with an Ftest\n")

  ESS2.R <- sum(mod2.R$resid^2)

  ESS2.UR <- sum(mod2.UR$resid^2)

  Fstat2 <- ((N-k) / q) * ((ESS2.R - ESS2.UR) / ESS2.UR)

  pv2<-pf(Fstat2, q, N-k)

  if( verbose ) {

    cat("    F-stat =",Fstat2," (",1-pv2,")\n")

    cat("    Critial Value =",cv," (",pcv,")\n")

  }

  if( Fstat2 < cv ) {

    if( verbose ) {

      cat("    Since F-stat is < Critial Value, we CAN NOT reject the null-hypothesis\n")

      cat("    Hence, we can conclude that Y does not causes X and may be able to conclude that X causes Y\n")

    }

    step2 <- T

  } else {

    if( verbose ) {

      cat("    Since F-stat is > Critial Value, we MUST reject the null-hypothesis\n")

      cat("    Hence, prehaps Y does cause X, in any event, we certainly CAN NOT conclude that X causes Y\n")

    }

    step2 <- F

  }

  if( verbose )

    cat("Step 3 - summarized our test results and draw a conclusion\n")

  if( step1 & step2 ) {

    if( verbose ) {

      cat("  Since we were able to reject the null hypothesis in step 1 but failed to reject in step 2,\n")

      cat("  We can indeed conclude that X causes Y\n\n")

    }

    return(TRUE)

  }

  if( step1 & !step2 ) {

    if( verbose ) {

      cat("  Although we were able to reject the null hypothesis in step 1, we also rejected the null hypothesis in step 2,\n")

      cat("  Both series are probably driven by a third series\n\n")

    }

    return(FALSE)

  }

  if( !step1 & step2 ) {

    if( verbose ) {

      cat("  Since we accepted the null hypothesis in step 1 and also accepted the null hypothesis in step 2,\n")

      cat("  Both series are probably unrelated to each other in regards to causaility\n\n")

    }

    return(FALSE)

  }

  if( !step1 & !step2 ) {

    if( verbose ) {

      cat("  Since we could not reject the null hypothesis in step 1 but could reject the null hypothesis in step 2,\n")

      cat("  Our conclusion should be that Y actually causes X\n\n")

    }

    return(FALSE)

  }

}

 

 


From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Olanrewaju Sanni
Sent: Monday, April 07, 2008 4:36 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] Causality Test

 

Hi,

Can anyone tell me how to use s-plus finmetrics for Granger Causality Test? All contributions are highly appreciated.

 

Best Regards,

Lanre Sanni

 


You rock. That's why Blockbuster's offering you one month of Blockbuster Total Access, No Cost.

<Prev in Thread] Current Thread [Next in Thread>