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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 2 08:10:51 CEST 2014


Author: pragnya
Date: 2014-07-02 08:10:51 +0200 (Wed, 02 Jul 2014)
New Revision: 3459

Added:
   pkg/FactorAnalytics/R/covFM.r
   pkg/FactorAnalytics/man/covFM.Rd
Removed:
   pkg/FactorAnalytics/R/coef.sfm.r
   pkg/FactorAnalytics/R/coef.tsfm.R
   pkg/FactorAnalytics/R/factorModelCovariance.r
   pkg/FactorAnalytics/R/fitted.sfm.r
   pkg/FactorAnalytics/R/fitted.tsfm.r
   pkg/FactorAnalytics/R/residuals.sfm.r
   pkg/FactorAnalytics/R/residuals.tsfm.r
   pkg/FactorAnalytics/R/tsfm.r
   pkg/FactorAnalytics/man/coef.sfm.Rd
   pkg/FactorAnalytics/man/coef.tsfm.Rd
   pkg/FactorAnalytics/man/factorModelCovariance.Rd
   pkg/FactorAnalytics/man/fitted.sfm.Rd
   pkg/FactorAnalytics/man/fitted.tsfm.Rd
   pkg/FactorAnalytics/man/print.summary.tsfm.Rd
   pkg/FactorAnalytics/man/residuals.sfm.Rd
   pkg/FactorAnalytics/man/residuals.tsfm.Rd
Modified:
   pkg/FactorAnalytics/DESCRIPTION
   pkg/FactorAnalytics/NAMESPACE
   pkg/FactorAnalytics/R/fitTSFM.R
   pkg/FactorAnalytics/R/plot.tsfm.r
   pkg/FactorAnalytics/R/predict.tsfm.r
   pkg/FactorAnalytics/R/print.tsfm.r
   pkg/FactorAnalytics/R/summary.tsfm.r
   pkg/FactorAnalytics/man/fitTSFM.Rd
   pkg/FactorAnalytics/man/plot.tsfm.Rd
   pkg/FactorAnalytics/man/predict.tsfm.Rd
   pkg/FactorAnalytics/man/summary.tsfm.Rd
Log:
Added covFM method, combined generic accessor function Rd files for fitTSFM

Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/DESCRIPTION	2014-07-02 06:10:51 UTC (rev 3459)
@@ -3,16 +3,17 @@
 Title: Factor Analytics
 Version: 1.0
 Date: 2014-06-18
-Author: Eric Zivot and Yi-An Chen
-Maintainer: Yi-An Chen <chenyian at uw.edu>
+Author: Eric Zivot, Yi-An Chen and Sangeetha Srinivasan
+Maintainer: Sangeetha Srinivasan <sangee at uw.edu>
 Description: An R package for the estimation and risk analysis of linear factor
     models for asset returns and portfolios. It contains model fitting methods
     for the three major types of factor models: time series (or, macroeconomic)
     factor model, fundamental factor model and statistical factor model. They
     allow for different types of distributions to be specified for modeling the
     fat-tailed behavior of financial returns, including Edgeworth expansions.
-    Risk analysis measures such as VaR and ES and performance attribution (to 
-    factor-contributed and idiosyncratic returns) are also included. 
+    Risk analysis measures such as VaR and ES, as well as performance 
+    attribution for factor models (factor-contributed vs idiosyncratic returns) 
+    are included. 
 License: GPL-2
 Depends:
     R (>= 2.14.0),
@@ -30,3 +31,4 @@
     testthat, quantmod
 LazyLoad: yes
 LazyDataCompression: xz
+URL: http://r-forge.r-project.org/R/?group_id=579

Modified: pkg/FactorAnalytics/NAMESPACE
===================================================================
--- pkg/FactorAnalytics/NAMESPACE	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/NAMESPACE	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,8 +1,7 @@
 # Generated by roxygen2 (4.0.1): do not edit by hand
 
-S3method(coef,sfm)
 S3method(coef,tsfm)
-S3method(fitted,sfm)
+S3method(covFM,tsfm)
 S3method(fitted,tsfm)
 S3method(plot,FundamentalFactorModel)
 S3method(plot,StatFactorModel)
@@ -16,14 +15,13 @@
 S3method(print,pafm)
 S3method(print,summary.tsfm)
 S3method(print,tsfm)
-S3method(residuals,sfm)
 S3method(residuals,tsfm)
 S3method(summary,FundamentalFactorModel)
 S3method(summary,StatFactorModel)
 S3method(summary,pafm)
 S3method(summary,tsfm)
+export(covFM)
 export(dCornishFisher)
-export(factorModelCovariance)
 export(factorModelEsDecomposition)
 export(factorModelMonteCarlo)
 export(factorModelSdDecomposition)

