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.
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.
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.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With