r - Extracting coefficients while looping over variable names -
i'm working on time-series stuff in r (version 3.4.1), , extract coefficients regressions ran, in order further analysis.
all results far saved ugarchfit objects, complicated list objects, want extract coefficients in following manner.
what want in essence this:
for(i in list){ i_garch_mxreg <- i_garch@fit$robust.matcoef[5,1] }
"list" list object, every element name of 1 observation. now, want loop create new numeric object named specified in loop.
now doesn't work because index, 'i', isn't replaced want be.
how rewrite loop appropriately?
minimal working example:
list <- as.list(c("one", "two", "three")) one_a <- 1 two_a <- 2 three_a <- 3 (i in list){ i_b <- i_a }
what should give me be:
> one_b [1] 1 > two_b [1] 2 > three_b [1] 3
clarification: want extract coefficients form multiple list objects. these named in manner 'string'_obj. problem don't have function extract these coefficients, list "is not subsettable", have call individual objects via obj@fit$robust.matcoef[5,1]
(or there way?). wanted use loop take list of strings, , in every iteration, take 1 string, add 'string'_obj@fit$robust.matcoef[5,1]
, , save value object, named again " 'string'_name "
it might easier have list rather individual objects, suggest lapply, not primary concern right now.
there easy way this, unable find it. sorry confusion , help.
the following should match desired output:
# list l <- as.list(c("one", "two", "three")) one_a <- 1 two_a <- 2 three_a <- 3 # workspace: note there no one_b, two_b, three_b ls() [1] "l" "one_a" "three_a" "two_a" (i in l){ # first, let's define names characters, using paste: dest <- paste0(i, "_b") orig <- paste0(i, "_a") # let's assign values. since working # characters, functions assign , come in handy: assign(dest, get(orig) ) } # let's check 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 values correct: one_b [1] 1 two_b [1] 2 three_b [1] 3
to comment on functions used: assign
takes character first argument, supposed name of newly created object. second argument value of object. get
takes character , looks value of object in workspace same name character. instance, get("one_a")
yield 1.
also, follow on comment earlier: if had coefficients in list, following:
# hypothetical coefficients stored in list: lcoefs <- list(1,2,3) # let's name coefficients: lcoefs <- setnames(lcoefs, paste0(c("one", "two", "three"), "_c")) # push them global environment: list2env(lcoefs, env = .globalenv) # @ 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 address comments, here more realistic example, taking list
-structure account:
l <- as.list(c("one", "two", "three")) # let's "hide" values in list: one_a <- list(val = 1) two_a <- list(val = 2) three_a <- list(val = 3) (i in l){ dest <- paste0(i, "_b") orig <- paste0(i, "_a") # let's 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
Comments
Post a Comment