1

I have already read similar questions regarding the assign command. But it doesnt work for me.

Here is my code:

masums <- function(var) {
    lags <- var$p                    # number of lags in VAR
    eqn  <- length(var$varresult)    # number of equations in VAR
    dep  <- names(var$varresult)     # names of dependent variables

    for(i in 1:eqn) # compute coefficient matrices
        d <- dep[i]
        x <- paste("var$varresult$",d,"$coefficients",sep="")
        y <- as.matrix(GET THE VALUE OF "x" e.g. var$varresult$d$coefficients) # d="gap" for i=1
    }
    return(y)
}

Example: For i=1 d would have the value "gap", therefore I want the value of var$varresult$gap$coefficients.

var$varresult consists of elements of class "lm". Maybe the solution is easy and I am just to stressed to see it. Hope someone can help.

Edit: For a small example dataset:

library(vars)
y <- c(100*rnorm(100))
x <- seq(1,100,1)

vardata <- cbind(x,y)
var1    <- VAR(vardata,p=4,type="const")
var1$varresult$x$coefficients

Martin

5
  • You need to specify what "doesn't work". I see several potential problems here - (1) what are you doing with x? (2) you're only returning y from the last iteration of your loop - but you haven't explained what you're actually trying to do and what aspects of your current efforts aren't working. Commented May 21, 2014 at 11:14
  • 2
    I sense there's a better way of achieving what you're after. Can you provide us with a small reproducible example and desired output? Commented May 21, 2014 at 11:19
  • I have used R every day for 6 years, and never once found it necessary to use assign. There is probably a more R-like way of doing things. What is your real problem? Commented May 21, 2014 at 11:23
  • Yes I just extract the last coefficient matrix in this example. Of course this is not what I intend to do here later on. The problem is that I cannot use var$varresult$d$coefficients where d would be "gap" for the first lm object. It is not easy to make a reproduceable example, since I use a result of a VAR estimation wich is pretty big :/ Commented May 21, 2014 at 11:50
  • The actual problem to be clear is, how can I retrive the value of an object, If I use a variable in its name... Commented May 21, 2014 at 11:55

1 Answer 1

1

paste to construct a nested variable access in a string is the completely wrong approach.

What you are missing is the fact that x$y can also be written as x$['y'] (or x$[['y']], depending whether you want a single value in a list, or a column in a data.frame), and here 'y' is a string. So you can write this:

y <- as.matrix(var$varresult[[d]]$coefficients)

(assuming var$varresult is a list)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.