Get startedGet started for free

Exploring monthly returns of the 30 DJIA stocks

The 1991-2015 monthly returns on the 30 DJIA stocks are available in the workspace as the variable returns. This exercise will help you get comfortable with the data that you will be using for the remainder of the exercises.

Recall that if we compute the means column by column using colMeans(returns), or apply(returns, 2, "mean"), you then obtain the average return per asset. In this exercise, you will be computing the mean per row. You can do this similarly using the function rowMeans() and supplying the argument 1 instead of two to indicate row-wise calculations in the apply() function. By doing so, you obtain the time series of returns for the equally weighted portfolio.

This exercise is part of the course

Introduction to Portfolio Analysis in R

View Course

Exercise instructions

  • Verify that returns is an object of the xts-class using the function class().
  • Investigate the dimensions of returns using dim().
  • Create a vector of row means of returns using the function rowMeans(). Assign this to ew_preturns. Note that you could have used apply() here as well.
  • The solution obtained from rowMeans() is a numeric vector. Cast it back to a xts object using xts with time series stamp equal to the dates in returns.
  • Plot ew_preturns using plot.zoo().

Hands-on interactive exercise

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

# Verify the class of returns 


# Investigate the dimensions of returns


# Create a vector of row means


# Cast the numeric vector back to an xts object
ew_preturns <- xts(___, order.by = time(returns))

# Plot ew_preturns

Edit and Run Code