Deleted: pkg/FactorAnalytics/R/coef.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/coef.sfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/coef.sfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,23 +0,0 @@
-#' @title Extract coefficients from a fitted stochastic factor model
-#' 
-#' @description Method or helper function for fit object of class \code{sfm}. 
-#' 
-#' @param object a fit object of class \code{sfm} which is returned by 
-#' \code{\link{fitSFM}} 
-#' @param ... other arguments passed
-#' 
-#' @return
-#' \item{coef.mat}{an N x (K+1) matrix of all coefficients}
-#' where, N is the number of assets and K is the number of factors.
-#' 
-#' @author Eric Zivot and Sangeetha Srinivasan
-#' 
-#' @seealso \code{\link{fitTSFM}}
-#' 
-#' @method coef sfm
-#' @export
-
-coef.sfm <- function(object,...){
-  coef.mat <- t(sapply(object$asset.fit, coef))
-  return(coef.mat)
-}
\ No newline at end of file

Deleted: pkg/FactorAnalytics/R/coef.tsfm.R
===================================================================
--- pkg/FactorAnalytics/R/coef.tsfm.R	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/coef.tsfm.R	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,34 +0,0 @@
-#' @title Extract coefficients from a fitted time series factor model
-#' 
-#' @description Method or helper function for fit object of class \code{tsfm}. 
-#' 
-#' @param object a fit object of class \code{tsfm} which is returned by 
-#' \code{\link{fitTSFM}} 
-#' @param ... other arguments passed
-#' 
-#' @return 
-#' \item{coef.mat}{an N x (K+1) matrix of all coefficients}
-#' where, N is the number of assets and K is the number of factors.
-#' 
-#' @author Eric Zivot and Sangeetha Srinivasan
-#' 
-#' @seealso \code{\link{fitTSFM}}
-#' 
-#' @examples
-#' \dontrun{
-#' data(managers.df)
-#' fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
-#'                factor.names=colnames(managers.df[,7:9]), 
-#'                market.name="SP500.TR",
-#'                data=data, fit.method="OLS", variable.selection="none", 
-#'                add.up.market=TRUE, add.market.sqd=TRUE)
-#' coef(fit)
-#' }
-#' 
-#' @method coef tsfm
-#' @export
-
-coef.tsfm <- function(object,...){
-  coef.mat <- t(sapply(object$asset.fit, coef))
-  return(coef.mat)
-}

Added: pkg/FactorAnalytics/R/covFM.r
===================================================================
--- pkg/FactorAnalytics/R/covFM.r	                        (rev 0)
+++ pkg/FactorAnalytics/R/covFM.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -0,0 +1,79 @@
+#' @title Covariance Matrix for assets' returns from fitted factor model.
+#' 
+#' @description Computes the covariance matrix for assets' returns based on a 
+#' fitted factor model. This is a generic function with methods for classes 
+#' \code{tsfm}, \code{sfm} and \code{ffm}.
+#' 
+#' @details \code{R(i, t)}, the return on asset \code{i} at time \code{t}, 
+#' is assumed to follow a factor model of the form, \cr \cr 
+#' \code{R(i,t) = alpha(i) + beta*F(t) + e(i,t)}, \cr \cr  
+#' where, \code{alpha(i)} is the intercept, \code{F(t)} is a {K x 1} vector of 
+#' the \code{K} factor values at time \code{t}, \code{beta} is a \code{1 x K} 
+#' vector of factor exposures and the error terms \code{e(i,t)} are serially 
+#' uncorrelated across time and contemporaneously uncorrelated across assets 
+#' so that \code{e(i,t) ~ iid(0,sig(i)^2)}. Thus, the variance of asset 
+#' \code{i}'s return is given by \cr \cr
+#' \code{var(R(i,t)) = beta*var(F(t))*tr(beta) + sig(i)^2}. \cr \cr
+#' And, the \code{N x N} covariance matrix of N asset returns is \cr \cr
+#' \code{var(R) = B*var(F(t))*tr(B) + D}, \cr \cr 
+#' where, B is the \code{N x K} matrix of factor betas and \code{D} is a 
+#' diagonal matrix with \code{sig(i)^2} along the diagonal.
+#' 
+#' @param object fit object of class \code{tsfm}, \code{sfm} or \code{ffm}.
+#' 
+#' @return The computed \code{N x N} covariance matrix for asset returns based 
+#' on the fitted factor model.
+#' 
+#' @author Eric Zivot, Yi-An Chen and Sangeetha Srinivasan.
+#' 
+#' @references 
+#' \enumerate{
+#' \item Zivot, Eric, and W. A. N. G. Jia-hui. "Modeling Financial Time Series 
+#' with S-Plus Springer-Verlag." (2006).
+#' }
+#' 
+#' @seealso \code{\link{fitTSFM}}, \code{\link{fitSFM}}, \code{\link{fitFFM}}
+#' 
+#' @examples
+#' \dontrun{
+#' # Time Series Factor model
+#' data(managers.df)
+#' factors = managers.df[, (7:9)]
+#' fit <- fitTSFM(assets.names=colnames(managers.df[, (1:6)]), 
+#'                factors.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df, 
+#'                fit.method="OLS")                              
+#' covFM(fit)
+#' 
+#' # Statistical Factor Model
+#' data(stat.fm.data)
+#' sfm.pca.fit <- fitStatisticalFactorModel(sfm.dat, k=2)
+#' #' covFM(t(sfm.pca.fit$loadings), var(sfm.pca.fit$factors), 
+#'                          sfm.pca.fit$resid.sd)
+#' 
+#' sfm.apca.fit <- fitSFM(sfm.apca.dat, k=2)
+#' 
+#' covFM(t(sfm.apca.fit$loadings), var(sfm.apca.fit$factors), 
+#'                       sfm.apca.fit$resid.sd)
+#'
+#' # 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 <- covFM(beta.mat1, fit.fund$factor.cov$cov, 
+#'                                        fit.fund$resid.sd)
+#' fit.fund$returns.cov$cov == ret.cov.fundm
+#' }
+#' 
+#' @rdname covFM
+#' @export
+
+covFM <- function(object){
+UseMethod("covFM")
+}

