R/VAR.R

Defines functions VAR

#' Fit a VAR(p) model 
#' 
#' @param x numeric data matrix 
#' @param p number of lags 
#' @noRd


VAR <- function(x, p) {
  n <- nrow(x)
  Y <- x[(p + 1):n, ]
  X <- c()
  for (i in 1:p) {
    X <- cbind(X, x[(p + 1 - i):(n - i), ])
  }
  A <- solve(t(X) %*% X) %*% t(X) %*% Y
  A[A >= 1] = 0.99
  res <- Y - X %*% A
  
  return(list(Y = Y, X = X, A = A, res = res))
}

Try the sparseDFM package in your browser

Any scripts or data that you put into this service are public.

sparseDFM documentation built on March 31, 2023, 10:15 p.m.