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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Dec 5 03:36:34 CET 2014


Author: pragnya
Date: 2014-12-05 03:36:33 +0100 (Fri, 05 Dec 2014)
New Revision: 3568

Modified:
   pkg/FactorAnalytics/DESCRIPTION
   pkg/FactorAnalytics/NAMESPACE
   pkg/FactorAnalytics/R/fitSfm.R
   pkg/FactorAnalytics/R/print.tsfm.r
   pkg/FactorAnalytics/R/summary.sfm.r
   pkg/FactorAnalytics/R/summary.tsfm.r
   pkg/FactorAnalytics/man/fitSfm.Rd
   pkg/FactorAnalytics/man/summary.sfm.Rd
   pkg/FactorAnalytics/man/summary.tsfm.Rd
   pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw
   pkg/FactorAnalytics/vignettes/fitTsfm_vignette.pdf
Log:
Edits to print and summary methods for fitTsfm and fitSfm

Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/DESCRIPTION	2014-12-05 02:36:33 UTC (rev 3568)
@@ -1,8 +1,8 @@
 Package: factorAnalytics
 Type: Package
 Title: Factor Analytics
-Version: 2.0.5
-Date: 2014-12-03
+Version: 2.0.6
+Date: 2014-12-04
 Author: Eric Zivot, Sangeetha Srinivasan and Yi-An Chen
 Maintainer: Sangeetha Srinivasan <sangee at uw.edu>
 Description: An R package for the estimation and risk analysis of linear factor

Modified: pkg/FactorAnalytics/NAMESPACE
===================================================================
--- pkg/FactorAnalytics/NAMESPACE	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/NAMESPACE	2014-12-05 02:36:33 UTC (rev 3568)
@@ -53,11 +53,9 @@
 importFrom(lattice,panel.grid)
 importFrom(lattice,xyplot)
 importFrom(leaps,regsubsets)
-importFrom(lmtest,coeftest)
 importFrom(lmtest,coeftest.default)
 importFrom(robust,lmRob)
 importFrom(robust,step.lmRob)
 importFrom(sandwich,vcovHAC.default)
-importFrom(sandwich,vcovHC)
 importFrom(sandwich,vcovHC.default)
 importFrom(strucchange,efp)

Modified: pkg/FactorAnalytics/R/fitSfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitSfm.R	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/R/fitSfm.R	2014-12-05 02:36:33 UTC (rev 3568)
@@ -67,8 +67,8 @@
 #' 
 #' An object of class \code{"sfm"} is a list containing the following 
 #' components:
-#' \item{asset.fit}{list of fitted objects of class \code{lm} for each asset, 
-#' from the time-series OLS regression of asset returns on estimated factors.}
+#' \item{asset.fit}{fitted object of class \code{"mlm"} or \code{"lm"} from the 
+#' time-series OLS regression of asset returns on estimated factors.}
 #' \item{k}{number of factors; as input or determined by "ck" or "bn" methods.}
 #' \item{factors}{T x K xts object of estimated factor realizations.}
 #' \item{loadings}{N x K matrix of factor loadings estimated by 

Modified: pkg/FactorAnalytics/R/print.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/print.tsfm.r	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/R/print.tsfm.r	2014-12-05 02:36:33 UTC (rev 3568)
@@ -38,7 +38,7 @@
   cat("\nFactor Betas:\n")
   B <- as.matrix(t(x$beta))
   if (x$variable.selection=="lars") { B[B==0] <- NA }
-  print(B, digits=digits, na.print="-", ...)
+  print(B, digits=digits, na.print=".", ...)
   cat("\nR-squared values:\n")
   print(x$r2, digits=digits, ...)
   cat("\nResidual Volatilities:\n")

Modified: pkg/FactorAnalytics/R/summary.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.sfm.r	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/R/summary.sfm.r	2014-12-05 02:36:33 UTC (rev 3568)
@@ -6,12 +6,13 @@
 #' @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) standard errors and t-statistics using 