Deleted: pkg/FactorAnalytics/R/factorModelCovariance.r
===================================================================
--- pkg/FactorAnalytics/R/factorModelCovariance.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/factorModelCovariance.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,102 +0,0 @@
-#' @title Factor model Covariance Matrix for assets' returns.
-#' 
-#' @description Computes the covariance matrix for assets' returns based on a 
-#' fitted factor model.
-#' 
-#' @details The return on asset \code{i} is assumed to follow a factor model 
-#' of the form, \cr \cr \code{R(i,t) = alpha + beta*F(t) + e(i,t)}, \cr \cr  
-#' where, \code{e(i,t) ~ iid(0,sig(i)^2)}, \code{beta} is a \code{1 x K} vector 
-#' of factor exposures and the error terms are serially uncorrelated and 
-#' contenporaneously uncorrelated across assets. Thus, the variance of asset 
-#' \code{i}'s return is given by \cr \cr
-#' \code{var(R(i,t)) = beta*var(F(t))*tr(beta) + sig(i)^2}. \cr \cr
-#' And, the \code{N x N} covariance matrix of N asset returns is \cr \cr
-#' \code{var(R) = B*var(F(t))*tr(B) + D}, \cr \cr 
-#' where, B is the \code{N x K} matrix of asset betas and \code{D} is a diagonal 
-#' matrix with \code{sig(i)^2} along the diagonal.
-#' 
-#' @param beta an \code{N x K} matrix of factor betas, where \code{N} is the
-#' number of assets and \code{K} is the number of factors.
-#' @param factor.cov a \code{K x K} factor covariance matrix.
-#' @param resid.sd an \code{N x 1} vector of asset specific residual
-#' volatilities from the factor model.
-#' 
-#' @return The computed \code{N x N} covariance matrix for asset returns based 
-#' on the given factor model parameters.
-#' 
-#' @author Eric Zivot, Yi-An Chen and Sangeetha Srinivasan.
-#' 
-#' @references Zivot, E. and J. Wang (2006), \emph{Modeling Financial Time
-#' Series with S-PLUS, Second Edition}, Springer-Verlag.
-#' 
-#' @seealso \code{\link{fitTSFM}}, \code{\link{fitSFM}}, \code{\link{fitFFM}}
-#' 
-#' @examples
-#' \dontrun{
-#' # Time Series Factor model
-#' data(managers.df)
-#' factors = managers.df[, (7:9)]
-#' fit <- fitTSFM(assets.names=colnames(managers.df[, (1:6)]), 
-#'                factors.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df, 
-#'                fit.method="OLS")
-#' factors = managers.df[, (7:8)]                               
-#' factorModelCovariance(fit$beta, var(factors), fit$resid.sd)
-#' 
-#' # Statistical Factor Model
-#' data(stat.fm.data)
-#' sfm.pca.fit <- fitStatisticalFactorModel(sfm.dat, k=2)
-#' #' factorModelCovariance(t(sfm.pca.fit$loadings), var(sfm.pca.fit$factors), 
-#'                          sfm.pca.fit$resid.sd)
-#' 
-#' sfm.apca.fit <- fitSFM(sfm.apca.dat, k=2)
-#' 
-#' factorModelCovariance(t(sfm.apca.fit$loadings), var(sfm.apca.fit$factors), 
-#'                       sfm.apca.fit$resid.sd)
-#'
-#' # 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 <- factorModelCovariance(beta.mat1, fit.fund$factor.cov$cov, 
-#'                                        fit.fund$resid.sd)
-#' fit.fund$returns.cov$cov == ret.cov.fundm
-#' }
-#' @export
-#' 
-
-factorModelCovariance <- function(beta, factor.cov, resid.sd) {
-  
-  beta = as.matrix(beta)
-  factor.cov = as.matrix(factor.cov)
-  sig2.e = as.vector(resid.sd)^2
-  
-  if (length(sig2.e) > 1) {
-    D.e = diag(as.vector(sig2.e))
-  } else {
-    D.e =  as.matrix(sig2.e)
-  }
-  
-  if (ncol(beta) != ncol(factor.cov)) {
-    stop("'beta' and 'factor.cov' must have same number of columns.")
-  }
-  
-  if (nrow(D.e) != nrow(beta)) {
-    stop("'beta' and 'D.e' must have same number of rows.")
-  }
-  
-  cov.fm = beta %*% factor.cov %*% t(beta) + D.e
-  
-  if (any(diag(chol(cov.fm)) == 0)) {
-    warning("Covariance matrix is not positive definite!")
-  }
-  
-  return(cov.fm)
-}
-

