Skip to content

Instantly share code, notes, and snippets.

@natbusa
Last active October 8, 2020 04:03
Show Gist options
  • Save natbusa/7329248 to your computer and use it in GitHub Desktop.
Save natbusa/7329248 to your computer and use it in GitHub Desktop.
retrieving us treasury rates
library('XML')
library('RCurl')
# Save the URL of the xml file in a variable
fileUrl="http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData?$filter=year(NEW_DATE)%20eq%202010"
###Using the rcurl package, first download, then parse
#Get the content
fileContent = getURL(fileUrl)
#parse xml from text string
xmlfile.fromstring = xmlParse(fileContent,asText=TRUE)
###Using the XML package, download and parse all-in-one
#parse xml from url
xmlfile.fromUrl = xmlParse(fileUrl)
# the xml file is now saved as an object you can easily work with in R:
class(xmlfile.fromUrl)
class(xmlfile.fromstring)
# RCurl offer more control on the download,
# but apart from that parsing should produce the same data structure
# Here below we proceed with xmlfile.fromUrl
# Use the xmlRoot-function to access the top node
xmltop = xmlRoot(xmlfile.fromUrl)
# have a look at the XML-code of the first subnodes:
xmlChildren(xmltop)[1:6]
#extract the subpaths (don't forget to pass the default namespace, or you will not find anything!)
bonds_dates = xpathSApply(xmltop, "//ns:entry/ns:content//d:NEW_DATE", function(x){xmlValue(x)}, namespaces=c(ns="http://www.w3.org/2005/Atom", d="http://schemas.microsoft.com/ado/2007/08/dataservices"))
bonds_1_year = xpathSApply(xmltop, "//ns:entry/ns:content//d:BC_1YEAR", function(x){as.numeric(xmlValue(x))}, namespaces=c(ns="http://www.w3.org/2005/Atom", d="http://schemas.microsoft.com/ado/2007/08/dataservices"))
bonds_3_year = xpathSApply(xmltop, "//ns:entry/ns:content//d:BC_3YEAR", function(x){as.numeric(xmlValue(x))}, namespaces=c(ns="http://www.w3.org/2005/Atom", d="http://schemas.microsoft.com/ado/2007/08/dataservices"))
bonds = data.frame(date=strptime(bonds_dates,format="%Y-%m-%dT%H:%M:%S", tz = "GMT"), y1=bonds_1_year, y3=bonds_3_year)
library('plyr')
bonds = arrange(bonds,date)
bonds$yearmonth = format(bonds$date, format="%y-%m")
bp = boxplot(bonds$y1 ~ bonds$yearmonth)
lines(lowess(bp$stats[3,], f=1/4))
plot(bonds$date, bonds$y1, type='l')
@dwjohnk
Copy link

dwjohnk commented Oct 8, 2020

This code didn't work in 2020. Any suggestions?

@natbusa
Copy link
Author

natbusa commented Oct 8, 2020

I guess you have to inspect the xml, fork the gist and edit yourself. That assuming that treasury.gov still publish the data

@dwjohnk
Copy link

dwjohnk commented Oct 8, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment