View source: R/causality_pred.R
causality_pred | R Documentation |
Test for Granger causality using out-of-sample prediction errors from an autoregression (AR) model, where some of the near-contemporaneous lags can be removed:
Y_t = \sum_{i=1}^{p1}\alpha_iY_{t-i} + \sum_{i=lag.restrict+1}^{p2}\beta_iX_{t-i} + e_t,
where Y_t
is the dependent variable,
X_t
is the cause variable,
p1
and p2
are the AR orders (if p.free = FALSE
, p1 = p2
),
lag.restrict
is the number of restricted first lags (see the argument lag.restrict
).
causality_pred(
y,
cause = NULL,
p = NULL,
p.free = FALSE,
lag.restrict = 0L,
lag.max = NULL,
k = 2,
B = 500L,
test = 0.3,
cl = 1L
)
y |
matrix, data frame, or |
cause |
name of the cause variable. If not specified, the first variable in
|
p |
a vector of one or two positive integers specifying the order |
p.free |
logical value indicating whether the autoregressive orders for the
dependent and cause variables should be selected independently.
The default |
lag.restrict |
integer for the number of short-term lags in the cause variable to remove from consideration (default is zero, meaning no lags are removed). This setting does not affect the dependent variable lags that are always present. |
lag.max |
a vector of one or two positive integers for the highest lag orders to explore.
The input of length one is recycled, then |
k |
numeric scalar specifying the weight of the equivalent degrees of freedom part
in the AIC formula. Default |
B |
number of bootstrap replications. Default is 500. |
test |
a numeric value specifying the size of the testing set. If |
cl |
parameter to specify computer cluster for bootstrapping passed to
the package
|
The tests include the MSE-t approach \insertCiteMcCracken_2007funtimes and MSE-correlation test as in Chapter 9.3 of \insertCiteGranger_Newbold_2016;textualfuntimes. The bootstrap is used to empirically derive distributions of the statistics.
In the implemented bootstrapping, residuals of the restricted model under the null hypothesis of no Granger causality are bootstrapped to generate new data under the null hypothesis. Then, the full and restricted models are re-estimated on the bootstrapped data to obtain new (bootstrapped) forecast errors.
In the current implementation, the bootstrapped p
-value is calculated using Equation 4.10 in
\insertCiteDavison_Hinkley_1997;textualfuntimes: p.value
= (1 + n
) / (B
+ 1),
where n
is the number of bootstrapped statistics smaller or equal to the observed statistic.
This function tests the Granger causation
of X
to Y
or from Y
to X
(to test in both directions, need to run the function twice, with different argument cause
).
To use the symmetric vector autoregression (VAR), use the function causality_predVAR
.
A list containing the following elements:
stat |
a table with the observed values of the test statistics and |
cause |
the cause variable. |
p |
the AR orders used for the dependent variable ( |
Vyacheslav Lyubchich
causality_predVAR
## Not run:
# Example 1: Canada time series (ts object)
Canada <- vars::Canada
causality_pred(Canada[,1:2], cause = "e", lag.max = 5, p.free = TRUE)
causality_pred(Canada[,1:2], cause = "e", lag.restrict = 3, lag.max = 15, p.free = TRUE)
# Example 2 (run in parallel, initiate the cluster automatically)
# Box & Jenkins time series
# of sales and a leading indicator, see ?BJsales
D <- cbind(BJsales.lead, BJsales)
causality_pred(D, cause = "BJsales.lead", lag.max = 5, B = 1000, cl = NULL)
# Example 3 (run in parallel, initiate the cluster manually)
# Initiate a local cluster
cores <- parallel::detectCores()
cl <- parallel::makeCluster(cores)
parallel::clusterSetRNGStream(cl, 123) # to make parallel computations reproducible
causality_pred(D, cause = "BJsales.lead", lag.max = 5, B = 1000, cl = cl)
causality_pred(D, cause = "BJsales.lead", lag.restrict = 3, p = 5, B = 1000, cl = cl)
parallel::stopCluster(cl)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.