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:
The complete script for creating the TED spread is provided below:
1# Get TED data from https://fred.stlouisfed.org/series/TEDRATE2# Load the data using read.csv34ted <- read.csv("TEDRATE.csv",stringsAsFactors = FALSE)56# The read.csv function interpret both columns as factors columns, so we need to change7# to the appropriate columns data types. Convert character columns to numeric and date8# columns, and remove NA values with complete.cases910ted$TEDRATE <-as.numeric(ted$TEDRATE)11ted$DATE <-as.Date(ted$DATE)12ted <- ted[complete.cases(ted),]1314# View the data15ted
1617# Plot the graph1819ggplot(ted, aes(DATE, TEDRATE))+ geom_line(color ="red")+ xlab("")+ ylab("Daily Prices")+20 ggtitle("TED Spread")21
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:
1# Get VIX data from https://fred.stlouisfed.org/series/VIXCLS2# Load the data using read.csv34VIX <- read.csv("VIXCLS.csv",stringsAsFactors = FALSE)56# Convert character columns to numeric and date columns, and 7# remove NA values # with complete.cases89VIX$VIXCLS <-as.numeric(VIX$VIXCLS)10VIX$DATE <-as.Date(VIX$DATE)11VIX <- VIX[complete.cases(VIX),]1213# View the data14VIX
1516# Plot the graph1718ggplot(VIX, aes(DATE, VIXCLS))+ geom_line(color ="blue")+ xlab("")+ ylab("Daily Prices")+19 ggtitle("VIX Prices")20
Unlock Premium Content
Upgrade your account to access the full article, downloads, and exercises.