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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Apr 15 02:10:22 CEST 2015


Author: pragnya
Date: 2015-04-15 02:10:21 +0200 (Wed, 15 Apr 2015)
New Revision: 3629

Modified:
   pkg/FactorAnalytics/DESCRIPTION
   pkg/FactorAnalytics/NAMESPACE
   pkg/FactorAnalytics/R/Misc.R
   pkg/FactorAnalytics/R/fitTsfm.R
   pkg/FactorAnalytics/R/plot.tsfm.r
   pkg/FactorAnalytics/R/summary.tsfm.r
   pkg/FactorAnalytics/man/CornishFisher.Rd
   pkg/FactorAnalytics/man/fitSfm.Rd
   pkg/FactorAnalytics/man/fitTsfm.Rd
   pkg/FactorAnalytics/man/fitTsfm.control.Rd
   pkg/FactorAnalytics/man/fitTsfmLagBeta.Rd
   pkg/FactorAnalytics/man/fitTsfmMT.Rd
   pkg/FactorAnalytics/man/fitTsfmUpDn.Rd
   pkg/FactorAnalytics/man/fmCov.Rd
   pkg/FactorAnalytics/man/fmEsDecomp.Rd
   pkg/FactorAnalytics/man/fmSdDecomp.Rd
   pkg/FactorAnalytics/man/fmVaRDecomp.Rd
   pkg/FactorAnalytics/man/fmmc.Rd
   pkg/FactorAnalytics/man/fmmc.estimate.se.Rd
   pkg/FactorAnalytics/man/paFm.Rd
   pkg/FactorAnalytics/man/plot.pafm.Rd
   pkg/FactorAnalytics/man/plot.sfm.Rd
   pkg/FactorAnalytics/man/plot.tsfm.Rd
   pkg/FactorAnalytics/man/plot.tsfmUpDn.Rd
   pkg/FactorAnalytics/man/predict.sfm.Rd
   pkg/FactorAnalytics/man/predict.tsfm.Rd
   pkg/FactorAnalytics/man/predict.tsfmUpDn.Rd
   pkg/FactorAnalytics/man/print.pafm.Rd
   pkg/FactorAnalytics/man/print.sfm.Rd
   pkg/FactorAnalytics/man/print.tsfm.Rd
   pkg/FactorAnalytics/man/print.tsfmUpDn.Rd
   pkg/FactorAnalytics/man/summary.pafm.Rd
   pkg/FactorAnalytics/man/summary.sfm.Rd
   pkg/FactorAnalytics/man/summary.tsfm.Rd
   pkg/FactorAnalytics/man/summary.tsfmUpDn.Rd
   pkg/FactorAnalytics/vignettes/fitTsfm_vignette.R
   pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw
   pkg/FactorAnalytics/vignettes/fitTsfm_vignette.pdf
Log:
Expanded and modified plot.tsfm; fixed minor bugs in fitTsfm, summary.tsfm

Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION	2015-04-13 06:38:17 UTC (rev 3628)
+++ pkg/FactorAnalytics/DESCRIPTION	2015-04-15 00:10:21 UTC (rev 3629)
@@ -1,42 +1,43 @@
-Package: factorAnalytics
-Type: Package
-Title: Factor Analytics
-Version:2.0.16
-Date:2015-03-21
-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
-    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, as well as performance 
-    attribution for factor models (factor-contributed vs idiosyncratic returns) 
-    are included. 
-License: GPL-2
-Depends:
-    R (>= 3.0.0),
-    xts (>= 0.9),
-    foreach (>= 1.4)
-Imports: 
-    PerformanceAnalytics(>= 1.1.0),
-    corrplot,  
-    robust, 
-    leaps, 
-    lars,
-    strucchange,
-    lmtest,
-    sandwich,
-    lattice,
-    MASS,
-    boot,
-    parallel,
-    doSNOW,
-    RCurl,
-    bestglm
-Suggests:
-    testthat, quantmod, knitr
-LazyLoad: yes
-LazyDataCompression: xz
-URL: http://r-forge.r-project.org/R/?group_id=579
+Package: factorAnalytics
+Type: Package
+Title: Factor Analytics
+Version:2.0.17
+Date:2015-04-14
+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
+    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, as well as performance 
+    attribution for factor models (factor-contributed vs idiosyncratic returns) 
+    are included. 
+License: GPL-2
+Depends:
+    R (>= 3.0.0),
+    xts (>= 0.9),
+    foreach (>= 1.4)
+Imports: 
+    PerformanceAnalytics(>= 1.4),
+    corrplot,  
+    robust, 
+    leaps, 
+    lars,
+    strucchange,
+    lmtest,
+    sandwich,
+    lattice,
+    MASS,
+    sn,
+    boot,
+    parallel,
+    doSNOW,
+    RCurl,
+    bestglm
+Suggests:
+    testthat, quantmod, knitr
+LazyLoad: yes
+LazyDataCompression: xz
+URL: http://r-forge.r-project.org/R/?group_id=579

