Computational Finance, Eric Zivot

require(xts)
require(ggplot2)
# Assign the URL to the CSV file
url <- "http://assets.datacamp.com/course/compfin/sbuxPrices.csv"

# Load the data frame using read.csv
sbux_df <- read.csv(file=url, header=TRUE, stringsAsFactors=FALSE)

# Check the structure of 'sbux_df'
str(sbux_df)
## 'data.frame':    181 obs. of  2 variables:
##  $ Date     : chr  "3/31/1993" "4/1/1993" "5/3/1993" "6/1/1993" ...
##  $ Adj.Close: num  1.13 1.15 1.43 1.46 1.41 1.44 1.63 1.59 1.32 1.32 ...
# Check the first and last part of 'sbux_df'
head(sbux_df)
##        Date Adj.Close
## 1 3/31/1993      1.13
## 2  4/1/1993      1.15
## 3  5/3/1993      1.43
## 4  6/1/1993      1.46
## 5  7/1/1993      1.41
## 6  8/2/1993      1.44
tail(sbux_df)
##          Date Adj.Close
## 176 10/1/2007     25.37
## 177 11/1/2007     22.24
## 178 12/3/2007     19.46
## 179  1/2/2008     17.98
## 180  2/1/2008     17.10
## 181  3/3/2008     16.64
# Get the closs of the Date column of 'sbux_df'
class(sbux_df$Date)
## [1] "character"
# Assign to the variable closing_prices all the adjusted closing prices while preserving the dimension information.
closing_prices <- sbux_df[1:181, "Adj.Close", drop=FALSE]
# Alternatively
closing_prices2 <- sbux_df[ , "Adj.Close", drop=FALSE]
head(closing_prices); head(closing_prices2)
##   Adj.Close
## 1      1.13
## 2      1.15
## 3      1.43
## 4      1.46
## 5      1.41
## 6      1.44
##   Adj.Close
## 1      1.13
## 2      1.15
## 3      1.43
## 4      1.46
## 5      1.41
## 6      1.44
# Assign to index_1 the row number of the row that contains the price on 3/1/1994.
# Assign to index_2 the row number of the row that contains the price on 3/1/1995.
index_1 <- which(sbux_df$Date == "3/1/1994")
index_2 <- which(sbux_df$Date == "3/1/1995")
index_1; index_2
## [1] 13
## [1] 25
# Assign to some_prices the SBUX closing prices between 3/1/1994 and 3/1/1995.
some_prices <- sbux_df[index_1:index_2, "Adj.Close"]
some_prices
##  [1] 1.45 1.77 1.69 1.50 1.72 1.68 1.37 1.61 1.59 1.63 1.43 1.42 1.43
# Create a new data frame that contains the price data with the dates as the row names
sbux_prices_df <- sbux_df[, "Adj.Close", drop=FALSE]
rownames(sbux_prices_df) <- sbux_df$Date
head(sbux_prices_df)
##           Adj.Close
## 3/31/1993      1.13
## 4/1/1993       1.15
## 5/3/1993       1.43
## 6/1/1993       1.46
## 7/1/1993       1.41
## 8/2/1993       1.44
# With Dates as rownames, you can subset directly on the dates.
# Find indices associated with the dates 3/1/1994 and 3/1/1995.
price_1 <- sbux_prices_df["3/1/1994",]
price_2 <- sbux_prices_df["3/1/1995",]

plot(sbux_df$Adj.Close, type="l", ylab="Adjusted Close", main="Monthly closing price of SBUX", col="blue", lwd=2)

# Denote n the number of time periods
n <- nrow(sbux_prices_df)

# Calculating simple returns
# Your task in this exercise is to compute the simple returns for every time point n
sbux_ret <- (sbux_prices_df[2:n,1]-sbux_prices_df[1:(n-1),1])/sbux_prices_df[1:(n-1),1]

# Notice that sbux_ret is not a data frame object
class(sbux_ret)
## [1] "numeric"