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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Mar 17 12:10:27 CET 2016


Author: pragnya
Date: 2016-03-17 12:10:27 +0100 (Thu, 17 Mar 2016)
New Revision: 4011

Added:
   pkg/FactorAnalytics/R/summary.ffm.R
   pkg/FactorAnalytics/man/summary.ffm.Rd
Modified:
   pkg/FactorAnalytics/NAMESPACE
   pkg/FactorAnalytics/R/fitFfm.R
   pkg/FactorAnalytics/R/fitTsfm.R
   pkg/FactorAnalytics/R/fmCov.R
   pkg/FactorAnalytics/R/print.ffm.R
   pkg/FactorAnalytics/man/fitFfm.Rd
   pkg/FactorAnalytics/man/fmCov.Rd
   pkg/FactorAnalytics/man/print.ffm.Rd
Log:
Adding summary and fmCov methods for fitFfm

Modified: pkg/FactorAnalytics/NAMESPACE
===================================================================
--- pkg/FactorAnalytics/NAMESPACE	2016-03-17 10:23:45 UTC (rev 4010)
+++ pkg/FactorAnalytics/NAMESPACE	2016-03-17 11:10:27 UTC (rev 4011)
@@ -6,6 +6,7 @@
 S3method(fitted,ffm)
 S3method(fitted,sfm)
 S3method(fitted,tsfm)
+S3method(fmCov,ffm)
 S3method(fmCov,sfm)
 S3method(fmCov,tsfm)
 S3method(fmEsDecomp,sfm)
@@ -24,6 +25,7 @@
 S3method(print,ffm)
 S3method(print,pafm)
 S3method(print,sfm)
+S3method(print,summary.ffm)
 S3method(print,summary.sfm)
 S3method(print,summary.tsfm)
 S3method(print,summary.tsfmUpDn)
@@ -32,6 +34,7 @@
 S3method(residuals,ffm)
 S3method(residuals,sfm)
 S3method(residuals,tsfm)
+S3method(summary,ffm)
 S3method(summary,pafm)
 S3method(summary,sfm)
 S3method(summary,tsfm)

Modified: pkg/FactorAnalytics/R/fitFfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitFfm.R	2016-03-17 10:23:45 UTC (rev 4010)
+++ pkg/FactorAnalytics/R/fitFfm.R	2016-03-17 11:10:27 UTC (rev 4011)
@@ -77,9 +77,10 @@
 #' 
 #' An object of class \code{"ffm"} is a list containing the following 
 #' components:
-#' \item{factor.fit}{list of fitted objects for each time period. Each object 
-#' is of class \code{lm} if \code{fit.method="LS" or "WLS"}, or, class 
-#' \code{lmRob} if \code{fit.method="Rob" or "W-Rob"}.}
+#' \item{factor.fit}{list of fitted objects that estimate factor returns in each 
+#' time period. Each fitted object is of class \code{lm} if 
+#' \code{fit.method="LS" or "WLS"}, or, class \code{lmRob} if 
+#' \code{fit.method="Rob" or "W-Rob"}.}
 #' \item{beta}{N x K matrix of factor exposures for the last time period.}
 #' \item{factor.returns}{xts object of K-factor returns (including intercept).}
 #' \item{residuals}{xts object of residuals for N-assets.}

Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R	2016-03-17 10:23:45 UTC (rev 4010)
+++ pkg/FactorAnalytics/R/fitTsfm.R	2016-03-17 11:10:27 UTC (rev 4011)
@@ -351,7 +351,7 @@
     
     # choose best subset of factors depending on specified subset size
     fm.subsets <- do.call("regsubsets", c(list(fm.formula,data=quote(reg.xts)), 
-                                        regsubsets.args))
+                                          regsubsets.args))
     sum.sub <- summary(fm.subsets)
     
     # choose best model of a given subset size nvmax=nvmin (or) 
@@ -457,7 +457,7 @@
 #
 makePaddedDataFrame <- function(l) {
   DF <- do.call("rbind", lapply(lapply(l, unlist), "[", 
-                              unique(unlist(c(sapply(l,names))))))
+                                unique(unlist(c(sapply(l,names))))))
   DF <- as.data.frame(DF)
   names(DF) <- unique(unlist(c(sapply(l,names))))
   # as.matrix(DF) # if matrix output needed

