[Returnanalytics-commits] r3461 - in pkg/FactorAnalytics: R inst/tests man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 3 06:56:05 CEST 2014


Author: pragnya
Date: 2014-07-03 06:56:04 +0200 (Thu, 03 Jul 2014)
New Revision: 3461

Modified:
   pkg/FactorAnalytics/R/factorModelEsDecomposition.R
   pkg/FactorAnalytics/R/factorModelVaRDecomposition.R
   pkg/FactorAnalytics/R/fitTSFM.R
   pkg/FactorAnalytics/R/paFM.r
   pkg/FactorAnalytics/R/plot.pafm.r
   pkg/FactorAnalytics/R/plot.tsfm.r
   pkg/FactorAnalytics/R/predict.tsfm.r
   pkg/FactorAnalytics/R/print.pafm.r
   pkg/FactorAnalytics/R/summary.tsfm.r
   pkg/FactorAnalytics/inst/tests/test-fitTSFM.r
   pkg/FactorAnalytics/man/factorModelEsDecomposition.Rd
   pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd
   pkg/FactorAnalytics/man/paFM.Rd
   pkg/FactorAnalytics/man/plot.pafm.Rd
   pkg/FactorAnalytics/man/plot.tsfm.Rd
   pkg/FactorAnalytics/man/predict.tsfm.Rd
   pkg/FactorAnalytics/man/print.pafm.Rd
   pkg/FactorAnalytics/man/summary.tsfm.Rd
Log:
Edits to examples related to fitTSFM and covFM

Modified: pkg/FactorAnalytics/R/factorModelEsDecomposition.R
===================================================================
--- pkg/FactorAnalytics/R/factorModelEsDecomposition.R	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/factorModelEsDecomposition.R	2014-07-03 04:56:04 UTC (rev 3461)
@@ -45,7 +45,7 @@
 #' \item Epperlein and Smillie (2006) "Cracking VAR with Kernels," Risk.
 #' }
 #' @examples
-#' 
+#' \dontrun{
 #' data(managers.df)
 #' fit.macro <- fitTSFM (asset.names=colnames(managers.df[,(1:6)]), 
 #'                       factor.names=c("EDHEC.LS.EQ","SP500.TR"),
@@ -76,6 +76,7 @@
 #' factorModelEsDecomposition(tmpData, 
 #'                           fit.fund$beta["STI",],
 #'                           fit.fund$resid.variance["STI"], tail.prob=0.05,VaR.method="historical")
+#' }
 #' 
 #' @export
 #' 

Modified: pkg/FactorAnalytics/R/factorModelVaRDecomposition.R
===================================================================
--- pkg/FactorAnalytics/R/factorModelVaRDecomposition.R	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/factorModelVaRDecomposition.R	2014-07-03 04:56:04 UTC (rev 3461)
@@ -41,7 +41,7 @@
 #' \item Meucci (2007). "Risk Contributions from Generic User-Defined Factors," Risk. 
 #' }
 #' @examples
-#' 
+#' \dontrun{
 #' data(managers.df)
 #' fit.macro <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
 #'                      factor.names=c("EDHEC.LS.EQ","SP500.TR"),
@@ -55,7 +55,7 @@
 #' factor.VaR.decomp.HAM1 = factorModelVaRDecomposition(tmpData, fit.macro$beta[1,],
 #'                                                   fit.macro$resid.sd[1], tail.prob=0.05,
 #'                                                   VaR.method="historical")
