Get startedGet started for free

Exploring risk-factor time series: individual equities

For some risk management applications, it is sufficient to model equity risk by looking at indexes. If you want a more detailed model of the risk in a portfolio of equities, you can drill down to the level of individual share prices.

In the previous chapter, you used DJ["2008/2009"] to extract the Dow Jones data from certain rows of an xts object by specifying a date range index. To also extract data from particular columns, you can add a column identifier, like a string name or numeric index, in the brackets following a comma. To select multiple columns, include these column identifiers in a vector. This [rows, columns] format is consistent with indexing most other two dimensional objects in R.

data[index, colname]
data[index, c(col1index, col2index)]

The qrmdata package also includes data for certain constituents, or the stocks or companies part of a larger index. The Dow Jones constituents data are contained in "DJ_const". In this exercise, you will view the names of all its stocks, select the Apple and Goldman Sachs share prices, and plot them using the command plot.zoo() to display multiple time series.

This exercise is part of the course

Quantitative Risk Management in R

View Course

Exercise instructions

  • Load the DJ constituents data "DJ_const" from qrmdata.
  • Use names() to view the names in DJ_const and head() to display the first few rows.
  • Extract only the Apple ("AAPL") and Goldman Sachs ("GS") share prices for 2008-2009 and assign them to object stocks.
  • Plot stocks using plot.zoo().

Hands-on interactive exercise

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

# Load DJ constituents data
___(___)

# Apply names() and head() to DJ_const
___(___)
___(___)

# Extract AAPL and GS in 2008-09 and assign to stocks
stocks <- ___

# Plot stocks with plot.zoo()
___(___)
Edit and Run Code