I am trying to run VAR-models between multiple variables, which is why I wrote a function to prepare the data and execute the vars::VAR()
function, feeding the result to the vars::irf()
function to generate an IRF model where I subseqyently extract relevant metrics. This function has an argument max_lag
, in which I define the maximum number of lags to consider when fitting the model.
Hence I define a function like this:
library(vars)
data(EuStockMarkets)
f1 <- function(max_lag = 6){
vars::irf(vars::VAR(EuStockMarkets, lag.max = max_lag))
}
I then call the function:
f1(max_lag = 12)
which results in
Error in VAR(y = ysampled, lag.max = max_lag) :
object 'max_lag' not found
Note that neither f1()
nor assigning a new object m <- vars::VAR(EuStockMarkets, lag.max = max_lag)
and then providing this as input to the irf()
function, nor pre-loading the package solve the issue.
This seems to be an issue specific to the vars
-package, as both
f2 <- function(test = "test"){
print(paste(test))
}
f2(test = "TEST")
[1] "TEST"
and
f3 <- function(col = "red"){
ggplot(starwars, aes(x = height, y = mass)) +
geom_point(color = col)
}
f3(col = "blue")
work.
I also moved out of my comfort zone and tried to use get('max_lag')
with the arguments parent.frame()
, environment()
, parent.env(environment())
, or calling a pre-defined environment (which is also not found), following this answer.
Suggestions both welcome regarding specific fix and general issue - I feel there is something substantial about R environments going on here.