[Eventstudies-commits] r387 - pkg/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Nov 18 12:30:23 CET 2014


Author: chiraganand
Date: 2014-11-18 12:30:22 +0100 (Tue, 18 Nov 2014)
New Revision: 387

Modified:
   pkg/R/eventstudy.R
   pkg/R/marketModel.R
Log:
New market model function, introduces resid argument for returning residuals.

Modified: pkg/R/eventstudy.R
===================================================================
--- pkg/R/eventstudy.R	2014-11-15 14:22:09 UTC (rev 386)
+++ pkg/R/eventstudy.R	2014-11-18 11:30:22 UTC (rev 387)
@@ -92,7 +92,7 @@
             return(NULL)
         }
 
-        abnormal.returns <- firm$z.e[event.period, "firm.returns"] - model$coefficients[1] -
+        abnormal.returns <- firm$z.e[event.period, "firm.returns"] - model$coefficients["(Intercept)"] -
             (model$exposures["market.returns"] * firm$z.e[event.period, "market.returns"])
 
         for (i in 2:length(model$exposures)) { # 2: not market returns
@@ -134,10 +134,10 @@
         }
         estimation.period <- attributes(firm)[["estimation.period"]]
         model <- marketModel(firm$z.e[estimation.period, "firm.returns"],
-                             firm$z.e[estimation.period, "market.returns"])
+                             firm$z.e[estimation.period, "market.returns"],resid = FALSE)
 
-        abnormal.returns <- firm$z.e[event.period, "firm.returns"] - model$coefficients[1] -
-          (model$coefficients[2] * firm$z.e[event.period, "market.returns"])
+          abnormal.returns <- firm$z.e[event.period, "firm.returns"] - model$coefficients["(Intercept)"] -
+          (model$coefficients["market.returns"] * firm$z.e[event.period, "market.returns"])
 
         return(abnormal.returns)
       })
@@ -334,15 +334,6 @@
 }
 
 
-adjusted.returns <- function(firm.returns, rhsvars, intercept, betas) {
-  returns <- merge(firm.returns, rhsvars, all = FALSE, fill = NA)
-  pred <- intercept + apply(rhsvars, 1, function(n) { n %*% t(betas) })
-  returns <- returns[, -match(colnames(rhsvars), colnames(returns))]
-  adj.ret <- returns - pred
-  adj.ret
-}
-
-
 #########################
 ## Functions for class es
 #########################

Modified: pkg/R/marketModel.R
===================================================================
--- pkg/R/marketModel.R	2014-11-15 14:22:09 UTC (rev 386)
+++ pkg/R/marketModel.R	2014-11-18 11:30:22 UTC (rev 387)
@@ -1,7 +1,32 @@
-marketModel <- function(firm.returns, market.returns) {
+marketModel <- function(firm.returns, market.returns,resid=TRUE) {
     returns <- merge(firm.returns, market.returns, all = FALSE, fill = NA)
     market.returns <- returns$market.returns
     returns <- returns[, -match("market.returns", colnames(returns))]
-    reg <- lm(returns ~ market.returns, na.action = na.exclude)
-    return(reg)
+    if(NCOL(returns)==1){ ## Output for a single firm
+        reg <- lm(returns ~ market.returns, na.action = na.exclude) #:DOC: na.exclude
+        if(resid==TRUE){ ## MM-residuals for a single firm
+            resid <- returns - predict(reg)
+            return(resid)
+        }else{ ## Model estimates for a single firm
+            return(reg)
+        }
+    }else{ ## Multi-firm case
+        reg <- list()
+        resids <- list()
+        if(resid==TRUE){ ## Residuals for the multi-firm case
+            for(i in 1:ncol(returns)){
+                reg[[i]] <- lm(returns[,i] ~ market.returns, na.action = na.exclude)
+                resids[[i]] <- returns[,i]-predict(reg[[i]])
+            }
+            names(resids) <- colnames(returns)
+            resid <- do.call("merge",resids)
+            return(resid)
+        }else{ ## Model estimates for the multi-firm case
+            for(i in 1:ncol(returns)){
+                reg[[i]] <- lm(returns[,i] ~ market.returns, na.action = na.exclude)    
+            }
+            names(reg) <- colnames(returns)
+            return(reg)
+        }
+    }
 }



More information about the Eventstudies-commits mailing list