Modified: pkg/FactorAnalytics/R/fitTSFM.R
===================================================================
--- pkg/FactorAnalytics/R/fitTSFM.R	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/fitTSFM.R	2014-07-02 06:10:51 UTC (rev 3459)
@@ -34,7 +34,9 @@
 #' regression, following Henriksson & Merton (1981), to account for market 
 #' timing (price movement of the general stock market relative to fixed income 
 #' securities). The coefficient can be interpreted as the number of free put 
-#' options.
+#' options. Similarly, if \code{add.market.sqd = TRUE}, (Rm-Rf)^2 is added as 
+#' a factor in the regression, following Treynor-Mazuy (1966), to account for 
+#' market timing with respect to volatility.
 #' 
 #' Finally, for both the "lars" and "lasso" methods, the "Cp" statistic 
 #' (defined in page 17 of Efron et al. (2002)) is calculated using 
@@ -42,14 +44,14 @@
 #' cross-validated mean squared prediction error using 
 #' \code{\link[lars]{cv.lars}}.
 #' 
-#' @param asset.names  vector containing names of assets, whose returns or 
+#' @param asset.names vector containing names of assets, whose returns or 
 #' excess returns are the dependent variable.
 #' @param factor.names vector containing names of the macroeconomic factors.
 #' @param market.name name of the column for market excess returns (Rm-Rf). 
-#' Is required only if \code{add.up.market} or \code{add.up.market.squared} 
+#' Is required only if \code{add.up.market} or \code{add.market.sqd} 
 #' are \code{TRUE}. 
 #' @param data vector, matrix, data.frame, xts, timeSeries or zoo object  
-#' containing column(s) named \code{asset.names}, \code{factor.names} and 
+#' containing column(s) named in \code{asset.names}, \code{factor.names} and 
 #' optionally, \code{market.name}.
 #' @param fit.method the estimation method, one of "OLS", "DLS" or "Robust". 
 #' See details. 
@@ -82,9 +84,19 @@
 #' and "DLS" fits. Scope argument is not available presently. Also plan to
 #' include other controls passed to \code{lmRob} soon.
 #' 
-#' @return fitTSFM returns an object of class 
-#' \code{tsfm}. The returned object is a list
-#' containing the following components:
+#' @return fitTSFM returns an object of class \code{tsfm}. 
+#' 
+#' The generic functions \code{summary}, \code{predict} and \code{plot} are 
+#' used to obtain and print a summary, predicted asset returns for new factor 
+#' data and plot selected characteristics for one or more assets. The generic 
+#' accessor functions \code{coefficients}, \code{fitted} and \code{residuals} 
+#' extract various useful features of the fit object. \code{coef.tsfm} extracts 
+#' coefficients from the fitted factor model and returns an N x (K+1) matrix of 
+#' all coefficients, \code{fitted.tsfm} gives an N x T data object of fitted 
+#' values and \code{residuals.tsfm} gives an N x T data object of residuals.
+#' 
+#' An object of class \code{tsfm} is a list containing the following 
+#' components:
 #' \item{asset.fit}{list of fitted objects for each asset. Each object is of 
 #' class \code{lm} if \code{fit.method="OLS" or "DLS"}, class \code{lmRob} if 
 #' the \code{fit.method="Robust"}, or class \code{lars} if 