-#' 
+#' }
 #' @export
 factorModelVaRDecomposition <-
 function(Data, beta.vec, sig2.e, tail.prob = 0.01,

Modified: pkg/FactorAnalytics/R/fitTSFM.R
===================================================================
--- pkg/FactorAnalytics/R/fitTSFM.R	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/fitTSFM.R	2014-07-03 04:56:04 UTC (rev 3461)
@@ -223,8 +223,10 @@
   # extract the fitted factor models, coefficients, r2 values and residual vol 
   # from returned factor model fits above
   coef.mat <- makePaddedDataFrame(lapply(reg.list, coef))
-  alpha <- coef.mat[, 1, drop = FALSE]
-  beta <- coef.mat[, -1, drop = FALSE]
+  alpha <- coef.mat[, 1, drop=FALSE]
+  # to make class of alpha numeric instead of matrix
+  # aplha <- coef.mat[,1]
+  beta <- coef.mat[, -1, drop=FALSE]
   r2 <- sapply(reg.list, function(x) summary(x)$r.squared)
   resid.sd <- sapply(reg.list, function(x) summary(x)$sigma)
   # create list of return values.
@@ -474,6 +476,7 @@
                         unique(unlist(c(sapply(l,names))))))
   DF <- as.data.frame(DF)
   names(DF) <- unique(unlist(c(sapply(l,names))))
+  # as.matrix(DF) # if matrix output needed
   DF
 }
 

Modified: pkg/FactorAnalytics/R/paFM.r
===================================================================
--- pkg/FactorAnalytics/R/paFM.r	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/paFM.r	2014-07-03 04:56:04 UTC (rev 3461)
@@ -30,14 +30,12 @@
 #' @seealso \code{\link{fitTSFM}}, \code{\link{fitSFM}}, \code{\link{fitFFM}}
 #' 
 #' @examples
-#' \dontrun{
 #' data(managers.df)
-#' fit.ts <- fitTSFM(assets.names=colnames(managers.df[, (1:6)]), 
-#'                   factors.names=c("EDHEC.LS.EQ","SP500.TR"), 
-#'                   data=managers.df, fit.method="OLS")
+#' fit <- fitTSFM(asset.names=colnames(managers.df[, (1:6)]), 
+#'                factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df, 
+#'                fit.method="OLS", variable.selection="none")
 #' # without benchmark
-#' fm.attr <- paFM(fit.ts)
-#' }
+#' fm.attr <- paFM(fit)
 #' 
 #' @export
 #' 
@@ -71,7 +69,7 @@
       # active portfolio management p.512 17A.9 
       # top-down method
       
-      cum.ret <-   Return.cumulative(actual.xts)
+      cum.ret <- Return.cumulative(actual.xts)
       # setup initial value
       attr.ret.xts.all <- xts(, as.Date(date))
       
@@ -95,7 +93,7 @@
       spec.ret.xts <- actual.xts - 
         xts(as.matrix(fit.lm$model[, -1])%*%as.matrix(fit.lm$coef[-1]), 
             as.Date(date))
-      cum.spec.ret[k] <- cum.ret - Return.cumulative(actual.xts - spec.ret.xts)
+      cum.spec.ret[k,1] <- cum.ret - Return.cumulative(actual.xts - spec.ret.xts)
       attr.list[[k]] <- merge(attr.ret.xts.all, spec.ret.xts)
       colnames(attr.list[[k]]) <- c(factorName, "specific.returns")
     }

Modified: pkg/FactorAnalytics/R/plot.pafm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.pafm.r	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/plot.pafm.r	2014-07-03 04:56:04 UTC (rev 3461)
@@ -27,16 +27,17 @@
 #' @examples
 #' \dontrun{
 #' data(managers.df)
-#' fit.ts <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
-#'                                   factors.names=c("EDHEC.LS.EQ","SP500.TR"),
-#'                                   data=managers.df,fit.method="OLS")
-#'  fm.attr <- paFM(fit.ts)
+#' fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
+#'                factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df, 
+#'                fit.method="OLS", variable.selection="none")
+#' fm.attr <- paFM(fit)
 #' # plot all
 #' plot(fm.attr,legend.loc="topleft",max.show=6)
 #' dev.off()
 #' # plot only one assets "HAM1
 #' plot(fm.attr,plot.single=TRUE,fundName="HAM1")
 #' }
