[Eventstudies-commits] r98 - in pkg: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 23 11:27:36 CEST 2013


Author: vikram
Date: 2013-07-23 11:27:35 +0200 (Tue, 23 Jul 2013)
New Revision: 98

Added:
   pkg/R/inference.bootstrap.R
   pkg/man/eventstudy.Rd
   pkg/man/inference.bootstrap.Rd
   pkg/man/inference.wilcox.Rd
Removed:
   pkg/R/inference.Ecar.R
   pkg/man/inference.Ecar.Rd
Modified:
   pkg/DESCRIPTION
   pkg/NAMESPACE
   pkg/R/eventstudy.R
Log:
Added wilcox non parametric test, changed inference.Ecar to inference.bootstrap, added man page for eventstudy function and required changes 

Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2013-07-22 09:39:42 UTC (rev 97)
+++ pkg/DESCRIPTION	2013-07-23 09:27:35 UTC (rev 98)
@@ -4,7 +4,7 @@
 Version: 1.1
 Author: Ajay Shah, Vimal Balasubramaniam, Vikram Bahure
 Maintainer: Vikram Bahure <economics.vikram at gmail.com>
-Depends: R (>= 2.12.0), zoo, xts, boot, testthat, sandwich
+Depends: R (>= 2.12.0), zoo, xts, boot, testthat, sandwich, exactRankTests
 Description: Implementation of short and long term event study methodology
 License: GPL-2
 LazyLoad: yes

Modified: pkg/NAMESPACE
===================================================================
--- pkg/NAMESPACE	2013-07-22 09:39:42 UTC (rev 97)
+++ pkg/NAMESPACE	2013-07-23 09:27:35 UTC (rev 98)
@@ -1,4 +1,5 @@
-export(inference.Ecar, phys2eventtime, remap.cumsum, remap.cumprod, remap.event.reindex, ees, eesPlot)
+export(inference.bootstrap, inference.wilcox, phys2eventtime,
+       remap.cumsum, remap.cumprod, remap.event.reindex, ees, eesPlot)
 export(marketResidual,
        excessReturn
        )

Modified: pkg/R/eventstudy.R
===================================================================
--- pkg/R/eventstudy.R	2013-07-22 09:39:42 UTC (rev 97)
+++ pkg/R/eventstudy.R	2013-07-23 09:27:35 UTC (rev 98)
@@ -1,11 +1,16 @@
 eventstudy <- function(inputData = NULL,
                        eventList,
                        width = 10,
+                       levels =  FALSE,
                        type = "marketResidual",
                        to.remap = TRUE,
                        remap = "cumsum",
+                       inference = TRUE,
+                       inference.strategy = "bootstrap",
                        to.plot = TRUE,
-                       levels =  FALSE,
+                       xlab = "Event time",
+                       ylab = "Cumulative returns of response series",
+                       main = "Event study plot",
                        ...) {
                                         # type = "marketResidual", "excessReturn", "AMM", "None"
   if (type == "None" && !is.null(inputData)) {
@@ -46,9 +51,23 @@
                    reindex = remap.event.reindex(es.w)
                    )
   }
-
-### Bootstrap
-  result <- inference.Ecar(z.e = es.w, to.plot = to.plot)
-
+  
+### Inference: confidence intervals
+  if(inference == TRUE){
+    ## Bootstrap
+    if(inference.strategy == "bootstrap"){
+      result <- inference.bootstrap(z.e = es.w, to.plot = to.plot, xlab = xlab,
+                                    ylab = ylab, main = main)
+    }
+    ## Wilcoxon
+    if(inference.strategy == "wilcoxon"){
+      result <- wilcox.CI(es.w = es.w, to.plot = to.plot, xlab = xlab,
+                          ylab = ylab, main = main)
+    }
+  } else {
+    ## Providing event frame as default output
+    result <- es.w
+  }
+    
   return(result)
 }

