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)
symboldateopenhighlowclosevolumeadjusted
VOW.DE02.05.2014194.05194.05188.00188.5567018188.55
VOW.DE05.05.2014188.20189.20185.45188.6069295188.60
VOW.DE06.05.2014189.05189.90184.80186.0049051186.00
VOW.DE07.05.2014185.65187.00184.60184.7052484184.70
VOW.DE08.05.2014185.05189.00185.05188.9551892188.95
VOW.DE09.05.2014188.60189.50187.95188.4540368188.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)
dateopenhighlowclosevolumeadjustedsymbol
02.05.20149611.799627.389533.309556.02880623009556.02DAX
05.05.20149536.389548.179407.099529.50619116009529.50DAX
06.05.20149570.259571.639440.479467.53820629009467.53DAX
07.05.20149418.509554.359410.089521.30927326009521.30DAX
08.05.20149547.279622.309487.579607.401020225009607.40DAX
09.05.20149591.329602.869558.119581.45800841009581.45DAX

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 IDeventTimeartValueFirmReference MarketEstimation Window LengthGroup
1-100.00320.3137VOW.DEDAX250NA
2-100.02531.8201NSU.DEDAX250VW Group
3-100.00520.5652PAH3.DEDAX250VW Group
4-100.00850.9551BMW.DEDAX250Other
5-100.01011.4429DAI.DEDAX250Other
1-90.00060.0588VOW.DEDAX250NA

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

knitr::kable(head(arEventStudy$aarResults))
 leveleventTimeaarNPosstat1stat2stat3stat4stat5stat6stat7stat8stat9stat10
1VW Group-100.0112331.52471.69551.58711.94411.28591.49441.52251.94011.79282.7133
14Other-100.0093221.66691.326411.62504.95091.56582.00211.39523.13562.4105Inf
27VW Group-9-0.001531-0.2372-0.6144-1.4042-1.3290-0.5438-1.3223-0.2369-1.3262-1.5857-0.6998
40Other-90.0024220.47401.32641.29731.25050.53671.08260.39670.79201.3077NA
53VW Group-80.0035320.66520.54050.94761.15370.56180.77110.66431.15130.92430.7847
66Other-80.0135222.24891.32641.92862.34411.69541.74021.88231.48462.0982Inf

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)