Modified: pkg/FactorAnalytics/NAMESPACE
===================================================================
--- pkg/FactorAnalytics/NAMESPACE	2015-04-13 06:38:17 UTC (rev 3628)
+++ pkg/FactorAnalytics/NAMESPACE	2015-04-15 00:10:21 UTC (rev 3629)
@@ -1,82 +1,86 @@
-# Generated by roxygen2 (4.1.0): do not edit by hand
-
-S3method(coef,sfm)
-S3method(coef,tsfm)
-S3method(fitted,sfm)
-S3method(fitted,tsfm)
-S3method(fmCov,sfm)
-S3method(fmCov,tsfm)
-S3method(fmEsDecomp,sfm)
-S3method(fmEsDecomp,tsfm)
-S3method(fmSdDecomp,sfm)
-S3method(fmSdDecomp,tsfm)
-S3method(fmVaRDecomp,sfm)
-S3method(fmVaRDecomp,tsfm)
-S3method(plot,pafm)
-S3method(plot,sfm)
-S3method(plot,tsfm)
-S3method(plot,tsfmUpDn)
-S3method(predict,sfm)
-S3method(predict,tsfm)
-S3method(predict,tsfmUpDn)
-S3method(print,pafm)
-S3method(print,sfm)
-S3method(print,summary.sfm)
-S3method(print,summary.tsfm)
-S3method(print,summary.tsfmUpDn)
-S3method(print,tsfm)
-S3method(print,tsfmUpDn)
-S3method(residuals,sfm)
-S3method(residuals,tsfm)
-S3method(summary,pafm)
-S3method(summary,sfm)
-S3method(summary,tsfm)
-S3method(summary,tsfmUpDn)
-export(dCornishFisher)
-export(fitSfm)
-export(fitTsfm)
-export(fitTsfmLagBeta)
-export(fitTsfmMT)
-export(fitTsfmUpDn)
-export(fmCov)
-export(fmEsDecomp)
-export(fmSdDecomp)
-export(fmVaRDecomp)
-export(fmmc)
-export(fmmc.estimate.se)
-export(pCornishFisher)
-export(paFm)
-export(qCornishFisher)
-export(rCornishFisher)
-importFrom(MASS,ginv)
-importFrom(PerformanceAnalytics,Return.cumulative)
-importFrom(PerformanceAnalytics,VaR)
-importFrom(PerformanceAnalytics,chart.ACFplus)
-importFrom(PerformanceAnalytics,chart.Histogram)
-importFrom(PerformanceAnalytics,chart.QQPlot)
-importFrom(PerformanceAnalytics,chart.TimeSeries)
-importFrom(PerformanceAnalytics,checkData)
-importFrom(RCurl,merge.list)
-importFrom(bestglm,bestglm)
-importFrom(boot,boot)
-importFrom(corrplot,corrplot)
-importFrom(doSNOW,registerDoSNOW)
-importFrom(foreach,foreach)
-importFrom(lars,cv.lars)
-importFrom(lars,lars)
-importFrom(lattice,barchart)
-importFrom(lattice,panel.barchart)
-importFrom(lattice,panel.grid)
-importFrom(lattice,xyplot)
-importFrom(leaps,regsubsets)
-importFrom(lmtest,coeftest.default)
-importFrom(parallel,clusterEvalQ)
-importFrom(parallel,clusterExport)
-importFrom(parallel,detectCores)
-importFrom(parallel,makeCluster)
-importFrom(parallel,stopCluster)
-importFrom(robust,lmRob)
-importFrom(robust,step.lmRob)
-importFrom(sandwich,vcovHAC.default)
-importFrom(sandwich,vcovHC.default)
-importFrom(strucchange,efp)
+# Generated by roxygen2 (4.1.0): do not edit by hand
+
+S3method(coef,sfm)
+S3method(coef,tsfm)
+S3method(fitted,sfm)
+S3method(fitted,tsfm)
+S3method(fmCov,sfm)
+S3method(fmCov,tsfm)
+S3method(fmEsDecomp,sfm)
+S3method(fmEsDecomp,tsfm)
+S3method(fmSdDecomp,sfm)
+S3method(fmSdDecomp,tsfm)
+S3method(fmVaRDecomp,sfm)
+S3method(fmVaRDecomp,tsfm)
+S3method(plot,pafm)
+S3method(plot,sfm)
+S3method(plot,tsfm)
+S3method(plot,tsfmUpDn)
+S3method(predict,sfm)
+S3method(predict,tsfm)
+S3method(predict,tsfmUpDn)
+S3method(print,pafm)
+S3method(print,sfm)
+S3method(print,summary.sfm)
+S3method(print,summary.tsfm)
+S3method(print,summary.tsfmUpDn)
+S3method(print,tsfm)
+S3method(print,tsfmUpDn)
+S3method(residuals,sfm)
+S3method(residuals,tsfm)
+S3method(summary,pafm)
+S3method(summary,sfm)
+S3method(summary,tsfm)
+S3method(summary,tsfmUpDn)
+export(dCornishFisher)
+export(fitSfm)
+export(fitTsfm)
+export(fitTsfmLagBeta)
+export(fitTsfmMT)
+export(fitTsfmUpDn)
+export(fmCov)
+export(fmEsDecomp)
+export(fmSdDecomp)
+export(fmVaRDecomp)
+export(fmmc)
+export(fmmc.estimate.se)
+export(pCornishFisher)
+export(paFm)
+export(qCornishFisher)
+export(rCornishFisher)
+importFrom(MASS,ginv)
+importFrom(PerformanceAnalytics,Return.cumulative)
+importFrom(PerformanceAnalytics,VaR)
+importFrom(PerformanceAnalytics,chart.ACFplus)
+importFrom(PerformanceAnalytics,chart.Correlation)
+importFrom(PerformanceAnalytics,chart.Histogram)
+importFrom(PerformanceAnalytics,chart.QQPlot)
+importFrom(PerformanceAnalytics,chart.TimeSeries)
+importFrom(PerformanceAnalytics,checkData)
+importFrom(RCurl,merge.list)
+importFrom(bestglm,bestglm)
+importFrom(boot,boot)
+importFrom(corrplot,corrplot)
+importFrom(corrplot,corrplot.mixed)
+importFrom(doSNOW,registerDoSNOW)
+importFrom(foreach,foreach)
+importFrom(lars,cv.lars)
+importFrom(lars,lars)
+importFrom(lattice,barchart)
+importFrom(lattice,panel.barchart)
+importFrom(lattice,panel.grid)
+importFrom(lattice,xyplot)
+importFrom(leaps,regsubsets)
+importFrom(lmtest,coeftest.default)
+importFrom(parallel,clusterEvalQ)
+importFrom(parallel,clusterExport)
+importFrom(parallel,detectCores)
+importFrom(parallel,makeCluster)
+importFrom(parallel,stopCluster)
+importFrom(robust,lmRob)
+importFrom(robust,step.lmRob)
+importFrom(sandwich,vcovHAC.default)
+importFrom(sandwich,vcovHC.default)
+importFrom(sn,dst)
+importFrom(sn,st.mple)
+importFrom(strucchange,efp)

Modified: pkg/FactorAnalytics/R/Misc.R
===================================================================
--- pkg/FactorAnalytics/R/Misc.R	2015-04-13 06:38:17 UTC (rev 3628)
+++ pkg/FactorAnalytics/R/Misc.R	2015-04-15 00:10:21 UTC (rev 3629)
@@ -5,13 +5,14 @@
 #' where they are needed and the namespace file. 
 #' 
 #' @importFrom PerformanceAnalytics checkData VaR chart.TimeSeries chart.ACFplus
-#' chart.Histogram chart.QQPlot Return.cumulative
+#' chart.Histogram chart.QQPlot Return.cumulative chart.Correlation
 #' @importFrom robust lmRob step.lmRob
 #' @importFrom leaps regsubsets
 #' @importFrom lars lars cv.lars
 #' @importFrom lmtest coeftest.default
 #' @importFrom sandwich vcovHC.default vcovHAC.default
-#' @importFrom lattice barchart panel.barchart panel.grid xyplot
-#' @importFrom corrplot corrplot
+#' @importFrom lattice barchart panel.barchart panel.grid
+#' @importFrom corrplot corrplot.mixed
 #' @importFrom strucchange efp
-#' @importFrom MASS ginv 
\ No newline at end of file
+#' @importFrom MASS ginv 
+#' @importFrom sn dst st.mple
\ No newline at end of file

Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R	2015-04-13 06:38:17 UTC (rev 3628)
+++ pkg/FactorAnalytics/R/fitTsfm.R	2015-04-15 00:10:21 UTC (rev 3629)
@@ -92,7 +92,7 @@
 #' \item{fitted}{xts data object of fitted values; iff 
 #' \code{variable.selection="lars"}}
 #' \item{call}{the matched function call.}
-#' \item{data}{xts data object containing the assets and factors.}
+#' \item{data}{xts data object containing the asset(s) and factor(s) returns.}
 #' \item{asset.names}{asset.names as input.}
 #' \item{factor.names}{factor.names as input.}
 #' \item{mkt.name}{mkt.name as input}
@@ -185,7 +185,7 @@
   if (missing(factor.names) && !is.null(mkt.name)) {
     factor.names <- NULL
   }
-
+  
   # extract arguments to pass to different fit and variable selection functions
   decay <- control$decay
   nvmin <- control$nvmin
@@ -193,7 +193,7 @@
   m1 <- match(c("weights","model","x","y","qr"), 
               names(control), 0L)
   lm.args <- control[m1, drop=TRUE]