Deleted: pkg/R/inference.Ecar.R
===================================================================
--- pkg/R/inference.Ecar.R	2013-07-22 09:39:42 UTC (rev 97)
+++ pkg/R/inference.Ecar.R	2013-07-23 09:27:35 UTC (rev 98)
@@ -1,69 +0,0 @@
-library(boot)
-library(zoo)
-
-
-# This does bootstrap inference for the difference in the
-# average "car" between t1 and t2 (both in event time).
-# z.e is a zoo object, where rows are in event time
-# and columns are units of observation.
-# Sampling with replacement is done within the units of
-# observation. Each time, the Ecar(t1) and Ecar(t2) is
-# computed.
-# By default, the statistic of interest is the ratio
-#  Ecar(t2)/Ecar(t1)
-# But if operator="difference" is sent in, then the
-# statistic of interest shifts to Ecar(t2)-Ecar(t1).
-inference.change.boot <- function(z.e, t1, t2, operator="ratio", conf=.95) {
-  stopifnot(operator %in% c("ratio","difference"))
-
-  tmp <- t(as.matrix(z.e[c(t1,t2),]))
-  if (operator=="ratio") {
-    change <- tmp[,2]/tmp[,1]
-  }
-  if (operator=="difference") {
-    change <- tmp[,2]-tmp[,1]
-  }
-
-  mymean <- function(x,d) {mean(x[d], na.rm=TRUE)}
-  b <- boot(change, mymean, R=1000)
-  ci <- boot.ci(b, type="bca", conf=conf)
-  list(est=b$t0, lo=ci$bca[1,4], hi=ci$bca[1,5])
-}
-
-# Plotting inference
-plotInference <- function(inference){
-  big <- max(abs(inference))
-  hilo <- c(-big,big)
-  width <- (nrow(inference)-1)/2
-  plot(-width:width, inference[,"Mean"], type="l", lwd=2, ylim=hilo, col="blue",
-       xlab="Event time", ylab="Cumulative returns of response series",
-       main=paste("Eventstudy plot"))
-  points(-width:width, inference[,"Mean"])
-  lines(-width:width, inference[,"2.5%"], lwd=1, lty=2, col="blue")
-  lines(-width:width, inference[,"97.5%"], lwd=1, lty=2, col="blue")
-  abline(h=0,v=0)
-}
-
-# z.e is a zoo object with certain rows (e.g. from -10 to 10)
-# that define the event window, and columns with data for units.
-# This function does bootstrap inference for the entire
-# Ecar, i.e. main graph of the event study.
-inference.Ecar <- function(z.e,to.plot=FALSE) {
-  Ecar <- function(transposed, d) {
-    colMeans(transposed[d,], na.rm=TRUE)
-  }
-  tmp <- t(as.matrix(z.e))
-  b <- boot(tmp, Ecar, R=1000)
-
-  results <- NULL
-  for (i in 1:ncol(b$t)) {
-    results <- rbind(results, quantile(b$t[,i], prob=c(.025,.975)))
-  }
-  results <- cbind(results[,1], b$t0, results[,2])
-  rownames(results) <- rownames(z.e)
-  colnames(results) <- c("2.5%","Mean","97.5%")
-  if(to.plot==TRUE){
-    plotInference(inference=results)
-  }
-  return(results)
-}