-#' \code{\link[lmtest]{coeftest}}.
+#' heteroskedasticity-consistent (HC) or 
+#' heteroskedasticity-autocorrelation-consistent (HAC) standard errors and 
+#' t-statistics using \code{\link[lmtest]{coeftest}}.
 #'  
 #' @param object an object of class \code{sfm} returned by \code{fitSfm}.
-#' @param se.type one of "Default" or "HC"; option for computing HC standard 
-#' errors and t-statistics.
+#' @param se.type one of "Default", "HC" or "HAC"; option for computing HC/HAC 
+#' standard errors and t-statistics.
 #' @param x an object of class \code{summary.sfm}.
 #' @param digits number of significants digits to use when printing. 
 #' Default is 3.
@@ -25,14 +26,10 @@
 #' Object of class \code{summary.sfm} is a list of length N+2 containing:
 #' \item{call}{the function call to \code{fitSfm}}
 #' \item{se.type}{standard error type as input} 
-#' \item{}{summary of the fit object of class \code{mlm} for the factor model.}
+#' \item{sum.list}{list of summaries for the N fit objects of class \code{lm} 
+#' for each asset in the factor model.}
+#' \item{mimic}{N x K matrix of factor mimicking portfolio weights.}
 #' 
-#' @note For a more detailed printed summary for each asset, refer to 
-#' \code{\link[stats]{summary.lm}}, which includes F-statistics, 
-#' Multiple R-squared, Adjusted R-squared, further formats the coefficients, 
-#' standard errors, etc. and additionally gives significance stars if 
-#' \code{signif.stars} is TRUE. 
-#' 
 #' @author Sangeetha Srinivasan
 #' 
 #' @seealso \code{\link{fitSfm}}, \code{\link[stats]{summary.lm}}
@@ -45,8 +42,8 @@
 #' # summary of factor model fit for all assets
 #' summary(fit, "HAC")
 #' 
-#' @importFrom lmtest coeftest
-#' @importFrom sandwich vcovHC
+#' @importFrom lmtest coeftest.default
+#' @importFrom sandwich vcovHC.default vcovHAC.default
 #' 
 #' @method summary sfm
 #' @export
@@ -58,19 +55,26 @@
     stop("Invalid 'sfm' object")
   }
   
-  # extract list of mlm summary object for the entire model
-  mlm.fit.summary <- summary(object$asset.fit)
+  # extract list of summary.lm objects for all assets
+  sum.list <- summary(object$asset.fit)
+  names(sum.list) <- object$asset.names
   
-  # get coefficients and convert to HC standard errors and t-stats if specified
-  coefficients <-  coeftest(object$asset.fit, vcov.=vcovHC, data=sfm.data[,1])
-  if (se.type=="HC") {
-    coefficients <- coeftest(object$asset.fit, vcov.=vcovHC)
+  # convert to HC standard errors and t-stats if specified
+  if (se.type=="HC" || se.type=="HAC") {
+    for (i in object$asset.names) {
+      # need to get lm objects because "mlm" method doesn't exist for vcovHAC
+      f <- lm(as.numeric(object$data[,i]) ~ object$factors)
+      if (se.type=="HC") {
+        sum.list[[i]]$coefficients <- coeftest.default(f, vcov.=vcovHC.default)[,1:4]
+      } else if (se.type=="HAC") {
+        sum.list[[i]]$coefficients <- coeftest.default(f, vcov.=vcovHAC.default)[,1:4]
+      }
+    }
   }
   
   # include the call and se.type to fitSfm
-  sum <- list(call=object$call, se.type=se.type, coefficients=coefficients, 
-              mlm.fit.summary=mlm.fit.summary, r.squared=object$r2, 
-              sigma=object$resid.sd)
+  sum <- list(call=object$call, se.type=se.type, sum.list=sum.list, 
+              mimic=object$mimic)
   class(sum) <- "summary.sfm"
   return(sum)
 }
@@ -85,12 +89,20 @@
     cat("\nCall:\n")
     dput(cl)
   }
