The EventStudy package includes helper functions to download stock
price data and Fama-French factor data, pre-formatted for use with
EventStudyTask. This eliminates the need for manual data
preparation.
The code in this article is shown for illustration purposes and is not executed at build time because it requires a live network connection.
The download_stock_data() function downloads adjusted
close prices from Yahoo Finance. It requires either the
tidyquant or quantmod package.
library(EventStudy)
# Download a single stock
aapl <- download_stock_data("AAPL", from = "2023-01-01", to = "2024-12-31")
head(aapl)
#> # A tibble: 6 x 3
#> symbol date adjusted
#> <chr> <chr> <dbl>
#> 1 AAPL 02.01.2023 130.
#> 2 AAPL 03.01.2023 126.
#> ...When format_for_task = TRUE (default), dates are
converted to the dd.mm.yyyy format required by
EventStudyTask:
# Formatted for direct use
firm_data <- download_stock_data(
c("AAPL", "MSFT"),
from = "2022-01-01",
format_for_task = TRUE
)
# Download index data
index_data <- download_stock_data(
"^GSPC", # S&P 500
from = "2022-01-01",
format_for_task = TRUE
)
# Create the task directly
request <- tibble::tibble(
event_id = 1:2,
firm_symbol = c("AAPL", "MSFT"),
index_symbol = "^GSPC",
event_date = c("15.06.2023", "15.06.2023"),
group = "Tech",
event_window_start = -5,
event_window_end = 5,
shift_estimation_window = -6,
estimation_window_length = 120
)
task <- EventStudyTask$new(firm_data, index_data, request)The download_factor_data() function downloads
Fama-French factor data directly from the Kenneth French Data
Library.
# Download data
firm_data <- download_stock_data(c("AAPL", "MSFT"), from = "2022-01-01")
index_data <- download_stock_data("^GSPC", from = "2022-01-01")
factor_tbl <- download_factor_data(model = "ff3", frequency = "daily")
# Create task with factor data
task <- EventStudyTask$new(firm_data, index_data, request,
factor_tbl = factor_tbl)
# Use a factor model
ps <- ParameterSet$new(
return_model = FamaFrench3FactorModel$new()
)
task <- run_event_study(task, ps)The download_risk_free_rate() function extracts the
risk-free rate from the FF3 data:
When format_for_task = TRUE, factor data columns are
automatically renamed:
| Original | Renamed |
|---|---|
| Mkt-RF | market_excess |
| SMB | smb |
| HML | hml |
| RMW | rmw |
| CMA | cma |
| Mom | mom |
| RF | risk_free_rate |