Copied: pkg/R/inference.bootstrap.R (from rev 95, pkg/R/inference.Ecar.R)
===================================================================
--- pkg/R/inference.bootstrap.R	                        (rev 0)
+++ pkg/R/inference.bootstrap.R	2013-07-23 09:27:35 UTC (rev 98)
@@ -0,0 +1,96 @@
+library(boot)
+library(zoo)
+
+
+# This does bootstrap inference for the difference in the
+# average "car" between t1 and t2 (both in event time).
+# z.e is a zoo object, where rows are in event time
+# and columns are units of observation.
+# Sampling with replacement is done within the units of
+# observation. Each time, the Ecar(t1) and Ecar(t2) is
+# computed.
+# By default, the statistic of interest is the ratio
+#  Ecar(t2)/Ecar(t1)
+# But if operator="difference" is sent in, then the
+# statistic of interest shifts to Ecar(t2)-Ecar(t1).
+inference.change.boot <- function(z.e, t1, t2, operator="ratio", conf=.95) {
+  stopifnot(operator %in% c("ratio","difference"))
+
+  tmp <- t(as.matrix(z.e[c(t1,t2),]))
+  if (operator=="ratio") {
+    change <- tmp[,2]/tmp[,1]
+  }
+  if (operator=="difference") {
+    change <- tmp[,2]-tmp[,1]
+  }
+
+  mymean <- function(x,d) {mean(x[d], na.rm=TRUE)}
+  b <- boot(change, mymean, R=1000)
+  ci <- boot.ci(b, type="bca", conf=conf)
+  list(est=b$t0, lo=ci$bca[1,4], hi=ci$bca[1,5])
+}
+
+# Plotting inference
+plotInference <- function(inference, xlab, ylab, main){
+  big <- max(abs(inference))
+  hilo <- c(-big,big)
+  width <- (nrow(inference)-1)/2
+  plot(-width:width, inference[,"Mean"], type="l", lwd=2, ylim=hilo,
+       col="dark slate blue",
+       xlab= xlab, ylab = ylab,
+       main=paste(main))
+  points(-width:width, inference[,"Mean"])
+  lines(-width:width, inference[,"2.5%"], lwd=1, lty=2, col="dark slate blue")
+  lines(-width:width, inference[,"97.5%"], lwd=1, lty=2, col="dark slate blue")
+  abline(h=0,v=0)
+}
+
+# z.e is a zoo object with certain rows (e.g. from -10 to 10)
+# that define the event window, and columns with data for units.
+# This function does bootstrap inference for the entire
+# Ecar, i.e. main graph of the event study.
+inference.bootstrap <- function(z.e,to.plot=FALSE,
+                                xlab = "Event time",
+                                ylab = "Cumulative returns of response series",
+                                main = "Eventstudy plot") {
+  Ecar <- function(transposed, d) {
+    colMeans(transposed[d,], na.rm=TRUE)
+  }
+  tmp <- t(as.matrix(z.e))
+  b <- boot(tmp, Ecar, R=1000)
+
+  results <- NULL
+  for (i in 1:ncol(b$t)) {
+    results <- rbind(results, quantile(b$t[,i], prob=c(.025,.975)))
+  }
+  results <- cbind(results[,1], b$t0, results[,2])
+  rownames(results) <- rownames(z.e)
+  colnames(results) <- c("2.5%","Mean","97.5%")
+  if(to.plot==TRUE){
+    plotInference(inference=results, xlab, ylab, main)
+  }
+  return(results)
+}
+
+#####################
+## Wilcoxon sign test
+#####################
+inference.wilcox <- function(es.w, to.plot = TRUE, xlab = "Event time",
+                      ylab = "Cumulative returns of response series",
+                      main = "Eventstudy plot"
+                      ){
+  wx.res <- apply(es.w,1,function(x)
+                  res <- wilcox.exact(x, alternative = "two.sided",
+                                      conf.int = TRUE,
+                                      conf.level = 0.95)["conf.int"])
+  list <- unlist(wx.res, recursive = FALSE)
+  CI <- do.call(rbind, list)
+  Mean <- apply(es.w,1,mean,na.rm=TRUE)
+  result <- cbind(CI[,1], Mean, CI[,2])
+  colnames(result) <- c("2.5%","Mean","97.5%")
+  rownames(result) <- rownames(Mean)
+  if(to.plot = TRUE){
+    plotInference(inference = result, xlab, ylab, main)
+  }
+  return(result)
+}