@@ -99,33 +111,35 @@
 #' \item{factor.names}{factor.names as input.}
 #' \item{fit.method}{fit.method as input.}
 #' \item{variable.selection}{variable.selection as input.}
-#' Where N is the number of assets and K is the number of factors.
+#' Where N is the number of assets, K is the number of factors and T is the 
+#' number of time periods.
 #' 
-#' @family Factor Models
-#' 
 #' @author Eric Zivot, Yi-An Chen and Sangeetha Srinivasan.
 #' 
 #' @references 
 #' \enumerate{
-#' \item Christopherson, Carino and Ferson (2009). Portfolio Performance 
-#' Measurement and Benchmarking, McGraw Hill.
-#' \item Efron, Hastie, Johnstone and Tibshirani (2002) "Least Angle
-#' Regression" (with discussion) Annals of Statistics. Also refer to 
-#' \url{http://www-stat.stanford.edu/~hastie/Papers/LARS/LeastAngle_2002.pdf}. 
-#' \item Hastie, Tibshirani and Friedman (2008) Elements of Statistical 
-#' Learning 2nd edition, Springer, NY.
-#' \item Henriksson and Merton (1981). On market timing and investment 
-#' performance. II. Statistical procedures for evaluating forecasting skills, 
-#' Journal of Business, Vol 54, No 4.
+#' \item Christopherson, Jon A., David R. Carino, and Wayne E. Ferson. 
+#' Portfolio performance measurement and benchmarking. McGraw Hill 
+#' Professional, 2009.
+#' \item Efron, Bradley, Trevor Hastie, Iain Johnstone, and Robert Tibshirani. 
+#' "Least angle regression." The Annals of statistics 32, no. 2 (2004): 407-499. 
+#' \item Hastie, Trevor, Robert Tibshirani, Jerome Friedman, T. Hastie, J. 
+#' Friedman, and R. Tibshirani. The elements of statistical learning. Vol. 2, 
+#' no. 1. New York: Springer, 2009.
+#' \item Henriksson, Roy D., and Robert C. Merton. "On market timing and 
+#' investment performance. II. Statistical procedures for evaluating 
+#' forecasting skills." Journal of business (1981): 513-533.
+#' \item Treynor, Jack, and Kay Mazuy. "Can mutual funds outguess the market." 
+#' Harvard business review 44, no. 4 (1966): 131-136.
 #' }
 #' 
-#' @seealso The following generic method functions: \code{\link{plot.tsfm}}, 
-#' \code{\link{predict.tsfm}}, \code{\link{print.tsfm}} and 
-#' \code{\link{summary.tsfm}}. 
+#' @seealso The \code{tsfm} methods for generic functions: 
+#' \code{\link{plot.tsfm}}, \code{\link{predict.tsfm}}, 
+#' \code{\link{print.tsfm}} and \code{\link{summary.tsfm}}. 
 #' 
-#' And, the following extractor functions: \code{\link{coef.tsfm}}, 
-#' \code{\link{cov.tsfm}}, \code{\link{fitted.tsfm}} and 
-#' \code{\link{residuals.tsfm}}.
+#' And, the following extractor functions: \code{\link[stats]{coef}}, 
+#' \code{\link{covFM}}, \code{\link[stats]{fitted}} and 
+#' \code{\link[stats]{residuals}}.
 #' 
 #' \code{\link{paFM}} for Performance Attribution. 
 #' 
@@ -137,6 +151,8 @@
 #'                fit.method="OLS", variable.selection="none")
 #' # summary of HAM1 
 #' summary(fit$asset.fit$HAM1)
+#' # fitted values all 6 asset returns
+#' fitted(fit)
 #' # plot actual vs. fitted over time for HAM1
 #' # using chart.TimeSeries() function from PerformanceAnalytics package
 #' dataToPlot <- cbind(fitted(fit$asset.fit$HAM1), na.omit(managers.df$HAM1))
@@ -144,7 +160,6 @@
 #' chart.TimeSeries(dataToPlot, main="FM fit for HAM1",
 #'                  colorset=c("black","blue"), legend.loc="bottomleft")
 #'
