9

I am downloading data from FRED with the quantmod library (author Jeffrey A. Ryan). With Yahoo and Google data, I am able to set start and end dates. Can the same be done for FRED data?

The help page does not list "from" and "to" as options of quantmod's getSymbols function, from which I'm inferring that it is not currently possible.

Is there a way to set a range for the data to be downloaded or do I need to download the entire dataset and discard the data I don't need?

Thanks for your help. Below the code that illustrates the context:

The dates are ignored when downloading from FRED:

# environment in which to store data 
data <- new.env()

# set dates
date.start <- "2000-01-01"
date.end <- "2012-12-31"

# set tickers
tickers <- c("FEDFUNDS", "GDPPOT", "DGS10")

# import data from FRED database
library("quantmod")
getSymbols( tickers
  , src = "FRED"  # needed!
  , from = date.start  # ignored
  , to = date.end  # ignored
  , env = data
  , adjust = TRUE
)

head(data$FEDFUNDS)

head(data$FEDFUNDS)
           FEDFUNDS
1954-07-01     0.80
1954-08-01     1.22
1954-09-01     1.06
1954-10-01     0.85
1954-11-01     0.83
1954-12-01     1.28

EDIT: Solution

Thanks to GSee's suggestion below, I am using the following code to subset the data to within the range of dates specified above:

# subset data to within time range
  dtx <- data$FEDFUNDS
  dtx[paste(date.start,date.end,sep="/")]

Here I extracted the xts data from the environment before acting upon it. My follow-up question explores alternatives.

Follow-Up Question

I have asked some follow-up questions there: get xts objects from within an environment

micstr
  • 5,080
  • 8
  • 48
  • 76
PatrickT
  • 10,037
  • 9
  • 76
  • 111
  • Thanks. To help others who need to get dates from another series, say fund `returns`, and you are using FRED to get riskfree rate FEDfunds. You can use start() and end() on your xts return series as parameters for your subset i.e. xts["Startdate/enddate"]. `Rf <- dtx[paste(start(returns), end(returns), sep="/")]` – micstr Oct 12 '18 at 09:34

3 Answers3

7

You have to download all the data and subset later. getSymbols.FRED does not support the from argument like getSymbols.yahoo does.

GSee
  • 48,880
  • 13
  • 125
  • 145
5

Alternatively you can download FRED data from Quandl (http://www.quandl.com/help/r) which offers more than 4 million datasets including all of the FRED data. There is an API and R package available. ("Quandl"). Data can be returned in several formats formats e.g. data frame ("raw"), ts ("ts"), zoo ("zoo") and xts ("xts"). For example to download GDPPOT10 and specify the dates and have it returned as an xts object all you have to do is:

require(Quandl)
mydata = Quandl("FRED/GDPPOT", start_date="2005-01-03",end_date="2013-04-10",type="xts")
hvollmeier
  • 2,956
  • 1
  • 12
  • 17
  • Oh that's great hvollmeier, I haven't used the Quandl package before, I'm off to explore it now! Your suggestion is indeed simple. Great to know. – PatrickT Apr 12 '13 at 21:26
  • btw, I have already accepted GSee's answer, so a big thank you for adding your suggestion. – PatrickT Apr 12 '13 at 21:27
1

Quandl doesn't seem to offer all data from FRED, at least in terms data frequency. Quandl most likely offers only annual data which is not useful in many circumstance.

mark
  • 11
  • 1
  • can you give a specific example and an alternative download source? – PatrickT Oct 23 '15 at 06:59
  • Funny how I reached this thread a year later. I have found that, in some cases, ``Quandl`` has renamed the ``Fred`` datasets. Example: This data can be found on the ``Fred`` website as ``LRHUTTTTUSA156N`` and on the ``Quandl`` website as ``AUSURHARMADSMEI``. I did find quarterly and annual data, but the change of name is very inconvenient... – PatrickT Apr 28 '16 at 17:39