-  m2 <-  match(c("weights","model","x","y","nrep", "efficiency"), 
+  m2 <-  match(c("weights","model","x","y","nrep","efficiency"), 
                names(control), 0L)
   lmRob.args <- control[m2, drop=TRUE]
   m3 <-  match(c("scope","scale","direction","trace","steps","k"), 
@@ -410,14 +410,13 @@
   for (i in asset.names) {
     # completely remove NA cases
     reg.xts <- na.omit(dat.xts[, c(i, factor.names)])
-    # convert to matrix
-    reg.mat <- as.matrix(reg.xts)
+    # convert data to mat/vec
+    xmat <- as.matrix(reg.xts[,factor.names])
+    yvec <- as.matrix(reg.xts)[,i]
     # fit lars regression model
-    lars.fit <- do.call(lars, c(list(x=reg.mat[,factor.names],y=reg.mat[,i]),
-                                lars.args))
+    lars.fit <- do.call(lars, c(list(x=xmat, y=yvec),lars.args))
     lars.sum <- summary(lars.fit)
-    lars.cv <- do.call(cv.lars, c(list(x=reg.mat[,factor.names],y=reg.mat[,i], 
-                                       mode="step"),cv.lars.args))
+    lars.cv <- do.call(cv.lars, c(list(x=xmat,y=yvec,mode="step"),cv.lars.args))
     # including plot.it=FALSE to cv.lars strangely gives an error: "Argument s 
     # out of range". And, specifying index=seq(nrow(lars.fit$beta)-1) resolves 
     # the issue, but care needs to be taken for small N
@@ -431,8 +430,7 @@
     }
     # get factor model coefficients & fitted values at the step obtained above
     coef.lars <- predict(lars.fit, s=s, type="coef", mode="step")
-    fitted.lars <- predict(lars.fit, reg.mat[,factor.names], s=s, type="fit", 
-                           mode="step")
+    fitted.lars <- predict(lars.fit, xmat, s=s, type="fit", mode="step")
     fitted.list[[i]] <- xts(fitted.lars$fit, index(reg.xts))
     # extract and assign the results
     asset.fit[[i]] = lars.fit
@@ -507,13 +505,13 @@
     if (length(object$asset.names)>1) {
       # 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,...)))
+      fitted.list = lapply(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) 
     } else {
-      fitted.xts <- checkData(fitted(object$asset.fit[[1]],...))
+      fitted.xts <- checkData(fitted(object$asset.fit[[1]]))
       colnames(fitted.xts) <- object$asset.names
     }
   }
@@ -535,13 +533,13 @@
     if (length(object$asset.names)>1) {
       # 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,...)))
+      residuals.list = lapply(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) 
     } else {
-      residuals.xts <- checkData(residuals(object$asset.fit[[1]],...))
+      residuals.xts <- checkData(residuals(object$asset.fit[[1]]))
       colnames(residuals.xts) <- object$asset.names
     }
   }

Modified: pkg/FactorAnalytics/R/plot.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.tsfm.r	2015-04-13 06:38:17 UTC (rev 3628)
+++ pkg/FactorAnalytics/R/plot.tsfm.r	2015-04-15 00:10:21 UTC (rev 3629)
@@ -20,12 +20,17 @@
 #' In case the \code{tsfm} object \code{x} contains only a single asset fit, 
 #' plot.tsfm can infer \code{asset.name} without user input. 
 #' 
-#' CUSUM plots (individual asset plot options 14, 15 and 16) are applicable 
+#' CUSUM plots (individual asset plot options 15, 16 and 17) are applicable 
 #' only for \code{fit.method="LS"}.
 #' 
-#' Rolling estimates (individual asset plot option 17) is not applicable for 
-#' \code{variable.slection="lars"}.
+#' Modified residuals, rolling regression and single factor model plots 
+#' (individual asset plot options 4, 18 and 19) are not applicable for 
+#' \code{variable.selection="lars"}.
 #' 
+#' The last option for plotting asset returns vs. factor returns (individual 
+#' asset plot option 19 and group plot 12) are only applicable for single factor
+#' models.
+#' 
 #' @param x an object of class \code{tsfm} produced by \code{fitTsfm}.
 #' @param which a number to indicate the type of plot. If a subset of the plots 
 #' is required, specify a subset of the numbers 1:10 for group plots and 1:16 
@@ -34,48 +39,53 @@
 #' For plots of a group of assets: \cr
 #' 1 = Factor model coefficients: Alpha, \cr
 #' 2 = Factor model coefficients: Betas, \cr
-#' 3 = Actual and Fitted asset returns, \cr
+#' 3 = Actual and fitted, \cr
 #' 4 = R-squared, \cr