-#'  
 #'  @export
 
 fitTSFM <- function(asset.names, factor.names, market.name, data=data, 
@@ -450,4 +465,81 @@
   w <- d^seq((t-1),0,-1)    
   # ensure that the weights sum to unity
   w/sum(w)
-}
\ No newline at end of file
+}
+
+
+#' @param object a fit object of class \code{tsfm} which is returned by 
+#' \code{fitTSFM}
+ 
+#' @rdname fitTSFM
+#' @method coef tsfm
+#' @export
+
+coef.tsfm <- function(object,...){
+  coef.mat <- t(sapply(object$asset.fit, coef))
+  return(coef.mat)
+}
+
+#' @rdname fitTSFM
+#' @method fitted tsfm
+#' @export
+
+fitted.tsfm <- function(object,...){
+  # get fitted values from each linear factor model fit 
+  # and convert them into xts/zoo objects
+  fitted.list = sapply(object$asset.fit, function(x) checkData(fitted(x)))
+  # this is a list of xts objects, indexed by the asset name
+  # merge the objects in the list into one xts object
+  fitted.xts <- do.call(merge, fitted.list)
+  return(fitted.xts)
+}
+
+
+#' @rdname fitTSFM
+#' @method residuals tsfm
+#' @export
+
+residuals.tsfm <- function(object ,...) {
+  # get residuals from each linear factor model fit 
+  # and convert them into xts/zoo objects
+  residuals.list = sapply(object$asset.fit, function(x) checkData(residuals(x)))
+  # this is a list of xts objects, indexed by the asset name
+  # merge the objects in the list into one xts object
+  residuals.xts <- do.call(merge, residuals.list)
+  return(residuals.xts)
+}
+
+#' @rdname fitTSFM
+#' @method covFM tsfm
+#' @export
+
+covFM.tsfm <- function(object) {
+  
+  # check input object validity
+  if (!inherits(object, c("tsfm", "sfm", "ffm"))) {
+    stop("Invalid argument: Object should be of class 'tsfm', 'sfm' or 'ffm'.")
+  }
+  
+  # get parameters and factors from factor model
+  beta <- object$beta
+  sig2.e = object$resid.sd^2
+  factor <- object$data[, colnames(object$beta)]
+  
+  # factor covariance matrix 
+  factor.cov = var(factor, use="na.or.complete")
+  
+  # residual covariance matrix D
+  if (length(sig2.e) > 1) {
+    D.e = diag(sig2.e)
+  } else {
+    D.e =  as.vector(sig2.e)
+  }
+  
+  cov.fm = beta %*% factor.cov %*% t(beta) + D.e
+  
+  if (any(diag(chol(cov.fm)) == 0)) {
+    warning("Covariance matrix is not positive definite!")
+  }
+  
+  return(cov.fm)
+}

Deleted: pkg/FactorAnalytics/R/fitted.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/fitted.sfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/fitted.sfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,28 +0,0 @@
-#' @title Get fitted values from a stochastic factor model
-#' 
-#' @description Method or helper function for fit object of class \code{sfm}. 
-#' 
-#' @param object a fit object of class \code{sfm} which is returned by 
-#' \code{\link{fitSFM}}
-#' @param ... other arguments passed 
-#' 
-#' @return 
-#' \item{fitted.xts}{an N x T data object of fitted values}
-#' where, N is the number of assets and T is the number of time periods.
-#' 
-#' @author Eric Zivot and Sangeetha Srinivasan
-#' 
-#' @seealso \code{\link{fitSFM}}
-#' 
-#' @method fitted sfm
-#' @export
-
-fitted.sfm <- function(object,...){
-  # get fitted values from each linear factor model fit 
-  # and convert them into xts/zoo objects
-  fitted.list = sapply(object$asset.fit, function(x) checkData(fitted(x)))
-  # this is a list of xts objects, indexed by the asset name
-  # merge the objects in the list into one xts object
-  fitted.xts <- do.call(merge, fitted.list)
-  return(fitted.xts)
-}

Deleted: pkg/FactorAnalytics/R/fitted.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/fitted.tsfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/fitted.tsfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,39 +0,0 @@
-#' @title Get fitted values from a time series factor model
-#' 
-#' @description Method or helper function for fit object of class \code{tsfm}. 
-#' 
-#' @param object a fit object of class \code{tsfm} which is returned by 
-#' \code{\link{fitTSFM}} 
-#' @param ... other arguments passed
-#' 
-#' @return 
-#' \item{fitted.xts}{an N x T data object of fitted values}
-#' where, N is the number of assets and T is the number of time periods.
-#' 
-#' @author Eric Zivot and Sangeetha Srinivasan
-#' 
-#' @seealso \code{\link{fitTSFM}}
-#' 
-#' @examples
-#' \dontrun{
-#' data(managers.df)
-#' fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
-#'                factor.names=colnames(managers.df[,7:9]), 
-#'                market.name="SP500.TR",
-#'                data=data, fit.method="OLS", variable.selection="none", 
-#'                add.up.market=TRUE, add.market.sqd=TRUE)
-#' fitted(fit)
-#' }
-#' 
-#' @method fitted tsfm
-#' @export
-
-fitted.tsfm <- function(object,...){
-  # get fitted values from each linear factor model fit 
-  # and convert them into xts/zoo objects
-  fitted.list = sapply(object$asset.fit, function(x) checkData(fitted(x)))
-  # this is a list of xts objects, indexed by the asset name
-  # merge the objects in the list into one xts object
-  fitted.xts <- do.call(merge, fitted.list)
-  return(fitted.xts)
-}

