Home > R > Non-standard assignment with getSymbols

Non-standard assignment with getSymbols

I recently came across a rather interesting investment blog, Timely Portfolio. I have a certain soft spot for that sort of thing, because using my data analysis skills to make a fortune is casually on my to-do list.

This blog makes regular use of a function getSymbols in the quantmod package. The power and simplicity of the function is fantastic: with one short line of code, you can retrieve historical data on any stock, bond or index that you fancy. It does have one oddity though. In R, we are all used to assigning values to variable with <-.

x <- mean(1:5)

Not for getSymbols this behaviour though. It uses a bizarre assignment procedure whereby the return value is assigned to a variable with the same name as the Symbols parameter, into an environment of your choice (the global environment by default). For example, getSymbols("DGS10",src="FRED") creates a variable named DGS10.

When retrieving many symbols, this can get a little clunky. Here’s a snippet from a recent post.

getSymbols("DGS10",src="FRED") #load 10yTreasury
getSymbols("DFII10",src="FRED") #load 10yTIP for real return
getSymbols("DTWEXB",src="FRED") #load US dollar
getSymbols("SP500",src="FRED") #load SP500

I see lots of code repetition, which means that is is a prime opportunity for some refactoring. These four lines can be condensed with a call to lapply by passing a vector to getSymbols (EDIT CREDIT: thanks Owe!).

symbol_names <- c("DGS10", "DFII10", "DTWEXB", "SP500")
#lapply(symbol_names, getSymbols, src = "FRED")  #see Owe's comment
getSymbols(symbol_names, src = "FRED")

Unfortunately, the non-standard assignment means that instead of having a nice list of each of our datasets, we have four separate variables. To fix this, we must create our own environment to store the results, then convert that to a list.

symbol_env <- new.env()
#lapply(symbol_names, getSymbols, src="FRED", env = symbol_env) 
getSymbols(symbol_names, src = "FRED", env = symbol_env)
list_of_symbols <- as.list(symbol_env)

Understanding environments is quite an advanced topic and a full explanation is beyond the scope of this post. In this case however, the idea is very simple. We need somewhere out of the way to store all the variables that getSymbols creates. This storage place is the environment symbol_env, which can be thought of a list with special variable scoping rules. Since environments and lists are such similar constructs, we can convert from one to the other with as.list. (list2env works in the other direction.)

About these ads
  1. timelyportfolio
    21st April, 2011 at 23:28 pm | #1

    Thanks for the mention. I agree the behavior is nontraditional. auto.assign=FALSE allows some ability to override. I always appreciate feedback and criticism. Look forward to following you.

  2. Owe jessen
    22nd April, 2011 at 8:49 am | #2

    Thanks for your post, the thing about creating a list is probably very helpful to me. Just a little nitpick: You are able to get the symbols from a vector of symbol names directly, in your example:

    symbol_names <- c("DGS10", "DFII10", "DTWEXB", "SP500")
    getSymbols(symbol_names, src="FRED")

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 160 other followers

%d bloggers like this: