Get startedGet started for free

Commodities data

The plotting function pairs() creates a pairwise scatterplot of the components of a multivariate time series with two or more dimensions. It is used on a zoo object rather than an xts object.

A roughly circular shape of a scatterplot indicates a low correlation between the log-returns of two different commodities. Generally speaking, low correlation is good in a portfolio as it implies that the assets are diversified. High correlation, on the other hand, represents a risk that must be properly modelled.

In this exercise, you will look at gold and oil prices over a 25 year period, calculate their daily and monthly log-returns, and plot them. The data gold and oil, containing the daily prices from 1990-2015 of gold and Brent crude oil, respectively, are available in your workspace.

This exercise is part of the course

Quantitative Risk Management in R

View Course

Exercise instructions

  • Use plot() to plot the gold and oil time series separately.
  • Calculate the daily log-returns of each commodity and assign to goldx and oilx, respectively.
  • Calculate the monthly log-returns of each commodity and assign to goldx_m and oilx_m, respectively.
  • Use merge() to merge goldx_m and oilx_m, in that order, into coms.
  • Plot coms, a multivariate series, with vertical bars.
  • Convert coms to a zoo object with as.zoo() and then apply pairs() to create a pairwise scatterplot.

Hands-on interactive exercise

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

# Plot gold and oil prices
___(___)
___(___)

# Calculate daily log-returns
goldx <- ___(___)
oilx <- ___(___)

# Calculate monthly log-returns
goldx_m <- ___(___)
oilx_m <- ___(___)

# Merge goldx_m and oilx_m into coms
coms <- ___(___, ___)

# Plot coms with vertical bars
___(___, ___)

# Make a pairwise scatterplot of coms
___(___)
Edit and Run Code