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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Dec 8 09:13:18 CET 2014


Author: pragnya
Date: 2014-12-08 09:13:18 +0100 (Mon, 08 Dec 2014)
New Revision: 3573

Added:
   pkg/FactorAnalytics/vignettes/fitSfm_vignette.R
   pkg/FactorAnalytics/vignettes/fitSfm_vignette.Rnw
   pkg/FactorAnalytics/vignettes/fitSfm_vignette.pdf
Modified:
   pkg/FactorAnalytics/DESCRIPTION
   pkg/FactorAnalytics/R/fitSfm.R
   pkg/FactorAnalytics/R/plot.sfm.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.R
   pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw
   pkg/FactorAnalytics/vignettes/fitTsfm_vignette.pdf
Log:
Added fitSfm_vignette. Minor edits to summary and plot. Fixed typos in fitTsfm_vignette.

Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION	2014-12-07 06:36:03 UTC (rev 3572)
+++ pkg/FactorAnalytics/DESCRIPTION	2014-12-08 08:13:18 UTC (rev 3573)
@@ -1,8 +1,8 @@
 Package: factorAnalytics
 Type: Package
 Title: Factor Analytics
-Version: 2.0.8
-Date: 2014-12-06
+Version: 2.0.9
+Date: 2014-12-07
 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/R/fitSfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitSfm.R	2014-12-07 06:36:03 UTC (rev 3572)
+++ pkg/FactorAnalytics/R/fitSfm.R	2014-12-08 08:13:18 UTC (rev 3573)
@@ -8,10 +8,10 @@
 #' 
 #' @details
 #' If \code{data} is not of class \code{"xts"}, rownames must provide an 
-#' \code{xts} compatible time index. Before model fitting, incomplete cases in 
-#' \code{data} are removed using \code{\link[stats]{na.omit}}. Also, if 
-#' \code{check=TRUE}, a warning is issued if any asset is found to have 
-#' identical observations. 
+#' \code{"xts"} compatible time index. Before model fitting, incomplete cases in 
+#' \code{data} are removed using \code{\link[stats]{na.omit}}. Specifying 
+#' \code{check=TRUE}, issues a warning if any asset is found to have identical 
+#' observations. 
 #' 
 #' Let \code{N} be the number of columns or assets and \code{T} be the number 
 #' of rows or observations. When \code{N < T}, Principal Component Analysis 
@@ -23,23 +23,24 @@
 #' Alternately, for APCA, a method to determine the number of factors can be 
 #' specified: \code{k="bn"} corresponds to Bai and Ng (2002) and \code{k="ck"} 
 #' corresponds to Connor and Korajczyk (1993). User can specify the maximum 
-#' number of factors, \code{max.k} to consider with these methods. If not, a 
-#' default maximum is calculated from \code{min(10, T-1)}.
+#' number of factors, \code{max.k} to consider with these methods. If not, it 
+#' is assumed to be either 10 or $T-1$, whichever is smaller. 
 #' 
-#' \code{refine} specifies whether a refinement of the APCA procedure (that may 
-#' improve efficiency) from Connor and Korajczyk (1988) is to be used. 
+#' \code{refine} specifies whether a refinement of the APCA procedure from 
+#' Connor and Korajczyk (1988) that may improve efficiency is to be used. 
 #' 
-#' \code{corr} specifies if the correlation matrix of returns should be used 
-#' for finding the principal components instead of the covariance matrix. This 
-#' is decided on a case-by-case basis. The variable with the highest variance 
-#' dominates the PCA when the covariance matrix is used (but, this may be 
-#' justified if a volatile asset is more interesting for some reason and 
-#' volatility information shouldn't be discarded). On the other hand, using the 
-#' correlation matrix standardizes the variables and makes them comparable, 
-#' avoiding penalizing variables with less dispersion. 
+#' When \code{corr=TRUE}, the correlation matrix of returns are used for 
+#' finding the principal components instead of the covariance matrix. This is 
+#' typically decided by practioners on a case-by-case basis. The variable with 
+#' the highest variance dominates the PCA when the covariance matrix is used. 
+#' However, this may be justified if a volatile asset is more interesting for 
+#' some reason and volatility information shouldn't be discarded. On the other 
+#' hand, using the correlation matrix standardizes the variables and makes them 
+#' comparable, avoiding penalizing variables with less dispersion. 
 #' 