Modified: pkg/FactorAnalytics/R/fmCov.R
===================================================================
--- pkg/FactorAnalytics/R/fmCov.R	2016-03-17 10:23:45 UTC (rev 4010)
+++ pkg/FactorAnalytics/R/fmCov.R	2016-03-17 11:10:27 UTC (rev 4011)
@@ -56,23 +56,14 @@
 #' data(StockReturns)
 #' sfm.pca.fit <- fitSfm(r.M, k=2)
 #' fmCov(sfm.pca.fit)
-#'                       
-#' \dontrun{
-#' # Fundamental Factor Model
-#' data(stock)
-#' # there are 447 assets  
-#' exposure.names <- c("BOOK2MARKET", "LOG.MARKETCAP") 
-#' beta.mat <- subset(stock, DATE=="2003-12-31")[, exposure.names]
-#' beta.mat1 <- cbind(rep(1, 447), beta.mat1)
-#' # FM return covariance 
-#' fit.fund <- fitFfm(exposure.names=c("BOOK2MARKET", "LOG.MARKETCAP"), 
-#'                    data=stock, returnsvar="RETURN", datevar="DATE", 
-#'                    assetvar="TICKER", wls=TRUE, regression="classic", 
-#'                    covariance="classic", full.resid.cov=FALSE)
-#' ret.cov.fundm <- fmCov(beta.mat1,fit.fund$factor.cov$cov,fit.fund$resid.sd)
-#' fit.fund$returns.cov$cov == ret.cov.fundm
-#' }
 #' 
+#' # Fundamental factor Model
+#' data(Stock.df)
+#' exposure.vars <- c("BOOK2MARKET", "LOG.MARKETCAP", "GICS.SECTOR")
+#' fit2 <- fitFfm(data=stock, asset.var="TICKER", ret.var="RETURN", 
+#'               date.var="DATE", exposure.vars=exposure.vars)
+#' fmCov(fit2)
+#' 
 #' @rdname fmCov
 #' @export
 
@@ -130,3 +121,12 @@
   return(object$Omega)
 }
 
+#' @rdname fmCov
+#' @method fmCov ffm
+#' @export
+
+fmCov.ffm <- function(object, use="pairwise.complete.obs", ...) {
+  
+  # already computed via fitFfm function
+  return(object$return.cov)
+}

Modified: pkg/FactorAnalytics/R/print.ffm.R
===================================================================
--- pkg/FactorAnalytics/R/print.ffm.R	2016-03-17 10:23:45 UTC (rev 4010)
+++ pkg/FactorAnalytics/R/print.ffm.R	2016-03-17 11:10:27 UTC (rev 4011)
@@ -5,6 +5,9 @@
 #' factor returns, cross-sectional r-squared values and residual variances 
 #' from the fitted object. 
 #' 
+#' Refer to \code{\link{summary.ffm}} for a more detailed summary of the fit at 
+#' each time period.
+#' 
 #' @param x an object of class \code{ffm} produced by \code{fitFfm}.
 #' @param digits an integer value, to indicate the required number of 
 #' significant digits. Default is 3.

