<jupyter_start><jupyter_text>Pre-modeling tests for Global Vector Autoregression (GVAR) framework  1. Granger causalityGranger causality in time series is based on the following research hypotheses:- An effect can only occur after a cause- Knowledge of the cause improves the prediction of the effectThis set of hypotheses can be tested by computing the forecast error variance in (a) the case where all time series are included in the prediction and (b) the case where the supposed causal time series is left out. If leaving out the causal time series increases the variance of the forecast error, then the left-out time series can be said to be Granger-causal of the other one. The statistical significance of the comparison can be measured via the F-statistic. (See [Stokes and Purdon, 2017)][1]).Also, keep in mind that the causality test needs to be done in concert with stationarity tests. It is possible that time-series that have not been appropriately differenced will not provide enough evidence to fail to reject the null hypothesis (that there is no Granger causality).One approach is to use the `causality` function in the `vars` library to demonstrate Granger causality testing. Further documentation on the function is available [here][2]. Another is to use the `grangers` [library][3].Given the above definition, a prediction has to be made before the Granger causality test can be run. Thus, any approach would require estimating a VAR model first. The relevant functions perform this automatically, however. It makes sense, though, to test for cointegration and stationarity (and adjust or transform the time series accordingly) before conducting the Granger causality test.[1]: https://www.pnas.org/content/114/34/E7063[2]: https://rdrr.io/cran/vars/man/causality.html[3]: https://github.com/MatFar88/grangers<jupyter_code>library(vars)
library(reshape2)
df <- read.csv('../../data/tidy/cases_mobility_activity.csv') #note that file path may change

## PREPROCESSING STEPS IN PREVIOUS FORMAT
df <- df[, c(2, 4, 8:191)] #note that this will be irrelevant once we are working with a clean dataframe

melted.df <- melt(df, id.vars = c('region', 'transportation_type'))
m.data <- dcast(melted.df, region + variable~transportation_type)
colnames(m.data) = c('Country', 'Date', 'cov', 'car', 'groc', 'parks', 'home', 'reta', 'tran', 'tstop', 'walk', 'work' )

#m.data$Country <- as.factor(m.data$Country)
head(m.data)
#Convert numbers to numeric
for (i in seq(3, ncol(m.data) ) ) {
    m.data[,i] = as.numeric(m.data[,i], na.pass=TRUE)
}

# Correct Google (add 100 to baseline)
m.data[,c( 'groc', 'parks', 'home', 'reta',  'tstop', 'work' )] = m.data[,c( 'groc', 'parks', 'home', 'reta',  'tstop', 'work' )] + 100

# Remove selected variables
endovars <-  c( 'cov', 'home', 'tstop', 'work' )
m.data <- subset(m.data, select = c('Country', endovars) )
#m.data <- na.omit(m.data)
head(m.data)
df.Arg = m.data[m.data$Country == 'Argentina',]
rownames(df.Arg) <- 1:nrow(df.Arg) #renumber rows
df.Arg <- subset(df.Arg, select = -Country) #remove Country column
df.Arg[2:184,c("cov")] = diff(df.Arg[1:184,c("cov")]) # first difference the covid cases (since these are cumulative)
# df.Arg[rowSums(is.na(df.Arg)) > 0,] # find where the NAs are (remove for this test)
# you can see here where smoothing helps; simply removing NAs will skip time rows
# we see that there are no NAs between rows 34 and 181

df.Arg = df.Arg[35:100,]
df.Arg[2:66,c("cov")] = diff(df.Arg[1:66,c("cov")]) # difference again for stationarity, perhaps.
df.Arg[2:66,c("home")] = diff(df.Arg[1:66,c("home")]) # difference again for stationarity, perhaps.
df.Arg[2:66,c("tstop")] = diff(df.Arg[1:66,c("tstop")]) # difference again for stationarity, perhaps.
df.Arg[2:66,c("work")] = diff(df.Arg[1:66,c("work")]) # difference again for stationarity, perhaps.

#df.Arg = log(df.Arg) #take logs
diff(df.Arg[1:30,'work'])
df.Arg[rowSums(is.na(df.Arg)) > 0,] # Check again for NAs
var.2c <- VAR(df.Arg, p = 10, type = "const")
res <- causality(var.2c, cause = "cov")

#use a robust HC variance-covariance matrix for the Granger test:
causality(var.2c, cause = "cov", vcov.=vcovHC(var.2c))

#use a wild-bootstrap procedure to for the Granger test
## Not run: causality(var.2c, cause = "cov", boot=TRUE, boot.runs=1000)
res$Granger[1]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13725490196078433, 'fraction_numerical': 0.022145328719723183, 'mean_word_length': 5.03899721448468, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '16519', 'license': 'MIT', 'n_tokens_mistral': 1482, 'n_tokens_neox': 1379, 'n_words': 513, 'path': 'bin/jupyter/var_tests.ipynb', 'repo_name': 'naskoap/covid-analysis', 'size': '4335'}