[Returnanalytics-commits] r3617 - in pkg/FactorAnalytics: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Mar 16 21:41:50 CET 2015


Author: pragnya
Date: 2015-03-16 21:41:50 +0100 (Mon, 16 Mar 2015)
New Revision: 3617

Modified:
   pkg/FactorAnalytics/R/fitTsfm.R
   pkg/FactorAnalytics/R/summary.tsfm.r
   pkg/FactorAnalytics/man/fitTsfm.Rd
   pkg/FactorAnalytics/man/summary.tsfm.Rd
Log:
Added option to trim summary; Add mkt.name to output tsfm object

Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R	2015-03-16 16:15:01 UTC (rev 3616)
+++ pkg/FactorAnalytics/R/fitTsfm.R	2015-03-16 20:41:50 UTC (rev 3617)
@@ -95,6 +95,7 @@
 #' \item{data}{xts data object containing the assets and factors.}
 #' \item{asset.names}{asset.names as input.}
 #' \item{factor.names}{factor.names as input.}
+#' \item{mkt.name}{mkt.name as input}
 #' \item{fit.method}{fit.method as input.}
 #' \item{variable.selection}{variable.selection as input.}
 #' Where N is the number of assets, K is the number of factors and T is the 
@@ -226,6 +227,8 @@
   # spaces get converted to periods in colnames of xts object after merge
   asset.names <- gsub(" ",".", asset.names, fixed=TRUE)
   factor.names <- gsub(" ",".", factor.names, fixed=TRUE)
+  mkt.name <- gsub(" ",".", mkt.name, fixed=TRUE)
+  rf.name <- gsub(" ",".", rf.name, fixed=TRUE)
   
   # Selects regression procedure based on specified variable.selection method.
   # Each method returns a list of fitted factor models for each asset.
@@ -243,7 +246,7 @@
     result.lars <- SelectLars(dat.xts, asset.names, factor.names, lars.args, 
                               cv.lars.args, lars.criterion)
     input <- list(call=call, data=dat.xts, asset.names=asset.names, 
-                  factor.names=factor.names, fit.method=NULL, 
+                  factor.names=factor.names, mkt.name=mkt.name, fit.method=NULL, 
                   variable.selection=variable.selection)
     result <- c(result.lars, input)
     class(result) <- "tsfm"
@@ -268,7 +271,8 @@
   result <- list(asset.fit=reg.list, alpha=alpha, beta=beta, r2=r2, 
                  resid.sd=resid.sd, call=call, data=dat.xts, 
                  asset.names=asset.names, factor.names=factor.names, 
-                 fit.method=fit.method, variable.selection=variable.selection)
+                 mkt.name=mkt.name, fit.method=fit.method, 
+                 variable.selection=variable.selection)
   class(result) <- "tsfm"
   return(result)
 }

