Reputation: 245
I have the below code to extract a named number from the class term_strc_nss. it works fine extracts the values for the beta vector and passes them along to the next function. However, the trade date which is 2012-12-31
and defines the slot in the class changes. So, I need to pass trade date as a variable into the code.
BetaVector <<- unname(term.structure$opt_result$`2012-12-31`$par[c("beta0",
"beta1","beta2", "tau1")])
The code below uses paste but when I run the function I get errors that seem to be related to the quote marks around the "beta0", "beta1", "beta2" and "tau1". I tried replacing the " with ' I can run the code but then the value was not passed because the beta vector is just a string of names.
BetaVector <<- paste("unname(term.structure$opt_result$`",tradedate,"`$par[c("beta0",
"beta1", "beta2", "tau1")])")
I guess I could create a method to extract the Beta coefficients but I will still face the same problem when creating the method. Is there a better way to extract a named number from a class?
Here is the dput from the term.structure. term.structure is an object termstrc_nss and is an S3 class what is the proper way to refer to it?
structure(list(`2012-12-31` = structure(list(par = structure(c(3.41273726187976,
-2.63342593294169, -5.34244663887461, 2.13363495349724), .Names = c("beta0",
"beta1", "beta2", "tau1")), value = 0.0088680383803467, counts = structure(c(15,
10), .Names = c("function", "gradient")), convergence = 0L, message = NULL,
outer.iterations = 2L, barrier.value = -0.000409627261066452), .Names = c("par",
"value", "counts", "convergence", "message", "outer.iterations",
"barrier.value"))), .Names = "2012-12-31")
Upvotes: 0
Views: 1199
Reputation: 121077
It sounds like you want to do some sort of eval-parse technique. This is almost always a bad idea since it makes your code nearly impossible to debug. Use square bracket indexing instead.
trade_date <- "2012-12-31"
BetaVector <<- unname(
term.structure$opt_result[[trade_date]]$par[c("beta0","beta1","beta2", "tau1")]
)
Upvotes: 4