Based on the Dieselgate scandal we want to show you how you are able to fetch data in R, perform an Event Study, and do some basic plots with our R package.
library(tidyquant)
library(dplyr)
library(readr)
We use the package tidyquant to fetch the automotive stock data from Yahoo Finance. As we cannot get the full volume size from this companies through Yahoo Finance API, we do not perform a volume Event Study in this vignette.
Let’s define the window from which we want to fetch the data of the German auto companies.
<- "2014-05-01"
startDate <- "2015-12-31" endDate
We focus us on the big five motor manufacturers in Germany, namely
# Firm Data
<- c("VOW.DE", "NSU.DE", "PAH3.DE", "BMW.DE", "DAI.DE")
firmSymbols <- c("VW preferred", "Audi", "Porsche Automobil Hld", "BMW", "Daimler")
firmNames %>%
firmSymbols ::tq_get(from = startDate, to = endDate) %>%
tidyquant::mutate(date = format(date, "%d.%m.%Y")) -> firmData
dplyr::kable(head(firmData), pad=0) knitr
As reference market we choose the DAX.
# Index Data
<- c("^GDAXI")
indexSymbol <- c("DAX")
indexName %>%
indexSymbol ::tq_get(from = startDate, to = endDate) %>%
tidyquant::mutate(date = format(date, "%d.%m.%Y")) -> indexData
dplyr$symbol <- "DAX"
indexData::kable(head(indexData), pad=0) knitr
Now, after we have fetched all the data, we prepare the data files for the API call, as described in the introductionary vignette. We prepare in this step already the volume data for later purposes.
# Price files for firms and market
%>%
firmData ::select(symbol, date, adjusted) %>%
dplyr::write_delim(path = "02_firmDataPrice.csv",
readrdelim = ";",
col_names = F)
%>%
indexData ::select(symbol, date, adjusted) %>%
dplyr::write_delim(path = "03_marketDataPrice.csv",
readrdelim = ";",
col_names = F)
# Volume files for firms and market
%>%
firmData ::select(symbol, date, volume) %>%
dplyr::write_delim(path = "02_firmDataVolume.csv",
readrdelim = ";",
col_names = F)
%>%
indexData ::select(symbol, date, volume) %>%
dplyr::write_delim(path = "03_marketDataVolume.csv",
readrdelim = ";",
col_names = F)
Finally, we have to prepare the request file. The parameters for this Event Study are:
Details of the format can be found in the introductionary vignette.
<- c(rep("VW Group", 3), rep("Other", 2))
group <- cbind(c(1:5), firmSymbols, rep(indexName, 5), rep("18.09.2015", 5), group, rep(-10, 5), rep(10, 5), rep(-11, 5), rep(250, 5))
request %>%
request as.data.frame() %>%
::write_delim("01_requestFile.csv", delim = ";", col_names = F) readr
After the preparation steps, we are now able to start the calculations. We use in all type of Event Studies the GARCH(1, 1) model. Please consider in your Event Studies that fitting this model is computationally expensive and delayed response from the API should be expected.
<- "573e58c665fcc08cc6e5a660beaad0cb"
key
library(EventStudy)
<- EventStudyAPI$new()
est $authentication(apiKey = key)
est
# get & set parameters for abnormal return Event Study
# we use a garch model and csv as return
# Attention: fitting a GARCH(1, 1) model is compute intensive
<- EventStudy::ARCApplicationInput$new()
esaParams $setResultFileType("csv")
esaParams$setBenchmarkModel("garch")
esaParams
<- c("request_file" = "01_requestFile.csv",
dataFiles "firm_data" = "02_firmDataPrice.csv",
"market_data" = "03_marketDataPrice.csv")
# check data files, you can do it also in our R6 class
::checkFiles(dataFiles)
EventStudy
# now let us perform the Event Study
<- est$performEventStudy(estParams = esaParams,
arEventStudy dataFiles = dataFiles,
downloadFiles = T)
Now, you can use the downloaded CSV (or your preferred data format) files in your analysis. During the creation of the arEventStudy
object we merge information from the request file, and the result files.
::kable(head(arEventStudy$arResults)) knitr
The averaged abnormal return (aar) data.frame
has the following shape:
::kable(head(arEventStudy$aarResults)) knitr
You can find the statistic naming in arEventStudy$aarStatistics
.
<- EventStudyAPI$new()
est $authentication(apiKey = key)
est
# get & set parameters for abnormal return Event Study
<- EventStudy::AVyCApplicationInput$new()
esaParams $setResultFileType("csv")
esaParams
<- est$performEventStudy(estParams = esaParams,
avycEventStudy dataFiles = dataFiles,
downloadFiles = T)
The prepared data.frames
in avycEventStudy
have a similar shape as for the abnormal return Event Study.