-#' 5 = Residual Volatility,\cr
-#' 6 = Factor Model Residual Correlation \cr
-#' 7 = Factor Model Correlation,\cr
-#' 8 = Factor Contribution to SD,\cr
-#' 9 = Factor Contribution to ES,\cr
-#' 10 = Factor Contribution to VaR \cr \cr
+#' 5 = Residual volatility,\cr
+#' 6 = Factor model residuals scatterplot matrix, with histograms, density overlays, correlations and significance stars, \cr
+#' 7 = Factor model residual correlation \cr
+#' 8 = Factor model return correlation,\cr
+#' 9 = Factor contribution to SD,\cr
+#' 10 = Factor contribution to ES,\cr
+#' 11 = Factor contribution to VaR, \cr
+#' 12 = Asset returns vs factor returns (single factor model) \cr \cr
 #' For individual asset plots:\cr
-#' 1 = Actual and fitted returns,\cr
-#' 2 = Actual vs fitted asset returns,\cr
-#' 3 = Residuals and fitted returns, \cr
-#' 4 = Sqrt. of Std. Residuals vs Fitted, \cr
+#' 1 = Actual and fitted,\cr
+#' 2 = Actual vs fitted,\cr
+#' 3 = Residuals vs fitted, \cr
+#' 4 = Sqrt. of modified residuals vs fitted, \cr
 #' 5 = Residuals with standard error bands, \cr
 #' 6 = Time series of squared residuals, \cr
 #' 7 = Time series of absolute residuals,\cr
 #' 8 = SACF and PACF of residuals,\cr
 #' 9 = SACF and PACF of squared residuals,\cr
 #' 10 = SACF and PACF of absolute residuals,\cr
-#' 11 = Density estimate of residuals, \cr
-#' 12 = Histogram of residuals with normal curve overlayed,\cr
-#' 13 = Normal QQ-plot of residuals,\cr
-#' 14 = CUSUM test-Recursive residuals,\cr
-#' 15 = CUSUM test-LS residuals,\cr
-#' 16 = Recursive estimates (RE) test of LS regression coefficients,\cr
-#' 17 = Rolling estimates over a 24-period observation window
+#' 11 = Non-parametric density of residuals with normal overlaid, \cr
+#' 12 = Non-parametric density of residuals with skew-t overlaid, \cr 
+#' 13 = Histogram of residuals with non-parametric density and normal overlaid,\cr
+#' 14 = QQ-plot of residuals,\cr
+#' 15 = CUSUM test-Recursive residuals,\cr
+#' 16 = CUSUM test-LS residuals,\cr
+#' 17 = Recursive estimates (RE) test of LS regression coefficients,\cr
+#' 18 = Rolling regression over a 24-period observation window, \cr
+#' 19 = Asset returns vs factor returns (single factor model)
 #' @param max.show maximum number of assets in a given plot. Default is 6.
 #' @param plot.single logical; If \code{TRUE} plots the characteristics of an 
 #' individual asset's factor model. The type of plot is given by 
 #' \code{which}. Default is \code{FALSE}.
 #' @param asset.name name of the individual asset to be plotted. Is necessary 
 #' if \code{x} contains multiple asset fits and \code{plot.single=TRUE}.
-#' @param colorset color palette to use for all the plots. Default is 
-#' \code{c(1:12)}. The 1st element will be used for individual time series 
-#' plots or the 1st series plotted, the 2nd element for the 2nd object in the 
-#' plot and so on.
+#' @param colorset color palette to use for all the plots. The 1st element will 
+#' be used for individual time series plots or the 1st series plotted, the 2nd 
+#' element for the 2nd object in the plot and so on.
 #' @param legend.loc places a legend into one of nine locations on the chart: 
 #' "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", 
 #' "right", or "center". Default is "bottomright". Use \code{legend.loc=NULL} 
 #' to suppress the legend.
 #' @param las one of {0, 1, 2, 3} to set the direction of axis labels, same as 
-#' in \code{plot}. Default here is 1.
+#' in \code{plot}. Default is 1.
+#' @param lwd set the line width, same as in \code{\link{plot}}. Default is 2.
+#' @param maxlag optional number of lags to be calculated for ACF
 #' @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".
@@ -83,63 +93,75 @@
 #' 
 #' @author Eric Zivot, Sangeetha Srinivasan and Yi-An Chen
 #' 
-#' @seealso \code{\link{fitTsfm}} and \code{\link{summary.tsfm}} for details
-#' about the time series factor model fit, extractor functions and summary 
-#' statistics.
+#' @seealso \code{\link{fitTsfm}}, \code{\link{residuals.tsfm}}, 
+#' \code{\link{fitted.tsfm}}, \code{\link{fmCov.tsfm}} and 
+#' \code{\link{summary.tsfm}} for time series factor model fitting and related 
+#' S3 methods. Refer to \code{\link{fmSdDecomp}}, \code{\link{fmEsDecomp}}, 
+#' \code{\link{fmVaRDecomp}} for factor model risk measures.
 #' 
