Dieselgate: An Example with Data Preparation

[This article was first published on Event Study Tools - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

With the VW Dieselgate event as empirical seeting, this page shows you how to fetch data in R, perform an Event Study, and do some basic plots with our R package.

library(tidyquant)
library(dplyr)
library(readr)

Data Preparation

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.

startDate <- "2014-05-01"
endDate <- "2015-12-31"

We focus us on the big five motor manufacturer in Germany, namely

  • VW Group
    • VW
    • Audi
    • Porsche
  • Daimler
  • BMW
# Firm Data
firmSymbols <- c("VOW.DE", "NSU.DE", "PAH3.DE", "BMW.DE", "DAI.DE")
firmNames <- c("VW preferred", "Audi", "Porsche Automobil Hld", "BMW", "Daimler")
firmSymbols %>% 
  tidyquant::tq_get(from = startDate, to = endDate) %>% 
  dplyr::mutate(date = format(date, "%d.%m.%Y")) -> firmData
knitr::kable(head(firmData), pad=0)
symbol date open high low close volume adjusted
VOW.DE 02.05.2014 194.05 194.05 188.00 188.55 67018 188.55
VOW.DE 05.05.2014 188.20 189.20 185.45 188.60 69295 188.60
VOW.DE 06.05.2014 189.05 189.90 184.80 186.00 49051 186.00
VOW.DE 07.05.2014 185.65 187.00 184.60 184.70 52484 184.70
VOW.DE 08.05.2014 185.05 189.00 185.05 188.95 51892 188.95
VOW.DE 09.05.2014 188.60 189.50 187.95 188.45 40368 188.45

As reference market we choose the DAX.

# Index Data
indexSymbol <- c("^GDAXI")
indexName <- c("DAX")
indexSymbol %>% 
  tidyquant::tq_get(from = startDate, to = endDate) %>% 
  dplyr::mutate(date = format(date, "%d.%m.%Y")) -> indexData
indexData$symbol <- "DAX"
knitr::kable(head(indexData), pad=0)
date open high low close volume adjusted symbol
02.05.2014 9611.79 9627.38 9533.30 9556.02 88062300 9556.02 DAX
05.05.2014 9536.38 9548.17 9407.09 9529.50 61911600 9529.50 DAX
06.05.2014 9570.25 9571.63 9440.47 9467.53 82062900 9467.53 DAX
07.05.2014 9418.50 9554.35 9410.08 9521.30 92732600 9521.30 DAX
08.05.2014 9547.27 9622.30 9487.57 9607.40 102022500 9607.40 DAX
09.05.2014 9591.32 9602.86 9558.11 9581.45 80084100 9581.45 DAX

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 %>% 
  dplyr::select(symbol, date, adjusted) %>% 
  readr::write_delim(path      = "02_firmDataPrice.csv", 
                     delim     = ";", 
                     col_names = F)

indexData %>% 
  dplyr::select(symbol, date, adjusted) %>% 
  readr::write_delim(path      = "03_marketDataPrice.csv", 
                     delim     = ";", 
                     col_names = F)

# Volume files for firms and market
firmData %>% 
  dplyr::select(symbol, date, volume) %>% 
  readr::write_delim(path      = "02_firmDataVolume.csv", 
                     delim     = ";", 
                     col_names = F)

indexData %>% 
  dplyr::select(symbol, date, volume) %>% 
  readr::write_delim(path      = "03_marketDataVolume.csv", 
                     delim     = ";", 
                     col_names = F)

Finally, we have to prepare the request file. The parameters for this Event Study are:

  • Estimation window: 250
  • Event window: -10 to 10
  • Event date: 18.09.2015

Details of the format can be found in the introductionary vignette.

group <- c(rep("VW Group", 3), rep("Other", 2))
request <- 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 %>% 
  as.data.frame() %>% 
  readr::write_delim("01_requestFile.csv", delim = ";", col_names = F)

Perform Event Studies: Abnromal Return, Volume, and Volatility

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 computational expensive and no nearly realtime response from the API can be expected.

Abnormal Return Event Study

key <- "573e58c665fcc08cc6e5a660beaad0cb"

library(EventStudy)
est <- EventStudyAPI$new()
est$authentication(apiKey = key)
## [1] TRUE
# 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
esaParams <- EventStudy::ARCApplicationInput$new()
esaParams$setResultFileType("csv")
esaParams$setBenchmarkModel("garch")

dataFiles <- c("request_file" = "01_requestFile.csv",
               "firm_data"    = "02_firmDataPrice.csv",
               "market_data"  = "03_marketDataPrice.csv")

# check data files, you can do it also in our R6 class
EventStudy::checkFiles(dataFiles)
## Checking request_fileChecking firm_dataChecking market_data
# now let us perform the Event Study
arEventStudy <- est$performEventStudy(estParams     = esaParams, 
                                      dataFiles     = dataFiles, 
                                      downloadFiles = T)
## [1] "Check batch process: Step 0"

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.

knitr::kable(head(arEventStudy$arResults))
Event ID eventTime ar tValue Firm Reference Market Estimation Window Length Group
1 -10 0.0032 0.3137 VOW.DE DAX 250 NA
2 -10 0.0253 1.8201 NSU.DE DAX 250 VW Group
3 -10 0.0052 0.5652 PAH3.DE DAX 250 VW Group
4 -10 0.0085 0.9551 BMW.DE DAX 250 Other
5 -10 0.0101 1.4429 DAI.DE DAX 250 Other
1 -9 0.0006 0.0588 VOW.DE DAX 250 NA

The averaged abnormal return (aar) data.frame has the following shape:

knitr::kable(head(arEventStudy$aarResults))
  level eventTime aar N Pos stat1 stat2 stat3 stat4 stat5 stat6 stat7 stat8 stat9 stat10
1 VW Group -10 0.0112 3 3 1.5247 1.6955 1.5871 1.9441 1.2859 1.4944 1.5225 1.9401 1.7928 2.7133
14 Other -10 0.0093 2 2 1.6669 1.3264 11.6250 4.9509 1.5658 2.0021 1.3952 3.1356 2.4105 Inf
27 VW Group -9 -0.0015 3 1 -0.2372 -0.6144 -1.4042 -1.3290 -0.5438 -1.3223 -0.2369 -1.3262 -1.5857 -0.6998
40 Other -9 0.0024 2 2 0.4740 1.3264 1.2973 1.2505 0.5367 1.0826 0.3967 0.7920 1.3077 NA
53 VW Group -8 0.0035 3 2 0.6652 0.5405 0.9476 1.1537 0.5618 0.7711 0.6643 1.1513 0.9243 0.7847
66 Other -8 0.0135 2 2 2.2489 1.3264 1.9286 2.3441 1.6954 1.7402 1.8823 1.4846 2.0982 Inf

You can find the statistic naming in arEventStudy$aarStatistics.

Abnormal Volatility Event Study

est <- EventStudyAPI$new()
est$authentication(apiKey = key)
## [1] TRUE
# get & set parameters for abnormal return Event Study
esaParams <- EventStudy::AVyCApplicationInput$new()
esaParams$setResultFileType("csv")
 
avycEventStudy <- est$performEventStudy(estParams    = esaParams, 
                                       dataFiles     = dataFiles,
                                       downloadFiles = T)
## [1] "Check batch process: Step 0"

The prepared data.frames in avycEventStudy have a similar shape as for the abnormal return Event Study.

Abnormal Volume Event Study

This will be added in a later stage.

# est <- EventStudyAPI$new()
# est$authentication(apiKey = key)
# 
# # get & set parameters for abnormal return Event Study
# esaParams <- EventStudy::AVCApplicationInput$new()
# esaParams$setResultFileType("csv")
# 
# avEventStudy <- est$performEventStudy(estParams = esaParams,
#                       dataFiles = c("request_file" = "01_requestFile.csv",
#                                     "firm_data"    = "02_firmDataVolume.csv",
#                                     "market_data"  = "03_marketDataVolume.csv"))

 

To leave a comment for the author, please follow the link and comment on their blog: Event Study Tools - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)