Added: pkg/man/eventstudy.Rd
===================================================================
--- pkg/man/eventstudy.Rd	                        (rev 0)
+++ pkg/man/eventstudy.Rd	2013-07-23 09:27:35 UTC (rev 98)
@@ -0,0 +1,59 @@
+\name{eventstudy}
+\alias{eventstudy}
+
+\title{Performs event study using different methods and computes confidence intervals using different inference models}
+
+\description{ This function generates event study output and further computes confidence intervals using bootstrap or wilcox method.
+}
+
+\usage{
+eventstudy(inputData, eventList, width = 10, type = "marketResidual", 
+to.remap = TRUE, remap = "cumsum", levels = FALSE, inference = TRUE,
+inference.strategy = "bootstrap", to.plot = TRUE, xlab = "Event time",
+ylab = "Cumulative returns of response series", main = "Event study plot", ...)
+}
+
+\arguments{
+  \item{inputData}{Data on which event study is to be performed}
+  \item{eventList}{A data frame with event dates. It has two columns 'unit' and 'when'. The first column 'unit' consists of column names of the event stock and 'when' is the respective event date}
+  \item{width}{It studies the performance of observations before and after the event}
+  \item{type}{This argument gives an option to use different market model adjustment like "marketResidual", "excessReturn", "AMM" and "None"}
+  \item{to.remap}{If TRUE then remap the event frame is done}
+  \item{remap}{This argument is used when to.remap is TRUE to estimate cumulative sum (cumsum), cumulative product (cumprod) or reindex the event frame}
+  \item{levels}{If the data is in returns format then levels is FALSE else TRUE}
+  \item{inference}{This argument is used to compute confidence interval for the estimator}
+  \item{inference.strategy}{If inference is TRUE then this argument gives an option to select different inference strategy to compute confidence intervals. Default to bootstrap.}
+  \item{xlab}{If to.plot is TRUE then the plot generated will take this X label}
+  \item{ylab}{If to.plot is TRUE then the plot generated will take this Y label}	
+  \item{main}{If to.plot is TRUE then the plot generated will take this as main title}
+  \item{...}{Accepts specific arguments for the model.}
+}
+\value{ Output is mean estimate  of abnormal returns and confidence interval using particular inference strategy
+}
+
+\author{Vikram Bahure}
+
+\seealso{ \code{\link{AMM}},
+\code{\link{marketResidual}},	
+\code{\link{excessReturn}},
+\code{\link{phys2eventtime}},
+\code{\link{inference.bootstrap}},
+\code{\link{inference.wilcox}},
+\code{\link{remap.cumsum}},
+\code{\link{remap.cumprod}},
+\code{\link{remap.event.reindex}},
+}
+
+\examples{ 
+## Performing event study
+data("StockPriceReturns")
+data("SplitDates")
+
+## Event study without adjustment
+es <- eventstudy(inputData = StockPriceReturns, eventList = SplitDates, width = 10,
+                 type = "None", to.remap = TRUE, remap = "cumsum", to.plot = FALSE,
+                 inference = TRUE, inference.strategy = "bootstrap")
+
+}
+
+\keyword{eventstudy}
\ No newline at end of file

Deleted: pkg/man/inference.Ecar.Rd
===================================================================
--- pkg/man/inference.Ecar.Rd	2013-07-22 09:39:42 UTC (rev 97)
+++ pkg/man/inference.Ecar.Rd	2013-07-23 09:27:35 UTC (rev 98)
@@ -1,36 +0,0 @@
-\name{inference.Ecar}
-\alias{inference.Ecar}
-
-\title{
-Bootstrap inference for the event study.
-}
-
-\description{
-This function does bootstrap inference to generate distribution of average of all the cumulative returns time-series.
- }
-
-\usage{
-inference.Ecar(z.e,to.plot=FALSE)
-}
-
-\arguments{
-  \item{z.e}{z.e is the first component of the list returned by the function phys2eventtime.}
-  \item{to.plot}{This argument will generate an eventstudy plot of the inference estimated. If to.plot is equal to TRUE then function would generate the plot else it would not. }
-}
-
-\value{
-A data frame with 3 columns, the lower confidence interval (CI), the mean and the upper CI which are the result of bootstrap inference.
-}
-
-\seealso{
-phys2eventtime
-}
-
-\examples{
-data(StockPriceReturns)
-data(SplitDates)
-es.results <- phys2eventtime(z=StockPriceReturns, events=SplitDates,width=5)
-es.w <- window(es.results$z.e, start=-5, end=+5)
-eventtime <- remap.cumsum(es.w, is.pc=FALSE, base=0)
-inference.Ecar(z.e=eventtime, to.plot=FALSE)
-}
\ No newline at end of file

