[R-SIG-Finance] Downloading data from ALFRED (ArchivaL FRED)
Paul Gilbert
pgilbert902 at gmail.com
Fri Aug 3 16:35:34 CEST 2012
Roger
Michael Cassidy at the St Louis Fed is working on an R package to
interface with their API, and I am sure he would be happy to know about
your efforts, and possibly collaborate. His expertise may be more with
the xml than the R, but that seems to be the part where you are stuck.
Paul
On 12-08-03 10:16 AM, Bos, Roger wrote:
> All,
>
> I know that quantmod has a function getSymbols to download data from the Fed's FRED database, but the Fed also has a second version of the FRED database called ALFRED that allows you to get data by vintage date (before revisions). Is there a function to access that database? I tried to modify the function but couldn't figure out the comparable URL for the ALFRED databse.
>
> So I took a look at their API offering ( http://api.stlouisfed.org/docs/fred/ ). Its easy to use, but I am having trouble getting the resulting XML data into a data frame I can actually work with. The code below works, but I am hesitant to give out my key, so I have XXXX'ed it out. You can get an API key for free in less than 60 seconds.
>
> library(XML)
> fred.url <- "http://api.stlouisfed.org/fred/series/observations?series_id=PSAVERT&file_type=xml&output_type=2&observation_start=2012-03-28&observation_end=2012-06-28&vintage_date=1960-09-28&api_key=XXXX"
> doc <- xmlParse(fred.url)
> doc
> xmlToDataFrame(doc)
> out <- xmlToList(doc)
> unlist(out)
>
> Even without running the code, someone might be able to help me out. The XML examples work so well, but it seems the XML output from the AFLRED site is slightly different. Here is what it looks like:
>
>> doc <- xmlParse(fred.url)
>
>> doc
> <?xml version="1.0" encoding="utf-8"?>
> <observations realtime_start="2012-08-03" realtime_end="2012-08-03" observation_start="2012-03-28" observation_end="2012-06-28" units="lin" output_type="2" file_type="xml" order_by="observation_date" sort_order="asc" count="3" offset="0" limit="100000">
> <observation date="2012-04-01" PSAVERT_20120803="3.6"/>
> <observation date="2012-05-01" PSAVERT_20120803="4.0"/>
> <observation date="2012-06-01" PSAVERT_20120803="4.4"/>
> </observations>
>
> That looks very reasonable to me, but when I try to convert it to a data frame, I get an error that 'data' must be of a vector type:
>
>> xmlToDataFrame(doc)
> Error in matrix(vals, length(nfields), byrow = TRUE) :
> 'data' must be of a vector type
>>
>
> xmlToList almost gets me there, and I could probably write some code to extract the data and put it into a data frame, but there is probably a much easier way and I am just missing something because this is my first time parsing XML.
>
>> out <- xmlToList(doc)
>
>> out
> $observation
> date PSAVERT_20120803
> "2012-04-01" "3.6"
>
> $observation
> date PSAVERT_20120803
> "2012-05-01" "4.0"
>
> $observation
> date PSAVERT_20120803
> "2012-06-01" "4.4"
>
> $.attrs
> realtime_start realtime_end observation_start observation_end units output_type
> "2012-08-03" "2012-08-03" "2012-03-28" "2012-06-28" "lin" "2"
> file_type order_by sort_order count offset limit
> "xml" "observation_date" "asc" "3" "0" "100000"
>
>
> I am open to any advice anyone has.
>
> Thanks,
>
> Roger
>
>
>
> Roger J. Bos, CFA
> Rothschild Asset Management, Inc.
> Tel +1 (212) 405-5471
> Email roger.bos at rothschild.com<mailto:roger.bos at rothschild.com>
> 1251 Avenue of the Americas, NY, NY 10020
>
>
> ***************************************************************
>
> This message is for the named person's use only. It may\...{{dropped:15}}
>
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.
>
More information about the R-SIG-Finance
mailing list