Konstantin B
Konstantin B

Reputation: 23

Extracting coefficients while looping over variable names

I'm working on some time-series stuff in R (version 3.4.1), and would like to extract coefficients from regressions I ran, in order to do further analysis.

All results are so far saved as uGARCHfit objects, which are basically complicated list objects, from which I want to extract the coefficients in the following manner.

What I want is in essence this:

for(i in list){
    i_GARCH_mxreg <- i_GARCH@fit$robust.matcoef[5,1]
}

"list" is a list object, where every element is the name of one observation. For now, I want my loop to create a new numeric object named as I specified in the loop.

Now this obviously doesn't work because the index, 'i', isn't replaced as I would want it to be.

How do I rewrite my loop appropriately?

Minimal working example:

list <- as.list(c("one", "two", "three"))
one_a <- 1
two_a <- 2
three_a <- 3
for (i in list){
   i_b <- i_a
}

what this should give me would be:

> one_b
[1] 1
> two_b
[1] 2
> three_b
[1] 3

Clarification: I want to extract the coefficients form multiple list objects. These are named in the manner 'string'_obj. The problem is that I don't have a function that would extract these coefficients, the list "is not subsettable", so I have to call the individual objects via obj@fit$robust.matcoef[5,1] (or is there another way?). I wanted to use the loop to take my list of strings, and in every iteration, take one string, add 'string'_obj@fit$robust.matcoef[5,1], and save this value into an object, named again with " 'string'_name "

It might well be easier to have this into a list rather than individual objects, as someone suggest lapply, but this is not my primary concern right now.

There is likely an easy way to do this, but I am unable to find it. Sorry for any confusion and thanks for any help.

Upvotes: 1

Views: 151

Answers (1)

coffeinjunky
coffeinjunky

Reputation: 11514

The following should match your desired output:

# your list
l <- as.list(c("one", "two", "three"))
one_a <- 1
two_a <- 2
three_a <- 3

# my workspace: note that there is no one_b, two_b, three_b
ls()
[1] "l"       "one_a"   "three_a" "two_a"  

for (i in l){
   # first, let's define the names as characters, using paste:
   dest <- paste0(i, "_b")
   orig <- paste0(i, "_a")
   # then let's assign the values. Since we are working with 
   # characters, the functions assign and get come in handy:
   assign(dest, get(orig) )
 }

# now let's check my workspace again. Note one_b, two_b, three_b
ls()
 [1] "dest"    "i"       "l"       "one_a"   "one_b"   "orig"    "three_a"
 [8] "three_b" "two_a"   "two_b"  

# let's check that the values are correct:
one_b
[1] 1
two_b
[1] 2
three_b
[1] 3

To comment on the functions used: assign takes a character as first argument, which is supposed to be the name of the newly created object. The second argument is the value of that object. get takes a character and looks up the value of the object in the workspace with the same name as that character. For instance, get("one_a") will yield 1.

Also, just to follow up on my comment earlier: If we already had all the coefficients in a list, we could do the following:

# hypothetical coefficients stored in list:
lcoefs <- list(1,2,3)
# let's name the coefficients:
lcoefs <- setNames(lcoefs, paste0(c("one", "two", "three"), "_c"))

# push them into the global environment:    
list2env(lcoefs, env = .GlobalEnv)

# look at environment:    
ls()
 [1] "dest"    "i"       "l"       "lcoefs"  "one_a"   "one_b"   "one_c"  
 [8] "orig"    "three_a" "three_b" "three_c" "two_a"   "two_b"   "two_c"  

one_c
[1] 1
two_c
[1] 2
three_c
[1] 3

And to address the comments, here a slightly more realistic example, taking the list-structure into account:

l <- as.list(c("one", "two", "three"))

# let's "hide" the values in a list:
one_a <- list(val = 1)
two_a <- list(val = 2)
three_a <- list(val = 3)

for (i in l){
  dest <- paste0(i, "_b")
  orig <- paste0(i, "_a")
  # let's get the list-object:
  tmp <- get(orig)
  # extract value:
  val <- tmp$val
  assign(dest, val )
}

one_b
[1] 1
two_b
[1] 2
three_b
[1] 3

Upvotes: 1

Related Questions