[Eventstudies-commits] r107 - in pkg: R man vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Aug 4 10:16:32 CEST 2013


Author: vikram
Date: 2013-08-04 10:16:32 +0200 (Sun, 04 Aug 2013)
New Revision: 107

Modified:
   pkg/R/AMM.R
   pkg/R/eventstudy.R
   pkg/R/remap.cumprod.R
   pkg/R/remap.cumsum.R
   pkg/R/remap.event.reindex.R
   pkg/man/onefirmAMM.Rd
   pkg/vignettes/eventstudies.Rnw
Log:
Modified AMM function to fit many firms, changed remap.cumsum to fit one column data; added no inference strategy ouput for one event date; Work in progress

Modified: pkg/R/AMM.R
===================================================================
--- pkg/R/AMM.R	2013-08-03 08:04:29 UTC (rev 106)
+++ pkg/R/AMM.R	2013-08-04 08:16:32 UTC (rev 107)
@@ -5,7 +5,7 @@
 AMM <- function(amm.type = NULL, ...) {
 
   ## List of models currently supported
-  modelsList <- c("onefirm","firmExposures")
+  modelsList <- c("onefirm","manyfirms","firmExposures")
 
   if (is.null(amm.type) || length(amm.type) != 1) {
     stop("Argument amm.type not provided or incorrect")
@@ -23,8 +23,6 @@
   switch.to.innov <- NULL
   verbose <- NULL
   dates <- NULL
-  regressand <- NULL
-  periodnames <- NULL
 
                                      # parse the input arguments for the model
   modelArgs <- list(...)
@@ -49,7 +47,7 @@
   
                                         # Checking remaining arguments
   if (match("nlags", names(modelArgs), nomatch = -1) == -1) {
-    nlags <- NA
+    nlags <- 1
   }
   if (match("verbose", names(modelArgs), nomatch = -1) == -1) {
     verbose <- FALSE
@@ -57,9 +55,6 @@
   if (match("dates", names(modelArgs), nomatch = -1) == -1) {
     dates <- NULL
   }
-  if (match("periodnames", names(modelArgs), nomatch = -1) == -1) {
-    periodnames <- NULL
-  }
 
   ## Assign values
   
@@ -75,8 +70,28 @@
     X <- makeX(rM1, others, switch.to.innov,
                rM1purge, nlags, dates, verbose)
     result <- onefirmAMM(rj, X, nlags, verbose, dates)
+    result <- result$residuals
   }
 
+  ##-----------
+  ## Many firms
+  ##-----------
+  if(amm.type == "manyfirms") {
+                                        # Checking required arguments
+    if (match("rj", names(modelArgs), nomatch = -1) == -1) {
+      stop("Input rj (firm data) is missing")
+    }
+    
+    X <- makeX(rM1, others, switch.to.innov,
+               rM1purge, nlags, dates, verbose)
+    result <- xts()
+    for(i in 1:NCOL(rj)){
+      tmp <- onefirmAMM(rj[,i], X, nlags, verbose, dates)
+      result <- merge(result,tmp$residuals)
+    }
+    colnames(result) <- colnames(rj)
+  }
+  
   ##---------------
   ## Firm exposures
   ##---------------
@@ -98,11 +113,19 @@
 #######################
 # AMM for one firm
 #######################
-onefirmAMM <- function(rj,X,nlags=NA,verbose=FALSE,dates=NULL,residual=TRUE){
-  exposures <- data.frame(matrix(NA,ncol=ncol(X),nrow=(length(dates)-1)))
+onefirmAMM <- function(rj,X,nlags=1,verbose=FALSE,dates=NULL,residual=TRUE){
+  ## Creating empty frames
+  if(is.null(dates)){
+    dates.no <- c(start(rj),end(rj))
+  } else{
+    dates.no <- dates
+  }
+  exposures <- data.frame(matrix(NA,ncol=ncol(X),nrow=(length(dates.no)-1)))
   colnames(exposures) <- colnames(X)
   sds <- exposures
   periodnames <- NULL
+  
+  ## Getting firm exposure, amm residuals
   if(is.null(dates)){
    res <- firmExposures(rj,X,verbose=verbose,nlags=nlags)
    exposures <- res$exposure

Modified: pkg/R/eventstudy.R
===================================================================
--- pkg/R/eventstudy.R	2013-08-03 08:04:29 UTC (rev 106)
+++ pkg/R/eventstudy.R	2013-08-04 08:16:32 UTC (rev 107)
@@ -30,11 +30,11 @@
   if (type == "AMM") {
     if(amm.type == "onefirm"){
       tmp.outputModel <- AMM(rj = inputData, ...)
-      outputModel <- zoo(tmp.outputModel$residual,index(tmp.outputModel))
+      outputModel <- zoo(coredata(tmp.outputModel),index(tmp.outputModel))
     }
     if(amm.type == "manyfirms"){
-      tmp.outputModel <- AMM(regressand = inputData, ...)
-      outputModel <- zoo(tmp.outputModel$residual,index(tmp.outputModel))
+      tmp.outputModel <- AMM(rj = inputData, ...)
+      outputModel <- zoo(coredata(tmp.outputModel),index(tmp.outputModel))
     }
     if(amm.type == "firmExposures"){
       stop("amm.type firmExposures not used for event study analysis")
@@ -51,11 +51,22 @@
   if (type == "excessReturn") {
     outputModel <- excessReturn(data.object = inputData, ...)
   }
-
+  ##Converting index outputModel to Date
+  index(outputModel) <- as.Date(index(outputModel))
+  
+  
 ### Convert to event frame
   es <- phys2eventtime(z=outputModel, events=eventList, width=width)
-  colnames(es) <- eventList[which(es$outcomes=="success"),1]
   es.w <- window(es$z.e, start = -width, end = width)
+  ## Adding column names to event output
+  cn.names <- eventList[which(es$outcomes=="success"),1]
+  if(length(cn.names)==1){
+    cat("Event date exists only for",cn.names,"\n")
+    inference <- FALSE
+    cat("No inference strategy for one column","\n")
+  } else {
+    colnames(es.w) <- cn.names
+  }
   
 ### Remapping event frame
   if (to.remap == TRUE) {

Modified: pkg/R/remap.cumprod.R
===================================================================
--- pkg/R/remap.cumprod.R	2013-08-03 08:04:29 UTC (rev 106)
+++ pkg/R/remap.cumprod.R	2013-08-04 08:16:32 UTC (rev 107)
@@ -6,7 +6,7 @@
 # is.returns is false                         in this case is.pc is ignored!
 #    values are like 1.01 for 1%
 remap.cumprod <- function(z, is.pc=TRUE, is.returns=TRUE, base=100) {
-  for (i in 1:ncol(z)) {
+  for (i in 1:NCOL(z)) {
     tmp <- z[,i]
     if (is.returns) {
       if (is.pc) {
@@ -15,7 +15,11 @@
       tmp <- 1+tmp
     }
     tmp[1] <- base
-    z[,i] <- cumprod(tmp)
+    if(NCOL(z)==1){
+      z <- cumprod(tmp)
+    } else {
+      z[,i] <- cumprod(tmp)
+    }
   }
   z
 }

Modified: pkg/R/remap.cumsum.R
===================================================================
--- pkg/R/remap.cumsum.R	2013-08-03 08:04:29 UTC (rev 106)
+++ pkg/R/remap.cumsum.R	2013-08-04 08:16:32 UTC (rev 107)
@@ -1,12 +1,16 @@
 
 # If is.pc then a value like "1" means 0.01
 remap.cumsum <- function(z, is.pc=TRUE, base=0) {
-  for (i in 1:ncol(z)) {
+  for (i in 1:NCOL(z)) {
     tmp <- z[,i]
     if (is.pc) {
       tmp <- tmp/100
     }
-    z[,i] <- base+cumsum(tmp)
+    if(NCOL(z)==1){
+      z <- tmp
+    } else {
+      z[,i] <- base+cumsum(tmp)
+    }
   }
   z
 }

Modified: pkg/R/remap.event.reindex.R
===================================================================
--- pkg/R/remap.event.reindex.R	2013-08-03 08:04:29 UTC (rev 106)
+++ pkg/R/remap.event.reindex.R	2013-08-04 08:16:32 UTC (rev 107)
@@ -5,8 +5,12 @@
 # values are scaled accordingly.
 remap.event.reindex <- function(z) {
   eventvals <- as.numeric(window(z, start=0, end=0))
-  for (i in 1:ncol(z)) {
-    z[,i] <- 100*z[,i]/eventvals[i]
+  for (i in 1:NCOL(z)) {
+    if(NCOL(z)==1){
+      z <- 100*z[,i]/eventvals[i]
+    } else {
+      z[,i] <- 100*z[,i]/eventvals[i]
+    }
   }
   z
 }

Modified: pkg/man/onefirmAMM.Rd
===================================================================
--- pkg/man/onefirmAMM.Rd	2013-08-03 08:04:29 UTC (rev 106)
+++ pkg/man/onefirmAMM.Rd	2013-08-04 08:16:32 UTC (rev 107)
@@ -56,7 +56,7 @@
 # Run AMM for one firm across different periods
 onefirmAMM(rj=Company_A,
             X=rhs.dat,
-            nlags=NA,
+            nlags=1,
             verbose=TRUE,
             dates= as.Date(c("2005-01-15","2006-01-07","2007-01-06",
                        "2008-01-05","2009-01-03")))

Modified: pkg/vignettes/eventstudies.Rnw
===================================================================
--- pkg/vignettes/eventstudies.Rnw	2013-08-03 08:04:29 UTC (rev 106)
+++ pkg/vignettes/eventstudies.Rnw	2013-08-04 08:16:32 UTC (rev 107)
@@ -328,7 +328,6 @@
 stock.data <- all.data[,-cn.names]
 
 of <- AMM(amm.type="onefirm",rj=y3c3$Company_A,
-            nlags=NA,
             verbose=TRUE,
             dates= as.Date(c("2005-01-15","2006-01-07","2007-01-06",
                        "2008-01-05","2009-01-03")),
@@ -336,14 +335,9 @@
            switch.to.innov=TRUE, rM1purge=TRUE, nlags=1)
 
 
-mf <- AMM(amm.type="manyfirms",regressand=StockPriceReturns[,1:3],
-          nlags=NA, verbose=TRUE, dates= NULL,
-          rM1=all.data$nifty, others=all.data$inr,
-          switch.to.innov=TRUE, rM1purge=TRUE, nlags=1)
-mf1 <- mf$residual
-mf1 <- mf1[complete.cases(mf1),]
-p1 <- phys2eventtime(z = mf1, events = SplitDates, width = 10)
-
+of.r <- AMM(amm.type="manyfirms", rj=stock.data[,1:5],verbose=TRUE, dates=NULL, 
+            rM1=all.data$nifty, others=all.data$inr, switch.to.innov=TRUE,
+            rM1purge=TRUE, nlags=1)
 es.ammonefirm <- eventstudy(inputData = stock.data[,1:4], 
                             eventList = SplitDates, 
                             width = 10, to.remap = TRUE, remap = "cumsum", 
@@ -351,7 +345,7 @@
                             inference.strategy = "bootstrap",
                             type = "AMM", amm.type="manyfirms",
                             rM1=all.data$nifty, others=all.data$inr,
-                            nlags=NA, verbose=TRUE,
+                            nlags=1, verbose=TRUE,
                             dates= NULL,
                             switch.to.innov=TRUE, rM1purge=TRUE, nlags=1)
 



More information about the Eventstudies-commits mailing list