Modified: pkg/FactorAnalytics/R/summary.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.tsfm.r	2015-03-16 16:15:01 UTC (rev 3616)
+++ pkg/FactorAnalytics/R/summary.tsfm.r	2015-03-16 20:41:50 UTC (rev 3617)
@@ -1,134 +1,147 @@
-#' @title Summarizing a fitted time series factor model
-#' 
-#' @description \code{summary} method for object of class \code{tsfm}. 
-#' Returned object is of class {summary.tsfm}.
-#' 
-#' @details The default \code{summary} method for a fitted \code{lm} object 
-#' computes the standard errors and t-statistics under the assumption of 
-#' homoskedasticty. Argument \code{se.type} gives the option to compute 
-#' heteroskedasticity-consistent (HC) or 
-#' heteroskedasticity-autocorrelation-consistent (HAC) standard errors and 
-#' t-statistics using \code{\link[lmtest]{coeftest}}. This option is meaningful 
-#' only if \code{fit.method = "OLS" or "DLS"}.
-#' 
-#' Standard errors are currently not available for 
-#' \code{variable.selection="lars"} as there seems to be no consensus on a 
-#' statistically valid method of calculating standard errors for the lasso 
-#' predictions.
-#'  
-#' @param object an object of class \code{tsfm} returned by \code{fitTsfm}.
-#' @param se.type one of "Default", "HC" or "HAC"; option for computing 
-#' HC/HAC standard errors and t-statistics. Default is "Default".
-#' @param x an object of class \code{summary.tsfm}.
-#' @param digits number of significants digits to use when printing. 
-#' Default is 3.
-#' @param ... futher arguments passed to or from other methods.
-#' 
-#' @return Returns an object of class \code{summary.tsfm}. 
-#' The print method for class \code{summary.tsfm} outputs the call, 
-#' coefficients (with standard errors and t-statistics), r-squared and 
-#' residual volatilty (under the homoskedasticity assumption) for all assets. 
-#' 
-#' Object of class \code{summary.tsfm} is a list of length N + 2 containing:
-#' \item{call}{the function call to \code{fitTsfm}}
-#' \item{se.type}{standard error type as input} 
-#' \item{sum.list}{list of summaries of the N fit objects (of class \code{lm}, 
-#' \code{lmRob} or \code{lars}) for each asset in the factor model.}
-#' 
-#' @author Sangeetha Srinivasan & Yi-An Chen.
-#' 
-#' @seealso \code{\link{fitTsfm}}, \code{\link[stats]{summary.lm}}
-#' 
-#' @examples
-#' data(managers)
-#' fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
-#'                factor.names=colnames(managers[,7:9]), 
-#'                data=managers)
-#' 
-#' # summary of factor model fit for all assets
-#' summary(fit, "HAC")
-#' 
-#' # summary of lm fit for a single asset
-#' summary(fit$asset.fit[[1]])
-#' 
-#' @importFrom lmtest coeftest.default
-#' @importFrom sandwich vcovHC.default vcovHAC.default
-#' 
-#' @method summary tsfm
-#' @export
-
-summary.tsfm <- function(object, se.type=c("Default","HC","HAC"), ...){
-  
-  # check input object validity
-  if (!inherits(object, "tsfm")) {
-    stop("Invalid 'tsfm' object")
-  }
-  
-  #set default for se.type
-  se.type = se.type[1]
-  
-  # note: fit.method=NULL for "lars" objects
-  if (object$fit.method=="Robust" && se.type!="Default") {
-    stop("Invalid argument: HC/HAC standard errors are applicable only if 
-         fit.method = 'OLS' or 'DLS'")
-  }
-  
-  # extract summary.lm objects for each asset
-  sum.list <- lapply(object$asset.fit, summary)
-  
-  # convert to HC/HAC standard errors and t-stats if specified
-  # extract coefficients separately for "lars" variable.selection method
-  for (i in object$asset.names) {
-    if (se.type=="HC") {
-      sum.list[[i]]$coefficients <- coeftest.default(object$asset.fit[[i]],
-                                                     vcov.=vcovHC.default)[,1:4]
-    } else if (se.type=="HAC") {
-      sum.list[[i]]$coefficients <- coeftest.default(object$asset.fit[[i]], 
-                                                     vcov.=vcovHAC.default)[,1:4]
-    }
-  }
-  
-  if (object$variable.selection=="lars") {
-    sum.list <- list()
-    for (i in object$asset.names) {
-      sum.list[[i]]$coefficients <- as.matrix(c(object$alpha[i], object$beta[i,]))
-      rownames(sum.list[[i]]$coefficients)[1]="(Intercept)"
-      colnames(sum.list[[i]]$coefficients)[1]="Estimate"
-      sum.list[[i]]$r.squared <- as.numeric(object$r2[i])
-      sum.list[[i]]$sigma <- as.numeric(object$resid.sd[i]) 
-    }
-  }
-  
-  # include the call and se.type to fitTsfm
-  sum <- list(call=object$call, se.type=se.type, sum.list=sum.list)
-  class(sum) <- "summary.tsfm"
-  return(sum)
-}
-
-
-#' @rdname summary.tsfm
-#' @method print summary.tsfm
-#' @export
-
-print.summary.tsfm <- function(x, digits=3, ...) {
-  if(!is.null(cl <- x$call)) {
-    cat("\nCall:\n")
-    dput(cl)
-  }
-  cat("\nFactor Model Coefficients:\n", sep="")
-  n <- length(x$sum.list)
-  for (i in 1:n) {
-    options(digits = digits)  
-    table.coef <- (x$sum.list)[[i]]$coefficients
-    if (dim(table.coef)[2] > 1) {
-      cat("\nAsset", i, ": ", names(x$sum.list[i]), "\n(", x$se.type, 
-          " Standard Errors & T-stats)\n\n", sep="")  
-    } else {
-      cat("\nAsset", i, ": ", names(x$sum.list[i]), "\n\n", sep="")  
-    }
-    r2 <- x$sum.list[[i]]$r.squared
-    sigma <- x$sum.list[[i]]$sigma
-    printCoefmat(table.coef, digits=digits, ...)
-    cat("\nR-squared: ", r2,", Residual Volatility: ", sigma,"\n", sep="")
-  }
-}
+#' @title Summarizing a fitted time series factor model
+#' 
+#' @description \code{summary} method for object of class \code{tsfm}. 
+#' Returned object is of class {summary.tsfm}.
+#' 
+#' @details The default \code{summary} method for a fitted \code{lm} object 
+#' computes the standard errors and t-statistics under the assumption of 
+#' homoskedasticty. Argument \code{se.type} gives the option to compute 
+#' heteroskedasticity-consistent (HC) or 
+#' heteroskedasticity-autocorrelation-consistent (HAC) standard errors and 
+#' t-statistics using \code{\link[lmtest]{coeftest}}. This option is meaningful 
+#' only if \code{fit.method = "OLS" or "DLS"}.
+#' 
+#' Standard errors are currently not available for 
+#' \code{variable.selection="lars"} as there seems to be no consensus on a 
+#' statistically valid method of calculating standard errors for the lasso 
+#' predictions.
+#'  
+#' @param object an object of class \code{tsfm} returned by \code{fitTsfm}.
+#' @param se.type one of "Default", "HC" or "HAC"; option for computing 
+#' HC/HAC standard errors and t-statistics. Default is "Default".
+#' @param x an object of class \code{summary.tsfm}.
+#' @param digits number of significants digits to use when printing. 
+#' Default is 3.
+#' @param trim option to supress labels and legend in the summary. When 
+#' \code{TRUE}, only the coefficient matrx with standard eroors is printed. 
+#' Default is \code{FALSE}.
+#' @param ... futher arguments passed to or from other methods.
+#' 
+#' @return Returns an object of class \code{summary.tsfm}. 
+#' The print method for class \code{summary.tsfm} outputs the call, 
+#' coefficients (with standard errors and t-statistics), r-squared and 
+#' residual volatilty (under the homoskedasticity assumption) for all assets. 
+#' 
+#' Object of class \code{summary.tsfm} is a list of length N + 2 containing:
+#' \item{call}{the function call to \code{fitTsfm}}
+#' \item{se.type}{standard error type as input} 
+#' \item{sum.list}{list of summaries of the N fit objects (of class \code{lm}, 
+#' \code{lmRob} or \code{lars}) for each asset in the factor model.}
+#' 
+#' @author Sangeetha Srinivasan & Yi-An Chen.
+#' 
+#' @seealso \code{\link{fitTsfm}}, \code{\link[stats]{summary.lm}}
+#' 
+#' @examples
+#' data(managers)
+#' fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
+#'                factor.names=colnames(managers[,7:9]), 
+#'                data=managers)
+#' 
+#' # summary of factor model fit for all assets
+#' summary(fit, "HAC")
+#' 
+#' # summary of lm fit for a single asset
+#' summary(fit$asset.fit[[1]])
+#' 
+#' @importFrom lmtest coeftest.default
+#' @importFrom sandwich vcovHC.default vcovHAC.default
+#' 
+#' @method summary tsfm
+#' @export
+
+summary.tsfm <- function(object, se.type=c("Default","HC","HAC"), ...){
+  
+  # check input object validity
+  if (!inherits(object, "tsfm")) {
+    stop("Invalid 'tsfm' object")
+  }
+  
+  #set default for se.type
+  se.type = se.type[1]
+  
+  # note: fit.method=NULL for "lars" objects
+  if (object$fit.method=="Robust" && se.type!="Default") {
+    stop("Invalid argument: HC/HAC standard errors are applicable only if 
+         fit.method = 'OLS' or 'DLS'")
+  }
+  
+  # extract summary.lm objects for each asset
+  sum.list <- lapply(object$asset.fit, summary)
+  
+  # convert to HC/HAC standard errors and t-stats if specified
+  # extract coefficients separately for "lars" variable.selection method
+  for (i in object$asset.names) {
+    if (se.type=="HC") {
+      sum.list[[i]]$coefficients <- coeftest.default(object$asset.fit[[i]],
+                                                     vcov.=vcovHC.default)[,1:4]
+    } else if (se.type=="HAC") {
+      sum.list[[i]]$coefficients <- coeftest.default(object$asset.fit[[i]], 
+                                                     vcov.=vcovHAC.default)[,1:4]
+    }
+  }
+  
+  if (object$variable.selection=="lars") {
+    sum.list <- list()
+    for (i in object$asset.names) {
+      sum.list[[i]]$coefficients <- as.matrix(c(object$alpha[i], object$beta[i,]))
+      rownames(sum.list[[i]]$coefficients)[1]="(Intercept)"
+      colnames(sum.list[[i]]$coefficients)[1]="Estimate"
+      sum.list[[i]]$r.squared <- as.numeric(object$r2[i])
+      sum.list[[i]]$sigma <- as.numeric(object$resid.sd[i]) 
+    }
+  }
+  
+  # include the call and se.type to fitTsfm
+  sum <- list(call=object$call, se.type=se.type, sum.list=sum.list)
+  class(sum) <- "summary.tsfm"
+  return(sum)
+}
+
+
+#' @rdname summary.tsfm
+#' @method print summary.tsfm
+#' @export
+
+print.summary.tsfm <- function(x, digits=3, trim=FALSE, ...) {
+  n <- length(x$sum.list)
+  if (trim==FALSE) {
+    if(!is.null(cl <- x$call)) {
+      cat("\nCall:\n")
+      dput(cl)
+    }
+    cat("\nFactor Model Coefficients:\n", sep="")
+    for (i in 1:n) {
+      options(digits = digits)  
+      table.coef <- (x$sum.list)[[i]]$coefficients
+      if (dim(table.coef)[2] > 1) {
+        cat("\nAsset", i, ": ", names(x$sum.list[i]), "\n(", x$se.type, 
+            " Standard Errors & T-stats)\n\n", sep="")  
+      } else {
+        cat("\nAsset", i, ": ", names(x$sum.list[i]), "\n\n", sep="")  
+      }
+      r2 <- x$sum.list[[i]]$r.squared
+      sigma <- x$sum.list[[i]]$sigma
+      printCoefmat(table.coef, digits=digits, ...)
+      cat("\nR-squared: ", r2,", Residual Volatility: ", sigma,"\n", sep="")
+    }
+  } else {
+    for (i in 1:n) {
+      options(digits = digits) 
+      table.coef <- (x$sum.list)[[i]]$coefficients
+      cat(names(x$sum.list[i]), "\n")
+      printCoefmat(table.coef, digits=digits, signif.legend=FALSE, ...)
+      cat("\n")
+    }
+  }
+}

Modified: pkg/FactorAnalytics/man/fitTsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTsfm.Rd	2015-03-16 16:15:01 UTC (rev 3616)
+++ pkg/FactorAnalytics/man/fitTsfm.Rd	2015-03-16 20:41:50 UTC (rev 3617)
@@ -74,6 +74,7 @@
 \item{data}{xts data object containing the assets and factors.}
 \item{asset.names}{asset.names as input.}
 \item{factor.names}{factor.names as input.}
+\item{mkt.name}{mkt.name as input}
 \item{fit.method}{fit.method as input.}
 \item{variable.selection}{variable.selection as input.}
 Where N is the number of assets, K is the number of factors and T is the

Modified: pkg/FactorAnalytics/man/summary.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.tsfm.Rd	2015-03-16 16:15:01 UTC (rev 3616)
+++ pkg/FactorAnalytics/man/summary.tsfm.Rd	2015-03-16 20:41:50 UTC (rev 3617)
@@ -1,73 +1,76 @@
-% Generated by roxygen2 (4.1.0): do not edit by hand
-% Please edit documentation in R/summary.tsfm.r
-\name{summary.tsfm}
-\alias{print.summary.tsfm}
-\alias{summary.tsfm}
-\title{Summarizing a fitted time series factor model}
-\usage{
-\method{summary}{tsfm}(object, se.type = c("Default", "HC", "HAC"), ...)
-
-\method{print}{summary.tsfm}(x, digits = 3, ...)
-}
-\arguments{
-\item{object}{an object of class \code{tsfm} returned by \code{fitTsfm}.}
-
-\item{se.type}{one of "Default", "HC" or "HAC"; option for computing
-HC/HAC standard errors and t-statistics. Default is "Default".}
-
-\item{...}{futher arguments passed to or from other methods.}
-
-\item{x}{an object of class \code{summary.tsfm}.}
-
-\item{digits}{number of significants digits to use when printing.
-Default is 3.}
-}
-\value{
-Returns an object of class \code{summary.tsfm}.
-The print method for class \code{summary.tsfm} outputs the call,
-coefficients (with standard errors and t-statistics), r-squared and
-residual volatilty (under the homoskedasticity assumption) for all assets.
-
-Object of class \code{summary.tsfm} is a list of length N + 2 containing:
-\item{call}{the function call to \code{fitTsfm}}
-\item{se.type}{standard error type as input}
-\item{sum.list}{list of summaries of the N fit objects (of class \code{lm},
-\code{lmRob} or \code{lars}) for each asset in the factor model.}
-}
-\description{
-\code{summary} method for object of class \code{tsfm}.
-Returned object is of class {summary.tsfm}.
-}
-\details{
-The default \code{summary} method for a fitted \code{lm} object
-computes the standard errors and t-statistics under the assumption of
-homoskedasticty. Argument \code{se.type} gives the option to compute
-heteroskedasticity-consistent (HC) or
-heteroskedasticity-autocorrelation-consistent (HAC) standard errors and
-t-statistics using \code{\link[lmtest]{coeftest}}. This option is meaningful
-only if \code{fit.method = "OLS" or "DLS"}.
-
-Standard errors are currently not available for
-\code{variable.selection="lars"} as there seems to be no consensus on a
-statistically valid method of calculating standard errors for the lasso
-predictions.
-}
-\examples{
-data(managers)
-fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
-               factor.names=colnames(managers[,7:9]),
-               data=managers)
-
-# summary of factor model fit for all assets
-summary(fit, "HAC")
-
-# summary of lm fit for a single asset
-summary(fit$asset.fit[[1]])
-}
-\author{
-Sangeetha Srinivasan & Yi-An Chen.
-}
-\seealso{
-\code{\link{fitTsfm}}, \code{\link[stats]{summary.lm}}
-}
-
+% Generated by roxygen2 (4.0.2): do not edit by hand
+\name{summary.tsfm}
+\alias{print.summary.tsfm}
+\alias{summary.tsfm}
+\title{Summarizing a fitted time series factor model}
+\usage{
+\method{summary}{tsfm}(object, se.type = c("Default", "HC", "HAC"), ...)
+
+\method{print}{summary.tsfm}(x, digits = 3, trim = FALSE, ...)
+}
+\arguments{
+\item{object}{an object of class \code{tsfm} returned by \code{fitTsfm}.}
+
+\item{se.type}{one of "Default", "HC" or "HAC"; option for computing
+HC/HAC standard errors and t-statistics. Default is "Default".}
+
+\item{...}{futher arguments passed to or from other methods.}
+
+\item{x}{an object of class \code{summary.tsfm}.}
+
+\item{digits}{number of significants digits to use when printing.
+Default is 3.}
+
+\item{trim}{option to supress labels and legend in the summary. When
+\code{TRUE}, only the coefficient matrx with standard eroors is printed.
+Default is \code{FALSE}.}
+}
+\value{
+Returns an object of class \code{summary.tsfm}.
+The print method for class \code{summary.tsfm} outputs the call,
+coefficients (with standard errors and t-statistics), r-squared and
+residual volatilty (under the homoskedasticity assumption) for all assets.
+
+Object of class \code{summary.tsfm} is a list of length N + 2 containing:
+\item{call}{the function call to \code{fitTsfm}}
+\item{se.type}{standard error type as input}
+\item{sum.list}{list of summaries of the N fit objects (of class \code{lm},
+\code{lmRob} or \code{lars}) for each asset in the factor model.}
+}
+\description{
+\code{summary} method for object of class \code{tsfm}.
+Returned object is of class {summary.tsfm}.
+}
+\details{
+The default \code{summary} method for a fitted \code{lm} object
+computes the standard errors and t-statistics under the assumption of
+homoskedasticty. Argument \code{se.type} gives the option to compute
+heteroskedasticity-consistent (HC) or
+heteroskedasticity-autocorrelation-consistent (HAC) standard errors and
+t-statistics using \code{\link[lmtest]{coeftest}}. This option is meaningful
+only if \code{fit.method = "OLS" or "DLS"}.
+
+Standard errors are currently not available for
+\code{variable.selection="lars"} as there seems to be no consensus on a
+statistically valid method of calculating standard errors for the lasso
+predictions.
+}
+\examples{
+data(managers)
+fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
+               factor.names=colnames(managers[,7:9]),
+               data=managers)
+
+# summary of factor model fit for all assets
+summary(fit, "HAC")
+
+# summary of lm fit for a single asset
+summary(fit$asset.fit[[1]])
+}
+\author{
+Sangeetha Srinivasan & Yi-An Chen.
+}
+\seealso{
+\code{\link{fitTsfm}}, \code{\link[stats]{summary.lm}}
+}
+



More information about the Returnanalytics-commits mailing list