0

I am trying to print values from lists in R.

When I use

cat("Relevant GPD Info for ", endowmentAssetClassVariableNames.v[i],"\n")

I receive the correct list item, e.g. "bbergComm",

but if I try to reference an item within that list item, e.g.

cat("Upper Thershold Return: ", paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh"), "\n")

I only get back

Upper Thershold Return:  bbergComm.gpdFit$upper.thresh, and not the value associated with the list item "bbergComm.gpdFit$upper.thresh.

I am running this in a loop, but cannot get my loop to give me the value I am referencing.

What can I do to get the value associated with a list item in a loop?

for(i in 1:length(endowmentAssetClassVariableNames.v)) {
+   cat("Relevant GPD Info for ",     endowmentAssetClassVariableNames.v[i],"\n")
+   t<-(paste0(endowmentAssetClassVariableNames.v[i],     ".gpdFit$upper.thresh"))
+   cat("Upper Thershold Return: ",     paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh"), "\n")
+ }

Relevant GPD Info for  peReturn 
Upper Thershold Return:  peReturn.gpdFit$upper.thresh 
Relevant GPD Info for  spReturn 
Upper Thershold Return:  spReturn.gpdFit$upper.thresh 
Relevant GPD Info for  hfReturn 
Upper Thershold Return:  hfReturn.gpdFit$upper.thresh 
Relevant GPD Info for  wilshireReturn 
Upper Thershold Return:  wilshireReturn.gpdFit$upper.thresh 
Relevant GPD Info for  bbergComm 
Upper Thershold Return:  bbergComm.gpdFit$upper.thresh 
Relevant GPD Info for  tripleABond 
Upper Thershold Return:  tripleABond.gpdFit$upper.thresh 

all of the gpd objects have the following structure

> str(peReturn.gpdFit)
List of 22
 $ n                    : int 3439
 $ data                 : num [1:3439] -0.1582 -0.0892 -0.0873 -0.0762 -0.0662 ...
 $ upper.exceed         : num [1:344] 0.0298 0.0721 0.0558 0.0201 0.0402 ...
 $ lower.exceed         : num [1:344] -0.0513 -0.0358 -0.0428 -0.0205 -0.0332 ...
 $ upper.thresh         : Named num 0.0165
  ..- attr(*, "names")= chr "90%"
 $ lower.thresh         : Named num -0.0161
  ..- attr(*, "names")= chr "10%"
 $ p.less.upper.thresh  : num 0.9
 $ p.larger.lower.thresh: num 0.9
 $ n.upper.exceed       : int 344
 $ n.lower.exceed       : int 344
 $ upper.method         : chr "ml"
 $ lower.method         : chr "ml"
 $ upper.par.ests       : Named num [1:2] 0.0108 0.1451
  ..- attr(*, "names")= chr [1:2] "lambda" "xi"
 $ lower.par.ests       : Named num [1:2] 0.0119 0.07
  ..- attr(*, "names")= chr [1:2] "lambda" "xi"
 $ upper.par.ses        : logi NA
 $ lower.par.ses        : logi NA
 $ upper.varcov         : logi NA
 $ lower.varcov         : logi NA
 $ upper.info           : logi NA
 $ lower.info           : logi NA
 $ upper.converged      : logi TRUE
 $ lower.converged      : logi TRUE
 - attr(*, "class")= chr "gpd"

I am looking to extract some of the gpd object values in a loop where I assign names associated with the object.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
fibrou
  • 313
  • 1
  • 5
  • 15
  • 1
    can you create a reproducible example? you probably just need to wrap paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh") inside eval and parse...i.e., `eval(parse(text = paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh")))` – Chris Apr 06 '18 at 20:47
  • Try `cat("Upper Thershold Return: ", get(paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh")), "\n")` This is not the most optimal way to program in R. It would probably be easier if you enumerate your list with actual objects instead of strings of names. – Vlo Apr 06 '18 at 21:00
  • A good reproducible example provides a little bit of sample input so it can be run. [Here's a great tutorial on making a reproducible example in R](https://stackoverflow.com/q/5963269/903061). – Gregor Thomas Apr 09 '18 at 19:35
  • @Gregor - my apologies. What else can I provide? thanks for all the help. – fibrou Apr 10 '18 at 01:50
  • The structure helps a lot. I understand now what, e.g., `peReturn.gpdFit` is, but what is `endowmentAssetClassVariableNames.v`? Is it a `list` of gpd objects? – Gregor Thomas Apr 10 '18 at 01:56
  • Anyway, try my updated answer. I was trying to add the `.gpdFit` as in your `cat` attempt, but from the structure you posted it doesn't look like that's the name of a sublist item. – Gregor Thomas Apr 10 '18 at 02:03
  • @Gregor - apologies for the delay. Yes, you are correct, the list is just a name-specific vector of gpd objects, where all have the ".gpdFit" suffix: endowmentAssetClassVariableNames.v <- c("peReturn", "spReturn", "hfReturn", "wilshireReturn", "bbergComm", "tripleABond") – fibrou Apr 16 '18 at 14:13

1 Answers1

0

Code inside strings isn't evaluated. Imagine how bad it would be if, when you ran

cat("Relevant GPD Info for ", endowmentAssetClassVariableNames.v[i],"\n")

R thought the for was the start of a for loop, and if you happened to have an elevation variable named elev = 176 it substituted 12 in for R176ant GPD Info....

You can't use $ with strings, but you can use [ or [[

So do it like this:

endowmentAssetClassVariableNames.v[[i]][["upper.thresh"]]
# or with the cat
cat("Relevant GPD Info for ", 
    endowmentAssetClassVariableNames.v[[i]][["upper.thresh"]],
    "\n")
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks for the help. Actually, I am looking to extract the "upper.thresh" value from gpd list object "bbergComm.gpdFit. Specifically, I am fitting a GPD distribution to multiple data series, and all I want to do is automate the extraction of the shape and scale parameters, which are stored in the "upper.thresh" value. – fibrou Apr 09 '18 at 18:41
  • Maybe if you made a reproducible example in your question it would make more sense? – Gregor Thomas Apr 09 '18 at 18:51
  • # extracting relevant GPD estimate data endowmentAssetClassVariableNames.v <- c("peReturn", "spReturn", "hfReturn", "wilshireReturn", "bbergComm", "tripleABond") > endowmentAssetClassVariableNames.v [1] "peReturn" "spReturn" "hfReturn" "wilshireReturn" "bbergComm" "tripleABond" – fibrou Apr 09 '18 at 19:09
  • > for(i in 1:length(endowmentAssetClassVariableNames.v)) { + cat("Relevant GPD Info for ", endowmentAssetClassVariableNames.v[i],"\n") + t<-(paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh")) + cat("Upper Thershold Return: ", paste0(endowmentAssetClassVariableNames.v[i], ".gpdFit$upper.thresh"), "\n") + } Relevant GPD Info for peReturn Upper Thershold Return: peReturn.gpdFit$upper.thresh Relevant GPD Info for spReturn Upper Thershold Return: spReturn.gpdFit$upper.thresh ..... – fibrou Apr 09 '18 at 19:11
  • Please, this is unreadable. Please remove these comments and instead edit your question. – Gregor Thomas Apr 09 '18 at 19:12
  • yep. Sorry. @gregor – fibrou Apr 09 '18 at 19:12