0

I know there are many answers available about this question. But I did not have success with any of those. I am trying to view the source code of the irf function available in the vars package of R. Here is what I have tried:

library(vars)
methods(irf)
vars:::irf.varest

It yields the following output.

function (x, impulse = NULL, response = NULL, n.ahead = 10, ortho = TRUE, 
    cumulative = FALSE, boot = TRUE, ci = 0.95, runs = 100, seed = NULL, 
    ...) 
{
    if (!(class(x) == "varest")) {
        stop("\nPlease provide an object of class 'varest', generated by 'VAR()'.\n")
    }
    y.names <- colnames(x$y)
    if (is.null(impulse)) {
        impulse <- y.names
    }
    else {
        impulse <- as.vector(as.character(impulse))
        if (any(!(impulse %in% y.names))) {
            stop("\nPlease provide variables names in impulse\nthat are in the set of endogenous variables.\n")
        }
        impulse <- subset(y.names, subset = y.names %in% impulse)
    }
    if (is.null(response)) {
        response <- y.names
    }
    else {
        response <- as.vector(as.character(response))
        if (any(!(response %in% y.names))) {
            stop("\nPlease provide variables names in response\nthat are in the set of endogenous variables.\n")
        }
        response <- subset(y.names, subset = y.names %in% response)
    }
    irs <- .irf(x = x, impulse = impulse, response = response, 
        y.names = y.names, n.ahead = n.ahead, ortho = ortho, 
        cumulative = cumulative)
    Lower <- NULL
    Upper <- NULL
    if (boot) {
        ci <- as.numeric(ci)
        if ((ci <= 0) | (ci >= 1)) {
            stop("\nPlease provide a number between 0 and 1 for the confidence interval.\n")
        }
        ci <- 1 - ci
        BOOT <- .boot(x = x, n.ahead = n.ahead, runs = runs, 
            ortho = ortho, cumulative = cumulative, impulse = impulse, 
            response = response, ci = ci, seed = seed, y.names = y.names)
        Lower <- BOOT$Lower
        Upper <- BOOT$Upper
    }
    result <- list(irf = irs, Lower = Lower, Upper = Upper, response = response, 
        impulse = impulse, ortho = ortho, cumulative = cumulative, 
        runs = runs, ci = ci, boot = boot, model = class(x))
    class(result) <- "varirf"
    return(result)
}
<bytecode: 0x0000028e2dd6ff38>
<environment: namespace:vars>

This only a partial solution. I want to see the source code of the .irf function (in the line irs <- .irf(x = x, impulse = impulse, response = response, ). How do I do that?

r2evans
  • 141,215
  • 6
  • 77
  • 149
Prasanna S
  • 353
  • 1
  • 10
  • 1
    Have you tried `vars:::.irf`? – r2evans Jul 12 '21 at 01:07
  • There is also this: https://github.com/cran/vars/blob/master/R/internal.R#L85; while this is not the originating git repo for that package (if it is even version controlled on git, I have no idea), it is *a* repo that contains the source files, so it gives you full access to all of its functions. – r2evans Jul 12 '21 at 01:09
  • Hello guys can you please help me out on this question https://stackoverflow.com/questions/68330838/conting-events-between-sequential-stages-in-a-process-using-r – R_Student Jul 12 '21 at 04:27

1 Answers1

1

Since it is a function within the vars package, you can look at the source code the same way that you did for irf.varest.

library(vars)
vars:::.irf

Output

function (x, impulse, response, y.names, n.ahead, ortho, cumulative) 
{
    if ((class(x) == "varest") || (class(x) == "vec2var")) {
        if (ortho) {
            irf <- Psi(x, nstep = n.ahead)
        }
        else {
            irf <- Phi(x, nstep = n.ahead)
        }
    }
    else if ((class(x) == "svarest") || (class(x) == "svecest")) {
        irf <- Phi(x, nstep = n.ahead)
    }
    dimnames(irf) <- list(y.names, y.names, NULL)
    idx <- length(impulse)
    irs <- list()
    for (i in 1:idx) {
        irs[[i]] <- matrix(t(irf[response, impulse[i], 1:(n.ahead + 
            1)]), nrow = n.ahead + 1)
        colnames(irs[[i]]) <- response
        if (cumulative) {
            if (length(response) > 1) 
                irs[[i]] <- apply(irs[[i]], 2, cumsum)
            if (length(response) == 1) {
                tmp <- matrix(cumsum(irs[[i]]))
                colnames(tmp) <- response
                irs[[i]] <- tmp
            }
        }
    }
    names(irs) <- impulse
    result <- irs
    return(result)
}
<bytecode: 0x7ff5bbb40138>
<environment: namespace:vars>
AndrewGB
  • 16,126
  • 5
  • 18
  • 49