Skip to content

Instantly share code, notes, and snippets.

@explodecomputer
Last active January 27, 2017 09:31
Show Gist options
  • Save explodecomputer/16502f14ae04abdd832d to your computer and use it in GitHub Desktop.
Save explodecomputer/16502f14ae04abdd832d to your computer and use it in GitHub Desktop.
Granger causality
library(lmtest)
data(ChickEgg)
chickegg <- as.data.frame(ChickEgg)
chickegg$Year <- 1:nrow(chickegg) + 1929
attach(chickegg)
library(forecast)
ndiffs(chicken, alpha=0.05, test=c("kpss"))
ndiffs(egg, alpha=0.05, test=c("kpss"))
dchick <- diff(chicken)
degg <- diff(egg)
plot.ts(dchick)
plot.ts(degg)
grangertest(dchick ~ degg, order=)
Find_Max_CCF<- function(a,b)
{
d <- ccf(a, b, plot = FALSE)
cor = d$acf[,,1]
lag = d$lag[,,1]
res = data.frame(cor,lag)
res_max = res[which.max(res$cor),]
return(res_max)
}
Find_Max_CCF(dchick, degg)
library(vars)
data(Canada)
var.2c <- VAR(Canada, p = 2, type = "const")
a <- causality(var.2c, cause = "e")
name
#use a robust HC variance-covariance matrix for the Granger test:
causality(var.2c, cause = "e", vcov.=vcovHC(var.2c))
#use a wild-bootstrap procedure to for the Granger test
causality(var.2c, cause = "e", boot=TRUE, boot.runs=1000)## End(Not run)
@Fahim2083
Copy link

Hi
I have a question, can we have Nonlinear Granger causality Codes according to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment