[Eventstudies-commits] r103 - in pkg: R inst/tests man vignettes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jul 24 13:29:02 CEST 2013
Author: vikram
Date: 2013-07-24 13:29:02 +0200 (Wed, 24 Jul 2013)
New Revision: 103
Modified:
pkg/R/AMM.R
pkg/R/eventstudy.R
pkg/R/excessReturn.R
pkg/R/marketResidual.R
pkg/inst/tests/test_marketresiduals.R
pkg/man/eventstudy.Rd
pkg/man/excessReturn.Rd
pkg/man/marketResidual.Rd
pkg/vignettes/eventstudies.Rnw
Log:
Made modification in the functionsand documentation; Modified vignette 99% only AMM part is remaining; Vimal the AMM function is not returning residuals, can you have look at it
Modified: pkg/R/AMM.R
===================================================================
--- pkg/R/AMM.R 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/R/AMM.R 2013-07-24 11:29:02 UTC (rev 103)
@@ -75,6 +75,20 @@
result <- onefirmAMM(rj, X, nlags, verbose, dates)
}
+ ##-----------
+ ## Many firms
+ ##-----------
+ if(amm.type == "manyfirms") {
+ # Checking required arguments
+ if (match("regerssand", names(modelArgs), nomatch = -1) == -1) {
+ stop("Input regressand (firm data) is missing")
+ }
+
+ X <- makeX(rM1, others, switch.to.innov,
+ rM1purge, nlags, dates, verbose)
+ result <- manyfirmsAMM(regressand, regressors=X, nlags, verbose, dates)
+ }
+
#---------------
# Firm exposures
#---------------
Modified: pkg/R/eventstudy.R
===================================================================
--- pkg/R/eventstudy.R 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/R/eventstudy.R 2013-07-24 11:29:02 UTC (rev 103)
@@ -15,10 +15,12 @@
# type = "marketResidual", "excessReturn", "AMM", "None"
if (type == "None" && !is.null(inputData)) {
outputModel <- inputData
- } else {
- stop("inputData or \"None\" type missing")
}
+ ## else {
+ ## stop("inputData or \"None\" type missing")
+ ## }
+
if (levels == TRUE) {
inputData <- diff(log(inputData)) * 100
}
@@ -31,18 +33,19 @@
## marketResidual
if (type == "marketResidual") {
- outputModel <- marketResidual(...)
+ outputModel <- marketResidual(data.object = inputData, ...)
}
## excessReturn
if (type == "excessReturn") {
- outputModel <- excessReturn(...)
+ outputModel <- excessReturn(data.object = inputData, ...)
}
### 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)
-
+
### Remapping event frame
if (to.remap == TRUE) {
es.w <- switch(remap,
Modified: pkg/R/excessReturn.R
===================================================================
--- pkg/R/excessReturn.R 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/R/excessReturn.R 2013-07-24 11:29:02 UTC (rev 103)
@@ -3,11 +3,16 @@
###############
# Argument:
# 1. data.object: This is a time series object with firm return and market return
-# 2. firm.name: It is the firm column name in the data object
-# 3. market.name: It is the market (index) column name in the data object
+# 2. market.name: It is the market (index) column name in the data object
# Output:
# Value: Excess market return
-excessReturn <- function(firm.name,market.name,data.object){
- ma.ret <- data.object[,firm.name]-data.object[,market.name]
+
+excessReturn <- function(data.object, market.name=NULL){
+ if(is.null(market.name)==TRUE){
+ stop("Column name for market index not provided")
+ }
+ cn.names <- colnames(data.object)
+ cn.names <- cn.names[-which(cn.names%in%market.name)]
+ ma.ret <- data.object[,cn.names]-data.object[,market.name]
return(ma.ret)
}
Modified: pkg/R/marketResidual.R
===================================================================
--- pkg/R/marketResidual.R 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/R/marketResidual.R 2013-07-24 11:29:02 UTC (rev 103)
@@ -1,27 +1,56 @@
#########################
# Market model adjustment
#########################
-# Argument:
-# 1. mm.formula: Here the input is the linear model (lm) formula for eg: a ~ b + c
-# If formula is not given then first column will be dependent and rest will be independent
-# 2. data.object: Single time series object with all the variables
-# Output:
-# Value: Market residual after extracting market returns from the firm return
-marketResidual <- function(mm.formula=NULL,data.object){
- # Storing NA observations
- na.date <- data.object[which(complete.cases(data.object)==FALSE)]
- # Extracting market residuals
+## Argument:
+## 1. data.object: Single time series object with all the variables
+## 2. market.name: Column name of market index in the data
+## Output:
+## Value: Market residual after extracting market returns from the firm return
+
+marketResidual <- function(data.object, market.name=NULL){
+### Checking arguments
+ if(is.null(market.name)==TRUE){
+ stop("Column name of market index not provided")
+ }
+ cn.names <- colnames(data.object)
+ cn.names <- cn.names[-which(cn.names%in%market.name)]
+ ## Market residual
+ formula <- paste(cn.names[1],"~",market.name,sep=" ")
+ tmp <- marketResidual.onefirm(mm.formula = formula,
+ data.object = data.object,
+ firm.name = cn.names[1])
+ ## tmp <- tmp[complete.cases(tmp),]
+ if(length(cn.names)>1){
+ for(i in 2:length(cn.names)){
+ ## Getting formula
+ formula <- paste(cn.names[i],"~",market.name,sep=" ")
+ ## Market residual
+ tmp.resid <- marketResidual.onefirm(mm.formula = formula,
+ data.object = data.object,
+ firm.name = cn.names[i])
+ ## tmp.resid <- tmp.resid[complete.cases(tmp.resid),]
+ tmp <- merge(tmp,tmp.resid,all=TRUE)
+ }
+ }
+ colnames(tmp) <- cn.names
+ return(tmp)
+}
+
+marketResidual.onefirm <- function(mm.formula=NULL,data.object,firm.name){
+### Market residual one firm
+ ## Storing NA observations
+ na.date <- data.object[which(complete.cases(data.object[,firm.name])==FALSE),
+ firm.name]
+### Checking arguments
if(is.null(mm.formula)==TRUE){
- formula <- paste(colnames(data.object)[1],"~",
- colnames(data.object)[2:NCOL(data.object)],sep="")
- reg <- lm(as.formula(formula),data=data.object)
- }else{
-
- reg <- lm(as.formula(mm.formula),data=data.object)
+ print("Formula for market residual model not provided")
}
+ ## Extracting market residuals
+ reg <- lm(as.formula(mm.formula),data=data.object)
resid <- xts(reg$residuals,as.Date(attr(reg$residuals,"names")))
suppressWarnings(tot.resid <- rbind(resid,
- xts(rep(NA,nrow(na.date)),
+ xts(rep(NA,NROW(na.date)),
index(na.date))))
return(tot.resid)
}
+
Modified: pkg/inst/tests/test_marketresiduals.R
===================================================================
--- pkg/inst/tests/test_marketresiduals.R 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/inst/tests/test_marketresiduals.R 2013-07-24 11:29:02 UTC (rev 103)
@@ -3,14 +3,16 @@
test_that("test.market.residuals", {
library(eventstudies)
-load(system.file("data", "mmData.rda", package = "eventstudies"))
+load(system.file("data", "StockPriceReturns.rda", package = "eventstudies"))
-mm.formula <- paste("ranbaxyacp","~","nifty",sep="")
-mm.result <- marketResidual(mm.formula=mm.formula,data.object=mmData)
+mm.result <- marketResidual(data.object=StockPriceReturns[,c("BHEL","nifty")],
+ market.name="nifty")
+mm.result <- mm.result[complete.cases(mm.result),]
# Calculating manually
-result <- lm(ranbaxyacp ~ nifty, data=mmData)
+result <- lm(BHEL ~ nifty, data=StockPriceReturns)
resid.res <- xts(result$resid,as.Date(attr(result$resid,"names")))
+colnames(resid.res) <- "BHEL"
expect_that(mm.result,equals(resid.res))
Modified: pkg/man/eventstudy.Rd
===================================================================
--- pkg/man/eventstudy.Rd 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/man/eventstudy.Rd 2013-07-24 11:29:02 UTC (rev 103)
@@ -8,18 +8,18 @@
\usage{
eventstudy(inputData = NULL,
- eventList,
- width = 10,
- levels = FALSE,
- type = "marketResidual",
- to.remap = TRUE,
- remap = "cumsum",
- inference = TRUE,
- inference.strategy = "bootstrap",
- to.plot = TRUE,
- xlab = "Event time",
- ylab = "Cumulative returns of response series",
- main = "Event study plot", ...)
+ eventList,
+ width = 10,
+ levels = FALSE,
+ type = "marketResidual",
+ to.remap = TRUE,
+ remap = "cumsum",
+ inference = TRUE,
+ inference.strategy = "bootstrap",
+ to.plot = TRUE,
+ xlab = "Event time",
+ ylab = "Cumulative returns of response series",
+ main = "Event study plot", ...)
}
\arguments{
Modified: pkg/man/excessReturn.Rd
===================================================================
--- pkg/man/excessReturn.Rd 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/man/excessReturn.Rd 2013-07-24 11:29:02 UTC (rev 103)
@@ -6,12 +6,11 @@
\description{ This function estimates excess return. If the the firm return is rj and market return is rM then output will be rj less rM.
}
-\usage{excessReturn(firm.name, market.name, data.object)
+\usage{excessReturn(data.object, market.name=NULL)
}
\arguments{
\item{data.object}{This is a time series object with firm return and market return}
- \item{firm.name}{It is the firm column name in the data object}
\item{market.name}{It is the market (index) column name in the data object}
}
@@ -20,9 +19,9 @@
\author{Vikram Bahure}
\examples{
-data(mmData)
-er.result <- excessReturn(firm.name="ranbaxyacp",market.name="nifty",
- data.object=mmData)
+data(StockPriceReturns)
+er.result <- excessReturn(market.name="nifty",
+ data.object=StockPriceReturns)
}
Modified: pkg/man/marketResidual.Rd
===================================================================
--- pkg/man/marketResidual.Rd 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/man/marketResidual.Rd 2013-07-24 11:29:02 UTC (rev 103)
@@ -6,14 +6,12 @@
\description{ This function extracts market return using regression from the firm return to get the residual return
}
-\usage{marketResidual(mm.formula=NULL, data.object)
+\usage{marketResidual(data.object, market.name=NULL)
}
\arguments{
- \item{data.object}{Single time series object with all the variables}
- \item{mm.formula}{Here the input is the linear model (lm) formula for eg: a ~ b + c
- If formula is not given then first column will be treated as
- dependent and rest will be independent}
+ \item{data.object}{Single time series object with firm/firms and stock index data}
+ \item{market.name}{Column name of the market index in the data}
}
\value{ Market residual after extracting market returns from the firm return
@@ -22,11 +20,9 @@
\author{Vikram Bahure}
\examples{
-data(mmData)
-# Forumla for market model
-mm.formula <- paste("ranbaxyacp","~","nifty","+","drug",sep="")
+data(StockPriceReturns)
# Extracting market residual
-mm.result <- marketResidual(mm.formula=mm.formula,data.object=mmData)
+mm.result <- marketResidual(data.object=StockPriceReturns, market.name="nifty")
}
Modified: pkg/vignettes/eventstudies.Rnw
===================================================================
--- pkg/vignettes/eventstudies.Rnw 2013-07-24 07:22:31 UTC (rev 102)
+++ pkg/vignettes/eventstudies.Rnw 2013-07-24 11:29:02 UTC (rev 103)
@@ -114,6 +114,14 @@
\section{Software approach} \label{s:approach}
The package offers the following functionalities:
+
+ \item Models for calculating returns. These include:
+ \begin{itemize}
+ \item Excess returns model
+ \item Market residual model
+ \item Augmented market model (AMM)
+ \end{itemize}
+
\begin{itemize}
\item Coverting the data-set to an event frame. This requires:
\begin{itemize}
@@ -121,12 +129,7 @@
\item Event dates object with two columns, \textit{unit} and
\textit{when}, the date of occurrence of the event.
\end{itemize}
- \item Models for calculating returns. These include:
- \begin{itemize}
- \item Market model
- \item Augmented market model
- \item Excess returns model
- \end{itemize}
+
\item Procedures for inference. These include:
\begin{itemize}
\item Bootstrapping
@@ -161,8 +164,48 @@
@
\subsection{Using the market model}
+<<>>=
+data(StockPriceReturns)
+# Excess return
+er.result <- excessReturn(market.name="nifty",
+ data.object=StockPriceReturns)
+@
+<<>>=
+# Extracting market residual
+mm.result <- marketResidual(data.object=StockPriceReturns, market.name="nifty")
+@
+
+%AMM model
+<<>>=
+# Create RHS before running AMM()
+data("y3c3")
+NIFTY_INDEX <- y3c3$NIFTY_INDEX
+INRUSD <- y3c3$INRUSD
+Company_A <- y3c3$Company_A
+Company_B <- y3c3$Company_B
+Company_C <- y3c3$Company_C
+regressand <- cbind(Company_A,Company_B,Company_C)
+
+## One firm
+of <- AMM(amm.type="onefirm",rj=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")),
+ rM1=NIFTY_INDEX, others=INRUSD,
+ switch.to.innov=TRUE, rM1purge=TRUE, nlags=1)
+## Many firms
+mf <- AMM(amm.type="manyfirm",regressand=regressand,
+ nlags=NA,
+ verbose=TRUE,
+ dates= as.Date(c("2005-01-15","2006-01-07","2007-01-06",
+ "2008-01-05","2009-01-03")),
+ rM1=NIFTY_INDEX, others=INRUSD,
+ switch.to.innov=TRUE, rM1purge=TRUE, nlags=1)
+
+@
\subsection{Converting physical dates to event frame}
The first step towards event study analysis is to convert the physical
dates to event time frame. The event date and the returns on that
@@ -206,7 +249,8 @@
es.cs[,1:3]
@
-\subsection{Bootstrap inference}
+\subsection{Inference procedures}
+\subsubsection{Bootstrap inference}
After converting to event frame and estimating the interest variable,
we need to check the stability of the result and derive other
estimates like standard errors and confidence intervals. For this,
@@ -224,7 +268,8 @@
@
\begin{figure}[t]
\begin{center}
- \caption{Stock splits event and response of respective stock returns}
+ \caption{Stock splits event and response of respective stock
+ returns: Bootstrap CI}
\setkeys{Gin}{width=0.8\linewidth}
\setkeys{Gin}{height=0.8\linewidth}
<<fig=TRUE,echo=FALSE>>=
@@ -234,7 +279,77 @@
\label{fig:one}
\end{figure}
+\subsubsection{Wilcoxon signed rank tests}
+It is a non-parametric inference test to compute confidence interval.
+<<>>=
+result <- inference.bootstrap(es.w=es.cs, to.plot=TRUE)
+@
+\begin{figure}[t]
+ \begin{center}
+ \caption{Stock splits event and response of respective stock
+ returns: Wilcoxon CI}
+ \setkeys{Gin}{width=0.8\linewidth}
+ \setkeys{Gin}{height=0.8\linewidth}
+<<fig=TRUE,echo=FALSE>>=
+ result <- inference.wilcox(es.w=es.cs, to.plot=TRUE)
+@
+\end{center}
+\label{fig:two}
+\end{figure}
+\subsection{General eventstudy function}
+This function is wrapper around all the internal functions. This
+function gives an option to compute returns like excess return, market
+residual and augmented market model. It also gives you an option to
+choose inference procedure from bootstrap and wilcoxon to generate
+confidence interval.
+
+<<>>=
+## Event study without adjustment
+es.na <- eventstudy(inputData = StockPriceReturns, eventList = SplitDates,
+ width = 10, to.remap = TRUE, remap = "cumsum",
+ to.plot = TRUE, inference = TRUE,
+ inference.strategy = "wilcoxon",
+ type = "None")
+
+## Event study using market residual and bootstrap
+es.mm <- eventstudy(inputData = StockPriceReturns, eventList = SplitDates,
+ width = 10, to.remap = TRUE, remap = "cumsum",
+ to.plot = TRUE, inference = TRUE,
+ inference.strategy = "bootstrap",
+ type = "marketResidual", market.name = "nifty")
+
+## Event study using excess return and bootstrap
+es.er <- eventstudy(inputData = StockPriceReturns, eventList = SplitDates,
+ width = 10, to.remap = TRUE, remap = "cumsum",
+ to.plot = TRUE, inference = TRUE,
+ inference.strategy = "bootstrap",
+ type = "excessReturn", market.name = "nifty")
+
+## Event study using augmented market model (AMM) and bootstrap
+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")),
+ rM1=y3c3$NIFTY_INDEX, others=y3c3$INRUSD,
+ switch.to.innov=TRUE, rM1purge=TRUE, nlags=1)
+
+## es.ammonefirm <- eventstudy(inputData = NULL,
+## eventList = SplitDates,
+## width = 10, to.remap = TRUE, remap = "cumsum",
+## to.plot = TRUE, inference = TRUE,
+## inference.strategy = "bootstrap",
+## type = "AMM", amm.type="onefirm",
+## rj=y3c3$Company_A,
+## rM1=y3c3$NIFTY_INDEX, others=y3c3$INRUSD,
+## nlags=NA, verbose=TRUE,
+## dates= as.Date(c("2005-01-15","2006-01-07",
+## "2007-01-06","2008-01-05","2009-01-03")),
+## switch.to.innov=TRUE, rM1purge=TRUE, nlags=1)
+@
+
+
\section{Computational details}
The package code is purely written in R. It has dependencies to zoo
(\href{http://cran.r-project.org/web/packages/zoo/index.html}{Zeileis
More information about the Eventstudies-commits
mailing list