Calculating Returns
Suppose you are valuing a healthcare company and need to determine the firm's beta using peer companies. One of the peer companies you identified is Mylan (myl
), which is a global healthcare company. In this exercise, you are asked to calculate Mylan's and the S&P 500 ETF's monthly returns over the last five years. Five years of monthly prices for myl
and spy
are stored in prices
.
Use the Delt()
function to calculate returns. The Delt()
function is from the package quantmod,
which has also been loaded in memory. Note that the Delt()
function requires two observations to calculate a return, so in the time series data that we have the first observation will have a missing value. Thus, after calculating the returns for the remaining dates, we want to delete that first observation (i.e., the observation with a missing return) from the data.
This exercise is part of the course
Equity Valuation in R
Exercise instructions
- Show first six observations of prices.
- Calculate monthly returns for MYL and SPY.
- Change label of first variable to
"myl"
. - Remove the first observation, which is a missing value.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Show first six observations of prices
___
# Calculate MYL monthly return
rets <- ___
# Calculate SPY monthly return
rets$spy <- ___
# Change label of first variable
___
# Remove first observation - NA
rets <- ___