R/time_aggregate.R

#' Time Aggregation
#'
#' This functions aggregates the AR, CAR, SCAR through time and returns the results for the individual events and the time aggregated.
#' @param return.data should be a data frame containing dates (Date) in the first column and returns in the rest.
#' @param market.data should be a data frame containing dates (Date) in the first column and market returns in the second.
#' @param model should be a chracter string: "constant-mean-return" or "market"
#' @param event.list should be a data frame created by the MakeEvents function.
#' @param event.window should be a vector of two elements representing the number of observations prior to and after the event date.
#' @param estimation.window is a numeric value representing the number of observations to estimate the normal returns over.
#' @keywords Keywords
#' @export
#' @examples
#' TimeAggregation(return.data = ReturnData, market.data = MarketData, model = "constant-mean-return",
#' event.list = EventList, event.window = c(10,10), estimation.window = 90)
#' TimeAggregation(return.data = ReturnData, market.data = MarketData, model = "market",
#' event.list = EventList, event.window = c(10,10), estimation.window = 90)

# This functions aggregates the AR, CAR, SCAR through time and returns the results for the individual events and the time aggregated.
# Input:
# return.data should be a data frame containing dates (Date) in the first column and returns in the rest.
# market.data should be a data frame containing dates (Date) in the first column and market returns in the second.
# model should be a chracter string: "constant-mean-return" or "market"
# event.list should be a data frame created by the MakeEvents function.
# event.window should be a vector of two elements representing the number of observations prior to and after the event date.
# estimation.window is a numeric value representing the number of observations to estimate the normal returns over.
# Output:
#

TimeAggregation <- function(return.data, market.data, model, event.list, event.window, estimation.window){
  # Set number of events studied and their indexes in the return.data
  events <- nrow(event.list)
  event.date.indexes <- match(event.list$Date, return.data$Date)
  event.time <- c(-event.window[1]:event.window[2])


  # Check that the event dates are all in the dataset
  if(anyNA(event.date.indexes)) stop("A specific event date is NOT in the return data!")

  # Check that the first event has an estimation window in the dataset
  if((event.date.indexes[1] - event.window[1] - estimation.window - 1) < 1) stop("The first event does not have a complete estimation window!")

  # check that the last event has an event window in the dataset
  if((tail(event.date.indexes, 1) + event.window[2]) > nrow(return.data)) stop("The last event does not have a complete event window!")

  # Check that the events don't cluster
  event.window.indexes <- c()
  for(i in 1:events){
    event.window.indexes <- c(event.window.indexes, (event.date.indexes[i] - event.window[1]):(event.date.indexes[i] + event.window[2]))
  }
  if(anyDuplicated(event.window.indexes) != 0) stop("The event windows overlap!")

  # No problems so far, let's proceed.

  # Calculate normal residuals, AR, CAR, SCAR, and averages across assets for all events individually
  individual.events <- list()

  for(i in 1:events){
    individual.events[[i]] <- AbnormalReturns(return.data = return.data, market.data = market.data, model = model,
                                            event.date = event.list[i,], event.window = event.window, estimation.window = estimation.window)
  }
  names(individual.events) <- event.list$Name

  # Aggregate the individual results through time (over all events)
  # Set result container and overwrite it in for-loop
  aggregated.results <- individual.events[[1]]$Averages[,-1]
  aggregated.results[, 2:4] <- 0
  for(i in 1:events){
    aggregated.results[,2:4] <- aggregated.results[, 2:4] + (individual.events[[i]]$Averages[, 3:5] / events)
  }

  result <- list(individual.events, aggregated.results)
  names(result) <- c("Individual Events", "Aggregated Results")

  return(result)

}
wbach12/p9eventstudy documentation built on May 4, 2019, 7:43 p.m.