Get Started

Plotting US Treasury yields

Since corporate bonds are riskier than US Treasuries, the risk-free rate is the baseline rate we would expect corporate bonds to yield. However, US Treasury yields are not constant and could change substantially through time. We can see this by plotting the US Treasury yield data over a long period.

In this exercise, you will use the quantmod() package to obtain and plot 10-Year US Treasury yield data from the Federal Reserve Electronic Database (FRED) from January 2006 to September 2016.

The getSymbols() command in quantmod allows you to access the FRED database by specifying the Symbols argument (in this case, to "DGS10" for 10-Year US Treasury Bonds). You'll also need to set the src argument equal to "FRED", and set auto.assign to FALSE.

This is a part of the course

“Bond Valuation and Analysis in R”

View Course

Exercise instructions

  • Load the quantmod package.
  • Use getSymbols() from quantmod to obtain data on DGS10 from FRED. Be sure to set the auto.assign argument to FALSE. Save this data to t10yr.
  • Subset your t10yr data to the period from January 2006 to September 2016 using the data["date1/date2"] format.
  • Use the pre-written code to plot your t10yr data.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Load quantmod package


# Obtain Treasury yield data
t10yr <- getSymbols(Symbols = "___", src = "___", auto.assign = ___)

# Subset data
t10yr <-

# Plot yields
plot(x = index(t10yr),
     y = t10yr$DGS10,
     xlab = "Date",
     ylab = "Yield (%)",
     type = "l",
     col = "red",
     main = "10-Year US Treasury Yields")

This exercise is part of the course

Bond Valuation and Analysis in R

IntermediateSkill Level
4.8+
4 reviews

Learn to use R to develop models to evaluate and analyze bonds as well as protect them from interest rate changes.

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free