[Returnanalytics-commits] r3622 - in pkg/FactorAnalytics: . R man tests vignettes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Mar 20 09:48:00 CET 2015
Author: pragnya
Date: 2015-03-20 09:48:00 +0100 (Fri, 20 Mar 2015)
New Revision: 3622
Added:
pkg/FactorAnalytics/tests/FactorAnalytics.pdf
Removed:
pkg/FactorAnalytics/tests/factorAnalytics.pdf
Modified:
pkg/FactorAnalytics/DESCRIPTION
pkg/FactorAnalytics/R/fitSfm.R
pkg/FactorAnalytics/R/fitTsfm.R
pkg/FactorAnalytics/R/fitTsfm.control.R
pkg/FactorAnalytics/R/fitTsfmLagBeta.r
pkg/FactorAnalytics/R/fitTsfmMT.r
pkg/FactorAnalytics/R/fitTsfmUpDn.r
pkg/FactorAnalytics/R/plot.sfm.r
pkg/FactorAnalytics/R/plot.tsfm.r
pkg/FactorAnalytics/R/plot.tsfmUpDn.r
pkg/FactorAnalytics/R/predict.tsfm.r
pkg/FactorAnalytics/R/predict.tsfmUpDn.r
pkg/FactorAnalytics/R/print.pafm.r
pkg/FactorAnalytics/R/print.tsfmUpDn.r
pkg/FactorAnalytics/R/summary.pafm.r
pkg/FactorAnalytics/R/summary.tsfm.r
pkg/FactorAnalytics/R/summary.tsfmUpDn.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/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:
Change OLS to LS. Fix CMD check warnings in fitTsfmUpDn
Modified: pkg/FactorAnalytics/DESCRIPTION
===================================================================
--- pkg/FactorAnalytics/DESCRIPTION 2015-03-20 07:33:31 UTC (rev 3621)
+++ pkg/FactorAnalytics/DESCRIPTION 2015-03-20 08:48:00 UTC (rev 3622)
@@ -1,8 +1,8 @@
Package: factorAnalytics
Type: Package
Title: Factor Analytics
-Version:2.0.15
-Date:2015-03-20
+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
Modified: pkg/FactorAnalytics/R/fitSfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitSfm.R 2015-03-20 07:33:31 UTC (rev 3621)
+++ pkg/FactorAnalytics/R/fitSfm.R 2015-03-20 08:48:00 UTC (rev 3622)
@@ -69,7 +69,7 @@
#' An object of class \code{"sfm"} is a list containing the following
#' components:
#' \item{asset.fit}{fitted object of class \code{"mlm"} or \code{"lm"} from the
-#' time-series OLS regression of asset returns on estimated factors.}
+#' time-series LS regression of asset returns on estimated factors.}
#' \item{k}{number of factors; as input or determined by "ck" or "bn" methods.}
#' \item{factors}{T x K xts object of estimated factor realizations.}
#' \item{loadings}{N x K matrix of factor loadings estimated by
@@ -77,7 +77,7 @@
#' \item{alpha}{length-N vector of estimated alphas.}
#' \item{r2}{length-N vector of R-squared values.}
#' \item{resid.sd}{length-N vector of residual standard deviations.}
-#' \item{residuals}{T x N xts object of residuals from the OLS regression.}
+#' \item{residuals}{T x N xts object of residuals from the LS regression.}
#' \item{Omega}{N x N return covariance matrix estimated by the factor model.}
#' \item{eigen}{length-N (or length-T for APCA) vector of eigenvalues of the
#' sample covariance matrix.}
@@ -255,7 +255,7 @@
# invert 1st principal component if most values are negative
if (median(f[,1]) < 0) {f[,1] <- -f[,1]}
- # OLS time series regression to get B: NxK matrix of factor loadings
+ # LS time series regression to get B: NxK matrix of factor loadings
f <- xts(f, index(R.xts))
asset.fit <- lm(R.xts ~ f, ...)
B <- t(coef(asset.fit)[-1, , drop=FALSE])
@@ -308,7 +308,7 @@
# invert 1st principal component if most values are negative
if (median(f[,1]) < 0) {f[,1] <- -f[,1]}
- # OLS time series regression to get B: NxK matrix of factor loadings
+ # LS time series regression to get B: NxK matrix of factor loadings
asset.fit <- lm(R.xts ~ f, ...)
B <- t(coef(asset.fit)[-1, , drop=FALSE])
alpha <- coef(asset.fit)[1,]
Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R 2015-03-20 07:33:31 UTC (rev 3621)
+++ pkg/FactorAnalytics/R/fitTsfm.R 2015-03-20 08:48:00 UTC (rev 3622)
@@ -2,7 +2,7 @@
#'
#' @description Fits a time series (a.k.a. macroeconomic) factor model for one
#' or more asset returns or excess returns using time series regression.
-#' Users can choose between ordinary least squares-OLS, discounted least
+#' Users can choose between ordinary least squares-LS, discounted least
#' squares-DLS (or) robust regression. Several variable selection options
#' including Stepwise, Subsets, Lars are available as well. An object of class
#' \code{"tsfm"} is returned.
@@ -12,7 +12,7 @@
#' the option to supply a risk free rate variable to subtract from each asset
#' return and factor to compute excess returns.
#'
-#' Estimation method "OLS" corresponds to ordinary least squares using
+#' Estimation method "LS" corresponds to ordinary least squares using
#' \code{\link[stats]{lm}}, "DLS" is discounted least squares (weighted least
#' squares with exponentially declining weights that sum to unity), and,
#' "Robust" is robust regression (using \code{\link[robust]{lmRob}}).
@@ -61,8 +61,8 @@
#' @param data vector, matrix, data.frame, xts, timeSeries or zoo object
#' containing column(s) named in \code{asset.names}, \code{factor.names} and
#' optionally, \code{mkt.name} and \code{rf.name}.
-#' @param fit.method the estimation method, one of "OLS", "DLS" or "Robust".
-#' See details. Default is "OLS".
+#' @param fit.method the estimation method, one of "LS", "DLS" or "Robust".
+#' See details. Default is "LS".
#' @param variable.selection the variable selection method, one of "none",
#' "stepwise","subsets","lars". See details. Default is "none".
#' \code{mkt.name} is required if any of these options are to be implemented.
@@ -82,7 +82,7 @@
#' An object of class \code{"tsfm"} is a list containing the following
#' components:
#' \item{asset.fit}{list of fitted objects for each asset. Each object is of
-#' class \code{lm} if \code{fit.method="OLS" or "DLS"}, class \code{lmRob} if
+#' class \code{lm} if \code{fit.method="LS" or "DLS"}, class \code{lmRob} if
#' the \code{fit.method="Robust"}, or class \code{lars} if
#' \code{variable.selection="lars"}.}
#' \item{alpha}{length-N vector of estimated alphas.}
@@ -165,7 +165,7 @@
#' @export
fitTsfm <- function(asset.names, factor.names, mkt.name=NULL, rf.name=NULL,
- data=data, fit.method=c("OLS","DLS","Robust"),
+ data=data, fit.method=c("LS","DLS","Robust"),
variable.selection=c("none","stepwise","subsets","lars"),
control=fitTsfm.control(...), ...) {
@@ -174,8 +174,8 @@
# set defaults and check input vailidity
fit.method = fit.method[1]
- if (!(fit.method %in% c("OLS","DLS","Robust"))) {
- stop("Invalid argument: fit.method must be 'OLS', 'DLS' or 'Robust'")
+ if (!(fit.method %in% c("LS","DLS","Robust"))) {
+ stop("Invalid argument: fit.method must be 'LS', 'DLS' or 'Robust'")
}
variable.selection = variable.selection[1]
if (!(variable.selection %in% c("none","stepwise","subsets","lars"))) {
@@ -294,7 +294,7 @@
fm.formula <- as.formula(paste(i," ~ ."))
# fit based on time series regression method chosen
- if (fit.method == "OLS") {
+ if (fit.method == "LS") {
reg.list[[i]] <- do.call(lm, c(list(fm.formula,data=reg.xts),lm.args))
} else if (fit.method == "DLS") {
lm.args$weights <- WeightsDLS(nrow(reg.xts), decay)
@@ -324,7 +324,7 @@
fm.formula <- as.formula(paste(i," ~ ."))
# fit based on time series regression method chosen
- if (fit.method == "OLS") {
+ if (fit.method == "LS") {
lm.fit <- do.call(lm, c(list(fm.formula,data=reg.xts),lm.args))
reg.list[[i]] <- do.call(step, c(list(lm.fit),step.args))
} else if (fit.method == "DLS") {
@@ -377,7 +377,7 @@
reg.xts <- na.omit(dat.xts[,c(i,names.sub)])
# fit based on time series regression method chosen
- if (fit.method == "OLS") {
+ if (fit.method == "LS") {
reg.list[[i]] <- do.call(lm, c(list(fm.formula,data=reg.xts),lm.args))
} else if (fit.method == "DLS") {
lm.args$weights <- WeightsDLS(nrow(reg.xts), decay)
Modified: pkg/FactorAnalytics/R/fitTsfm.control.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.control.R 2015-03-20 07:33:31 UTC (rev 3621)
+++ pkg/FactorAnalytics/R/fitTsfm.control.R 2015-03-20 08:48:00 UTC (rev 3622)
@@ -17,7 +17,7 @@
#' The scalar \code{decay} is used by \code{\link{fitTsfm}} to compute
#' exponentially decaying weights for \code{fit.method="DLS"}. Alternately, one
#' can directly specify \code{weights}, a weights vector, to be used with
-#' "OLS" or "Robust". Especially when fitting multiple assets, care should be
+#' "LS" or "Robust". Especially when fitting multiple assets, care should be
#' taken to ensure that the length of the weights vector matches the number of
#' observations (excluding cases ignored due to NAs).
#'
@@ -31,13 +31,13 @@
#' @param decay a scalar in (0, 1] to specify the decay factor for "DLS".
#' Default is 0.95.
#' @param weights an optional vector of weights to be used in the fitting
-#' process for \code{fit.method="OLS","Robust"}, or
+#' process for \code{fit.method="LS","Robust"}, or
#' \code{variable.selection="subsets"}. Should be \code{NULL} or a numeric
#' vector. The length of \code{weights} must be the same as the number of
#' observations. The weights must be nonnegative and it is strongly
#' recommended that they be strictly positive.
#' @param model,x,y,qr logicals passed to \code{lm} for
-#' \code{fit.method="OLS"}. If \code{TRUE} the corresponding components of the
+#' \code{fit.method="LS"}. If \code{TRUE} the corresponding components of the
#' fit (the model frame, the model matrix, the response, the QR decomposition)
#' are returned.
#' @param nrep the number of random subsamples to be drawn for
Modified: pkg/FactorAnalytics/R/fitTsfmLagBeta.r
===================================================================
--- pkg/FactorAnalytics/R/fitTsfmLagBeta.r 2015-03-20 07:33:31 UTC (rev 3621)
+++ pkg/FactorAnalytics/R/fitTsfmLagBeta.r 2015-03-20 08:48:00 UTC (rev 3622)
@@ -1,110 +1,110 @@
-#' @title Fit a lagged Betas factor model using time series regression
-#'
-#' @description This is a wrapper function to fits a time series lagged Betas factor model for one
-#' or more asset returns or excess returns using time series regression.
-#' Users can choose between ordinary least squares-OLS, discounted least
-#' squares-DLS (or) robust regression like \code{fitTsfm}.An object of class
-#' \code{"tsfm"} is returned.
-#'
-#' @details
-#' The lagged returns model estimates lagged market Beta. Specifically,
-#' \deqn{r_t = \alpha + \beta_0 MKT_t + \beta_1 MKT_t-1 + \ldots + \beta_K MKT_t-K + \epsilon_t, t=1 \ldots T}
-#' where \eqn{r_t} is the asset returns, and MKT is the market factor. It is usually
-#' needed for illiquid securities with stale prices. One can also report the sum of
-#' the lagged Betas:
-#' \deqn{\beta = \beta_0 + \beta_1 + \ldots + \beta_K }
-#'
-#' @param asset.names vector containing names of assets, whose returns or
-#' excess returns are the dependent variable.
-#' @param mkt.name name of the column for market returns. It
-#' is required for a lagged Betas factor model.
-#' @param rf.name name of the column of risk free rate variable to calculate
-#' excess returns for all assets (in \code{asset.names}) and the market factor (in
-#' \code{mkt.name}).Default is NULL, and no action is taken.
-#' @param LagBeta A integer number to specify numbers of lags of Betas to
-#' include in the model. The Default is 1.
-#' @param data vector, matrix, data.frame, xts, timeSeries or zoo object
-#' containing column(s) named in \code{asset.names}, \code{factor.names} and
-#' optionally, \code{mkt.name} and \code{rf.name}.
-#' @param fit.method the estimation method, one of "OLS", "DLS" or "Robust".
-#' See details. Default is "OLS".
-#' @param control list of control parameters. The default is constructed by
-#' the function \code{\link{fitTsfm.control}}. See the documentation for
-#' \code{\link{fitTsfm.control}} for details.
-#' @param ... arguments passed to \code{\link{fitTsfm.control}}
-#'
-#' @return \code{fitTsfmLagBeta} also returns an object of class \code{"tsfm"} like
-#' \code{fitTsfm}. The generic function such as \code{print}, \code{plot}, \code{predict}
-#' and \code{summary} methods exist. Also, the generic accessor functions \code{coef},
-#' \code{fitted}, \code{residuals} and \code{fmCov} can be applied as well.
-#'
-#' An object of class \code{"tsfm"} is a list containing the following
-#' components:
-#' \item{asset.fit}{list of fitted objects for each asset. Each object is of
-#' class \code{lm} if \code{fit.method="OLS" or "DLS"}, class \code{lmRob} if
-#' the \code{fit.method="Robust"}.}
-#' \item{alpha}{length-N vector of estimated alphas.}
-#' \item{beta}{N x (L+1) matrix of estimated betas.}
-#' \item{r2}{length-N vector of R-squared values.}
-#' \item{resid.sd}{length-N vector of residual standard deviations.}
-#' \item{call}{the matched function call.}
-#' \item{data}{xts data object containing the assets and factors.}
-#' \item{asset.names}{asset.names as input.}
-#' \item{fit.method}{fit.method as input.}
-#' Where N is the number of assets, L is the number of lagged market Betas and T is the
-#' number of time periods.
-#'
-#' @author Yi-An Chen.
-#'
-#' @references
-#' Scholes, M. and Williams, J. T. (1977). Estimating betas from non-synchronous
-#' data, Journal of Financial Economics, vol. 5, 1977, pp. 309-327
-#'
-#' @seealso
-#' The original time series function \code{\link{fitTsfm}} and its generic functions
-#' application.
-#'
-#' @examples
-#' # load data from the database
-#' data(managers)
-#'
-#' # example: A lagged Beetas model with OLS fit
-#' fit <- fitTsfmLagBeta(asset.names=colnames(managers[,(1:6)]),LagBeta=2,
-#' mkt.name="SP500.TR",rf.name="US.3m.TR",data=managers)
-#' summary(fit)
-#' fitted(fit)
-#'
-#' @importFrom PerformanceAnalytics checkData
-#' @importFrom robust lmRob step.lmRob
-#' @importFrom leaps regsubsets
-#' @importFrom lars lars cv.lars
-#'
-#' @export
-
-fitTsfmLagBeta <- function(asset.names, mkt.name, rf.name=NULL,
- data=data, fit.method=c("OLS","DLS","Robust"),LagBeta=1,
- control=fitTsfm.control(...),...) {
-
- if (is.null(mkt.name)) {
- stop("Missing argument: mkt.name has to be specified for lagged Betas model.")
- }
-
-
- if (as.integer(LagBeta) != LagBeta | LagBeta < 1 ) {
- stop("Invalid argument: LagBeta must be an integer and no less than 1. The default is 1.")
- }
-
- # Create market lag terms
- factor.names = mkt.name
- mktlag <- lag(data[,mkt.name],k=seq(1:LagBeta))
- for (i in 1:LagBeta) {
- colnames(mktlag)[i] <- paste("MktLag",i,sep="")
- factor.names <- c(factor.names,paste("MktLag",i,sep=""))
- }
- data <- merge(data,mktlag)
-
- fit <- fitTsfm(asset.names=asset.names,factor.names=factor.names,mkt.name=mkt.name,rf.name=rf.name,
- data=data,fit.method=fit.method,variable.selection="none",control=control)
-
- return(fit)
+#' @title Fit a lagged Betas factor model using time series regression
+#'
+#' @description This is a wrapper function to fits a time series lagged Betas factor model for one
+#' or more asset returns or excess returns using time series regression.
+#' Users can choose between ordinary least squares-LS, discounted least
+#' squares-DLS (or) robust regression like \code{fitTsfm}.An object of class
+#' \code{"tsfm"} is returned.
+#'
+#' @details
+#' The lagged returns model estimates lagged market Beta. Specifically,
+#' \deqn{r_t = \alpha + \beta_0 MKT_t + \beta_1 MKT_t-1 + \ldots + \beta_K MKT_t-K + \epsilon_t, t=1 \ldots T}
+#' where \eqn{r_t} is the asset returns, and MKT is the market factor. It is usually
+#' needed for illiquid securities with stale prices. One can also report the sum of
+#' the lagged Betas:
+#' \deqn{\beta = \beta_0 + \beta_1 + \ldots + \beta_K }
+#'
+#' @param asset.names vector containing names of assets, whose returns or
+#' excess returns are the dependent variable.
+#' @param mkt.name name of the column for market returns. It
+#' is required for a lagged Betas factor model.
+#' @param rf.name name of the column of risk free rate variable to calculate
+#' excess returns for all assets (in \code{asset.names}) and the market factor (in
+#' \code{mkt.name}).Default is NULL, and no action is taken.
+#' @param LagBeta A integer number to specify numbers of lags of Betas to
+#' include in the model. The Default is 1.
+#' @param data vector, matrix, data.frame, xts, timeSeries or zoo object
+#' containing column(s) named in \code{asset.names}, \code{factor.names} and
+#' optionally, \code{mkt.name} and \code{rf.name}.
+#' @param fit.method the estimation method, one of "LS", "DLS" or "Robust".
+#' See details. Default is "LS".
+#' @param control list of control parameters. The default is constructed by
+#' the function \code{\link{fitTsfm.control}}. See the documentation for
+#' \code{\link{fitTsfm.control}} for details.
+#' @param ... arguments passed to \code{\link{fitTsfm.control}}
+#'
+#' @return \code{fitTsfmLagBeta} also returns an object of class \code{"tsfm"} like
+#' \code{fitTsfm}. The generic function such as \code{print}, \code{plot}, \code{predict}
+#' and \code{summary} methods exist. Also, the generic accessor functions \code{coef},
+#' \code{fitted}, \code{residuals} and \code{fmCov} can be applied as well.
+#'
+#' An object of class \code{"tsfm"} is a list containing the following
+#' components:
+#' \item{asset.fit}{list of fitted objects for each asset. Each object is of
+#' class \code{lm} if \code{fit.method="LS" or "DLS"}, class \code{lmRob} if
+#' the \code{fit.method="Robust"}.}
+#' \item{alpha}{length-N vector of estimated alphas.}
+#' \item{beta}{N x (L+1) matrix of estimated betas.}
+#' \item{r2}{length-N vector of R-squared values.}
+#' \item{resid.sd}{length-N vector of residual standard deviations.}
+#' \item{call}{the matched function call.}
+#' \item{data}{xts data object containing the assets and factors.}
+#' \item{asset.names}{asset.names as input.}
+#' \item{fit.method}{fit.method as input.}
+#' Where N is the number of assets, L is the number of lagged market Betas and T is the
+#' number of time periods.
+#'
+#' @author Yi-An Chen.
+#'
+#' @references
+#' Scholes, M. and Williams, J. T. (1977). Estimating betas from non-synchronous
+#' data, Journal of Financial Economics, vol. 5, 1977, pp. 309-327
+#'
+#' @seealso
+#' The original time series function \code{\link{fitTsfm}} and its generic functions
+#' application.
+#'
+#' @examples
+#' # load data from the database
+#' data(managers)
+#'
+#' # example: A lagged Beetas model with LS fit
+#' fit <- fitTsfmLagBeta(asset.names=colnames(managers[,(1:6)]),LagBeta=2,
+#' mkt.name="SP500.TR",rf.name="US.3m.TR",data=managers)
+#' summary(fit)
+#' fitted(fit)
+#'
+#' @importFrom PerformanceAnalytics checkData
+#' @importFrom robust lmRob step.lmRob
+#' @importFrom leaps regsubsets
+#' @importFrom lars lars cv.lars
+#'
+#' @export
+
+fitTsfmLagBeta <- function(asset.names, mkt.name, rf.name=NULL,
+ data=data, fit.method=c("LS","DLS","Robust"),LagBeta=1,
+ control=fitTsfm.control(...),...) {
+
+ if (is.null(mkt.name)) {
+ stop("Missing argument: mkt.name has to be specified for lagged Betas model.")
+ }
+
+
+ if (as.integer(LagBeta) != LagBeta | LagBeta < 1 ) {
+ stop("Invalid argument: LagBeta must be an integer and no less than 1. The default is 1.")
+ }
+
+ # Create market lag terms
+ factor.names = mkt.name
+ mktlag <- lag(data[,mkt.name],k=seq(1:LagBeta))
+ for (i in 1:LagBeta) {
+ colnames(mktlag)[i] <- paste("MktLag",i,sep="")
+ factor.names <- c(factor.names,paste("MktLag",i,sep=""))
+ }
+ data <- merge(data,mktlag)
+
+ fit <- fitTsfm(asset.names=asset.names,factor.names=factor.names,mkt.name=mkt.name,rf.name=rf.name,
+ data=data,fit.method=fit.method,variable.selection="none",control=control)
+
+ return(fit)
}
\ No newline at end of file
Modified: pkg/FactorAnalytics/R/fitTsfmMT.r
===================================================================
--- pkg/FactorAnalytics/R/fitTsfmMT.r 2015-03-20 07:33:31 UTC (rev 3621)
+++ pkg/FactorAnalytics/R/fitTsfmMT.r 2015-03-20 08:48:00 UTC (rev 3622)
@@ -1,126 +1,126 @@
-#' @title Fit a time serie market timing factor model using time series regression
-#'
-#' @description This is a wrapper function to fits a time series market timing factor model for one
-#' or more asset returns or excess returns using time series regression.
-#' Users can choose between ordinary least squares-OLS, discounted least
-#' squares-DLS (or) robust regression. An object of class
-#' \code{"tsfm"} is returned.
-#'
-#' @details
-#' The Market timing accounts for the price movement of the general stock market relative to fixed income
-#' securities. It includes down.market = max(0, R_f-R_m) as a factor, following Henriksson & Merton
-#' (1981). The coefficient of this down-market factor can be interpreted as the
-#' number of "free" put options on the market provided by the manager's market-timings kills.
-#'
-#'
-#'
-#' @param asset.names vector containing names of assets, whose returns or
-#' excess returns are the dependent variable.
-#' @param mkt.name name of the column for market returns; It
-#' is required for a market timing model.
-#' @param rf.name name of the column of risk free rate variable to calculate
-#' excess returns for all assets (in \code{asset.names}) and the market factor (in
-#' \code{mkt.name}).Default is NULL, and no action is taken.
-#' @param data vector, matrix, data.frame, xts, timeSeries or zoo object
-#' containing column(s) named in \code{asset.names}, \code{factor.names} and
-#' optionally, \code{mkt.name} and \code{rf.name}.
-#' @param fit.method the estimation method, one of "OLS", "DLS" or "Robust".
-#' See details. Default is "OLS".
-#' @param control list of control parameters. The default is constructed by
-#' the function \code{\link{fitTsfm.control}}. See the documentation for
-#' \code{\link{fitTsfm.control}} for details.
-#' @param ... arguments passed to \code{\link{fitTsfm.control}}
-#'
-#' @return \code{fitTsfmMT} also returns an object of class \code{"tsfm"} like
-#' \code{fitTsfm}. The generic function such as \code{print}, \code{plot}, \code{predict}
-#' and \code{summary} methods exist. Also, the generic accessor functions \code{coef},
-#' \code{fitted}, \code{residuals} and \code{fmCov} can be applied as well.
-#'
-#' An object of class \code{"tsfm"} is a list containing the following
-#' components:
-#' \item{asset.fit}{list of fitted objects for each asset. Each object is of
-#' class \code{lm} if \code{fit.method="OLS" or "DLS"}, class \code{lmRob} if
-#' the \code{fit.method="Robust"}.}
-#' \item{alpha}{length-N vector of estimated alphas.}
-#' \item{beta}{N x 2 matrix of estimated betas.}
-#' \item{r2}{length-N vector of R-squared values.}
-#' \item{resid.sd}{length-N vector of residual standard deviations.}
-#' \item{call}{the matched function call.}
-#' \item{data}{xts data object containing the assets and factors.}
-#' \item{asset.names}{asset.names as input.}
-#' \item{fit.method}{fit.method as input.}
-#' Where N is the number of assets and T is the
-#' number of time periods.
-#'
-#' @author Yi-An Chen.
-#'
-#' @references
-#' Henriksson, R. D., & Merton, R. C. (1981). On market timing and investment
-#' performance. II. Statistical procedures for evaluating forecasting skills.
-#' Journal of business, 513-533.
-#'
-#' #' Christopherson, J. A., Carino, D. R., & Ferson, W. E. (2009). Portfolio
-#' performance measurement and benchmarking. McGraw Hill Professional. pp.127-133
-#'
-#' @seealso
-#' The original time series function \code{\link{fitTsfm}} and its generic functions
-#' application.
-#'
-#' @examples
-#' # load data from the database
-#' data(managers)
-#'
-#' # example: Market-timing factors with OLS fit
-#' fit <- fitTsfmMT(asset.names=colnames(managers[,(1:6)]),
-#' mkt.name="SP500.TR",rf.name="US.3m.TR",data=managers)
-#' summary(fit)
-#'
-#' @importFrom PerformanceAnalytics checkData
-#' @importFrom robust lmRob step.lmRob
-#' @importFrom leaps regsubsets
-#' @importFrom lars lars cv.lars
-#'
-#' @export
-
-fitTsfmMT <- function(asset.names,mkt.name, rf.name=NULL,
- data=data, fit.method=c("OLS","DLS","Robust"),
- control=fitTsfm.control(...),...) {
- if (is.null(mkt.name)){
- stop("Missing argument: mkt.name has to be specified for market timing model.")
- }
-
- # convert data into an xts object and hereafter work with xts objects
- data.xts <- checkData(data)
- # convert index to 'Date' format for uniformity
- time(data.xts) <- as.Date(time(data.xts))
-
- # extract columns to be used in the time series regression
- dat.xts <- merge(data.xts[,asset.names], data.xts[,mkt.name])
- ### After merging xts objects, the spaces in names get converted to periods
-
- # convert all asset and factor returns to excess return form if specified
- if (!is.null(rf.name)) {
- dat.xts <- "[<-"(dat.xts,,vapply(dat.xts, function(x) x-data.xts[,rf.name],
- FUN.VALUE = numeric(nrow(dat.xts))))
- }
-
- # mkt-timing factors: down.market=max(0,Rf-Rm), market.sqd=(Rm-Rf)^2
-
- down.market <- dat.xts[,mkt.name]
- down.market[down.market < 0 ] <- 0
- dat.xts <- merge.xts(dat.xts,down.market)
- colnames(dat.xts)[dim(dat.xts)[2]] <- "down.market"
- factor.names <- c(mkt.name,"down.market")
-
-# if("TM" %in% mkt.timing) {
-# market.sqd <- data.xts[,mkt.name]^2
-# dat.xts <- merge(dat.xts, market.sqd)
-# colnames(dat.xts)[dim(dat.xts)[2]] <- "market.sqd"
-# factor.names <- c(factor.names, "market.sqd")
-# }
-
- fit.Timing <- fitTsfm(asset.names=asset.names,factor.names=factor.names,rf.name=NULL,
- data=dat.xts,fit.method=fit.method,variable.selection="none",control=control)
-
-return(fit.Timing)
-}
+#' @title Fit a time serie market timing factor model using time series regression
+#'
+#' @description This is a wrapper function to fits a time series market timing factor model for one
+#' or more asset returns or excess returns using time series regression.
+#' Users can choose between ordinary least squares-LS, discounted least
+#' squares-DLS (or) robust regression. An object of class
+#' \code{"tsfm"} is returned.
+#'
+#' @details
+#' The Market timing accounts for the price movement of the general stock market relative to fixed income
+#' securities. It includes down.market = max(0, R_f-R_m) as a factor, following Henriksson & Merton
+#' (1981). The coefficient of this down-market factor can be interpreted as the
+#' number of "free" put options on the market provided by the manager's market-timings kills.
+#'
+#'
+#'
+#' @param asset.names vector containing names of assets, whose returns or
+#' excess returns are the dependent variable.
+#' @param mkt.name name of the column for market returns; It
+#' is required for a market timing model.
+#' @param rf.name name of the column of risk free rate variable to calculate
+#' excess returns for all assets (in \code{asset.names}) and the market factor (in
+#' \code{mkt.name}).Default is NULL, and no action is taken.
+#' @param data vector, matrix, data.frame, xts, timeSeries or zoo object
+#' containing column(s) named in \code{asset.names}, \code{factor.names} and
+#' optionally, \code{mkt.name} and \code{rf.name}.
+#' @param fit.method the estimation method, one of "LS", "DLS" or "Robust".
+#' See details. Default is "LS".
+#' @param control list of control parameters. The default is constructed by
+#' the function \code{\link{fitTsfm.control}}. See the documentation for
+#' \code{\link{fitTsfm.control}} for details.
+#' @param ... arguments passed to \code{\link{fitTsfm.control}}
+#'
+#' @return \code{fitTsfmMT} also returns an object of class \code{"tsfm"} like
+#' \code{fitTsfm}. The generic function such as \code{print}, \code{plot}, \code{predict}
+#' and \code{summary} methods exist. Also, the generic accessor functions \code{coef},
+#' \code{fitted}, \code{residuals} and \code{fmCov} can be applied as well.
+#'
+#' An object of class \code{"tsfm"} is a list containing the following
+#' components:
+#' \item{asset.fit}{list of fitted objects for each asset. Each object is of
+#' class \code{lm} if \code{fit.method="LS" or "DLS"}, class \code{lmRob} if
+#' the \code{fit.method="Robust"}.}
+#' \item{alpha}{length-N vector of estimated alphas.}
+#' \item{beta}{N x 2 matrix of estimated betas.}
+#' \item{r2}{length-N vector of R-squared values.}
+#' \item{resid.sd}{length-N vector of residual standard deviations.}
+#' \item{call}{the matched function call.}
+#' \item{data}{xts data object containing the assets and factors.}
+#' \item{asset.names}{asset.names as input.}
+#' \item{fit.method}{fit.method as input.}
+#' Where N is the number of assets and T is the
+#' number of time periods.
+#'
+#' @author Yi-An Chen.
+#'
+#' @references
+#' Henriksson, R. D., & Merton, R. C. (1981). On market timing and investment
+#' performance. II. Statistical procedures for evaluating forecasting skills.
+#' Journal of business, 513-533.
+#'
+#' #' Christopherson, J. A., Carino, D. R., & Ferson, W. E. (2009). Portfolio
+#' performance measurement and benchmarking. McGraw Hill Professional. pp.127-133
+#'
+#' @seealso
+#' The original time series function \code{\link{fitTsfm}} and its generic functions
+#' application.
+#'
+#' @examples
+#' # load data from the database
+#' data(managers)
+#'
+#' # example: Market-timing factors with LS fit
+#' fit <- fitTsfmMT(asset.names=colnames(managers[,(1:6)]),
+#' mkt.name="SP500.TR",rf.name="US.3m.TR",data=managers)
+#' summary(fit)
+#'
+#' @importFrom PerformanceAnalytics checkData
+#' @importFrom robust lmRob step.lmRob
+#' @importFrom leaps regsubsets
+#' @importFrom lars lars cv.lars
+#'
+#' @export
+
+fitTsfmMT <- function(asset.names,mkt.name, rf.name=NULL,
+ data=data, fit.method=c("LS","DLS","Robust"),
+ control=fitTsfm.control(...),...) {
+ if (is.null(mkt.name)){
+ stop("Missing argument: mkt.name has to be specified for market timing model.")
+ }
+
+ # convert data into an xts object and hereafter work with xts objects
+ data.xts <- checkData(data)
+ # convert index to 'Date' format for uniformity
+ time(data.xts) <- as.Date(time(data.xts))
+
+ # extract columns to be used in the time series regression
+ dat.xts <- merge(data.xts[,asset.names], data.xts[,mkt.name])
+ ### After merging xts objects, the spaces in names get converted to periods
+
+ # convert all asset and factor returns to excess return form if specified
+ if (!is.null(rf.name)) {
+ dat.xts <- "[<-"(dat.xts,,vapply(dat.xts, function(x) x-data.xts[,rf.name],
+ FUN.VALUE = numeric(nrow(dat.xts))))
+ }
+
+ # mkt-timing factors: down.market=max(0,Rf-Rm), market.sqd=(Rm-Rf)^2
+
+ down.market <- dat.xts[,mkt.name]
+ down.market[down.market < 0 ] <- 0
+ dat.xts <- merge.xts(dat.xts,down.market)
+ colnames(dat.xts)[dim(dat.xts)[2]] <- "down.market"
+ factor.names <- c(mkt.name,"down.market")
+
+# if("TM" %in% mkt.timing) {
+# market.sqd <- data.xts[,mkt.name]^2
+# dat.xts <- merge(dat.xts, market.sqd)
+# colnames(dat.xts)[dim(dat.xts)[2]] <- "market.sqd"
+# factor.names <- c(factor.names, "market.sqd")
+# }
+
+ fit.Timing <- fitTsfm(asset.names=asset.names,factor.names=factor.names,rf.name=NULL,
+ data=dat.xts,fit.method=fit.method,variable.selection="none",control=control)
+
+return(fit.Timing)
+}
Modified: pkg/FactorAnalytics/R/fitTsfmUpDn.r
===================================================================
--- pkg/FactorAnalytics/R/fitTsfmUpDn.r 2015-03-20 07:33:31 UTC (rev 3621)
+++ pkg/FactorAnalytics/R/fitTsfmUpDn.r 2015-03-20 08:48:00 UTC (rev 3622)
@@ -1,144 +1,144 @@
-#' @title Fit a up and down market factor model using time series regression
-#'
-#' @description This is a wrapper function to fits a up and down market model for one
-#' or more asset returns or excess returns using time series regression.
-#' Users can choose between ordinary least squares-OLS, discounted least
-#' squares-DLS (or) robust regression. An object of class
-#' \code{"tsfmUpDn"} is returned.
-#'
-#' @details
-#' \code{fitTsfmUpDn} will use \code{fitTsfm} to fit a time series model for up and down market respectively. If
-#' risk free rate is provided, the up market is the excess market returns which is no less than 0.
-#' The goal of up and down market model is to capture two different market Betas in the up and down markets.
-#'
-#'
-#' @param asset.names vector containing names of assets, whose returns or
-#' excess returns are the dependent variable.
-#' @param mkt.name name of the column for market returns. It
-#' is required for a up/down market model.
-#' @param rf.name name of the column of risk free rate variable to calculate
-#' excess returns for all assets (in \code{asset.names}) and the market factor (in
-#' \code{mkt.name}). Default is \code{NULL}, and no action is taken.
-#' @param data vector, matrix, data.frame, xts, timeSeries or zoo object
-#' containing column(s) named in \code{asset.names}, \code{factor.names} and
-#' optionally, \code{mkt.name} and \code{rf.name}.
-#' @param fit.method the estimation method, one of "OLS", "DLS" or "Robust".
-#' See details. Default is "OLS".
-#' @param control list of control parameters. The default is constructed by
-#' the function \code{\link{fitTsfm.control}}. See the documentation for
-#' \code{\link{fitTsfm.control}} for details.
-#' @param ... arguments passed to \code{\link{fitTsfm.control}}
-#'
-#' @return
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 3622
More information about the Returnanalytics-commits
mailing list