Modified: pkg/FactorAnalytics/R/plot.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.tsfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/plot.tsfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -41,7 +41,7 @@
 #'  10= CUSUM plot of recursive residuals,\cr
 #'  11= CUSUM plot of OLS residuals,\cr
 #'  12= CUSUM plot of recursive estimates relative to full sample estimates,\cr
-#'  13= rolling estimates over an observation window of length 24.
+#'  13= rolling estimates over a 24-period observation window
 #' @param VaR.method a method for computing VaR; one of "modified", "gaussian",
 #' "historical" or "kernel". VaR is computed using 
 #' \code{\link[PerformanceAnalytics]{VaR}}. Default is "historical".
@@ -49,8 +49,7 @@
 #' 
 #' @author Eric Zivot, Yi-An Chen and Sangeetha Srinivasan
 #' 
-#' @seealso \code{\link{fitTSFM}}, \code{\link{summary.tsfm}}, 
-#' \code{\link{tsfm}}
+#' @seealso \code{\link{fitTSFM}}, \code{\link{summary.tsfm}}
 #' 
 #' @examples
 #' 
@@ -96,7 +95,7 @@
       ##                  10    CUSUM plot of recursive residuals
       ##                  11    CUSUM plot of OLS residuals
       ##                  12    CUSUM plot of recursive estimates relative to full sample estimates