-  cat("\nFactor Model Coefficients:", "\n(", x$se.type, 
-      " Standard Errors & T-stats)\n\n", sep="")
-  c <- x$coefficients
-  print(c, digits=digits, ...)
-  r2 <- x$r.squared
-  print(r2, digits=digits, ...)
-  sig <- x$sigma
-  print(sig, digits=digits, ...)
+  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="")
+  }
 }

Modified: pkg/FactorAnalytics/R/summary.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.tsfm.r	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/R/summary.tsfm.r	2014-12-05 02:36:33 UTC (rev 3568)
@@ -31,15 +31,9 @@
 #' 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{}{summaries of the N fit objects (of class \code{lm}, \code{lmRob} 
-#' or \code{lars}) for each asset in the factor model.}
+#' \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.}
 #' 
-#' @note For a more detailed printed summary for each asset, refer to 
-#' \code{\link[stats]{summary.lm}} or \code{\link[robust]{lmRob}}, which 
-#' include F-statistics, Multiple R-squared, Adjusted R-squared and further 
-#' format the coefficients, standard errors, etc. and additionally give 
-#' significance stars if \code{signif.stars} is TRUE. 
-#' 
 #' @author Sangeetha Srinivasan & Yi-An Chen.
 #' 
 #' @seealso \code{\link{fitTsfm}}, \code{\link[stats]{summary.lm}}
@@ -74,33 +68,33 @@
   }
   
   # extract summary.lm objects for each asset
-  sum <- lapply(object$asset.fit, summary)
+  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[[i]]$coefficients <- coeftest.default(object$asset.fit[[i]],
-                                                vcov.=vcovHC.default)[,1:4]
+      sum.list[[i]]$coefficients <- coeftest.default(object$asset.fit[[i]],
+                                                     vcov.=vcovHC.default)[,1:4]
     } else if (se.type=="HAC") {
-      sum[[i]]$coefficients <- coeftest.default(object$asset.fit[[i]], 
-                                                vcov.=vcovHAC.default)[,1:4]
+      sum.list[[i]]$coefficients <- coeftest.default(object$asset.fit[[i]], 
+                                                     vcov.=vcovHAC.default)[,1:4]
     }
   }
   
   if (object$variable.selection=="lars") {
-    sum <- list()
+    sum.list <- list()
     for (i in object$asset.names) {
-      sum[[i]]$coefficients <- as.matrix(c(object$alpha[i], object$beta[i,]))
-      rownames(sum[[i]]$coefficients)[1]="(Intercept)"
-      colnames(sum[[i]]$coefficients)[1]="Estimate"
-      sum[[i]]$r.squared <- as.numeric(object$r2[i])
-      sum[[i]]$sigma <- as.numeric(object$resid.sd[i]) 
+      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 <- c(list(call=object$call, se.type=se.type), sum)
+  sum <- list(call=object$call, se.type=se.type, sum.list=sum.list)
   class(sum) <- "summary.tsfm"
   return(sum)
 }
@@ -115,20 +109,20 @@
     cat("\nCall:\n")
     dput(cl)
   }
-  cat("\nFactor Model Coefficients:\n", 
-      sep="")
-  n <- length(x)
-  for (i in 3:n) {
+  cat("\nFactor Model Coefficients:\n", sep="")
+  n <- length(x$sum.list)
+  for (i in 1:n) {
     options(digits = digits)  
-    if (dim(x[[i]]$coefficients)[2] > 1) {
-      cat("\nAsset", i-2, ": ", names(x[i]), "\n(", x$se.type, 
+    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-2, ": ", names(x[i]), "\n\n", sep="")  
+      cat("\nAsset", i, ": ", names(x$sum.list[i]), "\n\n", sep="")  
     }
-    table.coef <- x[[i]]$coefficients
+    r2 <- x$sum.list[[i]]$r.squared
+    sigma <- x$sum.list[[i]]$sigma
     printCoefmat(table.coef, digits=digits, ...)
-    cat("\nR-squared: ", x[[i]]$r.squared,", Residual Volatility: "
-        , x[[i]]$sigma,"\n", sep="")
+    cat("\nR-squared: ", r2,", Residual Volatility: ", sigma,"\n", sep="")
   }
 }