+#' 
 #' @method plot pafm  
 #' @export
 #' 

Modified: pkg/FactorAnalytics/R/plot.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.tsfm.r	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/plot.tsfm.r	2014-07-03 04:56:04 UTC (rev 3461)
@@ -58,7 +58,8 @@
 #' data(managers.df)
 #' fit.macro <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
 #'                      factor.names=c("EDHEC.LS.EQ","SP500.TR"),
-#'                      data=managers.df,fit.method="OLS")
+#'                      data=managers.df, fit.method="OLS",
+#'                      variable.selection="none")
 #' # plot all assets and show only the first 4 assets.
 #' plot(fit.macro,max.show=4)
 #' # plot of an individual asset, "HAM1" 
@@ -373,7 +374,7 @@
              },    
              
              "4L" = {
-               cov.fm<- factorModelCovariance(x$beta,cov.factors,x$resid.variance)    
+               cov.fm<- covFM(x)    
                cor.fm = cov2cor(cov.fm)
                rownames(cor.fm) = colnames(cor.fm)
                ord <- order(cor.fm[1,])

Modified: pkg/FactorAnalytics/R/predict.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/predict.tsfm.r	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/predict.tsfm.r	2014-07-03 04:56:04 UTC (rev 3461)
@@ -24,8 +24,8 @@
 #' ret.assets = managers.df[,(1:6)]
 #' # fit the factor model with OLS
 #' fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
-#'                factor.names=c("EDHEC.LS.EQ","SP500.TR"),
-#'                data=managers.df, fit.method="OLS")
+#'                factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df, 
+#'                fit.method="OLS", variable.selection="none")
 #' 
 #' pred.fit <- predict(fit)
 #' newdata <- data.frame(EDHEC.LS.EQ = rnorm(n=120), SP500.TR = rnorm(n=120) )

Modified: pkg/FactorAnalytics/R/print.pafm.r
===================================================================
--- pkg/FactorAnalytics/R/print.pafm.r	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/print.pafm.r	2014-07-03 04:56:04 UTC (rev 3461)
@@ -8,17 +8,15 @@
 #' @param ...  Other arguments for \code{print} methods.
 #' @author Yi-An Chen.
 #' @examples
-#' \dontrun{
 #' # load data from the database
 #'  data(managers.df)
-#'  # fit the factor model with OLS
-#'  fit.ts <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
-#'                                factors.names=c("EDHEC.LS.EQ","SP500.TR"),
-#'                                data=managers.df,fit.method="OLS")
-#'   
-#'   fm.attr <- paFM(fit.ts)
-#'   print(fm.attr)
-#'   }
+#' # fit the factor model with OLS
+#' fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
+#'                factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df, 
+#'                fit.method="OLS", variable.selection="none")
+#' fm.attr <- paFM(fit)
+#' print(fm.attr)
+#'
 #' @method print pafm  
 #' @export   
 #' 

Modified: pkg/FactorAnalytics/R/summary.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.tsfm.r	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/R/summary.tsfm.r	2014-07-03 04:56:04 UTC (rev 3461)
@@ -31,7 +31,7 @@
 #' or \code{lars}) for each asset in the factor model.}
 #' 
 #' @note For a more detailed printed summary for each asset, refer to 
-#' \code{\link[stats]{summary.lm}} or \code{\link[robustbase]{lmRob}}, which 
+#' \code{\link[stats]{summary.lm}} or \code{\link[robust]{lmRob}}, which 
 #' include F-statistics, Multiple R-squared, Adjusted R-squared and further 
 #' format the coefficients, standard errors, etc. and additionally give 
 #' significance stars if \code{signif.stars} is TRUE. 
@@ -72,9 +72,9 @@
   # convert to HC/HAC standard errors and t-stats if specified
   for (i in object$asset.names) {
     if (se.type == "HC") {
-      sum[[i]]$coefficients <- coeftest(fit$asset.fit[[i]], vcovHC)[,1:4]
+      sum[[i]]$coefficients <- coeftest(object$asset.fit[[i]], vcovHC)[,1:4]
     } else if (se.type == "HAC") {
-      sum[[i]]$coefficients <- coeftest(fit$asset.fit[[i]], vcovHAC)[,1:4]
+      sum[[i]]$coefficients <- coeftest(object$asset.fit[[i]], vcovHAC)[,1:4]
     }
   }
   

Modified: pkg/FactorAnalytics/inst/tests/test-fitTSFM.r
===================================================================
--- pkg/FactorAnalytics/inst/tests/test-fitTSFM.r	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/inst/tests/test-fitTSFM.r	2014-07-03 04:56:04 UTC (rev 3461)
@@ -25,7 +25,7 @@
     fit.method = "OLS",
     variable.selection="none")
 