Added: pkg/FactorAnalytics/R/summary.ffm.R
===================================================================
--- pkg/FactorAnalytics/R/summary.ffm.R	                        (rev 0)
+++ pkg/FactorAnalytics/R/summary.ffm.R	2016-03-17 11:10:27 UTC (rev 4011)
@@ -0,0 +1,99 @@
+#' @title Summarizing a fitted fundamental factor model
+#' 
+#' @description \code{summary} method for object of class \code{ffm}. 
+#' Returned object is of class {summary.ffm}.
+#' 
+#' @details The default \code{summary} method for a fitted \code{lm} object 
+#' computes the standard errors and t-statistics under the assumption of 
+#' homoskedasticty.
+#' 
+#' Note: This gives a summary of the fited factor returns at each time period. 
+#' If \code{T} is large, you might prefer the more succint summary produced by 
+#' \code{\link{print.ffm}}.
+#'  
+#' @param object an object of class \code{ffm} returned by \code{fitFfm}.
+#' @param x an object of class \code{summary.ffm}.
+#' @param digits number of significants digits to use when printing. 
+#' Default is 3.
+#' @param labels option to print labels and legend in the summary. Default is 
+#' \code{TRUE}. When \code{FALSE}, only the coefficient matrx with standard 
+#' errors is printed. 
+#' @param ... futher arguments passed to or from other methods.
+#' 
+#' @return Returns an object of class \code{summary.ffm}. 
+#' The print method for class \code{summary.ffm} 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.ffm} is a list of length N + 2 containing:
+#' \item{call}{the function call to \code{fitFfm}}
+#' \item{sum.list}{list of summaries of the T fit objects (of class \code{lm} or 
+#' \code{lmRob}) for each time period in the factor model.}
+#' 
+#' @author Sangeetha Srinivasan & Yi-An Chen.
+#' 
+#' @seealso \code{\link{fitFfm}}, \code{\link[stats]{summary.lm}}
+#' 
+#' @examples
+#' data(Stock.df)
+#' exposure.vars <- c("BOOK2MARKET", "LOG.MARKETCAP", "GICS.SECTOR")
+#' fit2 <- fitFfm(data=stock, asset.var="TICKER", ret.var="RETURN", 
+#'                date.var="DATE", exposure.vars=exposure.vars)
+#' 
+#' # summary of factor returns estimated in each time period
+#' summary(fit2)
+#' 
+#' # summary of lm fit for a single period
+#' summary(fit2$factor.fit[[1]])
+#' 
+#' @method summary ffm
+#' @export
+
+summary.ffm <- function(object, ...){
+  
+  # check input object validity
+  if (!inherits(object, "ffm")) {
+    stop("Invalid 'ffm' object")
+  }
+  
+  # extract summary.lm objects for each factor
+  sum.list <- lapply(object$factor.fit, summary)
+  
+  # include the call and se.type to fitFfm
+  sum <- list(call=object$call, sum.list=sum.list)
+  class(sum) <- "summary.ffm"
+  return(sum)
+}
+
+
+#' @rdname summary.ffm
+#' @method print summary.ffm
+#' @export
+
+print.summary.ffm <- function(x, digits=3, labels=TRUE, ...) {
+  n <- length(x$sum.list)
+  if (labels==TRUE) {
+    if(!is.null(cl <- x$call)) {
+      cat("\nCall:\n")
+      dput(cl)
+    }
+    cat("\nFactor Returns:\n", sep="")
+    for (i in 1:n) {
+      options(digits = digits)  
+      table.coef <- (x$sum.list)[[i]]$coefficients
+      cat("\nTime Period ", 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/fitFfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitFfm.Rd	2016-03-17 10:23:45 UTC (rev 4010)
+++ pkg/FactorAnalytics/man/fitFfm.Rd	2016-03-17 11:10:27 UTC (rev 4011)
@@ -66,9 +66,10 @@
 
 An object of class \code{"ffm"} is a list containing the following 
 components:
-\item{factor.fit}{list of fitted objects for each time period. Each object 
-is of class \code{lm} if \code{fit.method="LS" or "WLS"}, or, class 
-\code{lmRob} if \code{fit.method="Rob" or "W-Rob"}.}
+\item{factor.fit}{list of fitted objects that estimate factor returns in each 
+time period. Each fitted object is of class \code{lm} if 
+\code{fit.method="LS" or "WLS"}, or, class \code{lmRob} if 
+\code{fit.method="Rob" or "W-Rob"}.}
 \item{beta}{N x K matrix of factor exposures for the last time period.}
 \item{factor.returns}{xts object of K-factor returns (including intercept).}
 \item{residuals}{xts object of residuals for N-assets.}

Modified: pkg/FactorAnalytics/man/fmCov.Rd
===================================================================
--- pkg/FactorAnalytics/man/fmCov.Rd	2016-03-17 10:23:45 UTC (rev 4010)
+++ pkg/FactorAnalytics/man/fmCov.Rd	2016-03-17 11:10:27 UTC (rev 4011)
@@ -2,6 +2,7 @@
 % Please edit documentation in R/fmCov.R
 \name{fmCov}
 \alias{fmCov}
+\alias{fmCov.ffm}
 \alias{fmCov.sfm}
 \alias{fmCov.tsfm}
 \title{Covariance Matrix for assets' returns from fitted factor model.}
@@ -11,6 +12,8 @@
 \method{fmCov}{tsfm}(object, factor.cov, use = "pairwise.complete.obs", ...)
 
 \method{fmCov}{sfm}(object, use = "pairwise.complete.obs", ...)
+
+\method{fmCov}{ffm}(object, use = "pairwise.complete.obs", ...)
 }
 \arguments{
 \item{object}{fit object of class \code{tsfm}, \code{sfm} or \code{ffm}.}
@@ -64,23 +67,14 @@
 data(StockReturns)
 sfm.pca.fit <- fitSfm(r.M, k=2)
 fmCov(sfm.pca.fit)
-                      
-\dontrun{
-# Fundamental Factor Model
-data(stock)
-# there are 447 assets  
-exposure.names <- c("BOOK2MARKET", "LOG.MARKETCAP") 
-beta.mat <- subset(stock, DATE=="2003-12-31")[, exposure.names]
-beta.mat1 <- cbind(rep(1, 447), beta.mat1)
-# FM return covariance 
-fit.fund <- fitFfm(exposure.names=c("BOOK2MARKET", "LOG.MARKETCAP"), 
-                   data=stock, returnsvar="RETURN", datevar="DATE", 
-                   assetvar="TICKER", wls=TRUE, regression="classic", 
-                   covariance="classic", full.resid.cov=FALSE)
-ret.cov.fundm <- fmCov(beta.mat1,fit.fund$factor.cov$cov,fit.fund$resid.sd)
-fit.fund$returns.cov$cov == ret.cov.fundm
-}
 
+# Fundamental factor Model
+data(Stock.df)
+exposure.vars <- c("BOOK2MARKET", "LOG.MARKETCAP", "GICS.SECTOR")
+fit2 <- fitFfm(data=stock, asset.var="TICKER", ret.var="RETURN", 
+              date.var="DATE", exposure.vars=exposure.vars)
+fmCov(fit2)
+
 }
 \author{
 Eric Zivot, Yi-An Chen and Sangeetha Srinivasan.

Modified: pkg/FactorAnalytics/man/print.ffm.Rd
===================================================================
--- pkg/FactorAnalytics/man/print.ffm.Rd	2016-03-17 10:23:45 UTC (rev 4010)
+++ pkg/FactorAnalytics/man/print.ffm.Rd	2016-03-17 11:10:27 UTC (rev 4011)
@@ -18,7 +18,10 @@
 S3 \code{print} method for object of class \code{ffm}. Prints 
 the call, factor model dimension and summary statistics for the estimated
 factor returns, cross-sectional r-squared values and residual variances 
-from the fitted object.
+from the fitted object. 
+
+Refer to \code{\link{summary.ffm}} for a more detailed summary of the fit at 
+each time period.
 }
 \examples{
 data(Stock.df)

Added: pkg/FactorAnalytics/man/summary.ffm.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.ffm.Rd	                        (rev 0)
+++ pkg/FactorAnalytics/man/summary.ffm.Rd	2016-03-17 11:10:27 UTC (rev 4011)
@@ -0,0 +1,69 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/summary.ffm.R
+\name{summary.ffm}
+\alias{print.summary.ffm}
+\alias{summary.ffm}
+\title{Summarizing a fitted fundamental factor model}
+\usage{
+\method{summary}{ffm}(object, ...)
+
+\method{print}{summary.ffm}(x, digits = 3, labels = TRUE, ...)
+}
+\arguments{
+\item{object}{an object of class \code{ffm} returned by \code{fitFfm}.}
+
+\item{...}{futher arguments passed to or from other methods.}
+
+\item{x}{an object of class \code{summary.ffm}.}
+
+\item{digits}{number of significants digits to use when printing. 
+Default is 3.}
+
+\item{labels}{option to print labels and legend in the summary. Default is 
+\code{TRUE}. When \code{FALSE}, only the coefficient matrx with standard 
+errors is printed.}
+}
+\value{
+Returns an object of class \code{summary.ffm}. 
+The print method for class \code{summary.ffm} 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.ffm} is a list of length N + 2 containing:
+\item{call}{the function call to \code{fitFfm}}
+\item{sum.list}{list of summaries of the T fit objects (of class \code{lm} or 
+\code{lmRob}) for each time period in the factor model.}
+}
+\description{
+\code{summary} method for object of class \code{ffm}. 
+Returned object is of class {summary.ffm}.
+}
+\details{
+The default \code{summary} method for a fitted \code{lm} object 
+computes the standard errors and t-statistics under the assumption of 
+homoskedasticty.
+
+Note: This gives a summary of the fited factor returns at each time period. 
+If \code{T} is large, you might prefer the more succint summary produced by 
+\code{\link{print.ffm}}.
+}
+\examples{
+data(Stock.df)
+exposure.vars <- c("BOOK2MARKET", "LOG.MARKETCAP", "GICS.SECTOR")
+fit2 <- fitFfm(data=stock, asset.var="TICKER", ret.var="RETURN", 
+               date.var="DATE", exposure.vars=exposure.vars)
+
+# summary of factor returns estimated in each time period
+summary(fit2)
+
+# summary of lm fit for a single period
+summary(fit2$factor.fit[[1]])
+
+}
+\author{
+Sangeetha Srinivasan & Yi-An Chen.
+}
+\seealso{
+\code{\link{fitFfm}}, \code{\link[stats]{summary.lm}}
+}
+



More information about the Returnanalytics-commits mailing list