VARrf: Estimate VAR by random forest

Description Usage Arguments Value Examples

View source: R/VARrf.R

Description

Estimate VAR by random forest

Usage

1
VARrf(data, indx = NULL, pmax = 5, p = NULL, s = 1)

Arguments

data

a data.frame, and all variables is endogenous variables in VAR.

indx

if data is time series, indx is NULL. If data is panal data, indx is a character string vector whose length is 2, the 1st element is coloumn name of individual id, and the 2nd element is coloumn names of time.

pmax

a max lag order, default value is 5.

p

a fixed lag order. When p is set up, pmax is unuseful.

s

a horizon in local projections, default is 1. Gnerally don't change it.

Value

An list:

  1. fit_rfAn object of class rfsrc, grow from the package randomForestSRC, and it has minimum OOB MSE in 1:pmax.

  2. poptimal lag order,and it has minimum OOB MSE.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
library(magrittr)
B1 <- matrix(c(0.2, 0.1, 0.2, 0.3,0.3,0.4,0.2,0.1), 2)
sim_dt <- tsDyn::VAR.sim(B1, n = 200, lag = 2,include = 'none',show.parMat = T) %>%
  as.data.frame()
  colnames(sim_dt) <- c('y1','y2')
# fit a VAR with max lag order equal 5
fit <- VARrf(sim_dt, pmax = 5)
# fit a VAR with fixed lag order equal 4
fit <- VARrf(sim_dt, p = 4)
# IRF
picdata <- IRFrf(data = sim_dt, pmax = 5, s = 12, shockvar = 1)
ggplot(data = picdata, aes(x = s, y = y1)) + geom_line() +
  geom_hline(yintercept = 0) + theme_bw()
# Generalized IRF
IRFrf_gen(data = sim_dt[1:20,], pmax = 3, s = 5, shockvar = 1, ncores = 7)

common2016/VARrf documentation built on July 27, 2020, 2:41 a.m.