-  expect_that(ff.mod$beta,is_equivalent_to(t(coef(ff4)[-1,])))
+  expect_that(as.matrix(ff.mod$beta),is_equivalent_to(t(coef(ff4)[-1,])))
   expect_that(as.numeric(ff.mod$r2),equals(as.numeric(sapply(X = sum4, FUN = "[", "r.squared"))))
   
 })

Modified: pkg/FactorAnalytics/man/factorModelEsDecomposition.Rd
===================================================================
--- pkg/FactorAnalytics/man/factorModelEsDecomposition.Rd	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/man/factorModelEsDecomposition.Rd	2014-07-03 04:56:04 UTC (rev 3461)
@@ -50,6 +50,7 @@
 theorem:\cr \code{ES.fm = sum(cES.fm) = sum(beta.star*mES.fm)} \cr
 }
 \examples{
+\dontrun{
 data(managers.df)
 fit.macro <- fitTSFM (asset.names=colnames(managers.df[,(1:6)]),
                       factor.names=c("EDHEC.LS.EQ","SP500.TR"),
@@ -81,6 +82,7 @@
                           fit.fund$beta["STI",],
                           fit.fund$resid.variance["STI"], tail.prob=0.05,VaR.method="historical")
 }
+}
 \author{
 Eric Zviot and Yi-An Chen.
 }

Modified: pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd
===================================================================
--- pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd	2014-07-03 04:56:04 UTC (rev 3461)
@@ -48,6 +48,7 @@
 theorem:\cr \code{VaR.fm = sum(cVaR.fm) = sum(beta.star*mVaR.fm)} \cr
 }
 \examples{
+\dontrun{
 data(managers.df)
 fit.macro <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
                      factor.names=c("EDHEC.LS.EQ","SP500.TR"),
@@ -62,6 +63,7 @@
                                                   fit.macro$resid.sd[1], tail.prob=0.05,
                                                   VaR.method="historical")
 }
+}
 \author{
 Eric Zivot and Yi-An Chen
 }

Modified: pkg/FactorAnalytics/man/paFM.Rd
===================================================================
--- pkg/FactorAnalytics/man/paFM.Rd	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/man/paFM.Rd	2014-07-03 04:56:04 UTC (rev 3461)
@@ -33,15 +33,13 @@
 returns is \code{u_t}.
 }
 \examples{
-\dontrun{
 data(managers.df)
-fit.ts <- fitTSFM(assets.names=colnames(managers.df[, (1:6)]),
-                  factors.names=c("EDHEC.LS.EQ","SP500.TR"),
-                  data=managers.df, fit.method="OLS")
+fit <- fitTSFM(asset.names=colnames(managers.df[, (1:6)]),
+               factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df,
+               fit.method="OLS", variable.selection="none")
 # without benchmark
-fm.attr <- paFM(fit.ts)
+fm.attr <- paFM(fit)
 }
-}
 \author{
 Yi-An Chen and Sangeetha Srinivasan
 }

