From noreply at r-forge.r-project.org Thu Feb 6 13:50:40 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Thu, 6 Feb 2014 13:50:40 +0100 (CET) Subject: [Eventstudies-commits] r162 - / pkg pkg/R Message-ID: <20140206125040.71E9118663E@r-forge.r-project.org> Author: vikram Date: 2014-02-06 13:50:39 +0100 (Thu, 06 Feb 2014) New Revision: 162 Added: pkg/R/lmAmm.R Modified: pkg/NAMESPACE pkg/R/AMM.R todo.org Log: Added new lmAMM function; fixing the AMM function then moving on to event studies; Work in progress Modified: pkg/NAMESPACE =================================================================== --- pkg/NAMESPACE 2014-01-22 10:01:36 UTC (rev 161) +++ pkg/NAMESPACE 2014-02-06 12:50:39 UTC (rev 162) @@ -2,8 +2,14 @@ remap.cumsum, remap.cumprod, remap.event.reindex, ees, eesPlot) export(marketResidual, excessReturn) -export(AMM, - onefirmAMM, - manyfirmsAMM, - firmExposures, - makeX) +export(manyfirmsAMM, lmAMM, makeX) + + +S3method(summary, amm) +S3method(summary, es) + +S3method(print, amm) +S3method(print, es) + +S3method(plot, amm) +S3method(plot, es) Modified: pkg/R/AMM.R =================================================================== --- pkg/R/AMM.R 2014-01-22 10:01:36 UTC (rev 161) +++ pkg/R/AMM.R 2014-02-06 12:50:39 UTC (rev 162) @@ -184,7 +184,7 @@ exposures <- matrix(NA,nrow=nfirms,ncol=nperiods*ncol(regressors)) rownames(exposures) <- colnames(regressand) - tmp <- NULL + tmp <- NULL for(i in 1:length(periodnames)){ for(j in 1:ncol(regressors)){ tmp <- c(tmp, paste(colnames(regressors)[j], Added: pkg/R/lmAmm.R =================================================================== --- pkg/R/lmAmm.R (rev 0) +++ pkg/R/lmAmm.R 2014-02-06 12:50:39 UTC (rev 162) @@ -0,0 +1,300 @@ + + +######################## +# Many firms AMM +######################## +manyfirmsAMM <- +function(regressand,regressors, + lags,dates=NULL, periodnames=NULL,verbose=FALSE){ + require("doMC") + registerDoMC() + if(is.null(dates)){ + dates=c(start(regressors),end(regressors)) + periodnames="Full" + } + nperiods <- length(periodnames) + if(length(dates) != (nperiods+1)){ + cat("Mistake in length of dates versus length of periods.\n") + return(NULL) + } + nfirms <- ncol(regressand) + + # Let's get "exposure' and 'sds'. Setting up structures:- + + exposures <- matrix(NA,nrow=nfirms,ncol=nperiods*ncol(regressors)) + rownames(exposures) <- colnames(regressand) + tmp <- NULL + for(i in 1:length(periodnames)){ + for(j in 1:ncol(regressors)){ + tmp <- c(tmp, paste(colnames(regressors)[j], + periodnames[i],sep=".")) + } + } + colnames(exposures) <- tmp + sds <- exposures + colnames(sds) <- paste("sd",colnames(exposures),sep=".") + + # Setup a list structure for an OLS that failed + empty <- list(exposures=rep(NA,ncol(regressors)), + s.exposures=rep(NA,ncol(regressors))) + + for(i in 1:ncol(regressand)){ + cat("Doing",colnames(regressand)[i],"\n") + if (verbose) {cat ("Doing", colnames(regressand)[i], "\n")} + firm.returns <- regressand[,i] + dataset <- cbind(firm.returns, regressors) # This is the full time-series + this.exp <- this.sds <- NULL + for(j in 1:nperiods){ # now we chop it up + t1 <- dates[j] + t2 <- dates[j+1] + this <- window(dataset,start=t1, end=t2) + fe <- lmAMM(this[,1],this[,-1],nlags=lags,verbose) + if(is.null(fe)) {fe <- empty} + this.exp <- c(this.exp, fe$exposures) + this.sds <- c(this.sds, fe$s.exposures) + } + exposures[colnames(regressand)[i],] <- this.exp + sds[colnames(regressand)[i],] <- this.sds + } + list(exposures=exposures, sds=sds, sig=exposures/sds) +} + +############################################### +## Estimating one firm's exposure in one period +############################################### +lmAMM <- function(firm.returns, X, nlags=NA, verbose=FALSE) { + do.ols <- function(nlags) { + tmp <- cbind(firm.returns, X[,1]) # Assume 1st is stock index, and no lags are required there. + labels <- c("firm.returns","market.returns") + if (NCOL(X) > 1) { + for (i in 2:NCOL(X)) { + for (j in 0:nlags) { + tmp <- cbind(tmp, lag(X[,i], -j)) + labels <- c(labels, paste(colnames(X)[i], j, sep=".")) + } + } + } + tmp <- na.omit(tmp) + if (nrow(tmp) < 30) { # refuse to do the work. + return(NULL) # returns out of do.ols() only + } + + colnames(tmp) <- labels # So the OLS results will look nice + lm(firm.returns ~ ., data=as.data.frame(tmp)) + } + + if (is.na(nlags)) { + if (verbose) {cat("Trying to find the best lag structure...\n")} + bestlag <- 0 + bestm <- NULL + bestAIC <- Inf + for (trylag in 0:min(10,log10(length(firm.returns)))) { + thism <- do.ols(trylag) + thisAIC <- AIC(thism, k=log(length(thism$fitted.values))) + if (verbose) {cat(trylag, " lags, SBC = ", thisAIC, "\n")} + if (thisAIC < bestAIC) { + bestlag <- trylag + bestAIC <- thisAIC + bestm <- thism + } + } + nlags <- bestlag + m <- bestm + } else { + m <- do.ols(nlags) + if (is.null(m)) {return(NULL)} + } + # In either event, you endup holding an "m" here. + if (verbose) {cat("\n\nThe OLS:\n"); print(summary(m))} + + # Compute a series of exposure measures, and their standard errors. + beta <- m$coefficients + Sigma <- vcovHAC(m) + # First the market.returns + exposures <- beta[2] # no lags for market.returns + s.exposures <- sqrt(Sigma[2,2]) + # From here on, there's a block of 1+nlags coeffs for each + # of the non-market.returns regressors. + if (NCOL(X) > 1) { + for (i in 2:NCOL(X)) { + n.block1 <- 2 + ((i-2)*(1+nlags)) # Just 2 for the 1st case. + n.block2 <- length(beta) - n.block1 - (1 + nlags) + w <- c(rep(0, n.block1), rep(1, 1+nlags), rep(0, n.block2)) + exposures <- c(exposures, w %*% beta) + s.exposures <- c(s.exposures, sqrt(w %*% Sigma %*% w)) + } + } + results <- m + names(exposures) <- names(s.exposures) <- colnames(X) + results$exposures <- exposures + results$s.exposures <- s.exposures + results$nlags <- nlags + class(results) <- "amm" + return(results) +} + + +########################### +# Maintaing NAs in AR model +########################### +ARinnovations <- function(x) { + stopifnot(NCOL(x) == 1) + dt <- NULL + if (class(x) == "zoo") { + dt <- index(x) + x <- as.numeric(x) + } + non.na.locations <- !is.na(x) + x <- x[non.na.locations] + m <- ar(x) + e <- m$resid + # Relocate with the old NA structure + result <- rep(NA, length(non.na.locations)) + result[non.na.locations] <- e + if (!is.null(dt)) {result <- zoo(result, order.by=dt)} + list(result=result, m=m) +} + +# --------------------------------------------------------------------------- +# The workhorse called by makeX to return a nice matrix of RHS +# variables to be used in an analysis. +do.one.piece <- function(market.returns, others, switch.to.innov, market.returns.purge, nlags, verbose=FALSE) { + thedates <- index(market.returns) + if (verbose) { + cat(" Doing work for period from ", + as.character(head(thedates,1)), " to ", + as.character(tail(thedates,1)), "\n") + } + otherlags <- NULL + for (i in 1:NCOL(others)) { + if (switch.to.innov[i]) { + a <- ARinnovations(others[,i]) + innovated <- a$result + if (verbose) { + cat(" AR model for ", colnames(others, do.NULL=FALSE)[i], "\n") + print(a$m) + } + otherlags <- c(otherlags, a$m$order) + } else { + innovated <- others[,i] + otherlags <- c(otherlags, 0) + } + if (i > 1) { + innov <- cbind(innov, innovated) + } else { + innov <- innovated + } + } + if (NCOL(innov) > 1) {colnames(innov) <- colnames(others)} + market.returns.purged <- market.returns + if (market.returns.purge) { + firstpass <- TRUE + for (i in 1:NCOL(innov)) { + for (j in 0:nlags) { + if (firstpass) { + z <- lag(innov[,i],-j) + labels <- paste(colnames(innov,do.NULL=FALSE)[i],j,sep=".") + firstpass <- FALSE + } else { + z <- cbind(z, lag(innov[,i], -j)) + labels <- c(labels, paste(colnames(innov,do.NULL=FALSE)[i],j,sep=".")) + } + } + } + if (NCOL(z) > 1) {colnames(z) <- labels} + m <- lm(market.returns ~ ., as.data.frame(cbind(market.returns, z))) + if (verbose) { + cat(" Model explaining market.returns:\n") + print(summary(m)) + } + how.many.NAs <- nlags + max(otherlags) + market.returns.purged <- zoo(c(rep(NA,how.many.NAs),m$residuals), + order.by=thedates) + } + # if (verbose) {cat(" Finished do.one.piece()\n")} + list(market.returns.purged=market.returns.purged, innov=innov) +} + +# A function that calls do.one.piece, and works through several +# different periods to provide the right RHS matrix. +makeX <- function(market.returns, others, + switch.to.innov=rep(TRUE, NCOL(others)), + market.returns.purge=TRUE, + nlags=5, + dates=NULL, + verbose=FALSE) { + if (verbose) {cat("0. Checking args\n")} + stopifnot(all.equal(index(market.returns), index(others)), + length(switch.to.innov)==NCOL(others)) + if (!is.null(dates)) { + stopifnot(class(dates) == "Date") + } + if (verbose) {cat("1. Checking dates.\n")} + if (is.null(dates)) { + dates <- c(start(market.returns),end(market.returns)) + } + if(head(dates,1)!=head(index(market.returns),1)){ + stop("Start date provided and the start date of the dataset do not match \n") + } + if(tail(dates,1)!=tail(index(market.returns),1)){ + stop("End date provided and the end date of the dataset do not match \n") + } + if (verbose) {cat("2. Run through all the pieces --\n")} + for (i in 1:(length(dates)-1)) { + t1 <- dates[i] + t2 <- dates[i+1] + if (i != (length(dates)-1)) {t2 <- t2 -1} + if (verbose) { + cat(" Focusing down from date = ", as.character(t1), " to ", as.character(t2), "\n") + } + tmp.market.returns <- window(market.returns, start=t1, end=t2) + tmp.others <- window(others, start=t1, end=t2) + a <- do.one.piece(tmp.market.returns, tmp.others, switch.to.innov, market.returns.purge, nlags, verbose) + if (i > 1) { + res.market.returns <- c(res.market.returns, a$market.returns.purged) + res.innov <- rbind(res.innov, a$innov) + } else { + res.market.returns <- a$market.returns.purged + res.innov <- a$innov + } + } + if (verbose) {cat("2. Make a clean X and send it back --\n")} + X <- cbind(res.market.returns, res.innov) + if (NCOL(res.innov) == 1) {colnames(X) <- c("market.returns","z")} + else {colnames(X) <- c("market.returns", colnames(res.innov))} + X +} + + +############################################ +## Summary, print and plot functions for AMM +############################################ +summary.amm <- function(amm) { + cat("\n", "Summary statistics of exposure: \n") + sstats <- cbind(amm$exposure, amm$s.exposure, + amm$exposure/amm$s.exposure) + colnames(sstats) <- c("Exposure", "Std.Err", "t statistic") + rownames(sstats) <- names(amm$exposures) + print(sstats) + cat("\n","Linear model AMM results: ","\n"); class(amm) <- "lm"; print(summary(amm)) +} + +print.amm <- function(amm){ + cat("\n") + print(amm$call) + cat("\n","Coefficients:","\n") + print(amm$coef) + cat("\n","Exposures:","\n") + print(amm$exposures) +} + +plot.amm <- function(amm){ + tmp.x <- zoo(as.numeric(resid(amm)), as.Date(names(resid(amm)))) + tmp.f <- zoo(amm$model$firm.returns, index(tmp.x)) + tmp <- merge(tmp.x,tmp.f) + colnames(tmp) <- c("amm.residuals","firm.returns") + plot(tmp, screen=1, lty=1:2, lwd=2, col=c("indian red", "navy blue"),ylab="", + xlab="") + legend("topleft",legend=c("AMM residual","Firm returns"),lty=1:2, lwd=2, + col=c("indian red", "navy blue"), bty='n') +} Modified: todo.org =================================================================== --- todo.org 2014-01-22 10:01:36 UTC (rev 161) +++ todo.org 2014-02-06 12:50:39 UTC (rev 162) @@ -4,7 +4,7 @@ - should work with data.frames, zoo, xts - create a generic "eventstudies" class - DESIGN!!! - + * phys2eventtime - more testing: all output is unitmissing - parallelise (provide an argument to the user) @@ -12,14 +12,14 @@ > stopifnot(sum(outcomes=="success") == NCOL(z.e)) - if data is missing within the width around the event date, we simply discard that particular observation from the dataset! - + * AMM() - vectorise it - parallelise it - don't ask the user for amm.type in the eventstudies() function - separate vignette - Rewriting? # XXX - + * Vignette - improvements in AMM() - missing entries in .bib From noreply at r-forge.r-project.org Thu Feb 6 13:51:28 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Thu, 6 Feb 2014 13:51:28 +0100 (CET) Subject: [Eventstudies-commits] r163 - pkg/R Message-ID: <20140206125128.DEBE6186762@r-forge.r-project.org> Author: vikram Date: 2014-02-06 13:51:28 +0100 (Thu, 06 Feb 2014) New Revision: 163 Removed: pkg/R/AMM.R Log: Removing old AMM wrapper function Deleted: pkg/R/AMM.R =================================================================== --- pkg/R/AMM.R 2014-02-06 12:50:39 UTC (rev 162) +++ pkg/R/AMM.R 2014-02-06 12:51:28 UTC (rev 163) @@ -1,516 +0,0 @@ -########################### -## Event study AMM function -########################### -########################## -# Generalised AMM function -########################## - -AMM <- function(...) { - - modelArgs <- list(...) - - # Checking required arguments - if (match("market.returns", names(modelArgs), nomatch = -1) == -1) { - stop("Input market.returns (stock market index) is missing") - } - if (match("others", names(modelArgs), nomatch = -1) == -1) { - stop("Input 'others' (time series of other regressor or interest) is missing") - } - if (match("market.returns.purge", names(modelArgs), nomatch = -1) == -1) { - stop("Input market.returns.purge is missing") - } - if (match("switch.to.innov", names(modelArgs), nomatch = -1) == -1) { - stop("Input switch.to.innov is missing") - } - - firm.returns <- modelArgs$firm.returns - market.returns <- modelArgs$market.returns - others <- modelArgs$others - switch.to.innov <- modelArgs$switch.to.innov - market.returns.purge <- modelArgs$market.returns.purge - - # Checking remaining arguments - if (match("nlags", names(modelArgs), nomatch = -1) == -1) { - nlags <- 0 - } else { - nlags <- modelArgs$nlags - } - if (match("verbose", names(modelArgs), nomatch = -1) == -1) { - verbose <- FALSE - } else { - verbose <- modelArgs$verbose - } - if (match("dates", names(modelArgs), nomatch = -1) == -1) { - dates <- NULL - } else { - dates <- modelArgs$dates - } - - ## One firm - if(NCOL(firm.returns)==1){ - # Checking required arguments - if (match("firm.returns", names(modelArgs), nomatch = -1) == -1) { - stop("Input firm.returns (firm data) is missing") - } - - X <- makeX(market.returns, others, switch.to.innov, - market.returns.purge, nlags, dates, verbose) - result <- onefirmAMM(firm.returns, X, nlags, verbose, dates) - result <- result$residuals - } - - ## Many firms - if(NCOL(firm.returns)>1){ - # Checking required arguments - if (match("firm.returns", names(modelArgs), nomatch = -1) == -1) { - stop("Input firm.returns (firm data) is missing") - } - if(NCOL(firm.returns)<2){ - stop("Less than two firms in inputData") - } - - X <- makeX(market.returns, others, switch.to.innov, - 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) - } - ## 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) -} - -####################### -# AMM for one firm -####################### -onefirmAMM <- function(firm.returns,X,nlags=1,verbose=FALSE,dates=NULL,residual=TRUE){ - ## Creating empty frames - if(is.null(dates)){ - dates.no <- c(start(firm.returns),end(firm.returns)) - } 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(firm.returns,X,verbose=verbose,nlags=nlags) - 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]) - rhs <- window(X,start=dates[1],end=dates[1+1]) - res <- firmExposures(firm.returns=tmp, - X=rhs, - verbose=verbose, - nlags=nlags) - exposures[1,] <- res$exposure - periodnames <- c(periodnames,paste(dates[1],dates[1+1],sep=" TO ")) - sds[1,] <- res$s.exposure - m.residuals <- xts(res$residuals,as.Date(attr(res$residuals,"names"))) - colnames(m.residuals) <- paste(dates[1],"to",dates[1+1],sep=".") - for(i in 2:(length(dates)-1)){ - tmp <- window(firm.returns,start=dates[i],end=dates[i+1]) - rhs <- window(X,start=dates[i],end=dates[i+1]) - res <- firmExposures(firm.returns=tmp, - X=rhs, - verbose=verbose, - nlags=nlags) - exposures[i,] <- res$exposure - periodnames <- c(periodnames,paste(dates[i],dates[i+1],sep=" TO ")) - sds[i,] <- res$s.exposure - period.resid <- xts(res$residuals,as.Date(attr(res$residuals,"names"))) - colnames(period.resid) <- paste(dates[i],"to",dates[i+1],sep=".") - m.residuals <- merge(m.residuals, period.resid, all=TRUE) - } - rownames(exposures) <- rownames(sds) <- periodnames - rval <- list(exposures=exposures,sds=sds,residuals=m.residuals) - } - return(rval) -} - -######################## -# Many firms AMM -######################## -manyfirmsAMM <- -function(regressand,regressors, - lags,dates=NULL, periodnames=NULL,verbose=FALSE){ - require("doMC") - registerDoMC() - if(is.null(dates)){ - dates=c(start(regressors),end(regressors)) - periodnames="Full" - } - nperiods <- length(periodnames) - if(length(dates) != (nperiods+1)){ - cat("Mistake in length of dates versus length of periods.\n") - return(NULL) - } - nfirms <- ncol(regressand) - - # Let's get "exposure' and 'sds'. Setting up structures:- - - exposures <- matrix(NA,nrow=nfirms,ncol=nperiods*ncol(regressors)) - rownames(exposures) <- colnames(regressand) - tmp <- NULL - for(i in 1:length(periodnames)){ - for(j in 1:ncol(regressors)){ - tmp <- c(tmp, paste(colnames(regressors)[j], - periodnames[i],sep=".")) - } - } - colnames(exposures) <- tmp - sds <- exposures - colnames(sds) <- paste("sd",colnames(exposures),sep=".") - - # Setup a list structure for an OLS that failed - empty <- list(exposures=rep(NA,ncol(regressors)), - s.exposures=rep(NA,ncol(regressors))) - - for(i in 1:ncol(regressand)){ - cat("Doing",colnames(regressand)[i]) - if (verbose) {cat ("Doing", colnames(regressand)[i])} - firm.returns <- regressand[,i] - dataset <- cbind(firm.returns, regressors) # This is the full time-series - this.exp <- this.sds <- NULL - for(j in 1:nperiods){ # now we chop it up - t1 <- dates[j] - t2 <- dates[j+1] - this <- window(dataset,start=t1, end=t2) - fe <- firmExposures(this[,1],this[,-1],nlags=lags,verbose) - if(is.null(fe)) {fe <- empty} - this.exp <- c(this.exp, fe$exposures) - this.sds <- c(this.sds, fe$s.exposures) - } - exposures[colnames(regressand)[i],] <- this.exp - sds[colnames(regressand)[i],] <- this.sds - } - list(exposures=exposures, sds=sds, sig=exposures/sds) -} - -############################################### -## Estimating one firm's exposure in one period. -############################################### -firmExposures <- function(firm.returns, X, nlags=NA, verbose=FALSE) { - do.ols <- function(nlags) { - tmp <- cbind(firm.returns, X[,1]) # Assume 1st is stock index, and no lags are required there. - labels <- c("firm.returns","market.returns") - if (NCOL(X) > 1) { - for (i in 2:NCOL(X)) { - for (j in 0:nlags) { - tmp <- cbind(tmp, lag(X[,i], -j)) - labels <- c(labels, paste(colnames(X)[i], j, sep=".")) - } - } - } - tmp <- na.omit(tmp) - if (nrow(tmp) < 30) { # refuse to do the work. - return(NULL) # returns out of do.ols() only - } - - colnames(tmp) <- labels # So the OLS results will look nice - lm(firm.returns ~ ., data=as.data.frame(tmp)) - } - - if (is.na(nlags)) { - if (verbose) {cat("Trying to find the best lag structure...\n")} - bestlag <- 0 - bestm <- NULL - bestAIC <- Inf - for (trylag in 0:min(10,log10(length(firm.returns)))) { - thism <- do.ols(trylag) - thisAIC <- AIC(thism, k=log(length(thism$fitted.values))) - if (verbose) {cat(trylag, " lags, SBC = ", thisAIC, "\n")} - if (thisAIC < bestAIC) { - bestlag <- trylag - bestAIC <- thisAIC - bestm <- thism - } - } - nlags <- bestlag - m <- bestm - } else { - m <- do.ols(nlags) - if (is.null(m)) {return(NULL)} - } - # In either event, you endup holding an "m" here. - if (verbose) {cat("\n\nThe OLS:\n"); print(summary(m))} - - # Compute a series of exposure measures, and their standard errors. - beta <- m$coefficients - Sigma <- vcovHAC(m) - # First the market.returns - exposures <- beta[2] # no lags for market.returns - s.exposures <- sqrt(Sigma[2,2]) - # From here on, there's a block of 1+nlags coeffs for each - # of the non-market.returns regressors. - if (NCOL(X) > 1) { - for (i in 2:NCOL(X)) { - n.block1 <- 2 + ((i-2)*(1+nlags)) # Just 2 for the 1st case. - n.block2 <- length(beta) - n.block1 - (1 + nlags) - w <- c(rep(0, n.block1), rep(1, 1+nlags), rep(0, n.block2)) - exposures <- c(exposures, w %*% beta) - s.exposures <- c(s.exposures, sqrt(w %*% Sigma %*% w)) - } - } - residuals <- m$resid - names(exposures) <- names(s.exposures) <- colnames(X) - results <- list(exposures=exposures,residuals=residuals, - s.exposures=s.exposures, nlags=nlags, - lm.res=m) - class(results) <- "amm" - results -} - -########################### -# Maintaing NAs in AR model -########################### -ARinnovations <- function(x) { - stopifnot(NCOL(x) == 1) - dt <- NULL - if (class(x) == "zoo") { - dt <- index(x) - x <- as.numeric(x) - } - non.na.locations <- !is.na(x) - x <- x[non.na.locations] - m <- ar(x) - e <- m$resid - # Relocate with the old NA structure - result <- rep(NA, length(non.na.locations)) - result[non.na.locations] <- e - if (!is.null(dt)) {result <- zoo(result, order.by=dt)} - list(result=result, m=m) -} - -# --------------------------------------------------------------------------- -# The workhorse called by makeX to return a nice matrix of RHS -# variables to be used in an analysis. -do.one.piece <- function(market.returns, others, switch.to.innov, market.returns.purge, nlags, verbose=FALSE) { - thedates <- index(market.returns) - if (verbose) { - cat(" Doing work for period from ", - as.character(head(thedates,1)), " to ", - as.character(tail(thedates,1)), "\n") - } - otherlags <- NULL - for (i in 1:NCOL(others)) { - if (switch.to.innov[i]) { - a <- ARinnovations(others[,i]) - innovated <- a$result - if (verbose) { - cat(" AR model for ", colnames(others, do.NULL=FALSE)[i], "\n") - print(a$m) - } - otherlags <- c(otherlags, a$m$order) - } else { - innovated <- others[,i] - otherlags <- c(otherlags, 0) - } - if (i > 1) { - innov <- cbind(innov, innovated) - } else { - innov <- innovated - } - } - if (NCOL(innov) > 1) {colnames(innov) <- colnames(others)} - market.returns.purged <- market.returns - if (market.returns.purge) { - firstpass <- TRUE - for (i in 1:NCOL(innov)) { - for (j in 0:nlags) { - if (firstpass) { - z <- lag(innov[,i],-j) - labels <- paste(colnames(innov,do.NULL=FALSE)[i],j,sep=".") - firstpass <- FALSE - } else { - z <- cbind(z, lag(innov[,i], -j)) - labels <- c(labels, paste(colnames(innov,do.NULL=FALSE)[i],j,sep=".")) - } - } - } - if (NCOL(z) > 1) {colnames(z) <- labels} - m <- lm(market.returns ~ ., as.data.frame(cbind(market.returns, z))) - if (verbose) { - cat(" Model explaining market.returns:\n") - print(summary(m)) - } - how.many.NAs <- nlags + max(otherlags) - market.returns.purged <- zoo(c(rep(NA,how.many.NAs),m$residuals), - order.by=thedates) - } - # if (verbose) {cat(" Finished do.one.piece()\n")} - list(market.returns.purged=market.returns.purged, innov=innov) -} - -# A function that calls do.one.piece, and works through several -# different periods to provide the right RHS matrix. -makeX <- function(market.returns, others, - switch.to.innov=rep(TRUE, NCOL(others)), - market.returns.purge=TRUE, - nlags=5, - dates=NULL, - verbose=FALSE) { - if (verbose) {cat("0. Checking args\n")} - stopifnot(all.equal(index(market.returns), index(others)), - length(switch.to.innov)==NCOL(others)) - if (!is.null(dates)) { - stopifnot(class(dates) == "Date") - } - if (verbose) {cat("1. Checking dates.\n")} - if (is.null(dates)) { - dates <- c(start(market.returns),end(market.returns)) - } - if(head(dates,1)!=head(index(market.returns),1)){ - stop("Start date provided and the start date of the dataset do not match \n") - } - if(tail(dates,1)!=tail(index(market.returns),1)){ - stop("End date provided and the end date of the dataset do not match \n") - } - if (verbose) {cat("2. Run through all the pieces --\n")} - for (i in 1:(length(dates)-1)) { - t1 <- dates[i] - t2 <- dates[i+1] - if (i != (length(dates)-1)) {t2 <- t2 -1} - if (verbose) { - cat(" Focusing down from date = ", as.character(t1), " to ", as.character(t2), "\n") - } - tmp.market.returns <- window(market.returns, start=t1, end=t2) - tmp.others <- window(others, start=t1, end=t2) - a <- do.one.piece(tmp.market.returns, tmp.others, switch.to.innov, market.returns.purge, nlags, verbose) - if (i > 1) { - res.market.returns <- c(res.market.returns, a$market.returns.purged) - res.innov <- rbind(res.innov, a$innov) - } else { - res.market.returns <- a$market.returns.purged - res.innov <- a$innov - } - } - if (verbose) {cat("2. Make a clean X and send it back --\n")} - X <- cbind(res.market.returns, res.innov) - if (NCOL(res.innov) == 1) {colnames(X) <- c("market.returns","z")} - else {colnames(X) <- c("market.returns", colnames(res.innov))} - X -} - -############################################################################# -# Exposure is a vector of exposures -# var.exposures is a vector of the s.error of each exposure -# X is a matrix of firm characteristics -# Note: They all should have the same number of rows. -lm.for.amm.exposure <- function(exposures, var.exposures, X) { - m.ols <- lm(exposures ~ -1 + X) -# Likelihood function for OLS-by-MLE where each obs has a known standard error -- - mine.lf <- function(theta, y, X, y.se2) { - if (theta[1] <= 0) return(NA) # Refuse to look at -ve sigma. - -sum(dnorm(y, mean= X %*% theta[-1], sd=sqrt(theta[1]+y.se2), log=TRUE)) - } - ## Use OLS as starting values for the MLE -- - seeds <- c(summary(m.ols)$sigma, coef(m.ols)) - p <- optim(seeds, method="L-BFGS-B", fn=mine.lf, hessian=TRUE, - lower=c(1e-2, -Inf, -Inf, -Inf), upper=rep(Inf, 4), - y=exposures, X=X, y.se2=var.exposures) - inverted <- solve(p$hessian) - # Now get to generation of a nicely organised matrix collecting up the results. - # First setup a clean set of OLS results -- - results <- summary(m.ols)$coefficients[,c(1,3)] - rownames(results) <- substr(rownames(results), 2, 1000) - colnames(results) <- c("OLS estimates", "OLS t stats") - results <- rbind(results, c(summary(m.ols)$sigma, "NA")) - rownames(results)[nrow(results)] <- "Sigma" -# Now augment it on the right with the MLE -- - tmp <- p$par/sqrt(diag(inverted)) - results <- cbind(results, cbind(c(p$par[-1], p$par[1]), - c(tmp[-1], tmp[1]))) - colnames(results) <- c(colnames(results)[1:2], "MLE estimates", "MLE t stats") - results -} - - -#################################################### - -print.amm <- -function(amm, verbose=FALSE) { - if (verbose) {print(summary(amm$lm.res))} - cat("Summary statistics of exposure:\n") - sstats <- cbind(amm$exposure, amm$s.exposure, - amm$exposure/amm$s.exposure) - colnames(sstats) <- c("Exposure", "Std.Err", "t statistic") - rownames(sstats) <- names(amm$exposures) - print(sstats) - return(0) -} - -################################################################################ -# You got to write a plot function that does cool stuff with the AMM results! -################################################################################ - -simulated.mean <- function (beta, stdev, fun=NULL, no.of.draws){ - na1 <- is.na(beta) - na2 <- is.na(stdev) - if (!identical(na1, na2)) { - cat("Panic - two is.na() are not identical.\n") - return(-1) - } - b <- beta[!is.na(beta)] - s <- stdev[!is.na(stdev)] - N <- length(b) - draws <- NULL - for (i in 1:no.of.draws) { - if(!is.null(fun)){ - draws <- c(draws, mean(fun((rnorm(N) * s) + b))) - }else{ - draws <- c(draws, mean(rnorm(N)*s+b)) - } - } - draws -} - -######################################### -kernel.plots <- function(draws, logscale=NULL) { - nplots <- ncol(draws) - hilo <- range(draws) - par(mfrow=c(nplots,1), mai=c(.4,.8,.2,.2)) - for (i in 1:nplots) { - if(!is.null(logscale)){ - plot(density(draws[,i]), main=colnames(draws)[i], xlab="", - col="blue", xlim=hilo, log=logscale,lwd=2) - }else{ - plot(density(draws[,i]), main=colnames(draws)[i], xlab="", - col="blue", xlim=hilo, lwd=2) - } - } - } - From noreply at r-forge.r-project.org Sat Feb 8 08:53:10 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sat, 8 Feb 2014 08:53:10 +0100 (CET) Subject: [Eventstudies-commits] r164 - in pkg: data man Message-ID: <20140208075311.10C091869AA@r-forge.r-project.org> Author: vikram Date: 2014-02-08 08:53:10 +0100 (Sat, 08 Feb 2014) New Revision: 164 Added: pkg/data/lmAMMData.rda pkg/man/lmAMM.Rd pkg/man/lmAMMData.Rd Removed: pkg/data/firmExposuresData.rda pkg/data/y3c3.rda pkg/man/firmExposures.Rd pkg/man/firmExposuresData.Rd Modified: pkg/man/manyfirmsAMM.Rd Log: Modified AMM functionality to fit like lm function with class as 'amm' Deleted: pkg/data/firmExposuresData.rda =================================================================== (Binary files differ) Added: pkg/data/lmAMMData.rda =================================================================== (Binary files differ) Property changes on: pkg/data/lmAMMData.rda ___________________________________________________________________ Added: svn:mime-type + application/x-gzip Deleted: pkg/data/y3c3.rda =================================================================== (Binary files differ) Deleted: pkg/man/firmExposures.Rd =================================================================== --- pkg/man/firmExposures.Rd 2014-02-06 12:51:28 UTC (rev 163) +++ pkg/man/firmExposures.Rd 2014-02-08 07:53:10 UTC (rev 164) @@ -1,72 +0,0 @@ -\name{firmExposures} -\alias{firmExposures} - -\title{A function that estimates exposure for a single firm in a -single period} - -\description{This function typically utilises an firm.returns vector - and an X matrix of explanatory variables obtained - using the makeX function. This would compute the exposure - for all columns in X. -} - -\usage{ -firmExposures(firm.returns, X, nlags = NA, verbose = FALSE) -} - -\arguments{ - \item{firm.returns}{A vector of data for one firm} - - \item{X}{A matrix of explanatory variables obtained from - the makeX function. The first variable is always the - stock market index. Other variables could be - such as currency or bond returns, or other variables as - desired by the user. - } - - \item{nlags}{Number of lags of explanatory variables. When unspecified - the best lag using the AIC is used. - } - - \item{verbose}{Default is FALSE. When set to TRUE, the function - prints detailed results while running this function. - } -} - - -\value{ The function returns the exposures, HAC adjusted standard - errors, the number of lags used, and the residuals from the fitted - model. - -\item{exposures}{This contains the exposure estimates for the firm -j} - -\item{s.exposures}{This contains the HAC adjusted standard error -of the exposures estimated for the firm firm.returns.} - -\item{nlags}{Specifies the lag length provided by the user} - -\item{lm.res}{The model estimates used in the analysis are stored here.} - -} - -\author{Ajay Shah, Vimal Balasubramaniam} - -\seealso{ -\code{\link{manyfirmsAMM}} -} - -\examples{ -# firmExposures -data("firmExposuresData") -firm.returns <- firmExposuresData$Company_A -market.returns <- firmExposuresData$NIFTY_INDEX -rM2 <- firmExposuresData$usdinr -rM3 <- firmExposuresData$baa -X <- makeX(market.returns, others=rM2, - switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE) -a <- firmExposures(firm.returns, X, nlags=0, verbose=FALSE) -print(a) -} - -\keyword{firmExposures} \ No newline at end of file Deleted: pkg/man/firmExposuresData.Rd =================================================================== --- pkg/man/firmExposuresData.Rd 2014-02-06 12:51:28 UTC (rev 163) +++ pkg/man/firmExposuresData.Rd 2014-02-08 07:53:10 UTC (rev 164) @@ -1,19 +0,0 @@ -\name{firmExposuresData} -\alias{firmExposuresData} - - -\title{Data for testing the amm package and examples therein} - -\description{This is a zoo data that is used for testing purposes} - -\usage{data(firmExposuresData)} - -\author{Vimal Balasubramaniam} - -\examples{ -library(zoo) -data(firmExposuresData) -str(firmExposuresData) -} - -\keyword{firmExposuresData} \ No newline at end of file Copied: pkg/man/lmAMM.Rd (from rev 161, pkg/man/firmExposures.Rd) =================================================================== --- pkg/man/lmAMM.Rd (rev 0) +++ pkg/man/lmAMM.Rd 2014-02-08 07:53:10 UTC (rev 164) @@ -0,0 +1,68 @@ +\name{lmAMM} +\alias{lmAMM} + +\title{Augmented market model (AMM) estimation} + +\description{This function relates firm returns to the regressors using the linear model (market index movements, currency fluctuations etc.). +It computes firm exposure for all the regressors in columns of X and subsequently estimates AMM residuals by purging out variation +from regressand. +} + +\usage{ +lmAMM(firm.returns, X, nlags = NA, verbose = FALSE) +} + +\arguments{ + \item{firm.returns}{A time series object of firm returns for AMM estimation} + + \item{X}{A time series of explanatory variables obtained from + the makeX function. The first variable is always the + stock market index. Other variables could be + such as currency or bond returns, or other variables as + desired by the user. + } + + \item{nlags}{Number of lags of explanatory variables. When unspecified + the best lag using the AIC is used. + } + + \item{verbose}{Default is FALSE. When set to TRUE, the function + prints detailed results while running this function. + } +} + + +\value{ The function returns the exposures, HAC adjusted standard + errors, the number of lags used, and the residuals from the fitted + model. + +\item{exposures}{This contains the exposure estimates for the firm +j} + +\item{s.exposures}{This contains the HAC adjusted standard error +of the exposures estimated for the firm firm.returns.} + +\item{nlags}{Specifies the lag length provided by the user} + +} + +\author{Ajay Shah, Vimal Balasubramaniam} + +\seealso{ +\code{\link{manyfirmsAMM}, \link{makeX}} +} + +\examples{ +# lmAMM +data("lmAMMData") +firm.returns <- lmAMMData[,c("Infosys")] +market.returns <- lmAMMData[,"index.nifty"] +currency.returns <- lmAMMData[,"currency.inrusd"] +## Creating regressors for AMM estimation using makeX function +X <- makeX(market.returns, others=currency.returns, + switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE) +a <- lmAMM(firm.returns, X, nlags=0, verbose=FALSE) +print(a) +} + +\keyword{lmAMM} \ No newline at end of file Copied: pkg/man/lmAMMData.Rd (from rev 161, pkg/man/firmExposuresData.Rd) =================================================================== --- pkg/man/lmAMMData.Rd (rev 0) +++ pkg/man/lmAMMData.Rd 2014-02-08 07:53:10 UTC (rev 164) @@ -0,0 +1,19 @@ +\name{lmAMMData} +\alias{lmAMMData} + + +\title{Firm returns, market returns (Nifty) and currency returns (INR/USD) for AMM estimation} + +\description{The data series is a daily time-series zoo object. The sample range for the data is from 2012-02-01 to 2014-01-31. It consists daily time series for firm returns (Infosys and TCS), market returns (Nifty returns) and currency returns (INR/USD). This data is used to demonstrate the AMM estimation.} + +\usage{data(lmAMMData)} + +\author{Vikram Bahure} + +\examples{ +library(zoo) +data(lmAMMData) +str(lmAMMData) +} + +\keyword{lmAMMData} \ No newline at end of file Modified: pkg/man/manyfirmsAMM.Rd =================================================================== --- pkg/man/manyfirmsAMM.Rd 2014-02-06 12:51:28 UTC (rev 163) +++ pkg/man/manyfirmsAMM.Rd 2014-02-08 07:53:10 UTC (rev 164) @@ -42,23 +42,21 @@ \examples{ # Running manyfirmsAMM() involves as many steps as working with onefirmAMM. - -data("y3c3", package="eventstudies") -NIFTY_INDEX <- y3c3$NIFTY_INDEX -INRUSD <- y3c3$INRUSD -Company_A <- y3c3$Company_A -Company_B <- y3c3$Company_B -Company_C <- y3c3$Company_C -regressors <- makeX(market.returns=NIFTY_INDEX, others=INRUSD, - switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, - dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", - "2008-01-05","2009-01-03")), verbose=FALSE) -regressand <- cbind(Company_A,Company_B,Company_C) +data("lmAMMData") +regressand <- lmAMMData[,c("Infosys","TCS")] +market.returns <- lmAMMData[,"index.nifty"] +currency.returns <- lmAMMData[,"currency.inrusd"] +## Creating regressors for AMM estimation using makeX function +regressors <- makeX(market.returns, others=currency.returns,, nlags=1, + switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE, + dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", + "2008-01-05","2009-01-03"))) +## Estimating exposure res <- manyfirmsAMM(regressand,regressors,lags=1, dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", "2008-01-05","2009-01-03")),periodnames=c("P1","P2","P3","P4"), verbose=FALSE) - +print(res) } \keyword{manyfirmsAMM} \ No newline at end of file From noreply at r-forge.r-project.org Thu Feb 13 10:52:45 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Thu, 13 Feb 2014 10:52:45 +0100 (CET) Subject: [Eventstudies-commits] r165 - in pkg: R man vignettes Message-ID: <20140213095245.497B918675C@r-forge.r-project.org> Author: vikram Date: 2014-02-13 10:52:44 +0100 (Thu, 13 Feb 2014) New Revision: 165 Added: pkg/man/subperiodmanyfirms.lmAMM.Rd Removed: pkg/man/manyfirmsAMM.Rd pkg/man/y3c3.Rd Modified: pkg/R/lmAmm.R pkg/man/lmAMM.Rd pkg/man/makeX.Rd pkg/man/onefirmAMM.Rd pkg/vignettes/amm.Rnw Log: Renamed and rewrote AMM functions, changed the structure of AMM functions; added new data for AMM examples and separate for eventstudies examples Modified: pkg/R/lmAmm.R =================================================================== --- pkg/R/lmAmm.R 2014-02-08 07:53:10 UTC (rev 164) +++ pkg/R/lmAmm.R 2014-02-13 09:52:44 UTC (rev 165) @@ -1,9 +1,70 @@ +############### +## One firm AMM +############### +## This function takes care of the structural break dates introduced by calculating exposure for sub periods differently +## This function is used when date are provided in the function +subperiod.lmAMM <- function(firm.returns,X,nlags=1,verbose=FALSE,dates=NULL,residual=TRUE){ + ## Creating empty frames + if(is.null(dates)){ + dates.no <- c(start(firm.returns),end(firm.returns)) + } 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 <- lmAMM(firm.returns,X,verbose=verbose,nlags=nlags) + 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]) + rhs <- window(X,start=dates[1],end=dates[1+1]) + res <- lmAMM(firm.returns=tmp, + X=rhs, + verbose=verbose, + nlags=nlags) + exposures[1,] <- res$exposure + periodnames <- c(periodnames,paste(dates[1],dates[1+1],sep=" TO ")) + sds[1,] <- res$s.exposure + m.residuals <- xts(res$residuals,as.Date(attr(res$residuals,"names"))) + colnames(m.residuals) <- paste(dates[1],"to",dates[1+1],sep=".") + for(i in 2:(length(dates)-1)){ + tmp <- window(firm.returns,start=dates[i],end=dates[i+1]) + rhs <- window(X,start=dates[i],end=dates[i+1]) + res <- lmAMM(firm.returns=tmp, + X=rhs, + verbose=verbose, + nlags=nlags) + exposures[i,] <- res$exposure + periodnames <- c(periodnames,paste(dates[i],dates[i+1],sep=" TO ")) + sds[i,] <- res$s.exposure + period.resid <- xts(res$residuals,as.Date(attr(res$residuals,"names"))) + colnames(period.resid) <- paste(dates[i],"to",dates[i+1],sep=".") + m.residuals <- merge(m.residuals, period.resid, all=TRUE) + } + rownames(exposures) <- rownames(sds) <- periodnames + rval <- list(exposures=exposures,sds=sds,residuals=m.residuals) + } + return(rval) +} - ######################## # Many firms AMM ######################## -manyfirmsAMM <- +subperiodmanyfirms.lmAMM <- function(regressand,regressors, lags,dates=NULL, periodnames=NULL,verbose=FALSE){ require("doMC") Modified: pkg/man/lmAMM.Rd =================================================================== --- pkg/man/lmAMM.Rd 2014-02-08 07:53:10 UTC (rev 164) +++ pkg/man/lmAMM.Rd 2014-02-13 09:52:44 UTC (rev 165) @@ -55,7 +55,7 @@ \examples{ # lmAMM data("lmAMMData") -firm.returns <- lmAMMData[,c("Infosys")] +firm.returns <- lmAMMData[,"Infosys"] market.returns <- lmAMMData[,"index.nifty"] currency.returns <- lmAMMData[,"currency.inrusd"] ## Creating regressors for AMM estimation using makeX function Modified: pkg/man/makeX.Rd =================================================================== --- pkg/man/makeX.Rd 2014-02-08 07:53:10 UTC (rev 164) +++ pkg/man/makeX.Rd 2014-02-13 09:52:44 UTC (rev 165) @@ -1,7 +1,7 @@ \name{makeX} \alias{makeX} \title{A function to prepare explanatory variables for computation -of Augmented Market Models +of Augmented Market Models used in lmAMM function } \description{ @@ -63,11 +63,9 @@ \examples{ # makeX -data("firmExposuresData") -firm.returns <- firmExposuresData$Company_A -market.returns <- firmExposuresData$NIFTY_INDEX -rM2 <- firmExposuresData$usdinr -rM3 <- firmExposuresData$baa +data("lmAMMData") +market.returns <- lmAMMData$index.nifty +rM2 <- lmAMMData$currency.inrusd X <- makeX(market.returns, others=rM2, switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE) } Deleted: pkg/man/manyfirmsAMM.Rd =================================================================== --- pkg/man/manyfirmsAMM.Rd 2014-02-08 07:53:10 UTC (rev 164) +++ pkg/man/manyfirmsAMM.Rd 2014-02-13 09:52:44 UTC (rev 165) @@ -1,62 +0,0 @@ -\name{manyfirmsAMM} -\alias{manyfirmsAMM} - -\title{A function to compute AMM for multiple firms across several -periods} - -\description{ - This function will compute AMM for multiple firms at once using the - matrix of data obtained from \code{makeX}, and a matrix of LHS variables} - -\usage{ -manyfirmsAMM(regressand, regressors, lags, dates = NULL, periodnames = NULL, verbose = FALSE) -} - -\arguments{ - \item{regressand}{A zoo matrix of all firms for which AMM is to - be computed} - - \item{regressors}{A zoo matrix containing at least two - regressors generally obtained after applying the makeX function } - - \item{lags}{ Specifies the number of lags to be used in the - market model} - - \item{dates}{A set of dates that mark out subperiods of - interest. If dates is NULL then full period is considered.} - - \item{periodnames}{Name for each subperiod that has been marked - by the dates above.} - - \item{verbose}{Whether detailed print while running this - function is required} -} - -\section{Warning}{Do not have any space between names provided under 'periodnames'} - -\author{Ajay Shah, Vimal Balasubramaniam} - -\seealso{ -\code{\link{onefirmAMM}} -} - -\examples{ -# Running manyfirmsAMM() involves as many steps as working with onefirmAMM. -data("lmAMMData") -regressand <- lmAMMData[,c("Infosys","TCS")] -market.returns <- lmAMMData[,"index.nifty"] -currency.returns <- lmAMMData[,"currency.inrusd"] -## Creating regressors for AMM estimation using makeX function -regressors <- makeX(market.returns, others=currency.returns,, nlags=1, - switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE, - dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", - "2008-01-05","2009-01-03"))) -## Estimating exposure -res <- manyfirmsAMM(regressand,regressors,lags=1, - dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", - "2008-01-05","2009-01-03")),periodnames=c("P1","P2","P3","P4"), - verbose=FALSE) -print(res) -} - -\keyword{manyfirmsAMM} \ No newline at end of file Modified: pkg/man/onefirmAMM.Rd =================================================================== --- pkg/man/onefirmAMM.Rd 2014-02-08 07:53:10 UTC (rev 164) +++ pkg/man/onefirmAMM.Rd 2014-02-13 09:52:44 UTC (rev 165) @@ -1,5 +1,5 @@ -\name{onefirmAMM} -\alias{onefirmAMM} +\name{subperiod.lmAMM} +\alias{subperiod.lmAMM} \title{A function that estimates exposure for a single firm over multiple periods} @@ -10,7 +10,7 @@ } \usage{ -onefirmAMM(firm.returns, X, nlags = 1, verbose = FALSE, dates = NULL, residual = TRUE) +subperiod.lmAMM(firm.returns, X, nlags = 1, verbose = FALSE, dates = NULL, residual = TRUE) } \arguments{ @@ -27,7 +27,7 @@ \item{verbose}{Default is FALSE. When set to TRUE, the function prints detailed results of using the function. } - \item{dates}{Default is NULL. If no dates are mentioned, onefirmAMM does + \item{dates}{Default is NULL. If no dates are mentioned, subperiod.lmAMM does what firmExposures() would do, i.e., estimate exposures for the full time period. } \item{residual}{Returns AMM Residuals if TRUE, AMM exposure @@ -43,23 +43,20 @@ \code{\link{manyfirmsAMM}}} \examples{ -# Create RHS before running onefirmAMM() -data("y3c3") -NIFTY_INDEX <- y3c3$NIFTY_INDEX -INRUSD <- y3c3$INRUSD -Company_A <- y3c3$Company_A -rhs.dat <- makeX(NIFTY_INDEX, others=INRUSD, - switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, - dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", - "2008-01-05","2009-01-03")), verbose=FALSE) - +# Create RHS before running subperiod.lmAMM() +data("lmAMMData") +firm.returns <- lmAMMData$Infosys +market.returns <- lmAMMData$index.nifty +currency.returns <- lmAMMData$currency.inrusd +regressors <- makeX(market.returns, others=currency.returns, + switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), verbose=FALSE) # Run AMM for one firm across different periods -onefirmAMM(firm.returns=Company_A, - X=rhs.dat, - nlags=1, - verbose=TRUE, - dates= as.Date(c("2005-01-15","2006-01-07","2007-01-06", - "2008-01-05","2009-01-03"))) +res <- subperiod.lmAMM(firm.returns, + X=regressors, + nlags=1, + verbose=TRUE, + dates= as.Date(c("2012-02-01","2013-01-01","2014-01-31"))) } -\keyword{onefirmAMM} \ No newline at end of file +\keyword{subperiod.lmAMM} \ No newline at end of file Copied: pkg/man/subperiodmanyfirms.lmAMM.Rd (from rev 164, pkg/man/manyfirmsAMM.Rd) =================================================================== --- pkg/man/subperiodmanyfirms.lmAMM.Rd (rev 0) +++ pkg/man/subperiodmanyfirms.lmAMM.Rd 2014-02-13 09:52:44 UTC (rev 165) @@ -0,0 +1,61 @@ +\name{subperiodmanyfirms.lmAMM} +\alias{subperiodmanyfirms.lmAMM} + +\title{A function to compute AMM for multiple firms across several +periods} + +\description{ + This function will compute AMM for multiple firms at once using the + matrix of data obtained from \code{makeX}, and a matrix of LHS variables} + +\usage{ +subperiodmanyfirms.lmAMM(regressand, regressors, lags, dates = NULL, periodnames = NULL, verbose = FALSE) +} + +\arguments{ + \item{regressand}{A zoo matrix of all firms for which AMM is to + be computed} + + \item{regressors}{A zoo matrix containing at least two + regressors generally obtained after applying the makeX function } + + \item{lags}{ Specifies the number of lags to be used in the + market model} + + \item{dates}{A set of dates that mark out subperiods of + interest. If dates is NULL then full period is considered.} + + \item{periodnames}{Name for each subperiod that has been marked + by the dates above.} + + \item{verbose}{Whether detailed print while running this + function is required} +} + +\section{Warning}{Do not have any space between names provided under 'periodnames'} + +\author{Ajay Shah, Vimal Balasubramaniam} + +\seealso{ +\code{\link{onefirmAMM}} +} + +\examples{ +# Running subperiodmanyfirms.lmAMM() involves as many steps as working with onefirmAMM. +data("lmAMMData") +regressand <- lmAMMData[,c("Infosys","TCS")] +market.returns <- lmAMMData[,"index.nifty"] +currency.returns <- lmAMMData[,"currency.inrusd"] +## Creating regressors for AMM estimation using makeX function +regressors <- makeX(market.returns, others=currency.returns, nlags=1, + switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31"))) +## Estimating exposure +res <- subperiodmanyfirms.lmAMM(regressand,regressors,lags=1, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), + periodnames=c("P1","P2"), + verbose=FALSE) +print(res) +} + +\keyword{subperiodmanyfirms.lmAMM} \ No newline at end of file Deleted: pkg/man/y3c3.Rd =================================================================== --- pkg/man/y3c3.Rd 2014-02-08 07:53:10 UTC (rev 164) +++ pkg/man/y3c3.Rd 2014-02-13 09:52:44 UTC (rev 165) @@ -1,17 +0,0 @@ -\name{y3c3} -\alias{y3c3} - - -\title{Three years and three random companies} - -\description{Data for testing and examples in the package} - -\usage{data(y3c3)} - -\examples{ -library(zoo) -data(y3c3) -str(y3c3) -} - -\keyword{y3c3} \ No newline at end of file Modified: pkg/vignettes/amm.Rnw =================================================================== --- pkg/vignettes/amm.Rnw 2014-02-08 07:53:10 UTC (rev 164) +++ pkg/vignettes/amm.Rnw 2014-02-13 09:52:44 UTC (rev 165) @@ -87,36 +87,34 @@ returns before using AMM model. <<>>= -# Create RHS before running onefirmAMM() +# Create RHS before running subperiod.lmAMM() library(eventstudies) -data("y3c3") -NIFTY <- y3c3$NIFTY_INDEX -INRUSD <- y3c3$INRUSD -Company_A <- y3c3$Company_A -regressors <- makeX(NIFTY, others=INRUSD, +data("lmAMMData") +nifty <- lmAMMData$index.nifty +inrusd <- lmAMMData$currency.inrusd +firm.returns <- lmAMMData$Infosys +regressors <- makeX(nifty, others=inrusd, switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, - dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", - "2008-01-05","2009-01-03")), verbose=FALSE) + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), verbose=FALSE) @ \subsubsection{Getting currency exposure} -The output of {makeX} function is used in \textit{onefirmAMM} and +The output of {makeX} function is used in \textit{subperiod.lmAMM} and \textit{AMM} function to get currency exposure of the firms and AMM residuals respectively. In the example below, we demonstrate the use -of \textit{onefirmAMM} function to estimate currency exposure for +of \textit{subperiod.lmAMM} function to estimate currency exposure for firms. -% MakeX and onefirmAMM +% MakeX and subperiod.lmAMM <<>>= # Run AMM for one firm across different periods deprintize<-function(f){ return(function(...) {capture.output(w<-f(...));return(w);}); } -firm.exposure <- deprintize(onefirmAMM)(firm.returns=Company_A, - X=regressors, - nlags=1, - verbose=TRUE, - dates= as.Date(c("2005-01-15", - "2006-01-07","2007-01-06", - "2008-01-05","2009-01-03"))) +firm.exposure <- deprintize(subperiod.lmAMM)(firm.returns=firm.returns, + X=regressors, + nlags=1, + verbose=TRUE, + dates= as.Date(c("2012-02-01", + "2013-01-01","2014-01-31"))) str(firm.exposure) @ From noreply at r-forge.r-project.org Thu Feb 13 10:55:06 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Thu, 13 Feb 2014 10:55:06 +0100 (CET) Subject: [Eventstudies-commits] r166 - in pkg: data man Message-ID: <20140213095506.35E7E1867F7@r-forge.r-project.org> Author: vikram Date: 2014-02-13 10:55:05 +0100 (Thu, 13 Feb 2014) New Revision: 166 Added: pkg/man/subperiod.lmAMM.Rd Removed: pkg/data/inr.rda pkg/data/nifty.index.rda pkg/man/onefirmAMM.Rd Log: Removing reduntant data sets Deleted: pkg/data/inr.rda =================================================================== (Binary files differ) Deleted: pkg/data/nifty.index.rda =================================================================== (Binary files differ) Deleted: pkg/man/onefirmAMM.Rd =================================================================== --- pkg/man/onefirmAMM.Rd 2014-02-13 09:52:44 UTC (rev 165) +++ pkg/man/onefirmAMM.Rd 2014-02-13 09:55:05 UTC (rev 166) @@ -1,62 +0,0 @@ -\name{subperiod.lmAMM} -\alias{subperiod.lmAMM} - -\title{A function that estimates exposure for a single firm over multiple periods} - -\description{This function typically utilises an firm.returns vector - and an X matrix of explanatory variables obtained out of - using the makeX function. This would compute the exposure - for all columns in X for the specified time periods. -} - -\usage{ -subperiod.lmAMM(firm.returns, X, nlags = 1, verbose = FALSE, dates = NULL, residual = TRUE) -} - -\arguments{ - \item{firm.returns}{A vector of data for one firm - } - \item{X}{A matrix of explanatory variables obtained from - the makeX function. The first variable is always the - stock market index. Other variables could be risk factors - such as currency or bond returns, or foreign portfolio inflows. - } - \item{nlags}{Number of lags of explanatory variables. When unspecified - the best lag using the AIC is used. - } - \item{verbose}{Default is FALSE. When set to TRUE, the function - prints detailed results of using the function. - } - \item{dates}{Default is NULL. If no dates are mentioned, subperiod.lmAMM does - what firmExposures() would do, i.e., estimate exposures for the full time period. - } - \item{residual}{Returns AMM Residuals if TRUE, AMM exposure - otherwise. Defaults to TRUE.} -} -\value{ The function returns the exposures, HAC adjusted standard - errors, the number of lags used, and the residuals from the fitted - model.} - -\author{Vimal Balasubramaniam} - -\seealso{ \code{\link{firmExposures}}, -\code{\link{manyfirmsAMM}}} - -\examples{ -# Create RHS before running subperiod.lmAMM() -data("lmAMMData") -firm.returns <- lmAMMData$Infosys -market.returns <- lmAMMData$index.nifty -currency.returns <- lmAMMData$currency.inrusd -regressors <- makeX(market.returns, others=currency.returns, - switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, - dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), verbose=FALSE) -# Run AMM for one firm across different periods -res <- subperiod.lmAMM(firm.returns, - X=regressors, - nlags=1, - verbose=TRUE, - dates= as.Date(c("2012-02-01","2013-01-01","2014-01-31"))) -} - -\keyword{subperiod.lmAMM} \ No newline at end of file Copied: pkg/man/subperiod.lmAMM.Rd (from rev 165, pkg/man/onefirmAMM.Rd) =================================================================== --- pkg/man/subperiod.lmAMM.Rd (rev 0) +++ pkg/man/subperiod.lmAMM.Rd 2014-02-13 09:55:05 UTC (rev 166) @@ -0,0 +1,62 @@ +\name{subperiod.lmAMM} +\alias{subperiod.lmAMM} + +\title{A function that estimates exposure for a single firm over multiple periods} + +\description{This function typically utilises an firm.returns vector + and an X matrix of explanatory variables obtained out of + using the makeX function. This would compute the exposure + for all columns in X for the specified time periods. +} + +\usage{ +subperiod.lmAMM(firm.returns, X, nlags = 1, verbose = FALSE, dates = NULL, residual = TRUE) +} + +\arguments{ + \item{firm.returns}{A vector of data for one firm + } + \item{X}{A matrix of explanatory variables obtained from + the makeX function. The first variable is always the + stock market index. Other variables could be risk factors + such as currency or bond returns, or foreign portfolio inflows. + } + \item{nlags}{Number of lags of explanatory variables. When unspecified + the best lag using the AIC is used. + } + \item{verbose}{Default is FALSE. When set to TRUE, the function + prints detailed results of using the function. + } + \item{dates}{Default is NULL. If no dates are mentioned, subperiod.lmAMM does + what firmExposures() would do, i.e., estimate exposures for the full time period. + } + \item{residual}{Returns AMM Residuals if TRUE, AMM exposure + otherwise. Defaults to TRUE.} +} +\value{ The function returns the exposures, HAC adjusted standard + errors, the number of lags used, and the residuals from the fitted + model.} + +\author{Vimal Balasubramaniam} + +\seealso{ \code{\link{firmExposures}}, +\code{\link{manyfirmsAMM}}} + +\examples{ +# Create RHS before running subperiod.lmAMM() +data("lmAMMData") +firm.returns <- lmAMMData$Infosys +market.returns <- lmAMMData$index.nifty +currency.returns <- lmAMMData$currency.inrusd +regressors <- makeX(market.returns, others=currency.returns, + switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), verbose=FALSE) +# Run AMM for one firm across different periods +res <- subperiod.lmAMM(firm.returns, + X=regressors, + nlags=1, + verbose=TRUE, + dates= as.Date(c("2012-02-01","2013-01-01","2014-01-31"))) +} + +\keyword{subperiod.lmAMM} \ No newline at end of file From noreply at r-forge.r-project.org Thu Feb 13 11:18:36 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Thu, 13 Feb 2014 11:18:36 +0100 (CET) Subject: [Eventstudies-commits] r167 - in pkg: data man Message-ID: <20140213101836.34CCE186B8B@r-forge.r-project.org> Author: vikram Date: 2014-02-13 11:18:35 +0100 (Thu, 13 Feb 2014) New Revision: 167 Added: pkg/data/inr.rda pkg/data/nifty.index.rda Modified: pkg/man/makeX.Rd Log: Adding data which was deleted by mistake; and some minor fixes Added: pkg/data/inr.rda =================================================================== (Binary files differ) Property changes on: pkg/data/inr.rda ___________________________________________________________________ Added: svn:mime-type + application/x-gzip Added: pkg/data/nifty.index.rda =================================================================== (Binary files differ) Property changes on: pkg/data/nifty.index.rda ___________________________________________________________________ Added: svn:mime-type + application/x-gzip Modified: pkg/man/makeX.Rd =================================================================== --- pkg/man/makeX.Rd 2014-02-13 09:55:05 UTC (rev 166) +++ pkg/man/makeX.Rd 2014-02-13 10:18:35 UTC (rev 167) @@ -65,8 +65,8 @@ # makeX data("lmAMMData") market.returns <- lmAMMData$index.nifty -rM2 <- lmAMMData$currency.inrusd -X <- makeX(market.returns, others=rM2, +currency.returns <- lmAMMData$currency.inrusd +X <- makeX(market.returns, others=currency.returns, switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE) } From noreply at r-forge.r-project.org Thu Feb 13 12:53:23 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Thu, 13 Feb 2014 12:53:23 +0100 (CET) Subject: [Eventstudies-commits] r168 - / pkg/R pkg/man pkg/vignettes Message-ID: <20140213115323.840A2186866@r-forge.r-project.org> Author: vikram Date: 2014-02-13 12:53:23 +0100 (Thu, 13 Feb 2014) New Revision: 168 Added: pkg/man/manyfirmssubperiod.lmAMM.Rd Removed: pkg/man/subperiodmanyfirms.lmAMM.Rd Modified: pkg/R/lmAmm.R pkg/vignettes/amm.Rnw todo.org Log: Done with AMM functions moving on to eventstudies Modified: pkg/R/lmAmm.R =================================================================== --- pkg/R/lmAmm.R 2014-02-13 10:18:35 UTC (rev 167) +++ pkg/R/lmAmm.R 2014-02-13 11:53:23 UTC (rev 168) @@ -64,7 +64,7 @@ ######################## # Many firms AMM ######################## -subperiodmanyfirms.lmAMM <- +manyfirmssubperiod.lmAMM <- function(regressand,regressors, lags,dates=NULL, periodnames=NULL,verbose=FALSE){ require("doMC") Copied: pkg/man/manyfirmssubperiod.lmAMM.Rd (from rev 165, pkg/man/subperiodmanyfirms.lmAMM.Rd) =================================================================== --- pkg/man/manyfirmssubperiod.lmAMM.Rd (rev 0) +++ pkg/man/manyfirmssubperiod.lmAMM.Rd 2014-02-13 11:53:23 UTC (rev 168) @@ -0,0 +1,61 @@ +\name{manyfirmssubperiod.lmAMM} +\alias{manyfirmssubperiod.lmAMM} + +\title{A function to compute AMM for multiple firms across several +periods} + +\description{ + This function will compute AMM for multiple firms at once using the + matrix of data obtained from \code{makeX}, and a matrix of LHS variables} + +\usage{ +manyfirmssubperiod.lmAMM(regressand, regressors, lags, dates = NULL, periodnames = NULL, verbose = FALSE) +} + +\arguments{ + \item{regressand}{A zoo matrix of all firms for which AMM is to + be computed} + + \item{regressors}{A zoo matrix containing at least two + regressors generally obtained after applying the makeX function } + + \item{lags}{ Specifies the number of lags to be used in the + market model} + + \item{dates}{A set of dates that mark out subperiods of + interest. If dates is NULL then full period is considered.} + + \item{periodnames}{Name for each subperiod that has been marked + by the dates above.} + + \item{verbose}{Whether detailed print while running this + function is required} +} + +\section{Warning}{Do not have any space between names provided under 'periodnames'} + +\author{Ajay Shah, Vimal Balasubramaniam} + +\seealso{ +\code{\link{onefirmAMM}} +} + +\examples{ +# Running manyfirmssubperiod.lmAMM() involves as many steps as working with onefirmAMM. +data("lmAMMData") +regressand <- lmAMMData[,c("Infosys","TCS")] +market.returns <- lmAMMData[,"index.nifty"] +currency.returns <- lmAMMData[,"currency.inrusd"] +## Creating regressors for AMM estimation using makeX function +regressors <- makeX(market.returns, others=currency.returns, nlags=1, + switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31"))) +## Estimating exposure +res <- manyfirmssubperiod.lmAMM(regressand,regressors,lags=1, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), + periodnames=c("P1","P2"), + verbose=FALSE) +print(res) +} + +\keyword{manyfirmssubperiod.lmAMM} \ No newline at end of file Deleted: pkg/man/subperiodmanyfirms.lmAMM.Rd =================================================================== --- pkg/man/subperiodmanyfirms.lmAMM.Rd 2014-02-13 10:18:35 UTC (rev 167) +++ pkg/man/subperiodmanyfirms.lmAMM.Rd 2014-02-13 11:53:23 UTC (rev 168) @@ -1,61 +0,0 @@ -\name{subperiodmanyfirms.lmAMM} -\alias{subperiodmanyfirms.lmAMM} - -\title{A function to compute AMM for multiple firms across several -periods} - -\description{ - This function will compute AMM for multiple firms at once using the - matrix of data obtained from \code{makeX}, and a matrix of LHS variables} - -\usage{ -subperiodmanyfirms.lmAMM(regressand, regressors, lags, dates = NULL, periodnames = NULL, verbose = FALSE) -} - -\arguments{ - \item{regressand}{A zoo matrix of all firms for which AMM is to - be computed} - - \item{regressors}{A zoo matrix containing at least two - regressors generally obtained after applying the makeX function } - - \item{lags}{ Specifies the number of lags to be used in the - market model} - - \item{dates}{A set of dates that mark out subperiods of - interest. If dates is NULL then full period is considered.} - - \item{periodnames}{Name for each subperiod that has been marked - by the dates above.} - - \item{verbose}{Whether detailed print while running this - function is required} -} - -\section{Warning}{Do not have any space between names provided under 'periodnames'} - -\author{Ajay Shah, Vimal Balasubramaniam} - -\seealso{ -\code{\link{onefirmAMM}} -} - -\examples{ -# Running subperiodmanyfirms.lmAMM() involves as many steps as working with onefirmAMM. -data("lmAMMData") -regressand <- lmAMMData[,c("Infosys","TCS")] -market.returns <- lmAMMData[,"index.nifty"] -currency.returns <- lmAMMData[,"currency.inrusd"] -## Creating regressors for AMM estimation using makeX function -regressors <- makeX(market.returns, others=currency.returns, nlags=1, - switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE, - dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31"))) -## Estimating exposure -res <- subperiodmanyfirms.lmAMM(regressand,regressors,lags=1, - dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), - periodnames=c("P1","P2"), - verbose=FALSE) -print(res) -} - -\keyword{subperiodmanyfirms.lmAMM} \ No newline at end of file Modified: pkg/vignettes/amm.Rnw =================================================================== --- pkg/vignettes/amm.Rnw 2014-02-13 10:18:35 UTC (rev 167) +++ pkg/vignettes/amm.Rnw 2014-02-13 11:53:23 UTC (rev 168) @@ -138,16 +138,12 @@ \textit{nifty.index} (market returns). <<>>= # Create RHS before running AMM() -data(StockPriceReturns) -data(SplitDates) -data(nifty.index) -data(inr) -inrusd <- diff(log(inr))*100 -all.data <- merge(StockPriceReturns,nifty.index,inrusd,all=TRUE) -StockPriceReturns <- all.data[,-which(colnames(all.data)%in%c("nifty.index", -"inr"))] -nifty.index <- all.data$nifty.index -inrusd <- all.data$inr +data("lmAMMData") +firm.returns <- lmAMMData[,"Infosys"] +market.returns <- lmAMMData[,"index.nifty"] +currency.returns <- lmAMMData[,"currency.inrusd"] +regressors <- makeX(market.returns, others=currency.returns, + switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE) @ Inputs into the \texttt{AMM} model also include \texttt{firm.returns} @@ -157,10 +153,8 @@ <<>>= # 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=1) +res.amm <- lmAMM(firm.returns=firm.returns, X=regressors, + verbose=FALSE, nlags=1) @ We can also perform event study analysis, directly on AMM residuals Modified: todo.org =================================================================== --- todo.org 2014-02-13 10:18:35 UTC (rev 167) +++ todo.org 2014-02-13 11:53:23 UTC (rev 168) @@ -27,6 +27,7 @@ - citation for Corrado (2011) seems misplaced in the beginning. It seems as though we are replicating his work on stock-splits, which isn't the case + - change the naming lmAMM and there is no more AMM wrapper * man pages - modify ess.Rd: put reference to Patnaik, Shah, and Singh (2013) From noreply at r-forge.r-project.org Thu Feb 13 15:45:45 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Thu, 13 Feb 2014 15:45:45 +0100 (CET) Subject: [Eventstudies-commits] r169 - in pkg: R vignettes Message-ID: <20140213144545.D14AA186736@r-forge.r-project.org> Author: vikram Date: 2014-02-13 15:45:45 +0100 (Thu, 13 Feb 2014) New Revision: 169 Modified: pkg/R/eventstudy.R pkg/vignettes/eventstudies.Rnw Log: Modified eventstudy function; Work in progress Modified: pkg/R/eventstudy.R =================================================================== --- pkg/R/eventstudy.R 2014-02-13 11:53:23 UTC (rev 168) +++ pkg/R/eventstudy.R 2014-02-13 14:45:45 UTC (rev 169) @@ -24,8 +24,25 @@ ### Run models ## AMM if (type == "AMM") { - tmp.outputModel <- AMM(firm.returns = firm.returns, ...) - outputModel <- zoo(coredata(tmp.outputModel),index(tmp.outputModel)) + ## AMM residual to time series + timeseriesAMM <- function(firm.returns,X,verbose=FALSE,nlags=1){ + tmp <- resid(lmAMM(firm.returns,X,nlags)) + tmp.res <- zoo(tmp,as.Date(names(tmp))) + } + if(NCOL(firm.returns)==1){ + ## One firm + outputModel <- timeseriesAMM(firm.returns=StockPriceReturns[,1], + X=regressors, verbose=FALSE, nlags=1) + } else { + ## More than one firm + # Extracting and merging + tmp.resid <- sapply(colnames(StockPriceReturns)[1:3],function(y) + timeseriesAMM(firm.returns=StockPriceReturns[,y], + X=regressors, + verbose=FALSE, + nlags=1)) + outputModel <- do.call("merge",tmp.resid) + } } ## marketResidual @@ -82,6 +99,27 @@ ## Providing event frame as default output result <- es.w } - - return(list(result, es$outcomes)) + final.result <- list(eventstudy.output=result, + outcomes=as.character(es$outcomes), + inference=inference.strategy, nlags=nlags) + class(final.result) <- "es" + return(final.result) } + +######################### +## Functions for class es +######################### +print.es <- function(){ + + +} + +summary.es <- function(){ + + +} + +plot.es <- function(){ + + +} Modified: pkg/vignettes/eventstudies.Rnw =================================================================== --- pkg/vignettes/eventstudies.Rnw 2014-02-13 11:53:23 UTC (rev 168) +++ pkg/vignettes/eventstudies.Rnw 2014-02-13 14:45:45 UTC (rev 169) @@ -203,17 +203,37 @@ regression appropriately. % AMM model -<<>>= # Create RHS before running AMM() +<<>>= # Create RHS before running lmAMM() data(inr) -inrusd <- diff(log(inr))*100 +inrusd <- diff(log(inr))*100 all.data <- merge(StockPriceReturns,nifty.index,inrusd,all=TRUE) StockPriceReturns <- all.data[,-which(colnames(all.data)%in%c("nifty.index", "inr"))] nifty.index <- all.data$nifty.index inrusd <- all.data$inr -## 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=1) +################### +## AMM residuals ## +################### +## Getting Regressors +regressors <- makeX(market.returns=nifty.index, others=inrusd, + market.returns.purge=TRUE, nlags=1) +## AMM residual to time series +timeseriesAMM <- function(firm.returns,X,verbose=FALSE,nlags=1){ + tmp <- resid(lmAMM(firm.returns,X,nlags)) + tmp.res <- zoo(tmp,as.Date(names(tmp))) +} +## One firm +amm.result <- timeseriesAMM(firm.returns=StockPriceReturns[,1], + X=regressors, verbose=FALSE, nlags=1) +## More than one firm + # Extracting and merging +tmp.resid <- sapply(colnames(StockPriceReturns)[1:3],function(y) + timeseriesAMM(firm.returns=StockPriceReturns[,y], + X=regressors, + verbose=FALSE, + nlags=1)) +amm.resid <- do.call("merge",tmp.resid) @ \subsection{Conversion to event-time frame} From noreply at r-forge.r-project.org Fri Feb 14 04:03:09 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 14 Feb 2014 04:03:09 +0100 (CET) Subject: [Eventstudies-commits] r170 - in pkg: R vignettes Message-ID: <20140214030309.DFE34186FF0@r-forge.r-project.org> Author: vikram Date: 2014-02-14 04:03:08 +0100 (Fri, 14 Feb 2014) New Revision: 170 Modified: pkg/R/eventstudy.R pkg/R/inference.bootstrap.R pkg/vignettes/eventstudies.Rnw Log: Modified eventstudy function, updating man pages for eventstudy, Work in progress Modified: pkg/R/eventstudy.R =================================================================== --- pkg/R/eventstudy.R 2014-02-13 14:45:45 UTC (rev 169) +++ pkg/R/eventstudy.R 2014-02-14 03:03:08 UTC (rev 170) @@ -7,10 +7,6 @@ remap = "cumsum", inference = TRUE, inference.strategy = "bootstrap", - to.plot = TRUE, - xlab = "Event time", - ylab = "Cumulative returns of response series", - main = "Event study plot", ...) { # type = "marketResidual", "excessReturn", "AMM", "None" if (type == "None" && !is.null(firm.returns)) { @@ -87,21 +83,21 @@ if(inference == TRUE){ ## Bootstrap if(inference.strategy == "bootstrap"){ - result <- inference.bootstrap(es.w = es.w, to.plot = to.plot, xlab = xlab, - ylab = ylab, main = main) + result <- inference.bootstrap(es.w = es.w, to.plot = FALSE) } ## Wilcoxon if(inference.strategy == "wilcoxon"){ - result <- inference.wilcox(es.w = es.w, to.plot = to.plot, xlab = xlab, - ylab = ylab, main = main) + result <- inference.wilcox(es.w = es.w, to.plot = FALSE) } } else { ## Providing event frame as default output result <- es.w } + if(to.remap==TRUE){remapping <- remap} else {remapping <- "None"} final.result <- list(eventstudy.output=result, outcomes=as.character(es$outcomes), - inference=inference.strategy, nlags=nlags) + inference=inference.strategy, + width=width, remap=remapping) class(final.result) <- "es" return(final.result) } @@ -109,17 +105,33 @@ ######################### ## Functions for class es ######################### -print.es <- function(){ - - +print.es <- function(es.object){ + cat("The", es.object$inference, "inference output for CI and", + colnames(es.object$eventstudy.output)[2], "response:", "\n") + es.object$eventstudy.output } -summary.es <- function(){ - - +summary.es <- function(es.object){ + cat("Event study", colnames(es.object$eventstudy.output)[2], "response with", + es.object$inference, "inference for CI:\n") + print(es.object$eventstudy.output) + cat("\n","Event outcome has",length(which(es.object$outcomes=="success")), + "successful outcomes out of", length(es.object$outcomes),"events:","\n") + es.object$outcomes } -plot.es <- function(){ - - +plot.es <- function(es.object, xlab="Event time", + ylab="", main="", col.es="dark slate blue"){ + big <- max(abs(es.object$eventstudy.output)) + hilo <- c(-big,big) + width <- (nrow(es.object$eventstudy.output)-1)/2 + plot(-width:width, es.object$eventstudy.output[,2], type="l", lwd=2, ylim=hilo, + col=col.es,xlab= xlab, ylab = ylab, + main=paste(main)) + points(-width:width, es.object$eventstudy.output[,2]) + lines(-width:width, es.object$eventstudy.output[,"2.5%"], + lwd=1, lty=2, col=col.es) + lines(-width:width, es.object$eventstudy.output[,"97.5%"], + lwd=1, lty=2, col=col.es) + abline(h=0,v=0) } Modified: pkg/R/inference.bootstrap.R =================================================================== --- pkg/R/inference.bootstrap.R 2014-02-13 14:45:45 UTC (rev 169) +++ pkg/R/inference.bootstrap.R 2014-02-14 03:03:08 UTC (rev 170) @@ -30,17 +30,19 @@ } # Plotting inference -plotInference <- function(inference, xlab, ylab, main){ - big <- max(abs(inference)) +plot.es <- function(es.object, xlab="Event time", + ylab="", main="", col.es="dark slate blue"){ + big <- max(abs(es.object$eventstudy.output)) hilo <- c(-big,big) - width <- (nrow(inference)-1)/2 - plot(-width:width, inference[,2], type="l", lwd=2, ylim=hilo, - col="dark slate blue", - xlab= xlab, ylab = ylab, + width <- (nrow(es.object$eventstudy.output)-1)/2 + plot(-width:width, es.object$eventstudy.output[,2], type="l", lwd=2, ylim=hilo, + col=col.es,xlab= xlab, ylab = ylab, main=paste(main)) - points(-width:width, inference[,2]) - lines(-width:width, inference[,"2.5%"], lwd=1, lty=2, col="dark slate blue") - lines(-width:width, inference[,"97.5%"], lwd=1, lty=2, col="dark slate blue") + points(-width:width, es.object$eventstudy.output[,2]) + lines(-width:width, es.object$eventstudy.output[,"2.5%"], + lwd=1, lty=2, col=col.es) + lines(-width:width, es.object$eventstudy.output[,"97.5%"], + lwd=1, lty=2, col=col.es) abline(h=0,v=0) } @@ -66,7 +68,7 @@ rownames(results) <- rownames(es.w) colnames(results) <- c("2.5%","Mean","97.5%") if(to.plot==TRUE){ - plotInference(inference=results, xlab, ylab, main) + plot.es(inference=results, xlab, ylab, main) } return(results) } @@ -101,7 +103,7 @@ colnames(result) <- c("2.5%","Median","97.5%") rownames(result) <- rownames(Median) if(to.plot == TRUE){ - plotInference(inference = result, xlab, ylab, main) + plot.es(inference = result, xlab, ylab, main) } return(result) } Modified: pkg/vignettes/eventstudies.Rnw =================================================================== --- pkg/vignettes/eventstudies.Rnw 2014-02-13 14:45:45 UTC (rev 169) +++ pkg/vignettes/eventstudies.Rnw 2014-02-14 03:03:08 UTC (rev 170) @@ -317,34 +317,31 @@ ## Event study without adjustment es.na <- eventstudy(firm.returns = StockPriceReturns, eventList = SplitDates, width = 10, to.remap = TRUE, - remap = "cumsum", - to.plot = TRUE, inference = TRUE, + remap = "cumsum", inference = TRUE, inference.strategy = "wilcoxon", type = "None") ## Event study using market residual and bootstrap es.mm <- eventstudy(firm.returns = StockPriceReturns, eventList = SplitDates, width = 10, to.remap = TRUE, remap = "cumsum", - to.plot = FALSE, inference = TRUE, - inference.strategy = "bootstrap", + inference = TRUE, inference.strategy = "bootstrap", type = "marketResidual", market.returns = nifty.index) -es.mm ## Event study using excess return and bootstrap es.er <- eventstudy(firm.returns = StockPriceReturns, eventList = SplitDates, width = 10, to.remap = TRUE, remap = "cumsum", - to.plot = FALSE, inference = TRUE, - inference.strategy = "bootstrap", type = "excessReturn", - market.returns = nifty.index) + inference = TRUE, inference.strategy = "bootstrap", + type = "excessReturn", market.returns = nifty.index) ## Event study using augmented market model (AMM) and bootstrap es.amm <- eventstudy(firm.returns = StockPriceReturns, eventList = SplitDates, width = 10, to.remap = TRUE, remap = "cumsum", - to.plot = FALSE, inference = TRUE, - inference.strategy = "bootstrap", + inference = TRUE, inference.strategy = "bootstrap", type = "AMM", market.returns = nifty.index, others=inrusd, verbose=FALSE, switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1) +print(es.na) +summary(es.na) @ From noreply at r-forge.r-project.org Mon Feb 17 07:19:09 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 17 Feb 2014 07:19:09 +0100 (CET) Subject: [Eventstudies-commits] r171 - in pkg: R man Message-ID: <20140217061910.1103F18691D@r-forge.r-project.org> Author: vikram Date: 2014-02-17 07:19:09 +0100 (Mon, 17 Feb 2014) New Revision: 171 Modified: pkg/R/eventstudy.R pkg/man/eventstudy.Rd Log: modified event study function and respective man page Modified: pkg/R/eventstudy.R =================================================================== --- pkg/R/eventstudy.R 2014-02-14 03:03:08 UTC (rev 170) +++ pkg/R/eventstudy.R 2014-02-17 06:19:09 UTC (rev 171) @@ -93,7 +93,7 @@ ## Providing event frame as default output result <- es.w } - if(to.remap==TRUE){remapping <- remap} else {remapping <- "None"} + if(to.remap==TRUE){remapping <- remap} else {remapping <- "none"} final.result <- list(eventstudy.output=result, outcomes=as.character(es$outcomes), inference=inference.strategy, @@ -108,7 +108,7 @@ print.es <- function(es.object){ cat("The", es.object$inference, "inference output for CI and", colnames(es.object$eventstudy.output)[2], "response:", "\n") - es.object$eventstudy.output + return(es.object$eventstudy.output) } summary.es <- function(es.object){ Modified: pkg/man/eventstudy.Rd =================================================================== --- pkg/man/eventstudy.Rd 2014-02-14 03:03:08 UTC (rev 170) +++ pkg/man/eventstudy.Rd 2014-02-17 06:19:09 UTC (rev 171) @@ -1,9 +1,9 @@ \name{eventstudy} \alias{eventstudy} -\title{A wrapper that performs eventstudies using different methods} +\title{Event study analysis} -\description{This function undertakes an eventstudy based on the inference methodology chosen by the user.} +\description{This function computes event study inference using different inference strategies for various market models} \usage{ eventstudy(firm.returns = NULL, @@ -15,10 +15,7 @@ remap = "cumsum", inference = TRUE, inference.strategy = "bootstrap", - to.plot = TRUE, - xlab = "Event time", - ylab = "Cumulative returns of response series", - main = "Event study plot", ...) + ...) } \arguments{ @@ -31,22 +28,26 @@ \item{is.levels}{If the data is in returns format then is.levels is FALSE else TRUE} \item{inference}{This argument is used to compute confidence interval for the estimator} \item{inference.strategy}{If inference is TRUE then this argument gives an option to select different inference strategy to compute confidence intervals. Default to bootstrap.} - \item{to.plot}{This argument will generate an eventstudy plot of the inference estimated. If to.plot is equal to TRUE then function would generate the plot else it would not. } - \item{xlab}{If to.plot is TRUE then the plot generated will take this X label} - \item{ylab}{If to.plot is TRUE then the plot generated will take this Y label} - \item{main}{If to.plot is TRUE then the plot generated will take this as main title} \item{...}{Accepts specific arguments for the model.} } \details{If type = "AMM", then the default output is "residual". } -\value{ Output is mean estimate of abnormal returns and confidence interval using particular inference strategy +\value{ + A list with class attribute \sQuote{\code{es}} holding the + following elements:\cr + + \item{eventstudy.output}{Output provides mean/median estimate with confidence interval} + \item{outcomes}{A vector has outcome of each event date. It provides 4 outcomes, 'success', 'wrongspan', 'wdatamissing' and 'unitmissing'. 'success' shows the successful use of event date, 'wdatamissing' appears when width data is missing around the event, if event date falls outside the range of physical date then it returns 'wrongspan' and 'unitmissing' when the unit (firm name) is missing in the event list.} + \item{inference}{Provides information about which inference strategy is utilised to estimate confidence intervals} + \item{width}{Window width for event study output } + \item{remap}{It shows which remapping technique used, for eg. 'cumsum' for cumulative sum, 'cumprod' for cumulative product, 'reindex' for reindexing the series and 'none' for no transformation.} } \author{Vikram Bahure} -\seealso{ \code{\link{AMM}}, +\seealso{ \code{\link{lmAMM}}, \code{\link{marketResidual}}, \code{\link{excessReturn}}, \code{\link{phys2eventtime}}, @@ -64,7 +65,7 @@ ## data("SplitDates") ## Event study without adjustment -## es <- eventstudy(firm.returns = StockPriceReturns, eventList = SplitDates, width = 10,type = "None", to.remap = TRUE, remap = "cumsum", to.plot = FALSE,inference = TRUE, inference.strategy = "bootstrap") +es <- eventstudy(firm.returns = StockPriceReturns, eventList = SplitDates, width = 10,type = "None", to.remap = TRUE, remap = "cumsum",inference = TRUE, inference.strategy = "bootstrap") } From noreply at r-forge.r-project.org Mon Feb 17 10:42:44 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 17 Feb 2014 10:42:44 +0100 (CET) Subject: [Eventstudies-commits] r172 - in pkg: data inst/tests man Message-ID: <20140217094244.45E65184BE1@r-forge.r-project.org> Author: vikram Date: 2014-02-17 10:42:43 +0100 (Mon, 17 Feb 2014) New Revision: 172 Added: pkg/inst/tests/test_lmAMM.R Removed: pkg/inst/tests/test_AMM.R Modified: pkg/data/lmAMMData.rda pkg/man/eventstudy.Rd Log: Modified test cases for new dataset Modified: pkg/data/lmAMMData.rda =================================================================== (Binary files differ) Deleted: pkg/inst/tests/test_AMM.R =================================================================== --- pkg/inst/tests/test_AMM.R 2014-02-17 06:19:09 UTC (rev 171) +++ pkg/inst/tests/test_AMM.R 2014-02-17 09:42:43 UTC (rev 172) @@ -1,126 +0,0 @@ -context("AMM") - -test_that("test.AMM", { - load(system.file("data", "firmExposuresData.rda", package = "eventstudies")) - - firm.returns <- firmExposuresData$Company_A - market.returns <- firmExposuresData$NIFTY_INDEX - inrusd <- firmExposuresData$usdinr - rM3 <- firmExposuresData$baa - - cat("\nDoing Testcase P2") - X <- makeX(market.returns, others=inrusd, - switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE) - a <- firmExposures(firm.returns, X, nlags=0, verbose=FALSE) - expect_that(c(a$exposures, a$s.exposures), - equals(structure(c(0.716160223601197,-0.673093436292401, - 0.152101606133946,1.02143820457251), - .Names = c("market.returns", "z", "market.returns", "z")), tolerance=1e-1)) - - cat("\nDoing Testcase P3") - X <- makeX(market.returns, others=inrusd, - switch.to.innov=TRUE, market.returns.purge=FALSE, verbose=FALSE) - a <- firmExposures(firm.returns, X, nlags=0, verbose=FALSE) - expect_that(c(a$exposures, a$s.exposures), - equals(structure(c(0.716160223601197,-0.673093436292401, - 0.152100337597009,1.02146106755333), - .Names = c("market.returns", "z", "market.returns", "z")), tolerance=1e-1)) - - cat("\nDoing Testcase P4") - a <- firmExposures(firm.returns, X, nlags=1, verbose=FALSE) - expect_that(c(a$exposures, a$s.exposures), - equals(structure(c( 0.736264286484902, -1.450805, - 0.177929844631439, 1.646730), - .Names = c("market.returns","z", "market.returns", "z")),tolerance=1e-1)) - - - cat("\nDoing Testcase P5") - X <- makeX(market.returns, others=inrusd, - switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, verbose=FALSE) - a <- firmExposures(firm.returns, X, nlags=1, verbose=FALSE) - expect_that(c(a$exposures, a$s.exposures), - equals(structure(c(0.7365566,-2.340171, - 0.1653025, 1.1436666), - .Names = c("market.returns", "z", "market.returns", "z")),tolerance=1e-1)) - - cat("\nDoing Testcase P6") - X <- makeX(market.returns, others=cbind(inrusd, rM3), - switch.to.innov=c(FALSE, FALSE), market.returns.purge=FALSE, verbose=FALSE) - a <- firmExposures(firm.returns, X, nlags=0, verbose=FALSE) - expect_that(c(a$exposures, a$s.exposures), - equals(structure(c(0.7230599,-0.7642377, - 0.207374104922771,0.173380799334299, - 1.01806122963342,0.467821650129292), - .Names = c("market.returns", "inrusd", "rM3", "market.returns", "inrusd", "rM3")),tolerance=1e-1)) - - cat("\nDoing Testcase P7") - X <- makeX(market.returns, others=cbind(inrusd, rM3), - switch.to.innov=c(TRUE, TRUE), market.returns.purge=TRUE, nlags=1, verbose=FALSE) - a <- firmExposures(firm.returns, X, nlags=1, verbose=FALSE) - - expect_that(c(a$exposures, a$s.exposures), - equals(structure(c(0.7482719,-1.9468851,-0.4802211, - 0.1740678,1.2455112,0.6146619), - .Names = c("market.returns", "inrusd", "rM3", "market.returns", "inrusd", "rM3")),tolerance=1e-1)) - -################################################################################ - # # - # THE NEXT CASES TESTS THE FUNCTION FOR THREE COMPANIES FOR THREE YEARS # - # # -################################################################################ - - - cat("\nDoing Testcases P8") - load(system.file("data", "y3c3.rda", package = "eventstudies")) - - NIFTY_INDEX <- y3c3$NIFTY_INDEX - INRUSD <- y3c3$INRUSD - Company_A <- y3c3$Company_A - Company_B <- y3c3$Company_B - Company_C <- y3c3$Company_C - - regressors <- makeX(NIFTY_INDEX, others=INRUSD, - switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, - dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", - "2008-01-05","2009-01-03")), verbose=FALSE) - - regressand <- cbind(Company_A,Company_B,Company_C) - - res <- manyfirmsAMM(regressand,regressors,lags=1, - dates=as.Date(c("2005-01-15","2006-01-07","2007-01-06", - "2008-01-05","2009-01-03")),periodnames=c("P1","P2","P3","P4"), - verbose=FALSE) - - expect_that(as.data.frame(res), - - equals(structure(list(market.returns.P1 = c(0.756294904326272, 0.359467326140834,0.914021428042946), - z.P1 = c(-2.23264294525560, -1.05654919420689,0.296635483126946), - market.returns.P2 = c(1.02094561445355, 0.988758963378838,0.879236409569888), - z.P2 = c(-4.72831391695047, -2.0508684999854,-1.02215809586573), - market.returns.P3 = c(1.20585808099744, 0.676388278572118,0.530718379431386), - z.P3 = c(-1.32677083522489, -2.74055730512260, -1.50032216697694), - market.returns.P4 = c(1.11331096371784, 0.437117737120777,0.663182186702262), - z.P4 = c(-2.05336868436562, -1.60350865767951,-0.466253391408585), - market.returns.P1 = c(0.143617135793294, 0.263130891045529,0.154272220123111), - z.P1 = c(1.20226371286803, 1.22122136357895,1.02442932195400), - market.returns.P2 = c(0.203037609116444, 0.123122376136099,0.121880488983820), - z.P2 = c(1.118400430819, 0.798694545623495,1.29755067543957), - market.returns.P3 = c(0.230304109532112, 0.289262660515515,0.164866239494693), - z.P3 = c(1.17618117392934, 0.795008683829453,0.650736332270758), - market.returns.P4 = c(0.231338818884745, 0.213858364836974,0.207154237634752), - z.P4 = c(0.771450066857429, 0.415931231130697,0.696448914066602), - market.returns.P1 = c(5.26604920888266, 1.36611602200152,5.9247311493511), - z.P1 = c(-1.85703263049467, -0.865157804896683,0.289561687438957), - market.returns.P2 = c(5.02835715460001, 8.0307007906172,7.21392256382075), - z.P2 = c(-4.2277468665565, -2.56777576762391,-0.787759673062059), - market.returns.P3 = c(5.23593818385294, 2.33831866638673,3.21908464133114), - z.P3 = c(-1.12803270842405, -3.44720423923131,-2.30557614900882), - market.returns.P4 = c(4.81246929972659, 2.04395903547657,3.20139329165723), - z.P4 = c(-2.66170005367969, -3.85522542589652,-0.669472493949494)), - .Names = c("market.returns.P1", "z.P1", "market.returns.P2","z.P2", "market.returns.P3", "z.P3", - "market.returns.P4", "z.P4", "market.returns.P1", "z.P1","market.returns.P2", "z.P2", "market.returns.P3", "z.P3", - "market.returns.P4", "z.P4", "market.returns.P1", "z.P1", "market.returns.P2", "z.P2", "market.returns.P3", - "z.P3", "market.returns.P4", "z.P4"), - row.names = c("Company_A","Company_B", "Company_C"), class = "data.frame"), - check.attributes=FALSE)) -}) Added: pkg/inst/tests/test_lmAMM.R =================================================================== --- pkg/inst/tests/test_lmAMM.R (rev 0) +++ pkg/inst/tests/test_lmAMM.R 2014-02-17 09:42:43 UTC (rev 172) @@ -0,0 +1,118 @@ +context("AMM") + +test_that("test.AMM", { + load(system.file("data", "lmAMMData.rda", package = "eventstudies")) + + firm.returns <- lmAMMData$Infosys + market.returns <- lmAMMData$index.nifty + inrusd <- lmAMMData$currency.inrusd + rM3 <- lmAMMData$call.money.rate + + cat("\nDoing Testcase P2\n") + X <- makeX(market.returns, others=inrusd, + switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE) + a <- lmAMM(firm.returns, X, nlags=0, verbose=FALSE) + expect_that(c(a$exposures, a$s.exposures), + equals(structure(c(0.7064442,0.3585404, + 0.0966792,0.1062146), + .Names = c("market.returns", "z", + "market.returns", "z")), tolerance=1e-1)) + + cat("\nDoing Testcase P3\n") + X <- makeX(market.returns, others=inrusd, + switch.to.innov=TRUE, market.returns.purge=FALSE, verbose=FALSE) + a <- lmAMM(firm.returns, X, nlags=0, verbose=FALSE) + expect_that(c(a$exposures, a$s.exposures), + equals(structure(c(0.67706599, 0.27939607, + 0.09719514, 0.10192811), + .Names = c("market.returns", "z", + "market.returns", "z")), tolerance=1e-1)) + + cat("\nDoing Testcase P4\n") + a <- lmAMM(firm.returns, X, nlags=1, verbose=FALSE) + expect_that(c(a$exposures, a$s.exposures), + equals(structure(c(0.68343189, 0.61069556, + 0.09794233, 0.12353826), + .Names = c("market.returns","z", + "market.returns", "z")),tolerance=1e-1)) + + + cat("\nDoing Testcase P5\n") + X <- makeX(market.returns, others=inrusd, + switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, verbose=FALSE) + a <- lmAMM(firm.returns, X, nlags=1, verbose=FALSE) + expect_that(c(a$exposures, a$s.exposures), + equals(structure(c(0.68343189, 0.06813816, + 0.09844400, 0.11950216), + .Names = c("market.returns", "z", + "market.returns", "z")),tolerance=1e-1)) + + cat("\nDoing Testcase P6\n") + X <- makeX(market.returns, others=cbind(inrusd, rM3), + switch.to.innov=c(FALSE, FALSE), market.returns.purge=FALSE, verbose=FALSE) + a <- lmAMM(firm.returns, X, nlags=0, verbose=FALSE) + expect_that(c(a$exposures, a$s.exposures), + equals(structure(c(0.70726513, 0.35942623, -77.52744495, + 0.09602279, 0.10668916, 259.10845540), + .Names = c("market.returns", "inrusd", "rM3", + "market.returns", "inrusd", "rM3")), + tolerance=1e-1)) + + cat("\nDoing Testcase P7\n") + X <- makeX(market.returns, others=cbind(inrusd, rM3), + switch.to.innov=c(TRUE, TRUE), market.returns.purge=TRUE, nlags=1, verbose=FALSE) + a <- lmAMM(firm.returns, X, nlags=1, verbose=FALSE) + + expect_that(c(a$exposures, a$s.exposures), + equals(structure(c(6.922458e-01, 6.542345e-02, 1.169788e+03, + 1.038158e-01, 1.214853e-01, 5.786265e+02), + .Names = c("market.returns", "inrusd", "rM3", + "market.returns", "inrusd", "rM3")), + tolerance=1e-1)) + +################################################################################ + # # + # THE NEXT CASES TESTS THE FUNCTION FOR THREE COMPANIES FOR THREE YEARS # + # # +################################################################################ + + + cat("\nDoing Testcases P8\n") + load(system.file("data", "lmAMMData.rda", package = "eventstudies")) + + nifty <- lmAMMData$index.nifty + inrusd <- lmAMMData$currency.inrusd + infosys <- lmAMMData$Infosys + tcs <- lmAMMData$TCS + + regressors <- makeX(nifty, others=inrusd, + switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-20")), + verbose=FALSE) + + regressand <- cbind(infosys,tcs) + + res <- manyfirmssubperiod.lmAMM(regressand,regressors,lags=1, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-20")), + periodnames=c("P1","P2"), + verbose=FALSE) + + match.res <- structure(list(exposures.market.returns.P1=c(0.8446433,0.6875982), + exposures.z.P1 = c(-0.05351359,0.36151838), + exposures.market.returns.P2=c(0.5865497,0.5822848), + exposures.z.P2 = c(0.1375913,-0.0993528), + + sds.market.returns.P1=c(0.1267067,0.1298345), + sds.z.P1 = c(0.1810424, 0.2865279), + sds.market.returns.P2=c(0.1353948,0.1111106), + sds.z.P2 = c(0.1644340,0.2235453), + + sig.market.returns.P1=c(6.666130,5.295958), + sig.z.P1 = c(-0.295586,1.261721), + sig.market.returns.P2=c(4.332144,5.240588), + sig.z.P2 = c(0.8367568,-0.4444415)), + row.names=c("infosys","tcs"),class="data.frame") + + expect_that(as.data.frame(res), + equals(match.res,check.attributes=FALSE,tolerance=1e-1)) +}) Modified: pkg/man/eventstudy.Rd =================================================================== --- pkg/man/eventstudy.Rd 2014-02-17 06:19:09 UTC (rev 171) +++ pkg/man/eventstudy.Rd 2014-02-17 09:42:43 UTC (rev 172) @@ -60,9 +60,9 @@ \examples{ ## Performing event study -## library(eventstudies) -## data("StockPriceReturns") -## data("SplitDates") +library(eventstudies) +data("StockPriceReturns") +data("SplitDates") ## Event study without adjustment es <- eventstudy(firm.returns = StockPriceReturns, eventList = SplitDates, width = 10,type = "None", to.remap = TRUE, remap = "cumsum",inference = TRUE, inference.strategy = "bootstrap") From noreply at r-forge.r-project.org Mon Feb 17 19:23:40 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 17 Feb 2014 19:23:40 +0100 (CET) Subject: [Eventstudies-commits] r173 - pkg Message-ID: <20140217182340.B565A186C83@r-forge.r-project.org> Author: chiraganand Date: 2014-02-17 19:23:40 +0100 (Mon, 17 Feb 2014) New Revision: 173 Modified: pkg/NAMESPACE Log: Removed manyFirmsAMM from NAMESPACE, fixes build error. Modified: pkg/NAMESPACE =================================================================== --- pkg/NAMESPACE 2014-02-17 09:42:43 UTC (rev 172) +++ pkg/NAMESPACE 2014-02-17 18:23:40 UTC (rev 173) @@ -2,7 +2,7 @@ remap.cumsum, remap.cumprod, remap.event.reindex, ees, eesPlot) export(marketResidual, excessReturn) -export(manyfirmsAMM, lmAMM, makeX) +export(lmAMM, makeX) S3method(summary, amm) From noreply at r-forge.r-project.org Tue Feb 18 04:23:30 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 18 Feb 2014 04:23:30 +0100 (CET) Subject: [Eventstudies-commits] r174 - pkg Message-ID: <20140218032330.873FA1869E0@r-forge.r-project.org> Author: vikram Date: 2014-02-18 04:23:27 +0100 (Tue, 18 Feb 2014) New Revision: 174 Modified: pkg/NAMESPACE Log: Modified NAMESPACE with S3 method and other functions Modified: pkg/NAMESPACE =================================================================== --- pkg/NAMESPACE 2014-02-17 18:23:40 UTC (rev 173) +++ pkg/NAMESPACE 2014-02-18 03:23:27 UTC (rev 174) @@ -2,7 +2,7 @@ remap.cumsum, remap.cumprod, remap.event.reindex, ees, eesPlot) export(marketResidual, excessReturn) -export(lmAMM, makeX) +export(subperiod.lmAMM, manyfirmssubperiod.lmAMM, lmAMM, makeX) S3method(summary, amm) From noreply at r-forge.r-project.org Tue Feb 18 07:18:26 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 18 Feb 2014 07:18:26 +0100 (CET) Subject: [Eventstudies-commits] r175 - in pkg: R man vignettes Message-ID: <20140218061826.8E1771868CB@r-forge.r-project.org> Author: vikram Date: 2014-02-18 07:18:26 +0100 (Tue, 18 Feb 2014) New Revision: 175 Modified: pkg/R/lmAmm.R pkg/man/manyfirmssubperiod.lmAMM.Rd pkg/vignettes/eventstudies.Rnw Log: Changed argument for manyfirmssubperiod.lmAMM function to have uniformity across all functions Modified: pkg/R/lmAmm.R =================================================================== --- pkg/R/lmAmm.R 2014-02-18 03:23:27 UTC (rev 174) +++ pkg/R/lmAmm.R 2014-02-18 06:18:26 UTC (rev 175) @@ -65,12 +65,12 @@ # Many firms AMM ######################## manyfirmssubperiod.lmAMM <- -function(regressand,regressors, +function(firm.returns,X, lags,dates=NULL, periodnames=NULL,verbose=FALSE){ require("doMC") registerDoMC() if(is.null(dates)){ - dates=c(start(regressors),end(regressors)) + dates=c(start(X),end(X)) periodnames="Full" } nperiods <- length(periodnames) @@ -78,16 +78,17 @@ cat("Mistake in length of dates versus length of periods.\n") return(NULL) } - nfirms <- ncol(regressand) + nfirms <- ncol(firm.returns) # Let's get "exposure' and 'sds'. Setting up structures:- - exposures <- matrix(NA,nrow=nfirms,ncol=nperiods*ncol(regressors)) - rownames(exposures) <- colnames(regressand) + exposures <- matrix(NA,nrow=nfirms,ncol=nperiods*ncol(X)) + exposures <- as.data.frame(exposures) + rownames(exposures) <- colnames(firm.returns) tmp <- NULL for(i in 1:length(periodnames)){ - for(j in 1:ncol(regressors)){ - tmp <- c(tmp, paste(colnames(regressors)[j], + for(j in 1:NCOL(X)){ + tmp <- c(tmp, paste(colnames(X)[j], periodnames[i],sep=".")) } } @@ -96,14 +97,14 @@ colnames(sds) <- paste("sd",colnames(exposures),sep=".") # Setup a list structure for an OLS that failed - empty <- list(exposures=rep(NA,ncol(regressors)), - s.exposures=rep(NA,ncol(regressors))) + empty <- list(exposures=rep(NA,ncol(X)), + s.exposures=rep(NA,ncol(X))) - for(i in 1:ncol(regressand)){ - cat("Doing",colnames(regressand)[i],"\n") - if (verbose) {cat ("Doing", colnames(regressand)[i], "\n")} - firm.returns <- regressand[,i] - dataset <- cbind(firm.returns, regressors) # This is the full time-series + for(i in 1:NCOL(firm.returns)){ + cat("AMM estimation for",colnames(firm.returns)[i],"\n") + if (verbose) {cat ("AMM estimation for", colnames(firm.returns)[i], "\n")} + stock.return <- firm.returns[,i] + dataset <- cbind(stock.return, X) # This is the full time-series this.exp <- this.sds <- NULL for(j in 1:nperiods){ # now we chop it up t1 <- dates[j] @@ -114,8 +115,8 @@ this.exp <- c(this.exp, fe$exposures) this.sds <- c(this.sds, fe$s.exposures) } - exposures[colnames(regressand)[i],] <- this.exp - sds[colnames(regressand)[i],] <- this.sds + exposures[colnames(firm.returns)[i],] <- this.exp + sds[colnames(firm.returns)[i],] <- this.sds } list(exposures=exposures, sds=sds, sig=exposures/sds) } Modified: pkg/man/manyfirmssubperiod.lmAMM.Rd =================================================================== --- pkg/man/manyfirmssubperiod.lmAMM.Rd 2014-02-18 03:23:27 UTC (rev 174) +++ pkg/man/manyfirmssubperiod.lmAMM.Rd 2014-02-18 06:18:26 UTC (rev 175) @@ -9,14 +9,14 @@ matrix of data obtained from \code{makeX}, and a matrix of LHS variables} \usage{ -manyfirmssubperiod.lmAMM(regressand, regressors, lags, dates = NULL, periodnames = NULL, verbose = FALSE) +manyfirmssubperiod.lmAMM(firm.returns, X, lags, dates = NULL, periodnames = NULL, verbose = FALSE) } \arguments{ - \item{regressand}{A zoo matrix of all firms for which AMM is to + \item{firm.returns}{A zoo matrix of all firms for which AMM is to be computed} - \item{regressors}{A zoo matrix containing at least two + \item{X}{A zoo matrix containing at least two regressors generally obtained after applying the makeX function } \item{lags}{ Specifies the number of lags to be used in the @@ -37,22 +37,22 @@ \author{Ajay Shah, Vimal Balasubramaniam} \seealso{ -\code{\link{onefirmAMM}} +\code{\link{lmAMM}} } \examples{ # Running manyfirmssubperiod.lmAMM() involves as many steps as working with onefirmAMM. data("lmAMMData") -regressand <- lmAMMData[,c("Infosys","TCS")] +firm.returns <- lmAMMData[,c("Infosys","TCS")] market.returns <- lmAMMData[,"index.nifty"] currency.returns <- lmAMMData[,"currency.inrusd"] -## Creating regressors for AMM estimation using makeX function -regressors <- makeX(market.returns, others=currency.returns, nlags=1, +## Creating X for AMM estimation using makeX function +X <- makeX(market.returns, others=currency.returns, nlags=1, switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE, - dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31"))) + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-20"))) ## Estimating exposure -res <- manyfirmssubperiod.lmAMM(regressand,regressors,lags=1, - dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), +res <- manyfirmssubperiod.lmAMM(firm.returns,X,lags=1, + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-20")), periodnames=c("P1","P2"), verbose=FALSE) print(res) Modified: pkg/vignettes/eventstudies.Rnw =================================================================== --- pkg/vignettes/eventstudies.Rnw 2014-02-18 03:23:27 UTC (rev 174) +++ pkg/vignettes/eventstudies.Rnw 2014-02-18 06:18:26 UTC (rev 175) @@ -91,15 +91,25 @@ \item \texttt{marketResidual}: estimation of market model to obtain idiosyncratic firm returns, controlling for the market returns. -\item \texttt{AMM}: estimation of the augmented market model which provides user the capability to run market models with orthogonalisation and obtain idiosyncratic returns. +\item \texttt{lmAMM}: estimation of the augmented market model which provides user the capability to run market models with orthogonalisation and obtain idiosyncratic returns. \end{itemize} - -The function \texttt{AMM} is a generic function that allows users to -run an augmented market model and undertake the analysis of the market -model in a regression setting and obtain idiosyncratic returns. Often times, there is a need for an auxilliary regression that purges the effect of the explanatory variables on one another. This function allows for the estimation of such a residual for a single firm using the function \texttt{onefirmAMM}. Advanced users may also want to look at \texttt{manyfirmsAMM}. +The function \texttt{lmAMM} is a generic function that allows users to +run an augmented market model (AMM) by using regressors provided by +\texttt{makeX} function and undertake the analysis of the market model +in a regression setting and obtain idiosyncratic +returns. The auxiliary regression that purges the effect of the +explanatory variables on one another is performed using \texttt{makeX} +function. \texttt{subpperiod.lmAMM} function allows for a single firm +AMM analysis for different periods in the sample. While +\texttt{manyfirmssubperiod.lmAMM}\footnote{User can use this function + to perform AMM for more than one firm by providing argument \textit{dates=NULL}} replicates the +\texttt{subperiod.lmAMM} analysis for more than one firms. -The output from all these models are also time series objects of class ``zoo'' or ``xts''. This becomes the input for the remaining steps in the event study analysis, of which the first step is to convert a timeseries object into the event-time frame. +The output of \texttt{lmAMM} function is an list object of class +\texttt{amm}. It includes the linear model output along with AMM +exposure, standard deviation, significance and residuals. These AMM +residuals are further used in event study analysis. \subsection{Converting data from physical time into event time} @@ -190,17 +200,16 @@ available. In this case, we would like to purge any currency returns from the outcome return of interest, and the \textit{a-priori} expectation is that the variance of the residual is reduced in this -process. In this case, the \texttt{AMM} model requires a time-series -of the exchange rate along with firm returns and market returns. This -is done by loading the \textit{inr} data, which is the INR-USD -exchange rate for the same period. The complete data set consisting of -stock returns, market returns, and exchange rate is first created. +process. In this case, the model requires a time-series of the +exchange rate along with firm returns and market returns. The complete +data set consisting of firm returns, market returns and exchange rate +for the same period\footnote{A balanced data without NAs is preferred} +is first created. -Inputs into the \texttt{AMM} model also include \texttt{firm.returns} -and \texttt{market.returns}. Currency returns can be specified using -\texttt{others}. In a general case, this proves to be a specification -with the flexibility to run auxiliary regressions to specify the -regression appropriately. +The first step is to create regressors using market returns and +exchange rate using \texttt{makeX} function. The output of +\texttt{makeX} function is further used in \texttt{lmAMM} along with +firm returns to compute augmented market model residuals. % AMM model <<>>= # Create RHS before running lmAMM() @@ -356,8 +365,6 @@ 2013}). R itself as well as these packages can be obtained from \href{http://CRAN.R-project.org/}{CRAN}. - - % \newpage \bibliographystyle{jss} \bibliography{es} From noreply at r-forge.r-project.org Tue Feb 18 08:53:35 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 18 Feb 2014 08:53:35 +0100 (CET) Subject: [Eventstudies-commits] r176 - pkg/vignettes Message-ID: <20140218075335.634FD1865EF@r-forge.r-project.org> Author: vikram Date: 2014-02-18 08:53:34 +0100 (Tue, 18 Feb 2014) New Revision: 176 Modified: pkg/vignettes/amm.Rnw pkg/vignettes/eventstudies.Rnw Log: Modified AMM vignette for new AMM functions Modified: pkg/vignettes/amm.Rnw =================================================================== --- pkg/vignettes/amm.Rnw 2014-02-18 06:18:26 UTC (rev 175) +++ pkg/vignettes/amm.Rnw 2014-02-18 07:53:34 UTC (rev 176) @@ -62,21 +62,24 @@ % Need to talk more about generalisation used for variables other than currency -\section{Currency exposure}\label{sec:ce} -The package has functions which enable the user to extract the -coefficients of currency exposure for firms using the AMM methodology -employed in \citet{patnaik2010amm}. +\section{Software approach}\label{sec:ce} +The package has functions which enable the user to compute linear +model AMM output, along with currency exposure, using the AMM +methodology employed in \citet{patnaik2010amm}. In the subsections +below we describe construction of data-set to input in \texttt{lmAMM} +function and further computing AMM output and currency exposure. + \subsection{Constructing data set} -We need to construct usable data set, before estimating currency -exposure on firm returns using this package. There are two steps to be -followed constructing regressors and regressands, to perform OLS as -shown in the \citet{patnaik2010amm}. +We need to construct usable data set, before performing AMM analysis +on firm returns using this package. There are two steps to be +followed constructing \texttt{X} (regressors) and firm returns +(regressands), to perform OLS as shown in the \citet{patnaik2010amm}. \subsubsection{Regressors \& Regressands} Regressors in the AMM equation are market returns and currency returns, while regressands is firm returns. All the variables should have balanced panel if not then merge the time series variable to get -one. \textit{y3c3} is an time series object with market returns as -\textit{NIFTY} and currency returns as \textit{INRUSD}. If +one. \textit{lmAMMData} is an time series object with market returns as +\textit{Nifty} and currency returns as \textit{INR/USD}. If currency exposure is to be estimated for different periods separately then argument \textit{dates} will be helpful or else \textit{NULL} will be provided to perform for full period. @@ -92,17 +95,64 @@ data("lmAMMData") nifty <- lmAMMData$index.nifty inrusd <- lmAMMData$currency.inrusd -firm.returns <- lmAMMData$Infosys +regressand <- lmAMMData[,c("Infosys","TCS")] regressors <- makeX(nifty, others=inrusd, switch.to.innov=TRUE, market.returns.purge=TRUE, nlags=1, - dates=as.Date(c("2012-02-01","2013-01-01","2014-01-31")), verbose=FALSE) + dates=as.Date(c("2012-02-01","2013-01-01","2014-01-20")), verbose=FALSE) @ -\subsubsection{Getting currency exposure} -The output of {makeX} function is used in \textit{subperiod.lmAMM} and -\textit{AMM} function to get currency exposure of the firms and AMM + +\subsection{Augmented market model} +Augmented market model output with a class of \textit{amm} is +generated using the function \texttt{lmAMM}. This function takes firm +returns (regressand) and regressor as input. Output of \texttt{lmAMM} +function is a list object with linear model output of AMM, +currency exposure, standard deviation and significance of the +exposure. +<<>>= +## AMM residual to time series +timeseries.lmAMM <- function(firm.returns,X,verbose=FALSE,nlags=1){ + tmp <- resid(lmAMM(firm.returns,X,nlags)) + tmp.res <- zoo(tmp,as.Date(names(tmp))) +} +## One firm +amm.output.one <- lmAMM(regressand[,1],X,nlags=1) +amm.resid.one <- timeseries.lmAMM(firm.returns=regressand[,1], + X=regressors, verbose=FALSE, nlags=1) +summary(amm.output.one) + +## More than one firm + # Extracting and merging +tmp.resid <- sapply(colnames(regressand)[1:2],function(y) + timeseriesAMM(firm.returns=regressand[,y], + X=regressors, + verbose=FALSE, + nlags=1)) +amm.resid <- zoo(tmp.resid,as.Date(rownames(tmp.resid))) +@ + +All the basic functionality are available for object with class +\textit{amm}. \texttt{print},\texttt{summary} and \texttt{plot} +commands can be used to do preliminary analysis. The plot +\ref{fig:amm} compares the AMM residuals with abnormal firm returns. +\begin{figure}[t] + \begin{center} + \label{fig:amm} + \caption{Augment market model} + \setkeys{Gin}{width=0.8\linewidth} + \setkeys{Gin}{height=0.8\linewidth} +<>= +plot(amm.output.one) +@ + \end{center} + \label{fig:one} +\end{figure} + +\subsection{Getting currency exposure} +The output of \texttt{makeX} function is used in \textit{subperiod.lmAMM} and +\textit{lmAMM} function to get currency exposure of the firms and AMM residuals respectively. In the example below, we demonstrate the use of \textit{subperiod.lmAMM} function to estimate currency exposure for -firms. +firms. % MakeX and subperiod.lmAMM <<>>= # Run AMM for one firm across different periods @@ -118,45 +168,6 @@ str(firm.exposure) @ - - -\section{AMM residuals}\label{sec:es} -Here, in this section we show how to get augmented market residuals -using \textit{eventstudies} package. The -\textit{AMM} function is a wrapper to get AMM residuals directly, and -\textit{eventstudy} function is a wrapper to perform event study -analysis for different specification as discussed in -\textit{eventstudies} vignette. The \textit{AMM} function performs -similar step as above currency exposure code-base but is -generalised to accept one firm or many firms. - -\subsection{Construction of data-set} -The \textit{AMM} function requires a balanced panel of firm returns, market -returns and currency returns (for this example). The balanced panel -should be a time series object. As we can see below, we create -\textit{all.data} by merging \textit{inrusd} (currency returns) and -\textit{nifty.index} (market returns). -<<>>= -# Create RHS before running AMM() -data("lmAMMData") -firm.returns <- lmAMMData[,"Infosys"] -market.returns <- lmAMMData[,"index.nifty"] -currency.returns <- lmAMMData[,"currency.inrusd"] -regressors <- makeX(market.returns, others=currency.returns, - switch.to.innov=FALSE, market.returns.purge=FALSE, verbose=FALSE) -@ - -Inputs into the \texttt{AMM} model also include \texttt{firm.returns} -and \texttt{market.returns}. Currency returns can be specified using -\texttt{others}. Once these inputs are provided we can extract AMM -residuals using following command. - -<<>>= -# AMM output -res.amm <- lmAMM(firm.returns=firm.returns, X=regressors, - verbose=FALSE, nlags=1) -@ - We can also perform event study analysis, directly on AMM residuals using \textit{eventstudy} function. which is presented in \textit{eventstudies} vignette. Modified: pkg/vignettes/eventstudies.Rnw =================================================================== --- pkg/vignettes/eventstudies.Rnw 2014-02-18 06:18:26 UTC (rev 175) +++ pkg/vignettes/eventstudies.Rnw 2014-02-18 07:53:34 UTC (rev 176) @@ -227,12 +227,12 @@ regressors <- makeX(market.returns=nifty.index, others=inrusd, market.returns.purge=TRUE, nlags=1) ## AMM residual to time series -timeseriesAMM <- function(firm.returns,X,verbose=FALSE,nlags=1){ +timeseries.lmAMM <- function(firm.returns,X,verbose=FALSE,nlags=1){ tmp <- resid(lmAMM(firm.returns,X,nlags)) tmp.res <- zoo(tmp,as.Date(names(tmp))) } ## One firm -amm.result <- timeseriesAMM(firm.returns=StockPriceReturns[,1], +amm.result <- timeseries.lmAMM(firm.returns=StockPriceReturns[,1], X=regressors, verbose=FALSE, nlags=1) ## More than one firm From noreply at r-forge.r-project.org Tue Feb 18 14:21:09 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Tue, 18 Feb 2014 14:21:09 +0100 (CET) Subject: [Eventstudies-commits] r177 - / Message-ID: <20140218132109.9FB1E1864AD@r-forge.r-project.org> Author: chiraganand Date: 2014-02-18 14:21:09 +0100 (Tue, 18 Feb 2014) New Revision: 177 Modified: todo.org Log: Added manual testing as a todo. Modified: todo.org =================================================================== --- todo.org 2014-02-18 07:53:34 UTC (rev 176) +++ todo.org 2014-02-18 13:21:09 UTC (rev 177) @@ -32,5 +32,5 @@ * man pages - modify ess.Rd: put reference to Patnaik, Shah, and Singh (2013) -* es-match - - synthetic returns +* Testing + - manual calculation of numbers in the tests