-#' Note: If the median of the 1st principal component is negative, all it's
-#' factor realizations are inverted to enable more meaningful interpretation.
+#' Finally, if the median of the 1st principal component is negative, all it's
+#' factor realizations are automatically inverted to enable more meaningful 
+#' interpretation.
 #' 
 #' @param data vector, matrix, data.frame, xts, timeSeries or zoo object with 
 #' asset returns. See details.
@@ -78,7 +79,8 @@
 #' \item{resid.sd}{length-N vector of residual standard deviations.}
 #' \item{residuals}{T x N xts object of residuals from the OLS regression.}
 #' \item{Omega}{N x N return covariance matrix estimated by the factor model.}
-#' \item{eigen}{length-K vector of eigenvalues of the sample covariance matrix.}
+#' \item{eigen}{length-N (or length-T for APCA) vector of eigenvalues of the 
+#' sample covariance matrix.}
 #' \item{mimic}{N x K matrix of factor mimicking portfolio weights.}
 #' \item{call}{the matched function call.}
 #' \item{data}{T x N xts data object containing the asset returns.}
@@ -86,9 +88,6 @@
 #' Where N is the number of assets, K is the number of factors, and T is the 
 #' number of observations.
 #' 
-#' \item{residuals}{T x N matrix of residuals from the regression.}
-#' \item{asset.ret}{N x T matrix of fitted asset returns from the factor model.}
-#' 
 #' @author Eric Zivot, Sangeetha Srinivasan and Yi-An Chen
 #' 
 #' @references 

