# Plotting the VIX Index and TED Spread in R

In this lesson, will will look at how to create the two graphs in R. Before you start, it is important to setup your basic environment:

### Setup Environment

Open RStudio and ensure that you have the basic setup in place.

• Create a new directory called ‘quantative-trading-strategies-r’ in your computer. We will use this director for all the exercises in this course.
• Set this directory as your working directory using the setwd() command. You can also check the current working directory using the getwd() command in R console.
• Use the command CTRL+L to clear the console (Use this whenever you feel the console is cluttered).
• Create a new R script where we will be writing our R script.
• Install and load the ggplot2 package by typing the following commands in the console:
#Install ggplot2
install.packages("ggplot2")

library("ggplot2")


In order to plot the TED spread, you can follow the following instructions:

Get TED data from https://fred.stlouisfed.org/series/TEDRATE

For convenience, we have also provided the data files below.

To read a csv with R, the best way to do it is with the command read.csv.

ted <- read.csv("TEDRATE.csv",stringsAsFactors = FALSE)


Step 3: Transform Data

The read.csv function interpret both columns as factors columns, so we need to change to the appropriate columns data types.

We will convert character columns to numeric and date columns, and remove NA values with complete.cases.

The complete.cases function returns a logical vector specifying which observations/rows have no missing values across the entire sequence.

ted$TEDRATE <- as.numeric(ted$TEDRATE)
ted$DATE <- as.Date(ted$DATE)
ted <- ted[complete.cases(ted),]


Finally, we have ‘ted’ as a dataframe containing our prepared data.

Step 4: Plot the Graph

Now, we can use ggplot2 library to plot the graph.

ggplot(ted, aes(DATE, TEDRATE)) + geom_line(color = "red") + xlab("") + ylab("Daily Prices")+ggtitle("TED Spread")


The complete script for creating the TED spread is provided below:

# Get TED data from https://fred.stlouisfed.org/series/TEDRATE

# The read.csv function interpret both columns as factors columns, so we need to change
# to the appropriate columns data types. Convert character columns to numeric and date
# columns, and remove NA values with complete.cases

ted$TEDRATE <- as.numeric(ted$TEDRATE)
ted$DATE <- as.Date(ted$DATE)
ted <- ted[complete.cases(ted),]

# View the data
ted

# Plot the graph

ggplot(ted, aes(DATE, TEDRATE)) + geom_line(color = "red") + xlab("") + ylab("Daily Prices")+


### Plot the VIX Index

You can follow the exact same steps above to plot the VIX volatility index. The complete script for it is provided below:

# Get VIX data from https://fred.stlouisfed.org/series/VIXCLS

# Convert character columns to numeric and date columns, and
# remove NA values # with complete.cases

VIX$VIXCLS <- as.numeric(VIX$VIXCLS)
VIX$DATE <- as.Date(VIX$DATE)
VIX <- VIX[complete.cases(VIX),]

# View the data
VIX

# Plot the graph

ggplot(VIX, aes(DATE, VIXCLS)) + geom_line(color = "blue") + xlab("") + ylab("Daily Prices")+
ggtitle("VIX Prices")


### You may find these interesting

Single Index Model
The Single Index Model (SIM) is an asset pricing model, according to which the returns on a security...
Omega Index
The Omega Index (or Omega ratio) was developed by Keating and Shadwick in 2002. It is a ratio of the...
Bond spread (or credit spread) refers to the difference in the yield of two [bonds](https://financet...
When you buy and sell [bonds](https://financetrain.com/general-characteristics-of-bonds "General Cha...