Downloading data

It is possible to carry out all the analysis of returns in R. The first issue is downloading data. The easiest way is to download a comma-separated-variable (csv) file. This is a common file format and (fortunately) the way that Yahoo finance will provide the files. There is a fuller discussion in the Quick R Guide.

You need to save the csv file to the working directory. In raw R the working directory can be changed by using “change dir..” from the “file” menu. In R-Studio, you will see the working directory at the top of the page. Where I have written “../Data/BAC.csv”, you will just need to write “BAC.csv”.

BAC <- read.csv("../Data/BAC.csv")
head(BAC)
##         Date  Open  High   Low Close    Volume Adj.Close
## 1 02/12/2013 15.84 15.97 15.06 15.57  81838900  15.31413
## 2 01/11/2013 14.00 15.98 13.80 15.82 105192300  15.55001
## 3 01/10/2013 13.85 14.72 13.68 13.97  93637500  13.73158
## 4 03/09/2013 14.30 14.83 13.60 13.80  90972500  13.56448
## 5 01/08/2013 14.85 14.97 13.98 14.12  91225700  13.86928
## 6 01/07/2013 12.95 15.03 12.73 14.60 117692600  14.34076

The function “head” will show the first 6 lines. Take a look at ?head to see other arguments.

Manipulate the data

Now we need to

  1. Remove the data that is not wanted
  2. Make sure that R knows that the first column contains dates
  3. Calculate returns
BAC$Date <- as.Date(BAC$Date, format = "%d/%m/%Y")
BAC <- BAC[,c(1,7)]
colnames(BAC) <- c("Date", "Close")
BAC <- BAC[rev(rownames(BAC)),]
head(BAC)
##           Date    Close
## 168 2000-01-03 15.51458
## 167 2000-02-01 14.73385
## 166 2000-03-01 16.98035
## 165 2000-04-03 15.86722
## 164 2000-05-01 18.11670
## 163 2000-06-01 14.05218

Notes

  1. You can find out more about the date object here
  2. Positions on a data.frame are identified as [a,b] where a is the row number and b is the column number. I have taken all the rows and just columns 1 and 7 (concatenating a vector of 1 and 7 to do that)
  3. I have changed the column names to “Date” and “Close” so that I do not have to keep typing “Adj.Close” each time.
  4. I have reversed the order of the data using the function “rev”. This is what R expects from a data frame and makes calculations like “diff” easier.
BACR <- diff(BAC$Close) / BAC$Close[-length(BAC$Close)] 
BACr <- diff(log(BAC$Close))
BAC <- cbind(BAC[-1,], BACR, BACr)
tail(BAC)
##         Date    Close        BACR        BACr
## 6 2013-07-01 14.34076  0.13530327  0.12689982
## 5 2013-08-01 13.86928 -0.03287672 -0.03342930
## 4 2013-09-03 13.56448 -0.02197656 -0.02222164
## 3 2013-10-01 13.73158  0.01231886  0.01224360
## 2 2013-11-01 15.55001  0.13242657  0.12436273
## 1 2013-12-02 15.31413 -0.01516900 -0.01528522

Using packages

This is all a little complicated. However, now you have the code, you can just cut-and-paste whenever you want to do this sort of operation. You can also use packages. These are blocks of pre-prepared code that have been created by other people. For example, you can use the “quantmod” package. You can find out more here and here

You download the package with the following command

install.packages("quantmod")
library(quantmod)
getSymbols("BAC", from = "2000-01-01", to = "2013-12-31")
## [1] "BAC"
BACR <- monthlyReturn(BAC, type = "arithmetic")
BACr <- monthlyReturn(BAC, type = "log")
plot(BACR, BAC$Date, type = 'l')

Assessment of BAC returns

The assessment of the sample of BAC returns can now be carried out. The “moments” package will have to be downloaded for the functions “skewness” and “kurtosis”. This is done in the same way as the “quantmod” package.

require(moments)
statNames <- c("mean", "std dev", "skewness", "kurtosis", "max", "min")
BAC.stats <- c(mean(BACr), sd(BACr), skewness(BACr), kurtosis(BACr), 
               max(BACr), min(BACr))
names(BAC.stats) <- statNames
round(BAC.stats, 4)
##     mean  std dev skewness kurtosis      max      min 
##  -0.0070   0.1409  -1.5192  11.4075   0.5461  -0.7607

This last code comes from Revolution Analytics

Remember to use the discussion board on StudentCentral if you have any questions about this.