Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function for Impulse Response Function

Tags:

function

r

I created following example code to draw an "impulse response function" in R with help of the [vars][1] package.

library(vars)
data(Canada)
Canada <- data.frame(Canada)

irfplot = function(x, y) {
  VAR <- VAR(cbind(x,y), p = 2, type = "trend")
  irf_o <-irf(VAR, impulse = colnames(VAR$y)[1], response = colnames(VAR$y)[2], boot = TRUE, cumulative = FALSE, n.ahead = 20, ci = 0.90)
  plot(irf_o)
}

irfplot(Canada["rw"],Canada["U"])

That should work so far. However, when trying to make the script more flexible by writing the function as

irfplot = function(x, y, lags, deter) {
      VAR <- VAR(cbind(x,y), p = lags, type = deter)
...

irfplot(Canada["rw"],Canada["U"], 2, "trend")

it returns:

Error in VAR(y = ysampled, p = lags, type = "trend") : 
  object 'lags' not found 

Question: How can the issue be resolved? I have some other functions that transfer values via objects, but for any reason, it does not work.

Thank you.

like image 915
Christopher Avatar asked Oct 13 '17 19:10

Christopher


People also ask

What is the function of impulse response?

In signal processing, the impulse response, or impulse response function (IRF), of a dynamic system is its output when presented with a brief input signal, called an impulse. More generally, an impulse response is the reaction of any dynamic system in response to some external change.

What is the equation for an impulse response?

Given the system equation, you can find the impulse response just by feeding x[n] = δ[n] into the system. If the system is linear and time-invariant (terms we'll define later), then you can use the impulse response to find the output for any input, using a method called convolution that we'll learn in two weeks.

What is impulse response function in structural dynamics?

The impulse response function is considered as a practical way of representing the behavior of economic variables in response to shocks to the vector δt.


1 Answers

Issue

The issue is the boot = TRUE argument to the irf() function. First note that, the following works just fine

irfplot <- function(x, y, lags, deter) {

  var_o   <- VAR(cbind(x, y), p = lags, type = deter)
  irf_o <- irf(var_o, 
               impulse = colnames(var_o$y)[1], 
               response = colnames(var_o$y)[2], boot = FALSE)
  plot(irf_o)
}
irfplot(Canada["rw"], Canada["U"], 3, "trend")

Changing boot to TRUE causes the error. What happens is that lags and deter apparaently do not get passed on correctly to the the function that performs bootstrapping. Although, I dont think its technically a bug, it would certainly be helpful if the author of the package changed it.

Solution

Whenever you want to pass on some function arguments in your top-level function to some lower level function its less error prone (as you see in your example) and generally recommended to use the ... argument.

irfplot <- function(x, y, ...) {

  var_o   <- VAR(cbind(x, y), ...)
  irf_o <- irf(var_o, 
               impulse = colnames(var_o$y)[1], 
               response = colnames(var_o$y)[2], boot = TRUE)
  plot(irf_o)
}
irfplot(Canada["rw"], Canada["U"], 3, "trend")
like image 182
Manuel R Avatar answered Oct 30 '22 06:10

Manuel R