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.

Estimating Yield To Maturity - The YTM measures the expected return to bond investors if they hold the bond until maturity. This number summarizes the compensation investors demand for the risk they are bearing by investing in a particular bond. We will discuss how one can estimate YTM of a bond.

Exercise 1: Price-yield relationshipExercise 2: Credit ratingsExercise 3: The yield on the Moody's Baa indexExercise 4: Value the 5% bond using the Baa yield you foundExercise 5: Plotting the price/yield relationshipExercise 6: Components of yieldExercise 7: Risk-free yieldExercise 8: Plotting US Treasury yields
Exercise 9: Plotting the investment grade spreadExercise 10: Estimating the yield of a bondExercise 11: Finding a bond's yieldExercise 12: Use uniroot function to find YTM

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