Learn R Programming

bivarhr (version 0.1.5)

run_varx: Fit VARX model with diagnostics for I and C

Description

Estimates a bivariate VAR model for I and C with exogenous covariates (VARX), and computes a set of standard diagnostics (stability, serial correlation, normality, ARCH). The fitted model and diagnostics are saved to disk and also returned.

Usage

run_varx(DT, p = 2)

Value

A list with components:

  • fit: the estimated VAR model (vars object).

  • stability: result of vars::stability() (or "try-error" on failure).

  • serial: result of vars::serial.test() (or "try-error" on failure).

  • normal: result of vars::normality.test() (or "try-error" on failure).

  • arch: result of vars::arch.test() (or "try-error" on failure).

Arguments

DT

A data.table (or data.frame) containing at least the following columns:

  • I, C: endogenous variables for the VAR.

  • EconCycle, PopDensity, Epidemics, Climate, War, t_norm: exogenous regressors included in the VARX.

p

Integer; lag order of the VAR part (number of lags for I and C).

Details

The endogenous vector is \(y_t = (I_t, C_t)'\) and the exogenous regressors are: EconCycle, PopDensity, Epidemics, Climate, War, t_norm. The model is fit using vars::VAR() with type = "const" and the exogenous matrix passed via exogen.

After estimation, the following diagnostics from vars are (attempted to be) computed:

  • vars::stability(fit, type = "OLS-CUSUM") for stability.

  • vars::serial.test(fit, lags.pt = 10, type = "PT.asymptotic") for serial correlation.

  • vars::normality.test(fit) for residual normality.

  • vars::arch.test(fit, lags.multi = 5) for ARCH effects.

Each diagnostic call is wrapped in try(), so if a diagnostic fails, the corresponding element in the output will contain a "try-error" instead of stopping the function.

The result is saved as an RDS file named "varx_fit.rds" in the directory specified by a global object dir_out (character scalar).

Examples

Run this code
# \donttest{
library(data.table)

# 1. Create dummy data with ALL required columns for VARX
# The function explicitly requires these specific exogenous variables
DT <- data.table(
  year = 2000:2049, # 50 obs to ensure diagnostics (lags.pt=10) don't fail
  I = rpois(50, lambda = 10),
  C = rpois(50, lambda = 8),
  # Exogenous regressors required by the function
  EconCycle = rnorm(50),
  PopDensity = rnorm(50),
  Epidemics = rnorm(50),
  Climate = rnorm(50),
  War = rnorm(50),
  t_norm = seq(-1, 1, length.out = 50)
)

# 2. Define global output directory using tempdir() (Fixes CRAN policy)
# run_varx looks for 'dir_out' in the global environment
tmp_dir <- tempdir()
dir_out <- file.path(tmp_dir, "varx")
if (!dir.exists(dir_out)) dir.create(dir_out, recursive = TRUE)

# 3. Run the function
# We use p=1 to keep it fast and stable for the example check
res_varx <- run_varx(DT, p = 1)

# Inspect the fitted VAR object if it didn't fail
if (!inherits(res_varx$fit, "try-error")) {
  print(res_varx$fit)
}
# }

Run the code above in your browser using DataLab