Modified: pkg/FactorAnalytics/man/fitSfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitSfm.Rd	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/man/fitSfm.Rd	2014-12-05 02:36:33 UTC (rev 3568)
@@ -53,8 +53,8 @@
 
 An object of class \code{"sfm"} is a list containing the following
 components:
-\item{asset.fit}{list of fitted objects of class \code{lm} for each asset,
-from the time-series OLS regression of asset returns on estimated factors.}
+\item{asset.fit}{fitted object of class \code{"mlm"} or \code{"lm"} from the
+time-series OLS regression of asset returns on estimated factors.}
 \item{k}{number of factors; as input or determined by "ck" or "bn" methods.}
 \item{factors}{T x K xts object of estimated factor realizations.}
 \item{loadings}{N x K matrix of factor loadings estimated by

Modified: pkg/FactorAnalytics/man/summary.sfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.sfm.Rd	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/man/summary.sfm.Rd	2014-12-05 02:36:33 UTC (rev 3568)
@@ -11,8 +11,8 @@
 \arguments{
 \item{object}{an object of class \code{sfm} returned by \code{fitSfm}.}
 
-\item{se.type}{one of "Default" or "HC"; option for computing HC standard
-errors and t-statistics.}
+\item{se.type}{one of "Default", "HC" or "HAC"; option for computing HC/HAC
+standard errors and t-statistics.}
 
 \item{...}{futher arguments passed to or from other methods.}
 
@@ -30,7 +30,9 @@
 Object of class \code{summary.sfm} is a list of length N+2 containing:
 \item{call}{the function call to \code{fitSfm}}
 \item{se.type}{standard error type as input}
-\item{}{summary of the fit object of class \code{mlm} for the factor model.}
+\item{sum.list}{list of summaries for the N fit objects of class \code{lm}
+for each asset in the factor model.}
+\item{mimic}{N x K matrix of factor mimicking portfolio weights.}
 }
 \description{
 \code{summary} method for object of class \code{sfm}.
@@ -40,16 +42,10 @@
 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) standard errors and t-statistics using
-\code{\link[lmtest]{coeftest}}.
+heteroskedasticity-consistent (HC) or
+heteroskedasticity-autocorrelation-consistent (HAC) standard errors and
+t-statistics using \code{\link[lmtest]{coeftest}}.
 }
-\note{
-For a more detailed printed summary for each asset, refer to
-\code{\link[stats]{summary.lm}}, which includes F-statistics,
-Multiple R-squared, Adjusted R-squared, further formats the coefficients,
-standard errors, etc. and additionally gives significance stars if
-\code{signif.stars} is TRUE.
-}
 \examples{
 data(stat.fm.data)
 # fit the factor model with PCA

Modified: pkg/FactorAnalytics/man/summary.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.tsfm.Rd	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/man/summary.tsfm.Rd	2014-12-05 02:36:33 UTC (rev 3568)
@@ -30,8 +30,8 @@
 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{}{summaries of the N fit objects (of class \code{lm}, \code{lmRob}
-or \code{lars}) for each asset in the factor model.}
+\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}.
@@ -50,13 +50,6 @@
 statistically valid method of calculating standard errors for the lasso
 predictions.
 }
-\note{
-For a more detailed printed summary for each asset, refer to
-\code{\link[stats]{summary.lm}} or \code{\link[robust]{lmRob}}, which
-include F-statistics, Multiple R-squared, Adjusted R-squared and further
-format the coefficients, standard errors, etc. and additionally give
-significance stars if \code{signif.stars} is TRUE.
-}
 \examples{
 data(managers)
 fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),