-#' \code{\link[strucchange]{efp}} for CUSUM tests.
+#' Here is a list of plotting functions used. (I=individual, G=Group)
+#' I(1,5,6,7), G(3) - \code{\link[PerformanceAnalytics]{chart.TimeSeries}}, 
+#' I(2,3,4,19), G(12) - \code{\link[graphics]{plot.default}},
+#' I(3,4) - \code{\link[graphics]{panel.smooth}},
+#' I(8,9,10) - \code{\link[PerformanceAnalytics]{chart.ACFplus}}, 
+#' I(11,12) - \code{\link[stats]{plot.density}},
+#' I(13) - \code{\link[PerformanceAnalytics]{chart.Histogram}},
+#' I(14) - \code{\link[PerformanceAnalytics]{chart.QQPlot}}, 
+#' I(15,16,17) - \code{\link[strucchange]{plot.efp}},
+#' I(18) - \code{\link[zoo]{plot.zoo}},
+#' G(1,2) - \code{\link[graphics]{barplot}}, 
+#' G(4,5,9,10,11) - \code{\link[lattice]{barchart}},
+#' G(6) - \code{\link[PerformanceAnalytics]{chart.Correlation}} and
+#' G(7,8) - \code{\link[corrplot]{corrplot.mixed}}. 
 #' 
-#' \code{\link[xts]{plot.xts}}, 
-#' \code{\link[PerformanceAnalytics]{chart.TimeSeries}}, 
-#' \code{\link[PerformanceAnalytics]{chart.ACFplus}}, 
-#' \code{\link[PerformanceAnalytics]{chart.Histogram}},
-#' \code{\link[PerformanceAnalytics]{chart.QQPlot}}, 
-#' \code{\link[graphics]{barplot}}, \code{\link[lattice]{barchart}} and 
-#' \code{\link[corrplot]{corrplot}} for plotting methods used.
-#' 
-#' \code{\link{fmSdDecomp}}, \code{\link{fmEsDecomp}}, 
-#' \code{\link{fmVaRDecomp}} for factor model risk measures.
-#' 
 #' @examples
 #' 
 #' # load data from the database
 #' data(managers)
 #' fit.macro <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
-#'                      factor.names=colnames(managers[,(7:8)]),
+#'                      factor.names=colnames(managers[,(7:9)]),
 #'                      rf.name="US.3m.TR", data=managers)
 #'     
-#' # for group plots (default), user can select type from menu prompt
+#' # for group plots (default), user can select plot option from menu prompt
 #' # menu is repeated to get multiple types of plots based on the same fit
 #' # plot(fit.macro)
 #'                
-#' # plot specific option(s) though which
+#' # choose specific plot option(s) using which
 #' # plot the factor betas of 1st 4 assets fitted above
 #' plot(fit.macro, max.show=4, which=2)
 #' 
-#' # plot factor model return correlation; angular order of the eigenvectors
-#' plot(fit.macro, which=7, order="AOE", method="ellipse", tl.pos = "d")
+#' # plot factor model residuals scatterplot matrix, with histograms, density 
+#' # overlays, correlations and significance stars
+#' plot(fit.macro, which=6)
 #' 
-#' # for individual plots: set plot.single=TRUE; specify asset.name
+#' # for individual plots: set plot.single=TRUE and specify asset.name
 #' # histogram of residuals from an individual asset's factor model fit 
-#' plot(fit.macro, plot.single=TRUE, asset.name="HAM1", which=12)
+#' plot(fit.macro, plot.single=TRUE, asset.name="HAM1", which=13)
 #' 
 #' @importFrom PerformanceAnalytics chart.TimeSeries chart.ACFplus
-#' chart.Histogram chart.QQPlot
+#' chart.Histogram chart.QQPlot chart.Correlation
 #' @importFrom lattice barchart panel.barchart panel.grid
-#' @importFrom corrplot corrplot
+#' @importFrom corrplot corrplot.mixed
 #' @importFrom strucchange efp
