[Returnanalytics-commits] r3445 - in pkg/FactorAnalytics: . R inst/tests man sandbox vignettes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Jun 29 05:59:31 CEST 2014
Author: pragnya
Date: 2014-06-29 05:59:29 +0200 (Sun, 29 Jun 2014)
New Revision: 3445
Added:
pkg/FactorAnalytics/man/print.summary.tsfm.Rd
Modified:
pkg/FactorAnalytics/DESCRIPTION
pkg/FactorAnalytics/NAMESPACE
pkg/FactorAnalytics/R/coef.sfm.r
pkg/FactorAnalytics/R/coef.tsfm.R
pkg/FactorAnalytics/R/factorModelCovariance.r
pkg/FactorAnalytics/R/factorModelEsDecomposition.R
pkg/FactorAnalytics/R/factorModelMonteCarlo.R
pkg/FactorAnalytics/R/factorModelVaRDecomposition.R
pkg/FactorAnalytics/R/fitTSFM.R
pkg/FactorAnalytics/R/fitted.sfm.r
pkg/FactorAnalytics/R/fitted.tsfm.r
pkg/FactorAnalytics/R/print.tsfm.r
pkg/FactorAnalytics/R/residuals.sfm.r
pkg/FactorAnalytics/R/residuals.tsfm.r
pkg/FactorAnalytics/R/summary.pafm.r
pkg/FactorAnalytics/R/summary.tsfm.r
pkg/FactorAnalytics/inst/tests/test-fitTSFM.r
pkg/FactorAnalytics/man/coef.sfm.Rd
pkg/FactorAnalytics/man/coef.tsfm.Rd
pkg/FactorAnalytics/man/factorModelEsDecomposition.Rd
pkg/FactorAnalytics/man/factorModelMonteCarlo.Rd
pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd
pkg/FactorAnalytics/man/fitTSFM.Rd
pkg/FactorAnalytics/man/fitted.sfm.Rd
pkg/FactorAnalytics/man/fitted.tsfm.Rd
pkg/FactorAnalytics/man/print.tsfm.Rd
pkg/FactorAnalytics/man/residuals.sfm.Rd
pkg/FactorAnalytics/man/residuals.tsfm.Rd
pkg/FactorAnalytics/man/summary.pafm.Rd
pkg/FactorAnalytics/man/summary.tsfm.Rd
pkg/FactorAnalytics/sandbox/test.vignette.r
pkg/FactorAnalytics/sandbox/testfile.r
pkg/FactorAnalytics/vignettes/fundamentalFM.Rnw
Log:
Add class summary.tsfm. Edit method functions, vignette, examples.
Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/DESCRIPTION 2014-06-29 03:59:29 UTC (rev 3445)
@@ -29,3 +29,4 @@
Suggests:
testthat, quantmod
LazyLoad: yes
+LazyDataCompression: xz
Modified: pkg/FactorAnalytics/NAMESPACE
===================================================================
--- pkg/FactorAnalytics/NAMESPACE 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/NAMESPACE 2014-06-29 03:59:29 UTC (rev 3445)
@@ -2,6 +2,7 @@
S3method(coef,sfm)
S3method(coef,tsfm)
+S3method(fitted,sfm)
S3method(fitted,tsfm)
S3method(plot,FundamentalFactorModel)
S3method(plot,StatFactorModel)
@@ -13,6 +14,7 @@
S3method(print,FundamentalFactorModel)
S3method(print,StatFactorModel)
S3method(print,pafm)
+S3method(print,summary.tsfm)
S3method(print,tsfm)
S3method(residuals,sfm)
S3method(residuals,tsfm)
Modified: pkg/FactorAnalytics/R/coef.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/coef.sfm.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/coef.sfm.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -2,8 +2,9 @@
#'
#' @description Method or helper function for fit object of class \code{sfm}.
#'
-#' @param x an object of class \code{sfm} which is returned by
+#' @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}
@@ -16,7 +17,7 @@
#' @method coef sfm
#' @export
-coef.sfm <- function(x){
- coef.mat <- t(sapply(x$asset.fit, coef))
+coef.sfm <- function(object,...){
+ coef.mat <- t(sapply(object$asset.fit, coef))
return(coef.mat)
}
\ No newline at end of file
Modified: pkg/FactorAnalytics/R/coef.tsfm.R
===================================================================
--- pkg/FactorAnalytics/R/coef.tsfm.R 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/coef.tsfm.R 2014-06-29 03:59:29 UTC (rev 3445)
@@ -2,8 +2,9 @@
#'
#' @description Method or helper function for fit object of class \code{tsfm}.
#'
-#' @param x an object of class \code{tsfm} which is returned by
+#' @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}
@@ -27,7 +28,7 @@
#' @method coef tsfm
#' @export
-coef.tsfm <- function(x){
- coef.mat <- t(sapply(x$asset.fit, coef))
+coef.tsfm <- function(object,...){
+ coef.mat <- t(sapply(object$asset.fit, coef))
return(coef.mat)
}
Modified: pkg/FactorAnalytics/R/factorModelCovariance.r
===================================================================
--- pkg/FactorAnalytics/R/factorModelCovariance.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/factorModelCovariance.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -77,7 +77,7 @@
factor.cov = as.matrix(factor.cov)
sig2.e = as.vector(resid.sd)^2
- if (length(sig.e) > 1) {
+ if (length(sig2.e) > 1) {
D.e = diag(as.vector(sig2.e))
} else {
D.e = as.matrix(sig2.e)
Modified: pkg/FactorAnalytics/R/factorModelEsDecomposition.R
===================================================================
--- pkg/FactorAnalytics/R/factorModelEsDecomposition.R 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/factorModelEsDecomposition.R 2014-06-29 03:59:29 UTC (rev 3445)
@@ -16,7 +16,7 @@
#' contain the returns on the \code{k} factors, and the \code{(k+2)}nd column
#' contain residuals scaled to have unit variance.
#' @param beta.vec \code{k x 1} vector of factor betas.
-#' @param sig2.e scalar, residual variance from factor model.
+#' @param sig.e scalar, residual variance from factor model.
#' @param tail.prob scalar, tail probability for VaR quantile. Typically 0.01
#' or 0.05.
#' @param VaR.method character, method for computing VaR. Valid choices are
@@ -47,16 +47,17 @@
#' @examples
#'
#' data(managers.df)
-#' fit.macro <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
-#' factors.names=c("EDHEC.LS.EQ","SP500.TR"),
-#' data=managers.df,fit.method="OLS")
+#' fit.macro <- 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")
#' # risk factor contribution to ETL
#' # combine fund returns, factor returns and residual returns for HAM1
#' tmpData = cbind(managers.df[,1],managers.df[,c("EDHEC.LS.EQ","SP500.TR")] ,
-#' residuals(fit.macro$asset.fit$HAM1)/sqrt(fit.macro$resid.variance[1]))
+#' residuals(fit.macro$asset.fit$HAM1)/sqrt(fit.macro$resid.sd[1]))
#' colnames(tmpData)[c(1,4)] = c("HAM1", "residual")
#' factor.es.decomp.HAM1 = factorModelEsDecomposition(tmpData, fit.macro$beta[1,],
-#' fit.macro$resid.variance[1], tail.prob=0.05,
+#' fit.macro$resid.sd[1], tail.prob=0.05,
#' VaR.method="historical" )
#'
#' # fundamental factor model
@@ -79,7 +80,7 @@
#' @export
#'
factorModelEsDecomposition <-
-function(Data, beta.vec, sig2.e, tail.prob = 0.05,
+function(Data, beta.vec, sig.e, tail.prob = 0.05,
VaR.method=c("modified", "gaussian", "historical", "kernel")) {
Data = as.matrix(Data)
@@ -92,7 +93,7 @@
stop("beta.vec is not an n x 1 matrix or a vector")
}
beta.names = c(names(beta.vec), "residual")
- beta.star.vec = c(beta.vec, sqrt(sig2.e))
+ beta.star.vec = c(beta.vec, sig.e)
names(beta.star.vec) = beta.names
## epsilon is calculated in the sense of minimizing mean square error by Silverman 1986
Modified: pkg/FactorAnalytics/R/factorModelMonteCarlo.R
===================================================================
--- pkg/FactorAnalytics/R/factorModelMonteCarlo.R 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/factorModelMonteCarlo.R 2014-06-29 03:59:29 UTC (rev 3445)
@@ -51,12 +51,13 @@
#'
#' # load data from the database
#' data(managers.df)
-#' fit <- fitTimeSeriesFactorModel(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")
#' factorData= managers.df[,c("EDHEC.LS.EQ","SP500.TR")]
#' Beta.mat=fit$beta
-#' residualData=as.matrix(fit$resid.variance,1,6)
+#' residualData=as.matrix((fit$resid.sd)^2,1,6)
#' n.boot=1000
#' # bootstrap returns data from factor model with residuals sample from normal distribution
#' bootData <- factorModelMonteCarlo(n.boot, factorData,Beta.mat, residual.dist="normal",
Modified: pkg/FactorAnalytics/R/factorModelVaRDecomposition.R
===================================================================
--- pkg/FactorAnalytics/R/factorModelVaRDecomposition.R 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/factorModelVaRDecomposition.R 2014-06-29 03:59:29 UTC (rev 3445)
@@ -43,16 +43,17 @@
#' @examples
#'
#' data(managers.df)
-#' fit.macro <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
-#' factors.names=c("EDHEC.LS.EQ","SP500.TR"),
-#' data=managers.df,fit.method="OLS")
+#' fit.macro <- 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")
#' # risk factor contribution to VaR
#' # combine fund returns, factor returns and residual returns for HAM1
#' tmpData = cbind(managers.df[,1],managers.df[,c("EDHEC.LS.EQ","SP500.TR")] ,
-#' residuals(fit.macro$asset.fit$HAM1)/sqrt(fit.macro$resid.variance[1]))
+#' residuals(fit.macro$asset.fit$HAM1)/fit.macro$resid.sd[1])
#' colnames(tmpData)[c(1,4)] = c("HAM1", "residual")
#' factor.VaR.decomp.HAM1 = factorModelVaRDecomposition(tmpData, fit.macro$beta[1,],
-#' fit.macro$resid.variance[1], tail.prob=0.05,
+#' fit.macro$resid.sd[1], tail.prob=0.05,
#' VaR.method="historical")
#'
#' @export
Modified: pkg/FactorAnalytics/R/fitTSFM.R
===================================================================
--- pkg/FactorAnalytics/R/fitTSFM.R 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/fitTSFM.R 2014-06-29 03:59:29 UTC (rev 3445)
@@ -130,21 +130,20 @@
#' \code{\link{paFM}} for Performance Attribution.
#'
#' @examples
-#' \dontrun{
#' # load data from the database
#' data(managers.df)
-#' fit <- fitTimeSeriesFactorModel(asset.names=colnames(managers.df[,(1:6)]),
-#' factor.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")
#' # summary of HAM1
#' summary(fit$asset.fit$HAM1)
#' # plot actual vs. fitted over time for HAM1
-#' # use chart.TimeSeries() function from PerformanceAnalytics package
+#' # using chart.TimeSeries() function from PerformanceAnalytics package
#' dataToPlot <- cbind(fitted(fit$asset.fit$HAM1), na.omit(managers.df$HAM1))
#' colnames(dataToPlot) <- c("Fitted","Actual")
#' chart.TimeSeries(dataToPlot, main="FM fit for HAM1",
#' colorset=c("black","blue"), legend.loc="bottomleft")
-#' }
+#'
#'
#' @export
@@ -291,8 +290,8 @@
reg.list[[i]] <- step(lm(fm.formula, data=reg.xts, weights=w),
direction=direction, steps=steps, k=k, trace=0)
} else if (fit.method == "Robust") {
- reg.list[[i]] <- step.lmRob(lmRob(fm.formula, data=reg.df), trace=FALSE,
- direction=direction, steps=steps, k=k)
+ reg.list[[i]] <- step.lmRob(lmRob(fm.formula, data=reg.xts), trace=FALSE,
+ direction=direction, steps=steps)
} else {
stop("Invalid argument: fit.method must be 'OLS', 'DLS' or 'Robust'")
}
Modified: pkg/FactorAnalytics/R/fitted.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/fitted.sfm.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/fitted.sfm.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -2,8 +2,9 @@
#'
#' @description Method or helper function for fit object of class \code{sfm}.
#'
-#' @param x an object of class \code{sfm} which is returned by
-#' \code{\link{fitSFM}}
+#' @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}
@@ -13,13 +14,13 @@
#'
#' @seealso \code{\link{fitSFM}}
#'
-#' @method fitted tsfm
+#' @method fitted sfm
#' @export
-fitted.sfm <- function(x){
+fitted.sfm <- function(object,...){
# get fitted values from each linear factor model fit
# and convert them into xts/zoo objects
- fitted.list = sapply(x$asset.fit, function(x) checkData(fitted(x)))
+ 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)
Modified: pkg/FactorAnalytics/R/fitted.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/fitted.tsfm.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/fitted.tsfm.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -2,8 +2,9 @@
#'
#' @description Method or helper function for fit object of class \code{tsfm}.
#'
-#' @param x an object of class \code{tsfm} which is returned by
+#' @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}
@@ -27,10 +28,10 @@
#' @method fitted tsfm
#' @export
-fitted.tsfm <- function(x){
+fitted.tsfm <- function(object,...){
# get fitted values from each linear factor model fit
# and convert them into xts/zoo objects
- fitted.list = sapply(x$asset.fit, function(x) checkData(fitted(x)))
+ 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)
Modified: pkg/FactorAnalytics/R/print.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/print.tsfm.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/print.tsfm.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -18,7 +18,7 @@
#' 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",
+#' data=managers.df, fit.method="OLS", variable.selection="none",
#' add.up.market=TRUE, add.market.sqd=TRUE)
#' print(fit)
#'
@@ -39,7 +39,7 @@
print(x$alpha, digits = digits, ...)
cat("\nFactor Betas:\n")
print(t(x$beta), digits = digits, ...)
- cat("\nRegression R-squared values:\n")
+ cat("\nR-squared values:\n")
print(x$r2, digits = digits, ...)
cat("\nResidual Volatilities:\n")
print(x$resid.sd, digits = digits, ...)
Modified: pkg/FactorAnalytics/R/residuals.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/residuals.sfm.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/residuals.sfm.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -2,8 +2,9 @@
#'
#' @description Method or helper function for fit object of class \code{sfm}.
#'
-#' @param x an object of class \code{sfm} which is returned by
+#' @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}
@@ -16,10 +17,10 @@
#' @method residuals sfm
#' @export
-residuals.sfm <- function(x) {
+residuals.sfm <- function(object,...) {
# get residuals from each linear factor model fit
# and convert them into xts/zoo objects
- residuals.list = sapply(x$asset.fit, function(x) checkData(residuals(x)))
+ 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)
Modified: pkg/FactorAnalytics/R/residuals.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/residuals.tsfm.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/residuals.tsfm.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -2,8 +2,9 @@
#'
#' @description Method or helper function for fit object of class \code{tsfm}.
#'
-#' @param x an object of class \code{tsfm} which is returned by
+#' @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}
@@ -27,10 +28,10 @@
#' @method residuals tsfm
#' @export
-residuals.tsfm <- function(x) {
+residuals.tsfm <- function(object ,...) {
# get residuals from each linear factor model fit
# and convert them into xts/zoo objects
- residuals.list = sapply(x$asset.fit, function(x) checkData(residuals(x)))
+ 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)
Modified: pkg/FactorAnalytics/R/summary.pafm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.pafm.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/summary.pafm.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -12,9 +12,10 @@
#' # 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")
+#' fit.ts <- 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.ts)
#' summary(fm.attr)
Modified: pkg/FactorAnalytics/R/summary.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/summary.tsfm.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/R/summary.tsfm.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -1,15 +1,23 @@
#' @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}.
+#' Resulting object is of class {summary.tsfm}. There is a generic
+#' \code{print} method for this object.
#'
#' @param object an object of class \code{tsfm} produced by \code{fitTSFM}.
-#' @param digits an integer value, to indicate the required number of
-#' significant digits. Default is 3.
-#' @param ... optional arguments passed to the \code{print} method.
+#' @param ... futher arguments passed to or from other methods.
#'
-#' @return Returns an object of class {summary.tsfm}.
+#' @return Returns an object of class \code{summary.tsfm}, which is a list
+#' containing the function call to \code{fitTSFM} and the
+#' \code{summary.lm} objects fitted for each asset in the factor model.
+#' The print method for class \code{summary.tsfm} outputs the call, coefficients,
+#' r-squared and residual volatilty for all assets.
#'
+#' @note For a more detailed printed summary for each asset, refer to
+#' \code{print.summary.lm}, which tries to be smart about formatting the
+#' coefficients, standard errors, etc. and additionally gives ‘significance
+#' stars’ if \code{signif.stars} is TRUE.
+#'
#' @author Yi-An Chen & Sangeetha Srinivasan.
#'
#' @seealso \code{\link{fitTSFM}}
@@ -18,30 +26,48 @@
#' 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",
+#' market.name="SP500.TR", data=managers.df,
+#' fit.method="OLS", variable.selection="none",
#' add.up.market=TRUE, add.market.sqd=TRUE)
#' summary(fit)
#'
#' @method summary tsfm
+#' @method print summary.tsfm
#' @export
-summary.tsfm <- function(object, digits=3, ...){
- if(!is.null(cl <- object$call)) {
- cat("\nCall:\n")
- dput(cl)
+summary.tsfm <- function(object, ...){
+ # check input object validity
+ if (!inherits(object, "tsfm")) {
+ stop("Invalid 'tsfm' object")
}
- cat("\nFactor Betas\n")
- n <- length(object$assets.names)
- for (i in 1:n) {
- options(digits = digits)
- cat("\n", object$assets.names[i], "\n")
- table.macro <- t(summary(object$asset.fit[[i]])$coefficients)
- colnames(table.macro)[1] <- "Intercept"
- print(table.macro,digits = digits,...)
- cat("\nR-squared =", object$r2[i] ,",Residual Volatility ="
- , object$resid.sd[i],"\n")
- }
-
+ # extract summary.lm objects for each asset
+ sum <- lapply(object$asset.fit, summary)
+ # include the call to fitTSFM
+ sum <- c(call=object$call, sum)
+ class(sum) <- "summary.tsfm"
+ return(sum)
}
+# summary.tsfm <- function(object, ...){
+# # check input object validity
+# if (!inherits(object, "tsfm")) {
+# stop("Invalid 'tsfm' object")
+# }
+# n <- length(object$asset.names)
+# asset.summary <- list()
+#
+# for (i in 1:n) {
+# coeff <- t(summary(object$asset.fit[[i]])$coefficients)
+# R2 <- object$r2[i]
+# SD <- object$resid.sd[i]
+# asset.summary[[i]] <- list(coeff, R2, SD)
+# names(asset.summary) <- ("Coefficients", "R.squared", "Residual.Volatility")
+# }
+#
+# out <- c(Call=object$call,
+# Coefficients=coeff,
+# R.squared=R2,
+# Residual.Volatility=SD)
+# class(out) <- "summary.tsfm"
+# return(out)
+# }
Modified: pkg/FactorAnalytics/inst/tests/test-fitTSFM.r
===================================================================
--- pkg/FactorAnalytics/inst/tests/test-fitTSFM.r 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/inst/tests/test-fitTSFM.r 2014-06-29 03:59:29 UTC (rev 3445)
@@ -1,30 +1,31 @@
-context("Test fitTimeSeriesFactorModel")
-
-test_that("fitTimeSeriesFactorModel is as expected", {
-
- # fit Carhart 4-factor model using lm
- fpath <- system.file("extdata", "timeSeriesReturns.csv", package="factorAnalytics")
- returns.z <- read.zoo(file=fpath,header=TRUE,sep=",",as.is=TRUE,FUN=as.yearmon)
- returns.z <- window(returns.z,start="2008-01-01",end="2012-12-31")
- assets <- names(returns.z)[1:30]
- ex.rets <- returns.z[,assets]-returns.z$rf
- carhart <- returns.z[,c("mktrf","smb","hml","umd")]
- ff4 <- lm(ex.rets ~ carhart)
- sum4 = summary(ff4)
- rsq4 <- as.numeric(sapply(X = sum4, FUN = "[", "r.squared"))
- Sigma.F <- var(carhart)
- beta.hat <- coef(ff4)[-1,]
- Sigma.eps <- diag(as.numeric(sapply(X = sum4, FUN = "[", "sigma")))
- Sigma.R <- t(beta.hat) %*% Sigma.F %*% beta.hat + Sigma.eps^2
-
- # fit Carhart 4-factor mode via fitTimeSeriesFactorModel
- ff.mod <- fitTimeSeriesFactorModel(
- assets.names = assets,
- factors.names = c("mktrf","smb", "hml","umd"),
- data = cbind(ex.rets,carhart),
- fit.method = "OLS")
-
- expect_that(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"))))
-
-})
+context("Test fitTSFM")
+
+test_that("fitTSFM is as expected", {
+
+ # fit Carhart 4-factor model using lm
+ fpath <- system.file("extdata", "timeSeriesReturns.csv", package="factorAnalytics")
+ returns.z <- read.zoo(file=fpath,header=TRUE,sep=",",as.is=TRUE,FUN=as.yearmon)
+ returns.z <- window(returns.z,start="2008-01-01",end="2012-12-31")
+ assets <- names(returns.z)[1:30]
+ ex.rets <- returns.z[,assets]-returns.z$rf
+ carhart <- returns.z[,c("mktrf","smb","hml","umd")]
+ ff4 <- lm(ex.rets ~ carhart)
+ sum4 = summary(ff4)
+ rsq4 <- as.numeric(sapply(X = sum4, FUN = "[", "r.squared"))
+ Sigma.F <- var(carhart)
+ beta.hat <- coef(ff4)[-1,]
+ Sigma.eps <- diag(as.numeric(sapply(X = sum4, FUN = "[", "sigma")))
+ Sigma.R <- t(beta.hat) %*% Sigma.F %*% beta.hat + Sigma.eps^2
+
+ # fit Carhart 4-factor mode via fitTSFM
+ ff.mod <- fitTSFM(
+ asset.names = assets,
+ factor.names = c("mktrf","smb", "hml","umd"),
+ data = cbind(ex.rets,carhart),
+ fit.method = "OLS",
+ variable.selection="none")
+
+ expect_that(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/coef.sfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/coef.sfm.Rd 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/man/coef.sfm.Rd 2014-06-29 03:59:29 UTC (rev 3445)
@@ -3,11 +3,13 @@
\alias{coef.sfm}
\title{Extract coefficients from a fitted stochastic factor model}
\usage{
-\method{coef}{sfm}(x)
+\method{coef}{sfm}(object, ...)
}
\arguments{
-\item{x}{an object of class \code{sfm} which is returned by
+\item{object}{a fit object of class \code{sfm} which is returned by
\code{\link{fitSFM}}}
+
+\item{...}{other arguments passed}
}
\value{
\item{coef.mat}{an N x (K+1) matrix of all coefficients}
Modified: pkg/FactorAnalytics/man/coef.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/coef.tsfm.Rd 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/man/coef.tsfm.Rd 2014-06-29 03:59:29 UTC (rev 3445)
@@ -3,11 +3,13 @@
\alias{coef.tsfm}
\title{Extract coefficients from a fitted time series factor model}
\usage{
-\method{coef}{tsfm}(x)
+\method{coef}{tsfm}(object, ...)
}
\arguments{
-\item{x}{an object of class \code{tsfm} which is returned by
+\item{object}{a fit object of class \code{tsfm} which is returned by
\code{\link{fitTSFM}}}
+
+\item{...}{other arguments passed}
}
\value{
\item{coef.mat}{an N x (K+1) matrix of all coefficients}
Modified: pkg/FactorAnalytics/man/factorModelEsDecomposition.Rd
===================================================================
--- pkg/FactorAnalytics/man/factorModelEsDecomposition.Rd 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/man/factorModelEsDecomposition.Rd 2014-06-29 03:59:29 UTC (rev 3445)
@@ -3,7 +3,7 @@
\alias{factorModelEsDecomposition}
\title{Compute Factor Model ES Decomposition}
\usage{
-factorModelEsDecomposition(Data, beta.vec, sig2.e, tail.prob = 0.05,
+factorModelEsDecomposition(Data, beta.vec, sig.e, tail.prob = 0.05,
VaR.method = c("modified", "gaussian", "historical", "kernel"))
}
\arguments{
@@ -14,7 +14,7 @@
\item{beta.vec}{\code{k x 1} vector of factor betas.}
-\item{sig2.e}{scalar, residual variance from factor model.}
+\item{sig.e}{scalar, residual variance from factor model.}
\item{tail.prob}{scalar, tail probability for VaR quantile. Typically 0.01
or 0.05.}
@@ -51,16 +51,17 @@
}
\examples{
data(managers.df)
-fit.macro <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
- factors.names=c("EDHEC.LS.EQ","SP500.TR"),
- data=managers.df,fit.method="OLS")
+fit.macro <- 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")
# risk factor contribution to ETL
# combine fund returns, factor returns and residual returns for HAM1
tmpData = cbind(managers.df[,1],managers.df[,c("EDHEC.LS.EQ","SP500.TR")] ,
-residuals(fit.macro$asset.fit$HAM1)/sqrt(fit.macro$resid.variance[1]))
+residuals(fit.macro$asset.fit$HAM1)/sqrt(fit.macro$resid.sd[1]))
colnames(tmpData)[c(1,4)] = c("HAM1", "residual")
factor.es.decomp.HAM1 = factorModelEsDecomposition(tmpData, fit.macro$beta[1,],
- fit.macro$resid.variance[1], tail.prob=0.05,
+ fit.macro$resid.sd[1], tail.prob=0.05,
VaR.method="historical" )
# fundamental factor model
Modified: pkg/FactorAnalytics/man/factorModelMonteCarlo.Rd
===================================================================
--- pkg/FactorAnalytics/man/factorModelMonteCarlo.Rd 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/man/factorModelMonteCarlo.Rd 2014-06-29 03:59:29 UTC (rev 3445)
@@ -70,12 +70,13 @@
\examples{
# load data from the database
data(managers.df)
-fit <- fitTimeSeriesFactorModel(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")
factorData= managers.df[,c("EDHEC.LS.EQ","SP500.TR")]
Beta.mat=fit$beta
-residualData=as.matrix(fit$resid.variance,1,6)
+residualData=as.matrix((fit$resid.sd)^2,1,6)
n.boot=1000
# bootstrap returns data from factor model with residuals sample from normal distribution
bootData <- factorModelMonteCarlo(n.boot, factorData,Beta.mat, residual.dist="normal",
Modified: pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd
===================================================================
--- pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/man/factorModelVaRDecomposition.Rd 2014-06-29 03:59:29 UTC (rev 3445)
@@ -49,16 +49,17 @@
}
\examples{
data(managers.df)
-fit.macro <- fitTimeSeriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
- factors.names=c("EDHEC.LS.EQ","SP500.TR"),
- data=managers.df,fit.method="OLS")
+fit.macro <- 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")
# risk factor contribution to VaR
# combine fund returns, factor returns and residual returns for HAM1
tmpData = cbind(managers.df[,1],managers.df[,c("EDHEC.LS.EQ","SP500.TR")] ,
-residuals(fit.macro$asset.fit$HAM1)/sqrt(fit.macro$resid.variance[1]))
+residuals(fit.macro$asset.fit$HAM1)/fit.macro$resid.sd[1])
colnames(tmpData)[c(1,4)] = c("HAM1", "residual")
factor.VaR.decomp.HAM1 = factorModelVaRDecomposition(tmpData, fit.macro$beta[1,],
- fit.macro$resid.variance[1], tail.prob=0.05,
+ fit.macro$resid.sd[1], tail.prob=0.05,
VaR.method="historical")
}
\author{
Modified: pkg/FactorAnalytics/man/fitTSFM.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTSFM.Rd 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/man/fitTSFM.Rd 2014-06-29 03:59:29 UTC (rev 3445)
@@ -130,21 +130,19 @@
\code{\link[lars]{cv.lars}}.
}
\examples{
-\dontrun{
# load data from the database
data(managers.df)
-fit <- fitTimeSeriesFactorModel(asset.names=colnames(managers.df[,(1:6)]),
- factor.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")
# summary of HAM1
summary(fit$asset.fit$HAM1)
# plot actual vs. fitted over time for HAM1
-# use chart.TimeSeries() function from PerformanceAnalytics package
+# using chart.TimeSeries() function from PerformanceAnalytics package
dataToPlot <- cbind(fitted(fit$asset.fit$HAM1), na.omit(managers.df$HAM1))
colnames(dataToPlot) <- c("Fitted","Actual")
chart.TimeSeries(dataToPlot, main="FM fit for HAM1",
colorset=c("black","blue"), legend.loc="bottomleft")
- }
}
\author{
Eric Zivot, Yi-An Chen and Sangeetha Srinivasan.
Modified: pkg/FactorAnalytics/man/fitted.sfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitted.sfm.Rd 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/man/fitted.sfm.Rd 2014-06-29 03:59:29 UTC (rev 3445)
@@ -3,11 +3,13 @@
\alias{fitted.sfm}
\title{Get fitted values from a stochastic factor model}
\usage{
-\method{fitted}{tsfm}(x)
+\method{fitted}{sfm}(object, ...)
}
\arguments{
-\item{x}{an object of class \code{sfm} which is returned by
+\item{object}{a fit object of class \code{sfm} which is returned by
\code{\link{fitSFM}}}
+
+\item{...}{other arguments passed}
}
\value{
\item{fitted.xts}{an N x T data object of fitted values}
Modified: pkg/FactorAnalytics/man/fitted.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitted.tsfm.Rd 2014-06-27 19:15:04 UTC (rev 3444)
+++ pkg/FactorAnalytics/man/fitted.tsfm.Rd 2014-06-29 03:59:29 UTC (rev 3445)
@@ -3,11 +3,13 @@
\alias{fitted.tsfm}
\title{Get fitted values from a time series factor model}
\usage{
-\method{fitted}{tsfm}(x)
+\method{fitted}{tsfm}(object, ...)
}
\arguments{
-\item{x}{an object of class \code{tsfm} which is returned by
+\item{object}{a fit object of class \code{tsfm} which is returned by
\code{\link{fitTSFM}}}
+
+\item{...}{other arguments passed}
}
\value{
\item{fitted.xts}{an N x T data object of fitted values}
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 3445
More information about the Returnanalytics-commits
mailing list