7.4 Doing the event study

Start with loading the required packages. In order to get stock data, we will use a specialized package called BatchGetSymbols available on CRAN.

library(BatchGetSymbols)
library(dplyr)
library(ggplot2)
library(lubridate)
library(data.table)
library(purrr)
library(psych)
library(reshape2)
library(caret)

Download stock price data using the events data frame. We specifically need the ticker symbols of the stocks.45 We will collect stock returns 400 days prior to the event and 15 days post event. We are not going to use all the data but if you are downloading it once, it is better to download more rather than less.

The function BatchGetSymbols() returns a list. One of the elements of the list is the data frame consisting of the stock prices. Rather than writing complicated code in the loop, we will simply store the entire list as an element of a larger list stkprc. Then we will keep storing downloaded lists in this larger list.

stkprc <- list()

for (i in 1:nrow(events)) {
  stkprc[[i]] <- BatchGetSymbols(
                  tickers = events$Ticker[i],
                  first.date = (events$Date_Attacked[i] - 400),
                  last.date = (events$Date_Attacked[i] + 15),
                  freq.data = "daily"
                  )
}

stkprc is a list of lists as each one of its elements is a list of 2 more elements. Next we need to create another list price_dt where we will only hold the dataframe with stock prices. Also, we need to create a variable that holds the information about the trading days from the event date. This is slightly more complicated than just taking the difference between two dates. This is because the stock market is not open on all the days. In the code below, I used which() function from base R to return the row number where the event date rests in the downloaded stock returns data. This has to be done for each of the 13 stocks and therefore, we use a for loop. Note that we create a variable event_diff which is the difference between the row number of a given date and the row number of the event date that we calculated earlier.

price_dt <- list()

for (i in 1:nrow(events)) {
  event_pos <- which(stkprc[[i]]$df.tickers$ref.date == 
                     events$Date_Attacked[i])
  price_dt[[i]] <- tryCatch(stkprc[[i]]$df.tickers %>% 
    mutate(event_diff = row_number() - event_pos),
    error = function(x) {
    message(x)
    return(NA)
    })
}

You probably noticed that there is a tryCatch() function used above. As it turns out, there were 2 events which did not take place on the day when stock markets were open. This means for these stocks, event_pos was NA and event_diff could not be computed. We could move the event date for these two stocks by a day or two. However, for this exercise we will refrain from doing that because there might be other events happening the next day, which will affect the stock prices.

The next two lines of code will remove the blank list elements.

price_dt[[which(is.na(price_dt))[1]]] <- NULL
price_dt[[which(is.na(price_dt))[1]]] <- NULL

Finally, we will stack all the 11 data sets and create one single data frame.46

price_stakced <- data.table::rbindlist(price_dt)

Get the frequency distribution of tickers to make sure that we actually stacked 11 data sets.

table(price_stakced$ticker)
## 
##  AMZN    BA   CBS   CCZ   DAL    GM   JWN   LMT MKGAF   NYT    TM 
##   286   287   286   287   285   285   286   285   287   286   285

Looks good.


  1. A ticker symbol is a short code used for identifying a company’s stock.

  2. I love using data.table for this. Check out https://www.ashwinmalshe.com/post/speed-comparison-rbind/