+#' @importFrom sn dst st.mple
 #' @importFrom robust lmRob
 #' 
 #' @method plot tsfm
 #' @export
 
 plot.tsfm <- function(x, which=NULL, max.show=6, plot.single=FALSE, 
-                      asset.name, colorset=(1:12), legend.loc="topleft", las=1, 
-                      VaR.method="historical", ...) {
+                      asset.name, legend.loc="topleft", las=1, lwd=2,
+                      colorset=c("royalblue","dimgray","olivedrab",
+                                 "firebrick","goldenrod","mediumorchid",
+                                 "deepskyblue","chocolate","darkslategray"),  
+                      maxlag=15, VaR.method="historical", ...) {
   
   which.vec <- which
   which <- which[1]
   
+  meth <- x$fit.method # one of "LS", "DLS" or "Robust"
+  if (is.null(meth)) {meth <- "Lars"}
+  
   if (plot.single==TRUE) {
     
     if (missing(asset.name) && length(x$asset.names)>1) {
@@ -151,12 +173,15 @@
       i <- asset.name
     }
     # extract info from the fitTsfm object
-    plotData <- merge.xts(x$data[,i], fitted(x)[,i])
-    colnames(plotData) <- c("Actual","Fitted")
-    Residuals <- residuals(x)[,i]
-    plotData2 <- merge.xts(Residuals, fitted(x)[,i])
-    colnames(plotData2) <- c("Residuals","Fitted")
+    plotData <- merge.xts(x$data[,i], fitted(x)[,i], residuals(x)[,i])
+    colnames(plotData) <- c("Actual","Fitted","Residuals")
+    Residuals <- na.omit(plotData[,"Residuals"])
     fit <- x$asset.fit[[i]]
+    resid.sd <- x$resid.sd[i]
+    den <- density(Residuals)
+    xval <- den$x
+    den.norm <- dnorm(xval, mean=mean(Residuals), sd=resid.sd)
+    den.st <- dst(xval, dp=st.mple(x=matrix(1,nrow(Residuals)), y=as.vector(Residuals))$dp)
     
     # plot selection
     repeat {
@@ -164,21 +189,23 @@
         which <- 
           menu(c("Actual and fitted asset returns",
                  "Actual vs fitted asset returns",
-                 "Residuals and fitted asset returns",
-                 "Sqrt. of Std. Residuals vs Fitted",
+                 "Residuals vs fitted asset returns",
+                 "Sqrt. of modified residuals vs fitted",
                  "Residuals with standard error bands",
                  "Time series of squared residuals",
                  "Time series of absolute residuals",
                  "SACF and PACF of residuals",
                  "SACF and PACF of squared residuals",
                  "SACF and PACF of absolute residuals",
-                 "Density Estimate of Residuals",
-                 "Histogram of residuals with normal curve overlayed",
-                 "Normal qq-plot of residuals",
+                 "Non-parametric density of residuals with normal overlaid",
+                 "Non-parametric density of residuals with skew-t overlaid",
+                 "Histogram of residuals with non-parametric density and normal overlaid",
+                 "QQ-plot of residuals",
                  "CUSUM test-Recursive residuals",
                  "CUSUM test-LS residuals",
                  "Recursive estimates (RE) test of LS regression coefficients",
-                 "Rolling estimates over a 24-period observation window"),
+                 "Rolling estimates over a 24-period observation window",
+                 "Asset returns vs factor returns (single factor model)"),
                title="\nMake a plot selection (or 0 to exit):")
       }
       
@@ -186,139 +213,170 @@
       
       switch(which,
              "1L" = {
-               ##  time series plot of actual and fitted asset returns
-               chart.TimeSeries(plotData, main=paste("Actual and Fitted:",i), 
-                                colorset=colorset, xlab="", ylab="Asset returns", 
+               ##  Time series plot of actual and fitted asset returns
+               chart.TimeSeries(plotData[,1:2], main=paste("Actual and fitted:",i), 
+                                colorset=colorset, xlab="", ylab="Returns", lwd=lwd,
                                 legend.loc=legend.loc, pch=NULL, las=las, ...)
              }, "2L" = {
-               ## fitted vs actual asset returns
-               plot(coredata(plotData[,1]), coredata(plotData[,2]), 
-                    xlab="Actual", ylab="Fitted",
-                    main=paste("Fitted vs Actual Returns:",i), las=las, ...)
+               ## Actual vs fitted asset returns
+               plot(x=coredata(plotData[,2]), y=coredata(plotData[,1]), 
+                    xlab="Fitted", ylab="Actual", col=colorset[1], 
+                    main=paste("Actual vs fitted:",i), las=las, ...)
+               abline(0, 1, col="dimgray",lty="dotted", lwd=lwd)
              }, "3L" = {
-               ## residuals vs fitted asset returns
-               plot(fit, which=1, main=paste("Residuals vs Fitted Returns:",i), 
-                    caption=NA, sub.caption="", las=las, ...)
+               ## Residuals vs fitted asset returns
+               plot(x=coredata(plotData[,2]), y=coredata(plotData[,3]), 
+                    xlab="Fitted", ylab="Residuals", col=colorset[1], 
+                    main=paste("Residuals vs fitted:",i), las=las, ...)
+               panel.smooth(x=coredata(plotData[,2]), y=coredata(plotData[,3]), 
+                            col=colorset[1], col.smooth=colorset[2], lwd=lwd)
              }, "4L" = {
-               ##  square root of absolute residuals vs fitted asset returns
-               plot(fit, which=3, main=paste("Sqrt. of Std. Residuals vs Fitted:",i), 
-                    caption=NA, sub.caption="", las=las, ...)
+               ##  Square root of absolute modified residuals vs fitted asset returns
+               if (meth=="Lars") {
+                 stop("This option is not available for 'lars' fits.")
+               } 
+               yval <- sqrt(abs(Residuals/sqrt(1-hatvalues(fit))))
+               plot(x=coredata(na.omit(plotData[,2])), y=yval, xlab="Fitted", 
+                    ylab=expression(sqrt(abs("Modified Residuals"))), col=colorset[1], 
+                    main=paste("Sqrt. modified residuals vs fitted:",i), las=las, ...)
+               panel.smooth(x=coredata(na.omit(plotData[,2])), y=yval, 
+                            col=colorset[1], col.smooth=colorset[2], lwd=lwd)
              }, "5L" = {
-               ## time series plot of residuals with standard error bands
+               ## Time series plot of residuals with standard error bands
                chart.TimeSeries(Residuals, main=paste("Residuals:",i), 
                                 colorset=colorset, xlab="", ylab="Residuals", 
-                                lwd=2, lty="solid", las=las, ...)
-               abline(h=1.96*x$resid.sd[i], lwd=2, lty="dotted", col="red")
-               abline(h=-1.96*x$resid.sd[i], lwd=2, lty="dotted", col="red")
-               legend(x=legend.loc, lty=c("solid","dotted"), 
-                      col=c(colorset[1],"red"), lwd=2, 
-                      legend=c("Residuals",expression("\u00b1 1.96"*sigma)))
+                                lwd=lwd, lty="solid", las=las, ...)
+               abline(h=1.96*x$resid.sd[i], lwd=lwd, lty="dotted", col=colorset[2])
+               abline(h=-1.96*x$resid.sd[i], lwd=lwd, lty="dotted", col=colorset[2])
+               legend(x=legend.loc, lty=c("solid","dotted"), col=c(colorset[1:2]), 
+                      lwd=lwd, bty="n", legend=c("Residuals",expression("\u00b1 1.96 "*sigma)))
              }, "6L" = {
-               ## time series plot of squared residuals
+               ## Time series plot of squared residuals
                chart.TimeSeries(Residuals^2, colorset=colorset, xlab="", 
-                                ylab=" Squared Residuals",
-                                main=paste("Squared Residuals:",i), 
-                                legend.loc=legend.loc, pch=NULL, las=las, ...)
+                                ylab=" Squared residuals", lwd=lwd,
+                                main=paste("Squared residuals:",i), 
+                                legend.loc=NULL, pch=NULL, las=las, ...)
              }, "7L" = {
-               ## time series plot of absolute residuals
+               ## Time series plot of absolute residuals
                chart.TimeSeries(abs(Residuals), colorset=colorset, xlab="", 
-                                ylab="Absolute Residuals",
-                                main=paste("Absolute Residuals:",i), 
-                                legend.loc=legend.loc, pch=NULL, las=las, ...)
+                                ylab="Absolute residuals", lwd=lwd,
+                                main=paste("Absolute residuals:",i), 
+                                legend.loc=NULL, pch=NULL, las=las, ...)
              }, "8L" = {
                ## SACF and PACF of residuals
-               chart.ACFplus(Residuals, col=colorset[1],
+               chart.ACFplus(Residuals, maxlag=maxlag,
                              main=paste("SACF & PACF - Residuals:",i), ...)
              }, "9L" = {
                ## SACF and PACF of squared residuals
-               chart.ACFplus(Residuals^2, col=colorset[1], ...,
-                             main=paste("SACF & PACF - Squared residuals:",i))
+               chart.ACFplus(Residuals^2, maxlag=maxlag,
+                             main=paste("SACF & PACF - Squared residuals:",i), ...)
              }, "10L" = {
                ## SACF and PACF of absolute residuals
-               chart.ACFplus(abs(Residuals), col=colorset[1], ...,
-                             main=paste("SACF & PACF - Absolute Residuals:",i))
+               chart.ACFplus(abs(Residuals), maxlag=maxlag,
+                             main=paste("SACF & PACF - Absolute residuals:",i), ...)
              }, "11L" = {
-               ## density estimate of residuals
-               plot(density(Residuals), xlab="Return residuals", 
-                    col=colorset, main=paste("Density estimate of Residuals:",i), ...)
-               rug(Residuals)
+               ## Non-parametric density of residuals with normal overlaid
+               ymax <- ceiling(max(0,den$y,den.norm))
+               plot(den, xlab="Return residuals", lwd=lwd, col=colorset[1], 
+                    ylim=c(0,ymax), main=paste("Density of residuals:",i), ...)
+               rug(Residuals, col="dimgray")
+               lines(xval, den.norm, col=colorset[2], lwd=lwd, lty="dashed")
+               legend(x=legend.loc, lty=c("solid","dashed"), col=c(colorset[1:2]), 
+                      lwd=lwd, bty="n", legend=c("KDE","Normal"))
              }, "12L" = {
-               ## histogram of residuals with normal curve overlayed
+               ## Non-parametric density of residuals with skew-t overlaid
+               ymax <- ceiling(max(0,den$y,den.st))
+               plot(den, xlab="Return residuals", lwd=lwd, col=colorset[1], 
[TRUNCATED]

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


More information about the Returnanalytics-commits mailing list