-      ##                  13    rolling estimates over an observation window of length 24
+      ##                  13    rolling estimates over a 24-period observation window
       which.plot.single<-which.plot.single[1]
       if (missing(asset.name) == TRUE) {
         stop("Neet to specify an asset to plot if plot.single is TRUE.")
@@ -129,7 +128,7 @@
                                     "CUSUM plot of recursive residuals",
                                     "CUSUM plot of OLS residuals",
                                     "CUSUM plot of recursive estimates relative to full sample estimates",
-                                    "rolling estimates over an observation window of length 24"),
+                                    "rolling estimates over a 24-period observation window"),
                                   title="\nMake a plot selection (or 0 to exit):\n")
         switch(which.plot.single,
                "1L" =  {
@@ -206,7 +205,7 @@
                    stop("CUMSUM applies only on OLS method")
                },
                "13L"= {
-                 ##  rolling regression over 24 month window
+                 ##  Rolling estimates over 24-period observation window 
                  if (as.character(x$call["fit.method"]) == "OLS") {   
                    rollReg <- function(data.z, formula) {
                      coef(lm(formula, data = as.data.frame(data.z)))  
@@ -214,7 +213,7 @@
                    reg.z = zoo(fit.lm$model, as.Date(rownames(fit.lm$model)))
                    rollReg.z = rollapply(reg.z, FUN=rollReg, fit.formula, width=24, by.column = FALSE, 
                                          align="right")
-                   plot(rollReg.z, main=paste("24-month rolling regression estimates:", asset.name, sep=" "))
+                   plot(rollReg.z, main=paste("Rolling estimates over 24-period observation window:", asset.name, sep=" "))
                  } else if (as.character(x$call["fit.method"]) == "DLS") {
                    decay.factor <- as.numeric(as.character(x$call["decay.factor"]))
                    t.length <- 24
@@ -231,7 +230,7 @@
                    fit.formula = as.formula(paste(asset.name,"~", paste(factorNames, collapse="+"), sep=" "))
                    rollReg.z = rollapply(reg.z, FUN=rollReg.w, fit.formula,w, width=24, by.column = FALSE, 
                                          align="right")
-                   plot(rollReg.z, main=paste("24-month rolling regression estimates:", asset.name, sep=" ")) 
+                   plot(rollReg.z, main=paste("Rolling estimates over 24-period observation window:", asset.name, sep=" ")) 
                  } 
                },
                invisible()

Modified: pkg/FactorAnalytics/R/predict.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/predict.tsfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/predict.tsfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -4,8 +4,7 @@
 #' calls the \code{predict} method for fitted objects of class \code{lm}, 
 #' \code{lmRob} or \code{lars} as appropriate.
 #' 
-#' @param object an object of class \code{\link[stats]{tsfm}} produced by 
-#' \code{fitTSFM}.
+#' @param object an object of class \code{tsfm} produced by \code{fitTSFM}.
 #' @param newdata a vector, matrix, data.frame, xts, timeSeries or zoo object 
 #' containing the variables with which to predict.
 #' @param ... optional arguments passed to \code{predict.lm} or
@@ -17,8 +16,7 @@
 #' 
 #' @author Yi-An Chen and Sangeetha Srinivasan
 #' 
-#' @seealso \code{\link{fitTSFM}}, \code{\link{summary.tsfm}}, 
-#' \code{\link{tsfm}}
+#' @seealso \code{\link{fitTSFM}}, \code{\link{summary.tsfm}}
 #' 
 #' @examples
 #' # load data from the database

Modified: pkg/FactorAnalytics/R/print.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/print.tsfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/print.tsfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -38,7 +38,7 @@
   cat("\nRegression Alphas:\n")
   print(x$alpha, digits = digits, ...)
   cat("\nFactor Betas:\n")
-  print(t(x$beta), digits = digits, ...)
+  print(x$beta, digits = digits, ...)
   cat("\nR-squared values:\n")
   print(x$r2, digits = digits, ...)
   cat("\nResidual Volatilities:\n")

Deleted: pkg/FactorAnalytics/R/residuals.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/residuals.sfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/residuals.sfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,28 +0,0 @@
-#' @title Get residuals from a fitted stochastic factor model
-#' 
-#' @description Method or helper function for fit object of class \code{sfm}. 
-#' 
-#' @param object a fit object of class \code{sfm} which is returned by 
-#' \code{\link{fitSFM}} 
-#' @param ... other arguments passed
-#' 
-#' @return 
-#' \item{residuals.xts}{an N x T data object of residuals}
-#' where, N is the number of assets and T is the number of time periods.
-#' 
-#' @author Eric Zivot and Sangeetha Srinivasan
-#' 
-#' @seealso \code{\link{fitSFM}}
-#' 
-#' @method residuals sfm
-#' @export
-
-residuals.sfm <- function(object,...) {
-  # get residuals from each linear factor model fit 
-  # and convert them into xts/zoo objects
-  residuals.list = sapply(object$asset.fit, function(x) checkData(residuals(x)))
-  # this is a list of xts objects, indexed by the asset name
-  # merge the objects in the list into one xts object
-  residuals.xts <- do.call(merge, residuals.list)
-  return(residuals.xts)
-}

Deleted: pkg/FactorAnalytics/R/residuals.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/residuals.tsfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/residuals.tsfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,39 +0,0 @@
-#' @title Get residuals from a fitted time series factor model
-#' 
-#' @description Method or helper function for fit object of class \code{tsfm}. 
-#' 
-#' @param object a fit object of class \code{tsfm} which is returned by 
-#' \code{\link{fitTSFM}} 
-#' @param ... other arguments passed
-#' 
-#' @return 
-#' \item{residuals.xts}{an N x T data object of residuals}
-#' where, N is the number of assets and T is the number of time periods.
-#' 
-#' @author Eric Zivot and Sangeetha Srinivasan
-#' 
-#' @seealso \code{\link{fitTSFM}}
-#' 
-#' @examples
-#' \dontrun{
-#' data(managers.df)
-#' fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
-#'                factor.names=colnames(managers.df[,7:9]), 
-#'                market.name="SP500.TR",
-#'                data=data, fit.method="OLS", variable.selection="none", 
-#'                add.up.market=TRUE, add.market.sqd=TRUE)
-#' residuals(fit)
-#' }
-#' 
-#' @method residuals tsfm
-#' @export
-
-residuals.tsfm <- function(object ,...) {
-  # get residuals from each linear factor model fit 
-  # and convert them into xts/zoo objects
-  residuals.list = sapply(object$asset.fit, function(x) checkData(residuals(x)))
-  # this is a list of xts objects, indexed by the asset name
-  # merge the objects in the list into one xts object
-  residuals.xts <- do.call(merge, residuals.list)
-  return(residuals.xts)
-}

Modified: pkg/FactorAnalytics/R/summary.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.tsfm.r	2014-07-02 02:03:40 UTC (rev 3458)
+++ pkg/FactorAnalytics/R/summary.tsfm.r	2014-07-02 06:10:51 UTC (rev 3459)
@@ -1,26 +1,28 @@
 #' @title Summarizing a fitted time series factor model
 #' 
-#' @description S3 \code{summary} method for object of class \code{tsfm}. 
-#' Resulting object is of class {summary.tsfm}. There is a generic 
-#' \code{print} method for this object.
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/returnanalytics -r 3459


More information about the Returnanalytics-commits mailing list