Modified: pkg/FactorAnalytics/R/plot.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.sfm.r	2014-12-07 06:36:03 UTC (rev 3572)
+++ pkg/FactorAnalytics/R/plot.sfm.r	2014-12-08 08:13:18 UTC (rev 3573)
@@ -326,7 +326,8 @@
                par(mfrow=c(ceiling(length(factor.subset)/2),2))
                for (i in factor.subset) {
                  main=paste("Beta values for ", colnames(x$loadings)[i])
-                 barplot(x$loadings[asset.subset,i], main=main, names.arg=asset.subset, 
+                 barplot(x$loadings[asset.subset,i], main=main, 
+                         names.arg=rownames(x$loadings)[asset.subset], 
                          col="darkblue", las=las, horiz=TRUE, ...)
                  abline(v=0, lwd=1, lty=1, col=1)
                }
@@ -398,10 +399,10 @@
                ## Factor mimicking portfolio weights - top long and short positions in each factor
                par(mfrow=c(ceiling(length(factor.subset)/2),2))
                for (i in factor.subset) {
-                 main=paste("Top positions in ", colnames(x$loadings)[i])
+                 main=paste("Top positions (%) in ", colnames(x$loadings)[i])
                  s <- summary(x, n.top=n.top)$mimic.sum[[i]]
-                 top <- as.numeric(s[,c(2,4)])
-                 names.arg <- as.vector(s[,c(1,3)])
+                 top <- 100*stack(s[,c(2,4)])$values
+                 names.arg <- stack(s[,c(1,3)])$values
                  barplot(top, main=main, names.arg=names.arg, col="darkblue", 
                          las=las, horiz=TRUE, ...)
                  abline(v=0, lwd=1, lty=1, col=1)
@@ -413,7 +414,7 @@
                for (i in factor.subset) {
                  main=paste("Correlations of top positions in ", colnames(x$loadings)[i])
                  s <- summary(x, n.top=n.top)$mimic.sum[[i]]
-                 names.arg <- as.vector(s[,c(1,3)])
+                 names.arg <- stack(s[,c(1,3)])$values
                  cor.fm <- cov2cor(fmCov(x))[names.arg,names.arg]
                  corrplot(cor.fm, ...)
                }

Modified: pkg/FactorAnalytics/R/summary.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.sfm.r	2014-12-07 06:36:03 UTC (rev 3572)
+++ pkg/FactorAnalytics/R/summary.sfm.r	2014-12-08 08:13:18 UTC (rev 3573)
@@ -12,7 +12,7 @@
 #'  
 #' @param object an object of class \code{sfm} returned by \code{fitSfm}.
 #' @param se.type one of "Default", "HC" or "HAC"; option for computing HC/HAC 
-#' standard errors and t-statistics.
+#' standard errors and t-statistics. Default is "Default".
 #' @param n.top scalar; number of largest and smallest weights to display for 
 #' each factor mimicking portfolio. Default is 3.
 #' @param x an object of class \code{summary.sfm}.
@@ -31,8 +31,8 @@
 #' \item{se.type}{standard error type as input} 
 #' \item{sum.list}{list of summaries for the N fit objects of class \code{lm} 
 #' for each asset in the factor model.}
-#' \item{mimic.sum}{list of matrices containing \code{n.top} largest and 
-#' smallest weights for each factor mimicking portfolio.}
+#' \item{mimic.sum}{list of data.frame objects containing \code{n.top} largest 
+#' and smallest weights for each factor mimicking portfolio.}
 #' 
 #' @author Sangeetha Srinivasan
 #' 
@@ -52,13 +52,16 @@
 #' @method summary sfm
 #' @export
 
-summary.sfm <- function(object, se.type="Default", n.top=3, ...){
+summary.sfm <- function(object, se.type=c("Default","HC","HAC"), n.top=3, ...){
   
   # check input object validity
   if (!inherits(object, "sfm")) {
     stop("Invalid 'sfm' object")
   }
   
+  #set default for se.type
+  se.type = se.type[1]
+  
   # extract list of summary.lm objects for all assets
   sum.list <- summary(object$asset.fit)
   names(sum.list) <- object$asset.names
@@ -82,10 +85,11 @@
   for (j in 1:object$k) {
     short <- sort(mimic[,j])[1:n.top]
     long <- sort(mimic[,j], decreasing=TRUE)[1:n.top]
-    mimic.sum[[j]] <- cbind(names(long),long,names(short),short)
+    mimic.sum[[j]] <- data.frame(names(long), long, names(short), short, 
+                                 stringsAsFactors=FALSE)
     rownames(mimic.sum[[j]]) <- 1:n.top
-    colnames(mimic.sum[[j]]) <- c("Top.Long.Name", "Top.Long.Weight", 
-                                  "Top.Short.Name", "Top.Short.Weight")
+    names(mimic.sum[[j]]) <- c("Top.Long.Name", "Top.Long.Weight", 
+                               "Top.Short.Name", "Top.Short.Weight")
   }
   names(mimic.sum) <- paste("F", 1:object$k, sep = ".")
   

Modified: pkg/FactorAnalytics/R/summary.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.tsfm.r	2014-12-07 06:36:03 UTC (rev 3572)
+++ pkg/FactorAnalytics/R/summary.tsfm.r	2014-12-08 08:13:18 UTC (rev 3573)
@@ -10,6 +10,7 @@
 #' 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 
@@ -17,7 +18,7 @@
 #'  
 #' @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.
+#' 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.
@@ -56,11 +57,16 @@
 #' @method summary tsfm
 #' @export
 
-summary.tsfm <- function(object, se.type="Default", ...){
+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 

Modified: pkg/FactorAnalytics/man/fitSfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitSfm.Rd	2014-12-07 06:36:03 UTC (rev 3572)
+++ pkg/FactorAnalytics/man/fitSfm.Rd	2014-12-08 08:13:18 UTC (rev 3573)
@@ -64,16 +64,14 @@
 \item{resid.sd}{length-N vector of residual standard deviations.}
 \item{residuals}{T x N xts object of residuals from the OLS regression.}
 \item{Omega}{N x N return covariance matrix estimated by the factor model.}
-\item{eigen}{length-K vector of eigenvalues of the sample covariance matrix.}
+\item{eigen}{length-N (or length-T for APCA) vector of eigenvalues of the
+sample covariance matrix.}
 \item{mimic}{N x K matrix of factor mimicking portfolio weights.}
 \item{call}{the matched function call.}
 \item{data}{T x N xts data object containing the asset returns.}
 \item{asset.names}{length-N vector of column names from data.}
 Where N is the number of assets, K is the number of factors, and T is the
 number of observations.
-
-\item{residuals}{T x N matrix of residuals from the regression.}
-\item{asset.ret}{N x T matrix of fitted asset returns from the factor model.}
 }
 \description{
 Fits a statistical factor model using Principal Component
@@ -84,10 +82,10 @@
 }
 \details{
 If \code{data} is not of class \code{"xts"}, rownames must provide an
-\code{xts} compatible time index. Before model fitting, incomplete cases in
-\code{data} are removed using \code{\link[stats]{na.omit}}. Also, if
-\code{check=TRUE}, a warning is issued if any asset is found to have
-identical observations.
+\code{"xts"} compatible time index. Before model fitting, incomplete cases in
+\code{data} are removed using \code{\link[stats]{na.omit}}. Specifying
+\code{check=TRUE}, issues a warning if any asset is found to have identical
+observations.
 
 Let \code{N} be the number of columns or assets and \code{T} be the number
 of rows or observations. When \code{N < T}, Principal Component Analysis
@@ -99,23 +97,24 @@
 Alternately, for APCA, a method to determine the number of factors can be
 specified: \code{k="bn"} corresponds to Bai and Ng (2002) and \code{k="ck"}
 corresponds to Connor and Korajczyk (1993). User can specify the maximum
-number of factors, \code{max.k} to consider with these methods. If not, a
-default maximum is calculated from \code{min(10, T-1)}.
+number of factors, \code{max.k} to consider with these methods. If not, it
+is assumed to be either 10 or $T-1$, whichever is smaller.
 
-\code{refine} specifies whether a refinement of the APCA procedure (that may
-improve efficiency) from Connor and Korajczyk (1988) is to be used.
+\code{refine} specifies whether a refinement of the APCA procedure from
+Connor and Korajczyk (1988) that may improve efficiency is to be used.
 
-\code{corr} specifies if the correlation matrix of returns should be used
-for finding the principal components instead of the covariance matrix. This
-is decided on a case-by-case basis. The variable with the highest variance
-dominates the PCA when the covariance matrix is used (but, this may be
-justified if a volatile asset is more interesting for some reason and
-volatility information shouldn't be discarded). On the other hand, using the
-correlation matrix standardizes the variables and makes them comparable,
-avoiding penalizing variables with less dispersion.
+When \code{corr=TRUE}, the correlation matrix of returns are used for
+finding the principal components instead of the covariance matrix. This is
+typically decided by practioners on a case-by-case basis. The variable with
+the highest variance dominates the PCA when the covariance matrix is used.
+However, this may be justified if a volatile asset is more interesting for
+some reason and volatility information shouldn't be discarded. On the other
+hand, using the correlation matrix standardizes the variables and makes them
+comparable, avoiding penalizing variables with less dispersion.
 
-Note: If the median of the 1st principal component is negative, all it's
-factor realizations are inverted to enable more meaningful interpretation.
+Finally, if the median of the 1st principal component is negative, all it's
+factor realizations are automatically inverted to enable more meaningful
+interpretation.
 }
 \examples{
 # load return data

Modified: pkg/FactorAnalytics/man/summary.sfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.sfm.Rd	2014-12-07 06:36:03 UTC (rev 3572)
+++ pkg/FactorAnalytics/man/summary.sfm.Rd	2014-12-08 08:13:18 UTC (rev 3573)
@@ -4,7 +4,8 @@
 \alias{summary.sfm}
 \title{Summarizing a fitted time series factor model}
 \usage{
-\method{summary}{sfm}(object, se.type = "Default", n.top = 3, ...)
+\method{summary}{sfm}(object, se.type = c("Default", "HC", "HAC"),
+  n.top = 3, ...)
 
 \method{print}{summary.sfm}(x, digits = 3, ...)
 }
@@ -12,7 +13,7 @@
 \item{object}{an object of class \code{sfm} returned by \code{fitSfm}.}
 
 \item{se.type}{one of "Default", "HC" or "HAC"; option for computing HC/HAC
-standard errors and t-statistics.}
+standard errors and t-statistics. Default is "Default".}
 
 \item{n.top}{scalar; number of largest and smallest weights to display for
 each factor mimicking portfolio. Default is 3.}
@@ -36,8 +37,8 @@
 \item{se.type}{standard error type as input}
 \item{sum.list}{list of summaries for the N fit objects of class \code{lm}
 for each asset in the factor model.}
-\item{mimic.sum}{list of matrices containing \code{n.top} largest and
-smallest weights for each factor mimicking portfolio.}
+\item{mimic.sum}{list of data.frame objects containing \code{n.top} largest
+and smallest weights for each factor mimicking portfolio.}
 }
 \description{
 \code{summary} method for object of class \code{sfm}.

Modified: pkg/FactorAnalytics/man/summary.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.tsfm.Rd	2014-12-07 06:36:03 UTC (rev 3572)
+++ pkg/FactorAnalytics/man/summary.tsfm.Rd	2014-12-08 08:13:18 UTC (rev 3573)
@@ -4,7 +4,7 @@
 \alias{summary.tsfm}
 \title{Summarizing a fitted time series factor model}
 \usage{
-\method{summary}{tsfm}(object, se.type = "Default", ...)
+\method{summary}{tsfm}(object, se.type = c("Default", "HC", "HAC"), ...)
 
 \method{print}{summary.tsfm}(x, digits = 3, ...)
 }
@@ -12,7 +12,7 @@
 \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.}
+HC/HAC standard errors and t-statistics. Default is "Default".}
 
 \item{...}{futher arguments passed to or from other methods.}
 
@@ -45,6 +45,7 @@
 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

Added: pkg/FactorAnalytics/vignettes/fitSfm_vignette.R
===================================================================
--- pkg/FactorAnalytics/vignettes/fitSfm_vignette.R	                        (rev 0)
+++ pkg/FactorAnalytics/vignettes/fitSfm_vignette.R	2014-12-08 08:13:18 UTC (rev 3573)
@@ -0,0 +1,300 @@
+
+## ----message=FALSE-------------------------------------------------------
+library(factorAnalytics)
+options(digits=3)
+
+
+## ------------------------------------------------------------------------
+# load the Rdata object
+data(StockReturns)
+# view class and dimensions
+class(r.M)
+dim(r.M)
+# variable names
+colnames(r.M)
+# range of observations
+range(rownames(r.M))
+
+
+## ------------------------------------------------------------------------
+class(r.W)
+dim(r.W)
+range(rownames(r.W))
+
+
+## ------------------------------------------------------------------------
+data(TreasuryYields)
+head(tr.yields)
+range(index(tr.yields))
+
+
+## ----tidy=TRUE-----------------------------------------------------------
+args(fitSfm)
+
+
+## ------------------------------------------------------------------------
+fit.pca <- fitSfm(r.M, k=2)
+
+
+## ------------------------------------------------------------------------
+class(fit.pca)
+names(fit.pca)
+
+
+## ------------------------------------------------------------------------
+fit.pca # print the fitted "sfm" object
+
+
+## ----fig.cap="Screeplot of eigenvalues from PCA fit", fig.width=6, fig.height=4----
+plot(fit.pca, which.plot.group=1, factor.subset=1:2, loop=FALSE, eig.max=0.9,
+     cex.names=0.9, cex.axis=0.9, cex.main=0.8)
+
+
+## ----fig.cap="Estimated factors from PCA fit", fig.width=7, fig.height=4----
+plot(fit.pca, which.plot.group=2, factor.subset=1:2, loop=FALSE)
+
+
+## ----fig.cap="Estimated loadings on PCA factors", fig.width=7, fig.height=5----
+plot(fit.pca, which.plot.group=3, asset.subset=1:15, factor.subset=1:2, 
+     loop=FALSE, cex.names=0.8, cex.axis=0.8, cex.main=0.8)
+
+
+## ----fig.cap="Largest and smallest weights in factor mimicking portfolios from PCA fit", fig.width=7, fig.height=4.5, fig.show='asis'----
+# Factor mimicking portfolio weights from PCA fit
+t(fit.pca$mimic)
+plot(fit.pca, which.plot.group=11, factor.subset=1:2, n.top=3, loop=FALSE, 
+     cex.names=0.8, cex.axis=0.8, cex.main=0.8)
+
+
+## ------------------------------------------------------------------------
+fit.apca <- fitSfm(r.W, k=15)
+
+
+## ----fig.cap="Screeplot of eigenvalues from APCA fit",fig.width=7,fig.height=4.5----
+plot(fit.apca, which.plot.group=1, loop=FALSE, eig.max=0.4, cex.names=0.75, 
+     cex.axis=0.8, cex.main=0.8)
+
+
+## ----fig.cap="First four factor returns from APCA fit", fig.width=7,fig.height=4.5----
+plot(fit.apca, which.plot.group=2, loop=FALSE)
+
+
+## ----fig.cap="Histogram of R-squared values for APCA fit", fig.width=6, fig.height=4----
+plot(fit.apca, which.plot.group=4, loop=FALSE)
+
+
+## ----fig.cap="Histogram of Residual volatilities for APCA fit", fig.width=6, fig.height=4----
+plot(fit.apca, which.plot.group=5, loop=FALSE)
+
+
+## ----fig.cap="Asset correlations: largest and smallest positions in the first factor portfolio of the APCA fit", fig.width=6, fig.height=6----
+plot(fit.apca, which.plot.group=12, factor.subset=1, loop=FALSE, n.top=5, 
+     method="ellipse")
+
+
+## ------------------------------------------------------------------------
+# APCA with the Bai & Ng method
+fit.apca.bn <- fitSfm(r.W, k="bn")
+summary(fit.apca.bn$loadings)
+
+# APCA with the Connor-Korajczyk method
+fit.apca.ck <- fitSfm(r.W, k="ck", sig=0.05)
+fit.apca.ck$k
+
+
+## ------------------------------------------------------------------------
+methods(class="sfm")
+
+
+## ----eval=FALSE----------------------------------------------------------
+## ## S3 method for class "sfm"
+## summary.sfm (object, se.type="Default", n.top=3, ...)
+
+
+## ------------------------------------------------------------------------
+# all estimated coefficients from PCA example
+coef(fit.pca)
+
+# compare returns data with fitted and residual values for CITCRP: fit.pca
+CITCRP.ts <- merge(fit.pca$data[,1], fitted(fit.pca)[,1], 
+                      residuals(fit.pca)[,1])
+colnames(CITCRP.ts) <- c("CITCRP.return","CITCRP.fitted","CITCRP.residual")
+tail(CITCRP.ts)
+
+# summary for fit.pca computing HAC standard erros
+sum.pca <- summary(fit.pca, se.type="HAC", n.top=3)
+names(sum.pca)
+
+# print the summary for the 1st asset
+sum.pca$sum.list[[1]]
+
+# print the summary for the factor mimicking portfolio weights
+sum.pca$mimic.sum
+
+
+## ----fig.cap="Treasury yields data for 11 different maturities", fig.width=7, fig.height=6----
+plot.zoo(tr.yields, main="Treasury yields", col=4)
+
+
+## ----fig.cap="Treasury yield curve at 3 different dates", fig.width=7, fig.height=4----
+# remove NAs
+dat <- na.omit(tr.yields)
+time = c(1/12,.25,.5,1, 2, 3, 5, 7, 10, 20, 30)
+plot(time, as.vector(dat[1,]), ylim=c(0,6), type="b", col=1, lwd=2, pch=19, 
+     ylab="Yield", xlab="T")
+lines(time, as.vector(dat[486,]), type="b", lwd=2, col=2, pch=19)
+lines(time, as.vector(dat[821,]), type="b", lwd=2, col=4, pch=19)
+legend("bottomright", c("07/31/01","07/02/07","10/31/08"), col=c(1,2,4), lwd=2)
+
+
+## ------------------------------------------------------------------------
+diff.yield <- na.omit(diff(tr.yields))
+dim(diff.yield)
+yield.pca <- fitSfm(diff.yield, k=3, corr=TRUE)
+
+
+## ----fig.cap="Screeplot of eigenvalues from APCA fit",fig.width=7,fig.height=4.5----
+plot(yield.pca, which.plot.group=1, factor.subset=1:3, eig.max=1, loop=FALSE, 
+     cex.names=0.9, cex.axis=0.9, cex.main=0.9)
+
+
+## ------------------------------------------------------------------------
+beta <- yield.pca$loadings
+summary(beta)
+summary(yield.pca)$mimic.sum
+
+
+## ----fig.cap="Factor loadings on the 1st three Principal components"-----
+plot(yield.pca, which.plot.group=3, factor.subset=1:3, asset.subset=1:11, 
+     loop=FALSE, cex.names=0.9, cex.axis=0.9, cex.main=0.9)
+
+
+## ----fig.cap="The loadings on the 1st three factors across maturities", fig.width=7, fig.height=4----
+plot(time, beta[,1], ylim=c(-.8,.8), type="b", col=1, lwd=2, pch=19, 
+     ylab="Factor loading", xlab="T")
+lines(time, beta[,2], type="b", lwd=2, col=2, pch=19)
+lines(time, beta[,3], type="b", lwd=2, col=4, pch=19)
+legend("bottomright", c("F.1","F.2","F.3"), col=c(1,2,4), lwd=2)
+
+
+## ----fig.cap="Effect of a unit change in the first 3 factors on the yield curve", fig.width=7, fig.height=10----
+mu <- colMeans(dat)
+par(mfrow=c(3,1))
+for (i in 1:3) {
+  plot(time, mu, ylim=c(2,5.3), type="b", col=1, lwd=4, pch=19, ylab="Yield", 
+     xlab="T")
+  lines(time, mu+beta[,i], type="b", lwd=2, col=3, pch=19)
+  lines(time, mu-beta[,i], type="b", lwd=2, col=2, pch=19)
+  legend("bottomright", 
+         c("mean", paste("mean+F.",i,sep=""), paste("mean-F.",i,sep="")), 
+         col=c(1,3,2), lwd=c(4,2,2))
+}
+
+
+## ----fig.cap="Factor model return correlation (pairwise complete obs)", warning=FALSE, fig.width=7, fig.height=7----
+Omega <- fmCov(fit.pca)
+# return correlation plot for all 15 assets; Angular Order of Eigenvectors
+plot(fit.pca, which.plot.group=7, factor.subset=1:2, asset.subset=1:15, 
+     loop=FALSE, order="AOE", method="ellipse")
+
+
+## ----fig.cap="Percentage factor contribution to SD", fig.width=7, fig.height=6, warning=FALSE----
+decomp <- fmSdDecomp(fit.pca)
+names(decomp)
+# get the factor model standard deviation for all assets
+decomp$Sd.fm
+# get the component contributions to Sd; print first 6 assets
+head(decomp$cSd)
+# plot the percentage component contributions to Sd for all 15 assets
+plot(fit.pca, which.plot.group=8, factor.subset=1:2, asset.subset=1:15, 
+     loop=FALSE)
+
+
+## ----fig.cap="Percentage factor contribution to VaR", fig.width=7, fig.height=5----
+decomp1 <- fmVaRDecomp(fit.apca, method="historical")
+names(decomp1)
+# factor model Value-at-Risk; print first 6 assets
+head(decomp1$VaR.fm)
+# marginal factor contributions to VaR from 1st 4 factors; print first 6 assets
+head(decomp1$mVaR[,1:4])
+# plot the 1st 4 factors % component contributions to VaR for the 1st 6 assets
+plot(fit.apca, which.plot.group=10, loop=FALSE, asset.subset=1:6)
+
+
+## ----fig.cap="Percentage factor contribution to ES", fig.width=7, fig.height=5----
+decomp2 <- fmEsDecomp(fit.apca, method="historical")
+names(decomp2)
+# factor model Expected Shortfall; print first 6 assets
+head(decomp2$ES.fm)
+# percentage component contributions to ES from 1st 4 factors; show 1st 6 assets
+head(decomp2$pcES[,1:4])
+# plot the 1st 4 factors % component contributions to ES for the 1st 6 assets
+plot(fit.apca, which.plot.group=9, loop=FALSE, asset.subset=1:6)
+
+
+## ----eval=FALSE----------------------------------------------------------
+## ## S3 method for class "sfm"
+## plot(x, which.plot.group=NULL, factor.subset=1:4, asset.subset=1:5, n.top=3,
+##      plot.single=FALSE, asset.name, which.plot.single=NULL, colorset=(1:12),
+##      legend.loc="topleft", las=1, VaR.method="historical", cum.var=TRUE,
+##      eig.max=0.9, loop=TRUE, ...)
+
+
+## ----eval=FALSE, results='hide'------------------------------------------
+## plot(fit.pca)
+## 
+## ## Make a plot selection (or 0 to exit):
+## ##
+## ##  1: Screeplot of eigenvalues
+## ##  2: Time series plot of estimated factors
+## ##  3: Estimated factor loadings
+## ##  4: Histogram of R-squared
+## ##  5: Histogram of residual volatility
+## ##  6: Factor model residual correlation
+## ##  7: Factor model return correlation
+## ##  8: Factor contribution to SD
+## ##  9: Factor contribution to ES
+## ## 10: Factor contribution to VaR
+## ## 11: Factor mimicking portfolio weights - top long and short positions in each factor
+## ## 12: Asset correlations - top long and short positions in each factor
+## ##
+## ## Selection:
+
+
+## ----eval=FALSE, results='hide'------------------------------------------
+## plot(fit.pca, plot.single=TRUE, asset.name="CITCRP")
+## 
+## ## Make a plot selection (or 0 to exit):
+## ##
+## ##  1: Time series plot of actual and fitted asset returns
+## ##  2: Time series plot of residuals with standard error bands
+## ##  3: Time series plot of squared residuals
+## ##  4: Time series plot of absolute residuals
+## ##  5: SACF and PACF of residuals
+## ##  6: SACF and PACF of squared residuals
+## ##  7: SACF and PACF of absolute residuals
+## ##  8: Histogram of residuals with normal curve overlayed
+## ##  9: Normal qq-plot of residuals
+## ## 10: CUSUM test-Recursive residuals
+## ## 11: CUSUM test-OLS residuals
+## ## 12: Recursive estimates (RE) test of OLS regression coefficients
+## ## 13: Rolling estimates over a 24-period observation window
+## ##
+## ## Selection:
+
+
+## ----fig.cap="Time series plot of residuals with standard error bands: CITCRP", fig.show='asis', fig.width=7, fig.height=4.5----
+plot(fit.pca, plot.single=TRUE, asset.name="CITCRP", which.plot.single=2, 
+     loop=FALSE)
+
+
+## ----fig.cap="SACF and PACF of absolute residuals: CITCRP", fig.show='asis', fig.width=7, fig.height=4.5----
+plot(fit.pca, plot.single=TRUE, asset.name="CITCRP", which.plot.single=7, 
+     loop=FALSE)
+
+
+## ----fig.cap="Histogram of residuals with normal curve overlayed for CITCRP", fig.show='asis', fig.width=7, fig.height=4.5----
+plot(fit.pca, plot.single=TRUE, asset.name="CITCRP", which.plot.single=8, 
+     loop=FALSE)
+
+

Added: pkg/FactorAnalytics/vignettes/fitSfm_vignette.Rnw
===================================================================
--- pkg/FactorAnalytics/vignettes/fitSfm_vignette.Rnw	                        (rev 0)
+++ pkg/FactorAnalytics/vignettes/fitSfm_vignette.Rnw	2014-12-08 08:13:18 UTC (rev 3573)
@@ -0,0 +1,561 @@
+\documentclass[a4paper]{article}
+\usepackage{Rd}
+\usepackage{amsmath}
+\usepackage[round]{natbib}
+\usepackage{bm}
+\usepackage{verbatim}
+\usepackage[latin1]{inputenc}
+\bibliographystyle{abbrvnat}
+\usepackage{url}
+
+\let\proglang=\textsf
+\renewcommand{\topfraction}{0.85}
+\renewcommand{\textfraction}{0.1}
+\renewcommand{\baselinestretch}{1.5}
+\setlength{\textwidth}{15cm} \setlength{\textheight}{22cm} \topmargin-1cm \evensidemargin0.5cm \oddsidemargin0.5cm
+
+\usepackage{lmodern}
+\usepackage[T1]{fontenc}
+
+% \VignetteIndexEntry{Fitting Statistical Factor Models: factorAnalytics vignette}
+%\VignetteEngine{knitr::knitr}
+
+\begin{document}
+
+\title{Fitting Statistical Factor Models: factorAnalytics vignette}
+\author{Sangeetha Srinivasan}
+\maketitle
+
+\begin{abstract}
+The purpose of this vignette is to demonstrate the use of \code{fitSfm} and related control, analysis and plot functions in the \code{factorAnalytics} package.
+\end{abstract}
+
+\tableofcontents
+\bigskip
+
+\newpage
+\section{Overview}
+
+\subsection{Load Package}
+
+The latest version of the \verb"factorAnalytics" package can be downloaded from R-forge through the following command:
+\begin{verbatim}
+install.packages("factorAnalytics", repos="http://R-Forge.R-project.org")
+\end{verbatim}
+Load the package and it's dependencies.
+<<message=FALSE>>=
+library(factorAnalytics)
+options(digits=3)
+@
+
+\subsection{Summary of related functions}
+Here's a list of the functions and methods demonstrated in this vignette:
+
+\begin{itemize}
+
+\item \verb"fitSfm(data, k, max.k, refine, sig, check, corr, ...)": Fits a statistical factor model for one or more asset returns using Principal Component Analysis (PCA). When the number of assets exceeds the number of time periods, Asymptotic Principal Component Analysis (APCA) is performed. Additionaly for APCA, user can specify a method (one of \citet{connor1993test} or \citet{bai2002determining}) to determine the number of factors and/or choose to use the \citet{connor1988risk} refinement to the APCA procedure. The returned object is of class "sfm" and contains the fitted "lm" object, estimated factor realizations, factor loadings, R-squared, residual volatility, factor model return covariance and the factor mimicking portfolio weights.
+
+\item \verb"coef(object, ...)": Extracts the coefficient matrix (intercept and factor betas) for all assets fit by the "sfm" object.
+
+\item \verb"fitted(object, ...)": Returns an "xts" data object of fitted asset returns from the factor model for all assets.
+
+\item \verb"residuals(object, ...)": Returns an "xts" data object of residuals from the fitted factor model for all assets.
+
+\item \verb"fmCov(object, use, ...)": Returns the \code{N x N} symmetric covariance matrix for asset returns based on the fitted factor model. \code{"use"} specifies how missing values are to be handled.
+
[TRUNCATED]

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


More information about the Returnanalytics-commits mailing list