Exploring the monthly S&P 500 returns
For the upcoming exercises, you will examine the monthly performance of the S&P 500. A picture is worth a thousand words. This is why most analyses of performance start by studying the time series plot of the value of an investment.
A plot of the S&P 500 for the period of 1986 until August 2016 is shown to your right. Each observation on this plot is an end-of-day value. This chart shows a number of booms and busts. Take a look at the chart, do you see why the 2000s are so often called the lost decade of investing?
The PerformanceAnalytics
and xts
-package are preloaded, and the daily prices for the S&P 500 are available in your workspace as the variable sp500
. This variable is of the xts time series class. This means that each observation has a time-stamp. Your job is to describe the monthly performance of the S&P 500. To do so, you will first need to aggregate daily price series to end-of-month prices. You will then calculate the monthly returns and visualize them in a table.
This exercise is part of the course
Introduction to Portfolio Analysis in R
Exercise instructions
- Use the function to.monthly() with the argument
sp500
and assign this tosp500_monthly
. - Print the first six rows of
sp500_monthly
. Notice how aggregating the data has resulted in a table of four columns holding the opening, lowest, highest, and closing price ofsp500
for each month. - Create
sp500_returns
using the functionReturn.calculate()
onsp500_monthly
using the closing prices (fourth column insp500_monthly
). - Use
plot.zoo()
to plot the time series ofsp500_returns
- Use the function table.CalendarReturns() in PerformanceAnalytics to present the monthly return data in a table format showing year by month.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Convert the daily frequency of sp500 to monthly frequency: sp500_monthly
sp500_monthly <-
# Print the first six rows of sp500_monthly
# Create sp500_returns using Return.calculate using the closing prices
sp500_returns <-
# Time series plot
# Produce the year x month table