Modified: pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw
===================================================================
--- pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw	2014-12-04 07:48:22 UTC (rev 3567)
+++ pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw	2014-12-05 02:36:33 UTC (rev 3568)
@@ -53,7 +53,7 @@
 
 \begin{itemize}
 
-\item \verb"fitTsfm(asset.names, factor.names, data, fit.method, variable.selection)": Fits a time series (a.k.a. macroeconomic) factor model for one or more asset returns or excess returns using time series regression. Ordinary least squares (OLS), discounted least squares (DLS) and robust regression fitting are possible. Variable selection methods include "stepwise", "subsets" and "lars". An object of class "tsfm" containing the fitted objects, model coefficients, R-squared and residual volatility are returned.
+\item \verb"fitTsfm(asset.names, factor.names, data, fit.method, variable.selection)": Fits a time series (a.k.a. macroeconomic) factor model for one or more asset returns or excess returns using time series regression. Ordinary least squares (OLS), discounted least squares (DLS) and robust regression fitting are possible. Variable selection methods include "stepwise", "subsets" and "lars". An object of class "tsfm" containing the fitted objects, model coefficients, R-squared and residual volatility is returned.
 
 \item \verb"coef(object)": Extracts the coefficient matrix (intercept and factor betas) for all assets fit by the "tsfm" object.
 
@@ -69,7 +69,7 @@
 
 \item \verb"fmEsDecomp(object, p, method, invert)": Returns a list containing the expected shortfall for asset returns based on the fitted factor model and the marginal, component and percentage component factor contributions estimated from the given sample. Arguments \code{"p"}, \code{"method"} and \code{invert} are the same as above.
 
-\item \verb"plot(x)": The \code{plot} method for class "tsfm" can be used for plotting factor model characteristics of an individual asset or a group of assets (default). The type of individual/group plot can be specified or chosen from a menu prompt (default if type not specified). Further the menu reappears (default) to enable multiple plots for the same asset(s) unless looping is disabled by setting \code{loop=FALSE}.
+\item \verb"plot(x)": The \code{plot} method for class "tsfm" can be used for plotting factor model characteristics of an individual asset or a group of assets (default). The type of individual/group plot can be specified or chosen from a menu prompt, which is the default if type is not specified. Further the menu reappears (default) to access multiple plots for the same asset(s) unless looping is disabled by setting \code{loop=FALSE}.
 
 \item \verb"predict(object, newdata)": The \code{predict} method for class "tsfm" returns a vector or matrix of predicted values for a new data sample or simulated values.
 
@@ -274,7 +274,7 @@
 Many useful generic accessor functions are available for "tsfm" fit objects. \code{coef()} returns a matrix of estimated model coefficients including the intercept. \code{fitted()} returns an xts data object of the component of asset returns explained by the factor model. \code{residuals()} returns an xts data object with the component of asset returns not explained by the factor model. 
 \code{predict()} uses the fitted factor model to estimate asset returns given a set of new or simulated factor return data.  
 
-\code{summary()} prints standard errors and t-statistics for all estimated coefficients in addition to R-squared values and residual volatilities. The \code{se.type} argument enables computing heteroskedasticity and auto-correlation consistent ("HC" and "HAC") estimates and standard errors whenever possible. A "summary.tsfm" object is returned which contains the summary objects returned by "lm", "lm.Rob" or "lars" for each asset fit. 
+\code{summary()} prints standard errors and t-statistics for all estimated coefficients in addition to R-squared values and residual volatilities. The \code{se.type} argument enables computing heteroskedasticity and auto-correlation consistent ("HC" and "HAC") estimates and standard errors whenever possible. A "summary.tsfm" object is returned which contains a list of summary objects returned by "lm", "lm.Rob" or "lars" for each asset fit. 
 
 Note: Standard errors are currently not available for the "lars" variable selection method, as there seems to be no consensus on a statistically valid method of calculating standard errors for the lasso predictions.
 

Modified: pkg/FactorAnalytics/vignettes/fitTsfm_vignette.pdf
===================================================================
(Binary files differ)



More information about the Returnanalytics-commits mailing list