[Eventstudies-commits] r137 - in pkg: R vignettes

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Aug 20 19:56:27 CEST 2013


Author: vikram
Date: 2013-08-20 19:56:27 +0200 (Tue, 20 Aug 2013)
New Revision: 137

Modified:
   pkg/R/AMM.R
   pkg/vignettes/eventstudies.Rnw
Log:
Modified AMM function to handle when there are all NAs in some firm

Modified: pkg/R/AMM.R
===================================================================
--- pkg/R/AMM.R	2013-08-19 17:46:02 UTC (rev 136)
+++ pkg/R/AMM.R	2013-08-20 17:56:27 UTC (rev 137)
@@ -77,12 +77,28 @@
                market.returns.purge, nlags, dates, verbose)
     tmp.result <- onefirmAMM(firm.returns[,1], X, nlags, verbose, dates)
     result <- tmp.result$residuals
+    remove.columns <- NULL
     for(i in 2:NCOL(firm.returns)){
+      cat("Computing AMM residuals for",
+          colnames(firm.returns)[i],"and", "column no.",i,"\n")
+      ## Checking number of observations
       tmp <- onefirmAMM(firm.returns[,i], X, nlags, verbose, dates)
+      if(is.null(tmp)){
+        remove.columns <- c(remove.columns,colnames(firm.returns)[i])
+        next("Cannot compute AMM residuals due to less observations")
+      }
       result <- merge(result,tmp$residuals)
     }
-    colnames(result) <- colnames(firm.returns)
+    ## Removing columns with less obs
+    if(is.null(remove.columns)!=TRUE){
+      cn.names <- colnames(firm.returns)[-which(colnames(firm.returns)%in%remove.columns)]
+      cat(length(remove.columns), "columns removed:",remove.columns,"\n")
+    } else {
+      cn.names <- colnames(firm.returns)
+    }
+    colnames(result) <- cn.names
   }
+  
   index(result) <- as.Date(index(result))
 
   return(result)
@@ -106,11 +122,16 @@
   ## Getting firm exposure, amm residuals
   if(is.null(dates)){
    res <- firmExposures(firm.returns,X,verbose=verbose,nlags=nlags)
-   exposures <- res$exposure
-   sds <- res$s.exposure
-   m.residuals <- xts(res$residuals,as.Date(attr(res$residuals,"names")))
-   if(residual==TRUE){
+   if(is.null(res)!=TRUE){
+     exposures <- res$exposure
+     sds <- res$s.exposure
      m.residuals <- xts(res$residuals,as.Date(attr(res$residuals,"names")))
+     if(residual==TRUE){
+       m.residuals <- xts(res$residuals,as.Date(attr(res$residuals,"names")))
+     }
+     rval <- list(exposures=exposures,sds=sds,residuals=m.residuals)
+   } else {
+     rval <- NULL
    }
  }else{
    tmp <- window(firm.returns,start=dates[1],end=dates[1+1])
@@ -139,8 +160,8 @@
      m.residuals <- merge(m.residuals, period.resid, all=TRUE)
    }
    rownames(exposures) <- rownames(sds) <- periodnames
- }
-  rval <- list(exposures=exposures,sds=sds,residuals=m.residuals)
+   rval <- list(exposures=exposures,sds=sds,residuals=m.residuals)
+ } 
   return(rval)
 }
 
@@ -496,3 +517,4 @@
           }
 	}
   }
+

Modified: pkg/vignettes/eventstudies.Rnw
===================================================================
--- pkg/vignettes/eventstudies.Rnw	2013-08-19 17:46:02 UTC (rev 136)
+++ pkg/vignettes/eventstudies.Rnw	2013-08-20 17:56:27 UTC (rev 137)
@@ -260,19 +260,12 @@
 nifty.index <- all.data$nifty.index 
 inrusd <- all.data$inr
 
-## AMM output ## For Full period: dates=NULL 
-
+## AMM output ##
 amm.residual <- AMM(firm.returns=StockPriceReturns[,1:3],
                     verbose=FALSE, market.returns=nifty.index,
                     others=inrusd, switch.to.innov=TRUE, 
                     market.returns.purge=TRUE, nlags=0)
 
-
-amm.output <- AMM(firm.returns=StockPriceReturns[,1:3],
-                  verbose=FALSE, market.returns=nifty.index,
-                  others=inrusd, switch.to.innov=TRUE, 
-                  market.returns.purge=TRUE,nlags=1)
-
 @
 
 \subsection{Conversion to event frame}



More information about the Eventstudies-commits mailing list