From noreply at r-forge.r-project.org Thu Mar 12 21:31:22 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Thu, 12 Mar 2015 21:31:22 +0100 (CET) Subject: [Eventstudies-commits] r406 - pkg/inst/tests Message-ID: <20150312203122.BB68018790A@r-forge.r-project.org> Author: sargam_jain Date: 2015-03-12 21:31:22 +0100 (Thu, 12 Mar 2015) New Revision: 406 Modified: pkg/inst/tests/test_compilation.txt Log: Added more possible test cases. Modified: pkg/inst/tests/test_compilation.txt =================================================================== --- pkg/inst/tests/test_compilation.txt 2015-02-26 08:07:05 UTC (rev 405) +++ pkg/inst/tests/test_compilation.txt 2015-03-12 20:31:22 UTC (rev 406) @@ -21,20 +21,13 @@ b) Check for following when zoo object with firm data and dates as index is a univariate series: # If univariate series is input as vector (not matrix), then the function fails. c) Check for following values for z (zoo object): - # Normal values: - * ? - * ? # Extreme values: - * ? - * ? + * NA values as index of zoo object d) Check for following values for event.list: - # Normal values: - * ? - * ? # Extreme values: - * ? - * ? - + * NA values in dates column + * NA values in names column + ****************** Models ******************** 2. Function marketModel @@ -90,8 +83,9 @@ * Inf/ -Inf b) Check for following values of es.w: # Extreme values: - * ? - * ? + * Only one row in zoo object, es.w (i.e. only one observation for each firm which is corresponding to event date) + * Only one column in zoo object, es.w (i.e. data input is only for one firm) + 10. Function inference.wilcox a) Check for following values of prob: # Normal values: @@ -100,8 +94,8 @@ * Any value less than 0 and greater than 1 b) Check for following values of es.w: # Extreme values: - * ? - * ? + * Only one row in zoo object, es.w (i.e. only one observation for each firm which is corresponding to event date) + * Only one column in zoo object, es.w (i.e. data input is only for one firm) ************************************************************************************************************ @@ -110,15 +104,22 @@ 1. Function phys2eventime a) Check for list components returned by the function + * Check for the structure of `z.e'. It should not be a transpose + (of the format in which it goes in inference.bootstrap()) b) Check for firm selected should not have NA in data for defined width c) Check for class of arguments d) Check for if the function handles following data discrepancies in events: * Missing dates in eventlist * Missing firm in eventlist e) When zoo object with firm data and dates as index is a univariate series, - function must work if its a matrix of one column. + function must work if its a matrix of one column (i.e. only one + observation for each firm which is corresponding to event date), or + if it is a matrix of one column (i.e. data input is only for one + firm). + f) Check for only one observation in data frame, event.list + (i.e. only one firm has an observed event) + - ****************** Models ******************** 2. Function marketModel @@ -144,6 +145,10 @@ ************* Inference procedures *********** 8. Function inference.bootstrap @think and read + a) Check the structure of es.w given as argument (it should be zoo + object indexed by event time and the `z.e' component of the list + returned by the ?phys2eventtime? function. + * Check the structure of es$z.e given as argument 9. Function inference.wilcox @think and read @@ -160,7 +165,7 @@ * Any value greater than no. of rows of firm.returns and less than zero b) Check that event.window does not overlap with estimation window (i.e. firm returns used to estimate market returns through any of the described models. - + c) ***************** Models ********************* 10. Check the function for all the models using small data sets: From noreply at r-forge.r-project.org Fri Mar 13 08:17:59 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 13 Mar 2015 08:17:59 +0100 (CET) Subject: [Eventstudies-commits] r407 - pkg/inst/tests Message-ID: <20150313071759.64EEE185763@r-forge.r-project.org> Author: chiraganand Date: 2015-03-13 08:17:59 +0100 (Fri, 13 Mar 2015) New Revision: 407 Modified: pkg/inst/tests/test_compilation.txt Log: Added markers for tests which need to be coded for this version. Modified: pkg/inst/tests/test_compilation.txt =================================================================== --- pkg/inst/tests/test_compilation.txt 2015-03-12 20:31:22 UTC (rev 406) +++ pkg/inst/tests/test_compilation.txt 2015-03-13 07:17:59 UTC (rev 407) @@ -17,8 +17,8 @@ * zero * +Inf * -Inf - * Values greater than the no. of rows in dataframe StockPriceReturns - b) Check for following when zoo object with firm data and dates as index is a univariate series: +> * Values greater than the no. of rows in dataframe StockPriceReturns +> b) Check for following when zoo object with firm data and dates as index is a univariate series: # If univariate series is input as vector (not matrix), then the function fails. c) Check for following values for z (zoo object): # Extreme values: @@ -31,40 +31,40 @@ ****************** Models ******************** 2. Function marketModel - a) Check whether: # of rows in df(firm returns) = # of rows in df(market returns) +> a) Check whether: # of rows in df(firm returns) = # of rows in df(market returns) b) Check for extreme values of no. of rows and columns i.e. 1 row or 1 col in dataset - c) Check for residuals = TRUE (whether data is in returns or price format) - d) Check for class of arguments +> c) Check for residuals = TRUE (whether data is in returns or price format) +> d) Check for class of arguments 3. Function excessReturn - a) Check whether: # of rows in df(firm returns) = # of rows in df(market returns) +> a) Check whether: # of rows in df(firm returns) = # of rows in df(market returns) b) Check for extreme values of no. of rows and columns i.e. 1 row or 1 col in dataset - c) Check for class of arguments +> c) Check for class of arguments 4. Function lmAMM - a) Check for whether X is the output of makeX or not (read makeX manual) - b) Check for other logical arguments (those with TRUE/FALSE) +> a) Check for whether X is the output of makeX or not (read makeX manual) +> b) Check for other logical arguments (those with TRUE/FALSE) 5. Function None - a) Check that no model arguments should be specified +> a) Check that no model arguments should be specified ************** Remap functions ************** 6. Function remap.cumsum - a) Check for is.pc = TRUE (whether data is in percentage) +> a) Check for is.pc = TRUE (whether data is in percentage) b) Check for following values of base: - # Normal values: +> # Normal values: * 0 * 100 # Extreme values: * -Inf * Inf 7. Function remap.cumprod - a) Check for is.pc = TRUE (whether data is in percentage) - b) Check for is.pc = TRUE (whether data is in returns or price format) +> a) Check for is.pc = TRUE (whether data is in percentage) +> b) Check for is.pc = TRUE (whether data is in returns or price format) c) Check for following values of base: - # Normal values: +> # Normal values: * 0 * 100 # Extreme values: @@ -74,7 +74,7 @@ ************* Inference procedures *********** 8. Function inference.bootstrap - a) Check for following values of boot.run: +> a) Check for following values of boot.run: # Normal values: * 1000 * 10 @@ -84,18 +84,18 @@ b) Check for following values of es.w: # Extreme values: * Only one row in zoo object, es.w (i.e. only one observation for each firm which is corresponding to event date) - * Only one column in zoo object, es.w (i.e. data input is only for one firm) +> * Only one column in zoo object, es.w (i.e. data input is only for one firm) 10. Function inference.wilcox a) Check for following values of prob: - # Normal values: +> # Normal values: * .10 # Extreme values: * Any value less than 0 and greater than 1 b) Check for following values of es.w: # Extreme values: * Only one row in zoo object, es.w (i.e. only one observation for each firm which is corresponding to event date) - * Only one column in zoo object, es.w (i.e. data input is only for one firm) +> * Only one column in zoo object, es.w (i.e. data input is only for one firm) ************************************************************************************************************ @@ -103,20 +103,20 @@ II. TESTS FOR FUNCTIONALITY 1. Function phys2eventime - a) Check for list components returned by the function + > a) Check for list components returned by the function * Check for the structure of `z.e'. It should not be a transpose (of the format in which it goes in inference.bootstrap()) - b) Check for firm selected should not have NA in data for defined width - c) Check for class of arguments - d) Check for if the function handles following data discrepancies in events: +> b) Check for firm selected should not have NA in data for defined width +> c) Check for class of arguments +> d) Check for if the function handles following data discrepancies in events: * Missing dates in eventlist * Missing firm in eventlist - e) When zoo object with firm data and dates as index is a univariate series, +> e) When zoo object with firm data and dates as index is a univariate series, function must work if its a matrix of one column (i.e. only one observation for each firm which is corresponding to event date), or if it is a matrix of one column (i.e. data input is only for one firm). - f) Check for only one observation in data frame, event.list +> f) Check for only one observation in data frame, event.list (i.e. only one firm has an observed event) @@ -129,7 +129,7 @@ a) Check with small sample data whether the result is correct or not. 4. Function lmaMM - a) Check for class of arguments +L a) Check for class of arguments b) Check the working and results of regression analysis manually: difficult @leave this for now @ remind to come up with test cases for lmAMM @@ -137,17 +137,17 @@ ************** Remap functions ************** 6. Function remap.cumsum - a) Cumulate by summing and tally +> a) Cumulate by summing and tally 7. Function remap.cumprod - a) Cumulate by multiplying and tally +> a) Cumulate by multiplying and tally ************* Inference procedures *********** 8. Function inference.bootstrap @think and read - a) Check the structure of es.w given as argument (it should be zoo +> a) Check the structure of es.w given as argument (it should be zoo object indexed by event time and the `z.e' component of the list - returned by the ?phys2eventtime? function. + returned by the "phys2eventtime" function. * Check the structure of es$z.e given as argument 9. Function inference.wilcox @think and read @@ -157,19 +157,19 @@ III. TESTS FOR AGGREGATE FUNCTION (eventstudy) 10. Check the following for argument, event.window: - # Normal values: +> # Normal values: * Any value less than no. of rows of firm.returns and greater than zero # Extreme values: * 0 * Inf/ -Inf * Any value greater than no. of rows of firm.returns and less than zero - b) Check that event.window does not overlap with estimation window (i.e. firm returns +> b) Check that event.window does not overlap with estimation window (i.e. firm returns used to estimate market returns through any of the described models. c) ***************** Models ********************* 10. Check the function for all the models using small data sets: - a) Check for the argument, type: by default, model type (argument in eventstudy +> a) Check for the argument, type: by default, model type (argument in eventstudy function) should be market model, else specify the type as any of the following: # none # lmAMM @@ -178,13 +178,13 @@ ***************** Remap functions ************ 11. Check the function for logical argument to.remap (taking values TRUE/ FALSE): - a) for cumsum - b) for cumprod +> a) for cumsum +> b) for cumprod ************* Inference procedures *********** -12. Check the function for logical argument inference (taking values TRUE/ FALSE): -13. Check the function for the argument inference.strategy taking following values: +> 12. Check the function for logical argument inference (taking values TRUE/ FALSE): +> 13. Check the function for the argument inference.strategy taking following values: a) bootstrap b) wilcoxin From noreply at r-forge.r-project.org Fri Mar 13 11:46:57 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 13 Mar 2015 11:46:57 +0100 (CET) Subject: [Eventstudies-commits] r408 - pkg/man Message-ID: <20150313104657.460BB187168@r-forge.r-project.org> Author: chiraganand Date: 2015-03-13 11:46:56 +0100 (Fri, 13 Mar 2015) New Revision: 408 Modified: pkg/man/phys2eventtime.Rd Log: Documented colnames of z.e. Modified: pkg/man/phys2eventtime.Rd =================================================================== --- pkg/man/phys2eventtime.Rd 2015-03-13 07:17:59 UTC (rev 407) +++ pkg/man/phys2eventtime.Rd 2015-03-13 10:46:56 UTC (rev 408) @@ -56,8 +56,10 @@ \value{ Returns a \sQuote{list} of two elements: - \item{z.e}{a \pkg{zoo} object indexed with event time; \dQuote{NULL} if - there are no \dQuote{success} in \dQuote{outcomes}.} + \item{z.e}{a \pkg{zoo} object containing data of successful events + indexed with event time; \dQuote{NULL} if there are no + \dQuote{success} in \dQuote{outcomes}. \sQuote{colnames} of z.e are + event numbers: row numbers of \dQuote{events}.} \item{outcomes}{a character vector with outcome definition for each event.} \itemize{ From noreply at r-forge.r-project.org Fri Mar 13 12:33:00 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 13 Mar 2015 12:33:00 +0100 (CET) Subject: [Eventstudies-commits] r409 - in pkg: R man Message-ID: <20150313113300.8C1D1187861@r-forge.r-project.org> Author: chiraganand Date: 2015-03-13 12:33:00 +0100 (Fri, 13 Mar 2015) New Revision: 409 Modified: pkg/R/eventstudy.R pkg/man/eventstudy.Rd Log: Updated documentation. Added explanation of estimation data missing, event period, and the whole event study process. Modified: pkg/R/eventstudy.R =================================================================== --- pkg/R/eventstudy.R 2015-03-13 10:46:56 UTC (rev 408) +++ pkg/R/eventstudy.R 2015-03-13 11:33:00 UTC (rev 409) @@ -33,7 +33,7 @@ stopifnot(!is.null(remap)) # compute estimation and event period - ## :DOC: event period starts from event time + 1 + # event period starts from event time + 1 event.period <- as.character((-event.window + 1):event.window) ### Run models @@ -109,7 +109,7 @@ null.values <- sapply(outputModel, is.null) if (length(which(null.values)) > 0) { outputModel <- outputModel[names(which(!null.values))] - outcomes[names(which(null.values))] <- "edatamissing" #:DOC: edatamissing: estimation data missing + outcomes[names(which(null.values))] <- "edatamissing" # estimation data missing } if (length(outputModel) == 0) { @@ -253,7 +253,7 @@ } ## end None - if (is.null(outputModel)) { #:DOC + if (is.null(outputModel)) { final.result <- list(result = NULL, outcomes = as.character(outcomes)) class(final.result) <- "es" @@ -298,7 +298,7 @@ final.result <- list(result = outputModel, outcomes = as.character(outcomes)) - if (exists("outputResiduals")) { # :DOC + if (exists("outputResiduals")) { attr(final.result, which = "model.residuals") <- outputResiduals } attr(final.result, which = "event.window") <- event.window @@ -324,7 +324,7 @@ returns.zoo <- lapply(1:nrow(event.list), function(i) { cat("i:", i, "\n") firm.name <- event.list[i, "name"] - ## :DOC:to pick out the common dates of data. can't work on + ## to pick out the common dates of data. can't work on ## event time if the dates of data do not match before ## converting to event time. # all = FALSE: pick up dates Modified: pkg/man/eventstudy.Rd =================================================================== --- pkg/man/eventstudy.Rd 2015-03-13 10:46:56 UTC (rev 408) +++ pkg/man/eventstudy.Rd 2015-03-13 11:33:00 UTC (rev 409) @@ -36,7 +36,8 @@ } \item{event.window}{an \sQuote{integer} of length 1 that specifies a - symmetric event window around the event date. + symmetric event window around the event time as specified in the + \code{index} of \dQuote{firm.returns}. } \item{type}{ @@ -82,9 +83,44 @@ } } -\details{ - \dQuote{firm.returns} can contain only one series. To study a single - series, use \sQuote{[} with \code{drop = FALSE} for subsetting the +\details{ This function is used to conduct event study analysis acting + as a wrapper over the functionality provided in the \pkg{eventstudies} + package. It provides an interface to select and control the process of + event study analysis. It includes choice of the statistical model for + doing in-sample estimation and computing coefficients, choice of + cumulative returns, and selection of inference procedure. Process used + to conduct a study is detailed below: + + \enumerate{ + \item{event.period: is defined as (-event.window, event.window].} + + \item{estimation.period: If \dQuote{type} is specified, then + estimation.period is calculated for each firm-event in + \dQuote{event.list}, starting from the start of the data span till + the start of event period (inclusive).} + + \item{For each firm-event, firm returns and other returns (as + applicable) are converted to event time using + \sQuote{phys2eventtime}. Data is merged using \sQuote{merge.zoo} + to make sure the indexes are consistent before conversion to event + time.} + + \item{The selected model \dQuote{type} is run on the series indexed + by event time and abnormal returns are computed.} + + \item{NULL values because of estimation data missing are removed + from the output and \dQuote{outcomes} object is updated with + \dQuote{edatamissing}.} + + \item{Remapping is done if \dQuote{to.remap} is TRUE using + value of \dQuote{remap} argument.} + + \item{Inference is done if \dQuote{inference} is TRUE using the + technique mentioned in \dQuote{inference.strategy}.} + } + + \dQuote{firm.returns} can contain a single series also. To study a single + series, use \sQuote{[} with \code{drop = FALSE} to subset the data set. See \code{\link{phys2eventtime}} for more details. \sQuote{NA} values in the returns data are converted to \code{0}. @@ -179,18 +215,30 @@ event. This will not appear when this function is used since the argument \sQuote{width} in \code{\link{phys2eventtime}} is set to 0.} \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{unitmissing: when the unit (firm name) is missing in the + event list.} + \item{edatamissing: when there is insufficient data to do model + estimation.} } } } The returned object contains input information in other attributes: \itemize{ + \item{\dQuote{model.residuals}:}{ + a \sQuote{list} of residual series as returned by the selected + model. For models which do not compute residuals, this attribute is + not returned.} + \item{\dQuote{inference}:}{ a \sQuote{character} providing information about which inference strategy was utilised to estimate the confidence intervals. } + \item{\dQuote{inference.strategy}:}{ + a \sQuote{character} providing the name of the selected model. + } + \item{\dQuote{event.window}:}{ a \sQuote{numeric} specifying the window width for event study output. } @@ -267,7 +315,7 @@ others = OtherReturns[, "USDINR"], market.returns.purge = TRUE, nlag.makeX = 5, - nlag.lmAMM = NULL + nlag.lmAMM = 5 ) ) str(es) From noreply at r-forge.r-project.org Fri Mar 13 12:34:08 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 13 Mar 2015 12:34:08 +0100 (CET) Subject: [Eventstudies-commits] r410 - pkg/man Message-ID: <20150313113408.CEE6B185F06@r-forge.r-project.org> Author: chiraganand Date: 2015-03-13 12:34:08 +0100 (Fri, 13 Mar 2015) New Revision: 410 Modified: pkg/man/lmAMM.Rd Log: Modified the value of nlags in the example. Modified: pkg/man/lmAMM.Rd =================================================================== --- pkg/man/lmAMM.Rd 2015-03-13 11:33:00 UTC (rev 409) +++ pkg/man/lmAMM.Rd 2015-03-13 11:34:08 UTC (rev 410) @@ -88,9 +88,10 @@ others = currency.returns, switch.to.innov = FALSE, market.returns.purge = FALSE, + nlags = 4, verbose = FALSE) -amm.result <- lmAMM(firm.returns, X, nlags = 0, verbose = FALSE) +amm.result <- lmAMM(firm.returns, X, nlags = 3, verbose = FALSE) plot(amm.result) amm.residual <- residuals(amm.result) From noreply at r-forge.r-project.org Fri Mar 13 12:35:11 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 13 Mar 2015 12:35:11 +0100 (CET) Subject: [Eventstudies-commits] r411 - pkg/man Message-ID: <20150313113511.CFEF218780B@r-forge.r-project.org> Author: chiraganand Date: 2015-03-13 12:35:11 +0100 (Fri, 13 Mar 2015) New Revision: 411 Modified: pkg/man/manyfirmssubperiod.lmAMM.Rd Log: Modifed dates in the example, fixed package data loading. Modified: pkg/man/manyfirmssubperiod.lmAMM.Rd =================================================================== --- pkg/man/manyfirmssubperiod.lmAMM.Rd 2015-03-13 11:34:08 UTC (rev 410) +++ pkg/man/manyfirmssubperiod.lmAMM.Rd 2015-03-13 11:35:11 UTC (rev 411) @@ -59,8 +59,8 @@ } \examples{ -data("StockPriceReturns") -data("OtherReturns") +data("StockPriceReturns", package = "eventstudies") +data("OtherReturns", package = "eventstudies") firm.returns <- StockPriceReturns[, c("Infosys","TCS")] market.returns <- OtherReturns$NiftyIndex @@ -72,12 +72,12 @@ switch.to.innov = FALSE, market.returns.purge = FALSE, verbose = FALSE, - dates = as.Date(c("2010-07-01", "2011-11-17", "2013-03-29"))) + dates = as.Date(c("2010-07-01", "2011-11-17", "2013-03-28"))) res <- manyfirmssubperiod.lmAMM(firm.returns = firm.returns, X = X, lags = 1, - dates = as.Date(c("2010-07-01", "2011-11-17", "2013-03-29")), + dates = as.Date(c("2010-07-01", "2011-11-17", "2013-03-28")), periodnames = c("P1", "P2"), verbose = FALSE) print(res) From noreply at r-forge.r-project.org Tue Mar 31 18:39:09 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 31 Mar 2015 18:39:09 +0200 (CEST) Subject: [Eventstudies-commits] r412 - in pkg: R inst/tests Message-ID: <20150331163910.02F5E18059E@r-forge.r-project.org> Author: sargam_jain Date: 2015-03-31 18:39:09 +0200 (Tue, 31 Mar 2015) New Revision: 412 Added: pkg/inst/tests/test_functionality_excessReturn.R pkg/inst/tests/test_functionality_phys2eventtime.R pkg/inst/tests/test_userinput_excessReturn.R pkg/inst/tests/test_userinput_phys2eventtime.R Modified: pkg/R/excessReturn.R pkg/R/inference.bootstrap.R pkg/R/phys2eventtime.R pkg/inst/tests/test_compilation.txt Log: Added the test cases for 'excessReturn' and 'phys2eventtime'. Made changes in functions code to include: 1. argument class checks, 2. univariate series in zoo object, 3. equal dimension (row) of zoo objects in 'excessReturn'. Modified: pkg/R/excessReturn.R =================================================================== --- pkg/R/excessReturn.R 2015-03-13 11:35:11 UTC (rev 411) +++ pkg/R/excessReturn.R 2015-03-31 16:39:09 UTC (rev 412) @@ -1,3 +1,9 @@ excessReturn <- function(firm.returns, market.returns) { - return(firm.returns - market.returns) + stopifnot(NROW(firm.returns) == NROW(market.returns)) + + stopifnot(class(firm.returns)=="zoo" || class(firm.returns)=="xts") + + stopifnot(class(market.returns)=="zoo" || class(market.returns)=="xts") + + return(firm.returns - market.returns) } Modified: pkg/R/inference.bootstrap.R =================================================================== --- pkg/R/inference.bootstrap.R 2015-03-13 11:35:11 UTC (rev 411) +++ pkg/R/inference.bootstrap.R 2015-03-31 16:39:09 UTC (rev 412) @@ -61,7 +61,7 @@ results <- NULL for (i in 1:ncol(b$t)) { - results <- rbind(results, quantile(b$t[,i], prob=c(.025,.975))) + results <- rbind(results, quantile(b$t[,i], prob=c(.025,.975), na.rm=TRUE)) } results <- cbind(results[,1], b$t0, results[,2]) rownames(results) <- rownames(es.w) Modified: pkg/R/phys2eventtime.R =================================================================== --- pkg/R/phys2eventtime.R 2015-03-13 11:35:11 UTC (rev 411) +++ pkg/R/phys2eventtime.R 2015-03-31 16:39:09 UTC (rev 412) @@ -12,25 +12,36 @@ # A vector of these outcomes is returned. phys2eventtime <- function(z, events, width=10) { - if (is.null(ncol(z))) { - stop(paste("'z' should be of class zoo/xts with at least one column. Use '[' with drop = FALSE")) - } - if (!any(class(events$when) %in% c("POSIXt", "Date"))) { - stop("events$when should be one of 'Date' or 'date-time' classes.") - } - if (!is.character(events$name)) { - stop("events$name should a character class.") - } - answer <- lapply(1:nrow(events), function(i) timeshift(events[i, ], z)) - outcomes <- sapply(answer, function(x) x$outcome) - z.e <- do.call(cbind, lapply(answer[outcomes == "success"], function(x) x$result)) + stopifnot(class(events)=="data.frame") + stopifnot(class(z)=="zoo" || class(z)=="xts") + + if (is.null(ncol(z))) { + stop(paste("'z' should be of class zoo/xts with at least one column. Use '[' with drop = FALSE")) + } + if (!any(class(events$when) %in% c("POSIXt", "Date"))) { + stop("events$when should be one of 'Date' or 'date-time' classes.") + } + if (any(is.na(events$when))) { + stop("events$when should not contain NA values.") + } + if (any(is.na(events$name))) { + stop("events$name should not contain NA values.") + } + + if (!is.character(events$name)) { + stop("events$name should a character class.") + } + answer <- lapply(1:nrow(events), function(i) timeshift(events[i, ], z)) + outcomes <- sapply(answer, function(x) x$outcome) + z.e <- do.call(cbind, lapply(answer[outcomes == "success"], function(x) x$result)) + ## If no successful outcome, return NULL to z.e. - if (length(z.e) == 0) { - return(list(z.e = NULL, outcomes = factor(outcomes))) + if (length(z.e) == 0) { + return(list(z.e = NULL, outcomes = factor(outcomes))) } - + colnames(z.e) <- which(outcomes == "success") ## :DOC events.attrib <- do.call(c, lapply(answer[outcomes == "success"], function(x) x$event)) Modified: pkg/inst/tests/test_compilation.txt =================================================================== --- pkg/inst/tests/test_compilation.txt 2015-03-13 11:35:11 UTC (rev 411) +++ pkg/inst/tests/test_compilation.txt 2015-03-31 16:39:09 UTC (rev 412) @@ -1,7 +1,7 @@ ###### Testing functions in eventstudies package ##### Tests are categorized as follows: -I. Tests for argument inputs (context: user input) +I. Tests for argument inputs (context: userinput) II. Tests for functionality (context: functionality) III. Tests for aggregate function (context: aggregate) IV. Example from book as test case (context: example) @@ -17,8 +17,7 @@ * zero * +Inf * -Inf -> * Values greater than the no. of rows in dataframe StockPriceReturns -> b) Check for following when zoo object with firm data and dates as index is a univariate series: +# b) Check for following when zoo object with firm data and dates as index is a univariate series: # If univariate series is input as vector (not matrix), then the function fails. c) Check for following values for z (zoo object): # Extreme values: @@ -39,7 +38,7 @@ 3. Function excessReturn > a) Check whether: # of rows in df(firm returns) = # of rows in df(market returns) b) Check for extreme values of no. of rows and columns i.e. 1 row or 1 col in dataset -> c) Check for class of arguments + 4. Function lmAMM > a) Check for whether X is the output of makeX or not (read makeX manual) @@ -103,20 +102,18 @@ II. TESTS FOR FUNCTIONALITY 1. Function phys2eventime - > a) Check for list components returned by the function +# a) Check for list components returned by the function * Check for the structure of `z.e'. It should not be a transpose (of the format in which it goes in inference.bootstrap()) -> b) Check for firm selected should not have NA in data for defined width -> c) Check for class of arguments -> d) Check for if the function handles following data discrepancies in events: +# b) Check for firm selected should not have NA in data for defined width +# c) Check for class of arguments +# d) Check for if the function handles following data discrepancies in events: * Missing dates in eventlist * Missing firm in eventlist -> e) When zoo object with firm data and dates as index is a univariate series, - function must work if its a matrix of one column (i.e. only one - observation for each firm which is corresponding to event date), or - if it is a matrix of one column (i.e. data input is only for one +# e) When zoo object with firm data and dates as index is a univariate series, + function must work if its a matrix of one column (i.e. data input is only for one firm). -> f) Check for only one observation in data frame, event.list +# f) Check for only one observation in data frame, event.list (i.e. only one firm has an observed event) @@ -127,7 +124,8 @@ 3. Function excessReturn a) Check with small sample data whether the result is correct or not. - + b) Check for class of arguments + 4. Function lmaMM L a) Check for class of arguments b) Check the working and results of regression analysis manually: difficult @leave this for now Added: pkg/inst/tests/test_functionality_excessReturn.R =================================================================== --- pkg/inst/tests/test_functionality_excessReturn.R (rev 0) +++ pkg/inst/tests/test_functionality_excessReturn.R 2015-03-31 16:39:09 UTC (rev 412) @@ -0,0 +1,84 @@ +library(testthat) +context("functionality") + +## 1. Test for class of arguments + +test <- that("functionality for excessReturn", { + library(eventstudies) + ## Data of stock prices and returns + test.firm <- structure(c(33.16, 34.0967, 35.3683, 34.46, 34.17, + 35.89, 36.19, 37.1317, 36.7033, 37.7933, + 37.8533, 285.325, 292.6, 290.025, 286.2, + 290.075, 295.05, 289.325, 285.625, 293.7, + 298.5, 289.05, 704.5438, 708.35, 735.835, + 710.6, 711.65, 731.012, 727.57, 715.0187, + 724.2, 713.1875, 695.1812), + .Dim = c(11L, 3L), + .Dimnames = list( NULL, c("ITC", + "Reliance", "TCS")), + index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "zoo") + test.firm <- diff(log(test.firm)) + + test.firm1 <- structure(c(33.16, 34.0967, 35.3683, 34.46, 34.17, + 35.89, 36.19, 37.1317, 36.7033, 37.7933, + 37.8533, 285.325, 292.6, 290.025, 286.2, + 290.075, 295.05, 289.325, 285.625, 293.7, + 298.5, 289.05, 704.5438, 708.35, 735.8375, + 710.625, 711.65, 731.013, 727.575, 715.01, + 724.2, 713.1875, 695.1812), + .Dim = c(11L, 3L), + .Dimnames = list( NULL, c("ITC", "Reliance", + "TCS")), index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "matrix") + test.firm1 <- diff(log(test.firm1)) + + ## Data for market prices and returns + test.market <- structure(c(285.546, 265.566, 290.025, 288.2, + 295.677, 298.990, 279.32, 286.62, + 296.7, 288.5, 284.05), + .Dim = c(11L, 1L), + .Dimnames = list( NULL, c("MarketIndex")), + index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "zoo") + test.market <- diff(log(test.market)) + + test.market1 <- structure(c(285.546, 265.566, 290.025, 288.2, + 295.677, 298.990, 279.32, 286.62, + 296.7, 288.5, 284.05), + .Dim = c(11L, 1L), + .Dimnames = list( NULL, c("MarketIndex")), + index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "matrix") + test.market1 <- diff(log(test.market1)) + + + ### Testing the class of arguments + cat("\nTesting for class of arguments input") + + er.testResult1 <- excessReturn(firm.returns = test.firm, + market.returns = test.market[,1]) + + er.testResult2 <- excessReturn(firm.returns = test.firm1, + market.returns = test.market1[,1]) + + + + + Added: pkg/inst/tests/test_functionality_phys2eventtime.R =================================================================== --- pkg/inst/tests/test_functionality_phys2eventtime.R (rev 0) +++ pkg/inst/tests/test_functionality_phys2eventtime.R 2015-03-31 16:39:09 UTC (rev 412) @@ -0,0 +1,139 @@ +library(testthat) +context("functionality") + +# 1. Test for class of arguments +# 2. Test for missing data in eventlist +# a. NAs in firm names +# b. NAs in event dates +# 3. Testing the functionality of "phys2eventtime" for components of +# the list returned from the function: +# a. Elements in outcomes +# b. Elements in z.e +# 4. Testing that firms should not have NA for defined width of +# phys2eventtime +# 5. Test for only one observation in events list. +test_that("functionality for phys2eventtime", { + library(eventstudies) + +### Data for testing ### + + test.data <- structure(c(33.16, 34.0967, 35.3683, 34.46, 34.17, + 35.89, 36.19, 37.1317, 36.7033, 37.7933, + 37.8533, 285.325, 292.6, 290.025, 286.2, + 290.075, 295.05, 289.325, 285.625, 293.7, + 298.5, 289.05, 704.5438, 708.35, 735.8375, + 710.625, 711.65, 731.0125, 727.575, 715.01, + 724.2, 713.1875, 695.1812), + .Dim = c(11L, 3L), + .Dimnames = list( NULL, c("ITC", "Reliance", + "TCS")), index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "zoo") + +### List of events + + test.eventslist <- data.frame(name=c("ITC","Reliance","TCS", + "ITC","Reliance","Junk"), + when=as.Date(c("2004-01-02", + "2004-01-08", "2004-01-14", + "2005-01-15", "2004-01-01", + "2005-01-01"))) + test.eventslist$name <- as.character(test.eventslist$name) + +### Test for class of arguments + + cat("\nTesting for class of arguments input") + + test.data1 <- structure(c(33.16, 34.0967, 35.3683, 34.46, 34.17, + 35.89, 36.19, 37.1317, 36.7033, 37.7933, + 37.8533, 285.325, 292.6, 290.025, 286.2, + 290.075, 295.05, 289.325, 285.625, 293.7, + 298.5, 289.05, 704.5438, 708.35, 735.8375, + 710.625, 711.65, 731.013, 727.575, 715.01, + 724.2, 713.1875, 695.1812), + .Dim = c(11L, 3L), + .Dimnames = list( NULL, c("ITC", "Reliance", + "TCS")), index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "matrix") + test.eventslist1 <- as.matrix(test.eventslist) + esConvertNormal0 <- phys2eventtime(z = test.data1, + events = test.eventslist, + width = 2) + + esConvertNormal0 <- phys2eventtime(z = test.data, + events = test.eventslist1, + width = 2) + +### Testing for missing data in eventslist + + cat("\nTesting for missing dates in eventslist") + test.eventslist2 <- data.frame(name=c("ITC","Reliance","TCS", + "ITC","Reliance","Junk"), + when=as.Date(c("2004-01-02", + "2004-01-08", "2004-01-14", + NA, "2004-01-01", + "2005-01-01"))) + test.eventslist2$name <- as.character(test.eventslist2$name) + esConvertNormal1 <- phys2eventtime(z = test.data, + events = test.eventslist2, + width = 2) + + cat("\nTesting for missing firm names in eventlist") + test.eventslist2 <- data.frame(name=c("ITC",NA,"TCS", + "ITC","Reliance","Junk"), + when=as.Date(c("2004-01-02", + "2004-01-08", "2004-01-14", + "2005-01-15", "2004-01-01", + "2005-01-01"))) + test.eventslist2$name <- as.character(test.eventslist2$name) + esConvertNormal2 <- phys2eventtime(z = test.data, + events = test.eventslist2, + width = 2) + +### Testing the function for outcomes + + cat("\nTesting for list component: outcomes") + esConvertNormal3 <- phys2eventtime(z = test.data, + events = test.eventslist, + width = 10) + expect_that(length(esConvertNormal3$outcomes), + equals(nrow(test.eventslist))) + + cat("\nTesting for list component: z.e") + analyseDate <- subset(test.eventslist, + test.eventslist$when >= index(test.data[1,])) + maxDate <- max(as.Date(analyseDate$when)) + minDate <- min(as.Date(analyseDate$when)) + l1 <- length(which(index(test.data) <= maxDate)) + l2 <- length(which(index(test.data) > minDate)) + elementsInz.e <- l1 + l2 + if(is.null(esConvertNormal3$z.e)) + {expect_that(nrow(esConvertNormal3$z.e), equals(NULL)) + }else{ + expect_that(nrow(esConvertNormal3$z.e), equals(elementsInz.e))} + +### Testing that firms should not have NA for defined width + + cat("\nTesting for no NA values in defined width") + esConvertNormal4 <- phys2eventtime(z = test.data, + events = test.eventslist, + width = 2) + expect_that(esConvertNormal4$z.e[8:12], not(equals(NA))) + + +### Testing for only one firm in eventslist + + test.eventslist3 <- test.eventslist[1,] + cat("\nTesting for only one firm data in events list") + esConvertNormal7 <- phys2eventtime(z = test.data2, + events = test.eventslist3, + width = 2) +}) + Added: pkg/inst/tests/test_userinput_excessReturn.R =================================================================== --- pkg/inst/tests/test_userinput_excessReturn.R (rev 0) +++ pkg/inst/tests/test_userinput_excessReturn.R 2015-03-31 16:39:09 UTC (rev 412) @@ -0,0 +1,67 @@ +library(testthat) +context("userinput") + +## 1. Test for number of rows in dataframe of firm returns and +## market returns should be equal. + +test_that("userinput for excessReturn", { + library(eventstudies) + ## Data of stock prices and returns + test.firm <- structure(c(33.16, 34.0967, 35.3683, 34.46, 34.17, + 35.89, 36.19, 37.1317, 36.7033, 37.7933, + 37.8533, 285.325, 292.6, 290.025, 286.2, + 290.075, 295.05, 289.325, 285.625, 293.7, + 298.5, 289.05, 704.5438, 708.35, 735.835, + 710.6, 711.65, 731.012, 727.57, 715.0187, + 724.2, 713.1875, 695.1812), + .Dim = c(11L, 3L), + .Dimnames = list( NULL, c("ITC", + "Reliance", "TCS")), + index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "zoo") + test.firm <- diff(log(test.firm)) + + + ## Data for market prices and returns + test.market <- structure(c(285.546, 265.566, 290.025, 288.2, + 295.677, 298.990, 279.32, 286.62, + 296.7, 288.5, 284.05), + .Dim = c(11L, 1L), + .Dimnames = list( NULL, c("MarketIndex")), + index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "zoo") + test.market <- diff(log(test.market)) + + ## List of events + test.eventslist <- data.frame(name=c("ITC","Reliance","TCS", + "ITC","Reliance","Junk"), + when=as.Date(c("2004-01-02", + "2004-01-08", "2004-01-14", + "2005-01-15", "2004-01-01", + "2005-01-01"))) + test.eventslist$name <- as.character(test.eventslist$name) + +### Testing the function for number of rows in dataframe of +### firm returns and market returns + + cat("\n Testing for no. of rows in firm returns and market returns") + test.market1 <- test.market[-1,] + er.testResult1 <- excessReturn(firm.returns = test.firm, + market.returns = test.market[,1]) + + + er.testResult2 <- excessReturn(firm.returns = test.firm, + market.returns = test.market1[,1]) + +}) + + + Added: pkg/inst/tests/test_userinput_phys2eventtime.R =================================================================== --- pkg/inst/tests/test_userinput_phys2eventtime.R (rev 0) +++ pkg/inst/tests/test_userinput_phys2eventtime.R 2015-03-31 16:39:09 UTC (rev 412) @@ -0,0 +1,61 @@ +library(testthat) +context("userinput") + +## 1. Test for normal values of width +## 2. Test for univariate zoo object: matrix and not a vector + + +test_that("userinput for phys2eventtime", { + library(eventstudies) +## Data of Stock Prices +test.data <- structure(c(33.16, 34.0967, 35.3683, 34.46, 34.17, + 35.89, 36.19, 37.1317, 36.7033, 37.7933, + 37.8533, 285.325, 292.6, 290.025, 286.2, + 290.075, 295.05, 289.325, 285.625, 293.7, + 298.5, 289.05, 704.5438, 708.35, 735.8375, + 710.625, 711.65, 731.0125, 727.575, 715.0187, + 724.2, 713.1875, 695.1812), + .Dim = c(11L, 3L), + .Dimnames = list( NULL, c("ITC", "Reliance", + "TCS")), index = structure(c(12418, + 12419, 12422, 12423, 12424, + 12425, 12426, 12429, 12430, + 12431, 12432), + class = "Date"), + class = "zoo") +## List of events +test.eventslist <- data.frame(name=c("ITC","Reliance","TCS", + "ITC","Reliance","Junk"), + when=as.Date(c("2004-01-02", + "2004-01-08", "2004-01-14", + "2005-01-15", "2004-01-01", + "2005-01-01"))) +test.eventslist$name <- as.character(test.eventslist$name) + + +### Testing function for normal values of width + + cat("\nTesting for normal input values") + esConvertNormal0 <- phys2eventtime(z = test.data, + events = test.eventslist, + width = 5) + cat("\nTesting for normal input values") + esConvertNormal1 <- phys2eventtime(z = test.data, + events = test.eventslist, + width = 10) + +### Testing for univariate zoo object: matrix and not a vector + + test.data2 <- test.data[,1, drop=FALSE] + test.data3 <- test.data[,1] + cat("\nTesting for univariate zoo object") + esConvertNormal2 <- phys2eventtime(z = test.data2, + events = test.eventslist, + width = 2) + + esConvertNormal3 <- phys2eventtime(z = test.data3, + events = test.eventslist, + width = 2) + +}) + From noreply at r-forge.r-project.org Tue Mar 31 21:18:21 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 31 Mar 2015 21:18:21 +0200 (CEST) Subject: [Eventstudies-commits] r413 - pkg/R Message-ID: <20150331191821.A87091876D6@r-forge.r-project.org> Author: chiraganand Date: 2015-03-31 21:18:21 +0200 (Tue, 31 Mar 2015) New Revision: 413 Modified: pkg/R/eventstudy.R Log: Fixed None output model, removed unnecessary cat statements. Modified: pkg/R/eventstudy.R =================================================================== --- pkg/R/eventstudy.R 2015-03-31 16:39:09 UTC (rev 412) +++ pkg/R/eventstudy.R 2015-03-31 19:18:21 UTC (rev 413) @@ -40,7 +40,6 @@ ## AMM if (type == "lmAMM") { - cat("preparing paramters\n") if (length(dim(model.args$market.returns)) == 2) { colnames(model.args$market.returns) <- "market.returns" # needed to fix market returns colname } @@ -126,7 +125,6 @@ ### marketModel if (type == "marketModel") { - cat("preparing paramters\n") if (length(dim(model.args$market.returns)) == 2) { colnames(model.args$market.returns) <- "market.returns" # needed to fix market returns colname } @@ -183,7 +181,6 @@ ### excessReturn if (type == "excessReturn") { - cat("preparing paramters\n") if (length(dim(model.args$market.returns)) == 2) { colnames(model.args$market.returns) <- "market.returns" # needed to fix market returns colname } @@ -245,10 +242,9 @@ inference = FALSE outputModel <- NULL } else { - returns.zoo <- returns.zoo[which(outcomes == "success")] - outputModel <- returns.zoo$z.e[event.period, ] - estimation.period <- as.character(index(returns.zoo$z.e)[1]:(-event.window)) - outputResiduals <- lapply(returns.zoo$z.e, '[', estimation.period) + returns.zoo <- returns.zoo$z.e[, as.character(which(outcomes == "success"))] + outputModel <- returns.zoo[event.period, ] + estimation.period <- as.character(index(returns.zoo)[1]:(-event.window)) } } ## end None @@ -322,7 +318,6 @@ if (length(other.returns.names) != 0) { # check for type = "None" returns.zoo <- lapply(1:nrow(event.list), function(i) { - cat("i:", i, "\n") firm.name <- event.list[i, "name"] ## to pick out the common dates of data. can't work on ## event time if the dates of data do not match before From noreply at r-forge.r-project.org Tue Mar 31 21:35:06 2015 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 31 Mar 2015 21:35:06 +0200 (CEST) Subject: [Eventstudies-commits] r414 - in pkg: R man Message-ID: <20150331193506.CA6B918783F@r-forge.r-project.org> Author: chiraganand Date: 2015-03-31 21:35:06 +0200 (Tue, 31 Mar 2015) New Revision: 414 Modified: pkg/R/marketModel.R pkg/man/marketModel.Rd Log: Fixed assignment of colnames, added details of NA handling. Modified: pkg/R/marketModel.R =================================================================== --- pkg/R/marketModel.R 2015-03-31 19:18:21 UTC (rev 413) +++ pkg/R/marketModel.R 2015-03-31 19:35:06 UTC (rev 414) @@ -5,7 +5,7 @@ if (NCOL(firm.returns) == 1) { merged.object <- merge.zoo(firm.returns, market.returns, all = TRUE) #:DOC reg <- lm(firm.returns ~ market.returns, data = merged.object, - na.action = na.exclude) # :DOC: na.exclude: NAs can + na.action = na.exclude) # na.exclude: NAs can # be seen in prediction if (residuals == TRUE) { @@ -19,12 +19,13 @@ reg <- list() resids <- list() - ## :DOC: we don't push the whole data.frame into lm() because it + ## we don't push the whole data.frame into lm() because it ## does na.omit, thereby removing rows from some firms even if ## they don't have NAs in them. for (i in 1:NCOL(firm.returns)) { + cat("i:", i, "\n") merged.object <- merge.zoo(firm.returns[, i], market.returns, all = TRUE) - colnames(merged.object)[i] <- "firm.returns" + colnames(merged.object)[1] <- "firm.returns" reg[[i]] <- lm(firm.returns ~ market.returns, data = merged.object, na.action = na.exclude) Modified: pkg/man/marketModel.Rd =================================================================== --- pkg/man/marketModel.Rd 2015-03-31 19:18:21 UTC (rev 413) +++ pkg/man/marketModel.Rd 2015-03-31 19:35:06 UTC (rev 414) @@ -4,7 +4,12 @@ \title{Extract residuals from a market model} \description{This function extracts residuals from a market model using - function \code{stats:lm}.} + function \code{stats:lm}. \sQuote{na.exclude} is passed as + \sQuote{na.action} for \sQuote{lm}. For more than one firm, the + function merges \dQuote{market.returns} with each element of + \dQuote{firm.returns} before passing to \sQuote{lm} so that rows of + firms without \sQuote{NA}s are not removed from the zoo object. +} \usage{marketModel(firm.returns, market.returns, residuals = TRUE)} @@ -21,7 +26,7 @@ \value{Residual returns unexplained by market index returns.} -\author{Vikram Bahure} +\author{Chirag Anand, Vikram Bahure} \examples{ data("StockPriceReturns") @@ -31,7 +36,7 @@ market.returns = OtherReturns$NiftyIndex, residuals = TRUE) -comparison <- merge(MarketResidual = mm.result$Infosys, +comparison <- merge(MarketModel = mm.result$Infosys, Infosys = StockPriceReturns$Infosys, NiftyIndex = OtherReturns$NiftyIndex, all = FALSE)