Modified: pkg/FactorAnalytics/man/plot.pafm.Rd
===================================================================
--- pkg/FactorAnalytics/man/plot.pafm.Rd	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/man/plot.pafm.Rd	2014-07-03 04:56:04 UTC (rev 3461)
@@ -42,10 +42,10 @@
 \examples{
 \dontrun{
 data(managers.df)
-fit.ts <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
-                                  factors.names=c("EDHEC.LS.EQ","SP500.TR"),
-                                  data=managers.df,fit.method="OLS")
- fm.attr <- paFM(fit.ts)
+fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
+               factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df,
+               fit.method="OLS", variable.selection="none")
+fm.attr <- paFM(fit)
 # plot all
 plot(fm.attr,legend.loc="topleft",max.show=6)
 dev.off()

Modified: pkg/FactorAnalytics/man/plot.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/plot.tsfm.Rd	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/man/plot.tsfm.Rd	2014-07-03 04:56:04 UTC (rev 3461)
@@ -73,7 +73,8 @@
 data(managers.df)
 fit.macro <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
                      factor.names=c("EDHEC.LS.EQ","SP500.TR"),
-                     data=managers.df,fit.method="OLS")
+                     data=managers.df, fit.method="OLS",
+                     variable.selection="none")
 # plot all assets and show only the first 4 assets.
 plot(fit.macro,max.show=4)
 # plot of an individual asset, "HAM1"

Modified: pkg/FactorAnalytics/man/predict.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/predict.tsfm.Rd	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/man/predict.tsfm.Rd	2014-07-03 04:56:04 UTC (rev 3461)
@@ -29,8 +29,8 @@
 ret.assets = managers.df[,(1:6)]
 # fit the factor model with OLS
 fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
-               factor.names=c("EDHEC.LS.EQ","SP500.TR"),
-               data=managers.df, fit.method="OLS")
+               factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df,
+               fit.method="OLS", variable.selection="none")
 
 pred.fit <- predict(fit)
 newdata <- data.frame(EDHEC.LS.EQ = rnorm(n=120), SP500.TR = rnorm(n=120) )

Modified: pkg/FactorAnalytics/man/print.pafm.Rd
===================================================================
--- pkg/FactorAnalytics/man/print.pafm.Rd	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/man/print.pafm.Rd	2014-07-03 04:56:04 UTC (rev 3461)
@@ -15,17 +15,14 @@
 Generic function of print method for \code{paFM}.
 }
 \examples{
-\dontrun{
 # load data from the database
  data(managers.df)
- # fit the factor model with OLS
- fit.ts <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
-                               factors.names=c("EDHEC.LS.EQ","SP500.TR"),
-                               data=managers.df,fit.method="OLS")
-
-  fm.attr <- paFM(fit.ts)
-  print(fm.attr)
-  }
+# fit the factor model with OLS
+fit <- fitTSFM(asset.names=colnames(managers.df[,(1:6)]),
+               factor.names=c("EDHEC.LS.EQ","SP500.TR"), data=managers.df,
+               fit.method="OLS", variable.selection="none")
+fm.attr <- paFM(fit)
+print(fm.attr)
 }
 \author{
 Yi-An Chen.

Modified: pkg/FactorAnalytics/man/summary.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.tsfm.Rd	2014-07-03 00:08:53 UTC (rev 3460)
+++ pkg/FactorAnalytics/man/summary.tsfm.Rd	2014-07-03 04:56:04 UTC (rev 3461)
@@ -48,7 +48,7 @@
 }
 \note{
 For a more detailed printed summary for each asset, refer to
-\code{\link[stats]{summary.lm}} or \code{\link[robustbase]{lmRob}}, which
+\code{\link[stats]{summary.lm}} or \code{\link[robust]{lmRob}}, which
 include F-statistics, Multiple R-squared, Adjusted R-squared and further
 format the coefficients, standard errors, etc. and additionally give
 significance stars if \code{signif.stars} is TRUE.



More information about the Returnanalytics-commits mailing list