[Eventstudies-commits] r263 - in pkg: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Mar 29 19:00:15 CET 2014


Author: chiraganand
Date: 2014-03-29 19:00:14 +0100 (Sat, 29 Mar 2014)
New Revision: 263

Modified:
   pkg/R/phys2eventtime.R
   pkg/man/phys2eventtime.Rd
Log:
Modified comments in the function, changed text in the manual.

Modified: pkg/R/phys2eventtime.R
===================================================================
--- pkg/R/phys2eventtime.R	2014-03-29 14:55:11 UTC (rev 262)
+++ pkg/R/phys2eventtime.R	2014-03-29 18:00:14 UTC (rev 263)
@@ -1,4 +1,4 @@
-# Upon input
+# Upon input   
 #   z is a zoo object containing input data. E.g. this could be all the 
 #     prices of a bunch of stocks. The column name is the unit name.
 #   events is a data.frame containing 2 columns. The first column
@@ -10,20 +10,17 @@
 #   wdatamissing: too many NAs within the crucial event window.
 #   success : all is well.
 # A vector of these outcomes is returned.
+
 phys2eventtime <- function(z, events, width=10) {
-  # Just in case events$outcome.unit has been sent in as a factor --
+  ## Ensuring class of event matrix
   events$outcome.unit <- as.character(events$outcome.unit)
-  if(is.factor(events$event.when)) stop("Sorry you provided a factor as an index")
-  # Given a zoo time-series z, and an event date "event.when",
-  # try to shift this vector into event time, where the event date
-  # becomes 0 and all other dates shift correspondingly.
-  # If this can't be done, then send back NULL with an error code.
-  ## takes the event list as an argument and uses already existing
-  ## time-series variable z
+  if(is.factor(events$event.when)) {
+    stop("The column 'event.when' cannot be a factor. Cannot proceed with data manipulation.")
+  }
 
-                                        # check the dimensions of "z""
+  ## z: physical time matrix. Check dimensions of "z"
   if (is.null(ncol(z))) {
-    stop(paste(deparse("z"), "should be a zoo series with at least one column."))
+    stop(paste(deparse("z"), "should be of class zoo/xts with at least one column."))
   }
 
   timeshift <- function(x) {
@@ -31,9 +28,7 @@
     if (!firm.present) {
       return(list(result=NULL, outcome="unitmissing"))
     }
-                                        # take the previous date if
-                                        # the exact event date is not
-                                        # found
+    ## Take previous date if exact data is not found.
     location <- findInterval(as.Date(x[2]), index(z[, x[1]]))
     if ((location <= 1) | (location >= length(index(z)))) {
       return(list(result=NULL, outcome="wrongspan"))
@@ -48,14 +43,15 @@
   outcomes <- as.character(do.call("c", answer[rownums]))
   z.e <- do.call("cbind", answer[rownums[which(answer[rownums] == "success")] - 1])
 
-  if (length(z.e) == 0) {               # no point of going forward
-    return(list(z.e = z.e, outcomes = factor(outcomes)))
+  ## If no successful outcome, return NULL to z.e. 
+  if (length(z.e) == 0) {               
+    return(list(z.e = NULL, outcomes = factor(outcomes)))
   }
 
   colnames(z.e) <- which(outcomes == "success")
 
-  ## Now worry about whether there's information within the event window
-  ## (This entire cleaning+checking can be switched off by specifying width=0)
+  ## Information verification within 'width'
+  ##   :: Will not be executed with width = 0
   badcolumns <- NULL
   if (width > 0) {
     for (i in 1:ncol(z.e)) {
@@ -72,7 +68,7 @@
       z.e <- z.e[, -badcolumns]
     }
   }
-  # Check that we're okay
+  ## Double check
   stopifnot(sum(outcomes=="success") == NCOL(z.e))
   list(z.e=z.e, outcomes=factor(outcomes))
 }

Modified: pkg/man/phys2eventtime.Rd
===================================================================
--- pkg/man/phys2eventtime.Rd	2014-03-29 14:55:11 UTC (rev 262)
+++ pkg/man/phys2eventtime.Rd	2014-03-29 18:00:14 UTC (rev 263)
@@ -2,43 +2,52 @@
 \alias{phys2eventtime}
 
 \title{
-  Function to convert physical dates to event dates.
+  Convert data from physical to event time
 }
 
-\description{ Given a zoo time-series and event dates, the function
-  converts the physical dates to event dates. The event date becomes
-  zero and all other dates shift accordingly.  }
+\description{\sQuote{phys2eventtime} is used to convert data from
+  physical to event time using information on events identified by the
+  user.}
 
 \usage{
 phys2eventtime(z, events, width = 10)
 }
 
 \arguments{
-  \item{z}{an object of class \pkg{zoo} containing time series data for
-    which the event frame is to be generated.}
+  \item{z}{an object of class \pkg{zoo} or \pkg{xts} containing data to
+    be converted into event time.}
 
   \item{events}{
-    \sQuote{data.frame} containing event dates. See \sQuote{Details}.
+    \sQuote{data.frame} containing event identifiers. See \sQuote{Details}.
   }
 
-  \item{width}{an \sQuote{integer} specifying the number of days on each
-  side of the event date.}
+  \item{width}{an \sQuote{integer} specifying the event window within
+    which data should be available to consider the outcome a 'success'. See
+  \sQuote{Details}.}
 
 }
 
 \details{
 
   \dQuote{events} object contains two columns: \dQuote{outcome.unit}
-  consists of column names of the event stock, and \dQuote{event.when}
-  is the respective event date. If an event date is not found, the
-  function takes the previous value of date in \code{index(z)} as the
-  event date.
+  consists of names of the event, and \dQuote{event.when} is the
+  respective event identifier. For instance, if \sQuote{z} is a matrix
+  of class \pkg{xts} with 10 stocks over 300 days, the names of stocks
+  in \sQuote{z} is the superset of names for the event and the time
+  period (i.e., 300 days) will be the superset for defining event dates.
+  If an event date does not lie within this period, the function
+  approximates to the nearest previous date using
+  \code{\link{findInterval}}.
 
-  For a given \dQuote{width}, if there is any \sQuote{NA} in the event
-  window, then the last observation is carried forward.
+  The argument \dQuote{width} provides the user with an option to define
+  successful events as those that have data within a window around the
+  event. If \dQuote{width} is 10 periods, those events with \sQuote{NA}
+  within 10 periods before and after the event will be classified as
+  \dQuote{wdatamissing}, otherwise, the event \dQuote{outcome} will be
+  classified as \dQuote{success}.
 
   Currently this function requires \dQuote{z} to have at least one
-  \emph{column}. It relies on \sQuote{colnames} of z for the series
+  \emph{column}. It relies on \sQuote{colnames} of \dQuote{z} for the series
   names, and matches it with the \dQuote{events} object. One can use
   \code{drop = FALSE} with \sQuote{[} to achieve a single-column object.
 }
@@ -46,24 +55,29 @@
 \value{
   Returns a \sQuote{list} of two elements:
 
-  \item{z.e}{a zoo object indexed with event time; \dQuote{NULL} if
-  there are no \dQuote{success} \dQuote{outcomes}.}
+  \item{z.e}{a \pkg{zoo}/\pkg{xts} object indexed with event time; \dQuote{NULL} if
+  there are no \dQuote{success} in \dQuote{outcomes}.}
 
-  \item{outcomes}{a character vector with outcome of each event date:}
-
+  \item{outcomes}{a character vector with outcome definition for each event.}
   \itemize{
-    \item{success: shows the successful use of event date.}
-    \item{wdatamissing: appears when width data is missing around the event.}
-    \item{wrongspan: if event date falls outside the range of physical date.}
-    \item{unitmissing: when the unit (firm name) is missing in the event list.}
+    \item{success: the successful use of an event.}
+    \item{wdatamissing: when the \dQuote{width} condition is not satisfied.}
+    \item{wrongspan: when event date cannot be mapped to the physical
+      time in \dQuote{z}.}
+    \item{unitmissing: when the unit (a column) is missing in \dQuote{z}.}
   }
 }
 
+\seealso{
+  \code{\link{findInterval}}
+}
+
 \examples{
 data(StockPriceReturns)
 data(SplitDates)
-result <- phys2eventtime(z = StockPriceReturns, events = SplitDates, width = 5)
+result <- phys2eventtime(z = StockPriceReturns,
+			 events = SplitDates,
+			 width = 5)
 }
 
-\keyword{ phys2eventime }
-
+\keyword{phys2eventime}



More information about the Eventstudies-commits mailing list