Copied: pkg/man/inference.bootstrap.Rd (from rev 95, pkg/man/inference.Ecar.Rd)
===================================================================
--- pkg/man/inference.bootstrap.Rd	                        (rev 0)
+++ pkg/man/inference.bootstrap.Rd	2013-07-23 09:27:35 UTC (rev 98)
@@ -0,0 +1,41 @@
+\name{inference.bootstrap}
+\alias{inference.bootstrap}
+
+\title{
+Bootstrap inference for the event study.
+}
+
+\description{
+This function does bootstrap inference to generate distribution of average of all the cumulative returns time-series.
+ }
+
+\usage{
+inference.bootstrap(z.e, to.plot = TRUE, xlab = "Event time", 
+			 ylab = "Cumulative returns of response series", 
+			 main = "Event study plot")
+}
+
+\arguments{
+  \item{z.e}{z.e is the first component of the list returned by the function phys2eventtime.}
+  \item{to.plot}{This argument will generate an eventstudy plot of the inference estimated. If to.plot is equal to TRUE then function would generate the plot else it would not. }
+  \item{xlab}{If to.plot is TRUE then the plot generated will take this X label}
+  \item{ylab}{If to.plot is TRUE then the plot generated will take this Y label}	
+  \item{main}{If to.plot is TRUE then the plot generated will take this as main title}	
+}
+
+\value{
+A data frame with 3 columns, the lower confidence interval (CI), the mean and the upper CI which are the result of bootstrap inference.
+}
+
+\seealso{
+phys2eventtime
+}
+
+\examples{
+data(StockPriceReturns)
+data(SplitDates)
+es.results <- phys2eventtime(z=StockPriceReturns, events=SplitDates,width=5)
+es.w <- window(es.results$z.e, start=-5, end=+5)
+eventtime <- remap.cumsum(es.w, is.pc=FALSE, base=0)
+inference.bootstrap(z.e=eventtime, to.plot=FALSE)
+}
\ No newline at end of file

Added: pkg/man/inference.wilcox.Rd
===================================================================
--- pkg/man/inference.wilcox.Rd	                        (rev 0)
+++ pkg/man/inference.wilcox.Rd	2013-07-23 09:27:35 UTC (rev 98)
@@ -0,0 +1,43 @@
+\name{inference.wilcox}
+\alias{inference.wilcox}
+
+\title{
+Wilcox inference for the event study.
+}
+
+\description{
+This function does wilcox inference to generate distribution of average of all the cumulative returns time-series.
+ }
+
+\usage{
+inference.wilcox(z.e,to.plot = TRUE, xlab = "Event time", 
+			     ylab = "Cumulative returns of response series", 
+			     main = "Event study plot")
+}
+
+\arguments{
+  \item{z.e}{z.e is the first component of the list returned by the function phys2eventtime.}
+  \item{to.plot}{This argument will generate an eventstudy plot of the inference estimated. If to.plot is equal to TRUE then function would generate the plot else it would not. }
+  \item{xlab}{If to.plot is TRUE then the plot generated will take this X label}
+  \item{ylab}{If to.plot is TRUE then the plot generated will take this Y label}	
+  \item{main}{If to.plot is TRUE then the plot generated will take this as main title}	
+}
+
+\value{
+A data frame with 3 columns, the lower confidence interval (CI), the mean and the upper CI which are the result of wilcox inference.
+}
+
+\author{Vikram Bahure}
+
+\seealso{
+phys2eventtime
+}
+
+\examples{
+data(StockPriceReturns)
+data(SplitDates)
+es.results <- phys2eventtime(z=StockPriceReturns, events=SplitDates,width=5)
+es.w <- window(es.results$z.e, start=-5, end=+5)
+eventtime <- remap.cumsum(es.w, is.pc=FALSE, base=0)
+inference.wilcox(z.e=eventtime, to.plot=FALSE)
+}
\ No newline at end of file



More information about the Eventstudies-commits mailing list