From noreply at r-forge.r-project.org Fri May 6 16:14:25 2016 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 6 May 2016 16:14:25 +0200 (CEST) Subject: [Returnanalytics-commits] r4019 - pkg/FactorAnalytics/R Message-ID: <20160506141425.A590E187696@r-forge.r-project.org> Author: pragnya Date: 2016-05-06 16:14:25 +0200 (Fri, 06 May 2016) New Revision: 4019 Modified: pkg/FactorAnalytics/R/fitFfm.R Log: Fixed typo in fitted.ffm Modified: pkg/FactorAnalytics/R/fitFfm.R =================================================================== --- pkg/FactorAnalytics/R/fitFfm.R 2016-04-29 17:21:48 UTC (rev 4018) +++ pkg/FactorAnalytics/R/fitFfm.R 2016-05-06 14:14:25 UTC (rev 4019) @@ -408,7 +408,7 @@ fitted.ffm <- function(object, ...) { # get fitted values for all assets in each time period # transpose and convert into xts/zoo objects - fitted.xts <- checkData(t(sapply(object$asset.fit, fitted))) + fitted.xts <- checkData(t(sapply(object$factor.fit, fitted))) names(fitted.xts) <- object$asset.names return(fitted.xts) } From noreply at r-forge.r-project.org Mon May 9 11:00:27 2016 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 9 May 2016 11:00:27 +0200 (CEST) Subject: [Returnanalytics-commits] r4020 - in pkg/FactorAnalytics: . R Message-ID: <20160509090027.4250E1877F7@r-forge.r-project.org> Author: pragnya Date: 2016-05-09 11:00:26 +0200 (Mon, 09 May 2016) New Revision: 4020 Modified: pkg/FactorAnalytics/DESCRIPTION pkg/FactorAnalytics/R/fitFfm.R Log: Renamed zScore function in fitFfm Modified: pkg/FactorAnalytics/DESCRIPTION =================================================================== --- pkg/FactorAnalytics/DESCRIPTION 2016-05-06 14:14:25 UTC (rev 4019) +++ pkg/FactorAnalytics/DESCRIPTION 2016-05-09 09:00:26 UTC (rev 4020) @@ -1,8 +1,8 @@ Package: factorAnalytics Type: Package Title: Factor Analytics -Version: 2.0.30 -Date: 2016-03-17 +Version: 2.0.31 +Date: 2016-05-09 Author: Eric Zivot, Sangeetha Srinivasan and Yi-An Chen Maintainer: Sangeetha Srinivasan Description: Linear factor model fitting for asset returns (three major types- Modified: pkg/FactorAnalytics/R/fitFfm.R =================================================================== --- pkg/FactorAnalytics/R/fitFfm.R 2016-05-06 14:14:25 UTC (rev 4019) +++ pkg/FactorAnalytics/R/fitFfm.R 2016-05-09 09:00:26 UTC (rev 4020) @@ -231,7 +231,7 @@ } # calculate z-scores looping through all numeric exposures for (i in exposures.num) { - std.expo.num <- by(data=data, INDICES=data[[date.var]], FUN=z.score, + std.expo.num <- by(data=data, INDICES=data[[date.var]], FUN=zScore, i=i, w=w, rob.stats=rob.stats) data[[i]] <- unlist(std.expo.num) } @@ -375,7 +375,7 @@ ## x is a data.frame object, i is a character string and w has same length as x # rob.stats is a logical argument to compute robust location and scale -z.score <- function (x, i, w, rob.stats) { +zScore <- function (x, i, w, rob.stats) { if (rob.stats) { x_bar <- median(w*x[[i]]) (x[[i]] - x_bar)/mad(x[[i]], center=x_bar) From noreply at r-forge.r-project.org Mon May 9 14:31:39 2016 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 9 May 2016 14:31:39 +0200 (CEST) Subject: [Returnanalytics-commits] r4021 - in pkg/FactorAnalytics: . R man Message-ID: <20160509123139.BE90A188168@r-forge.r-project.org> Author: pragnya Date: 2016-05-09 14:31:39 +0200 (Mon, 09 May 2016) New Revision: 4021 Added: pkg/FactorAnalytics/R/predict.ffm.R pkg/FactorAnalytics/man/predict.ffm.Rd Modified: pkg/FactorAnalytics/NAMESPACE pkg/FactorAnalytics/R/predict.tsfm.r pkg/FactorAnalytics/man/predict.tsfm.Rd Log: Updated predict methods for fitFfm and fitTsfm Modified: pkg/FactorAnalytics/NAMESPACE =================================================================== --- pkg/FactorAnalytics/NAMESPACE 2016-05-09 09:00:26 UTC (rev 4020) +++ pkg/FactorAnalytics/NAMESPACE 2016-05-09 12:31:39 UTC (rev 4021) @@ -21,6 +21,7 @@ S3method(plot,sfm) S3method(plot,tsfm) S3method(plot,tsfmUpDn) +S3method(predict,ffm) S3method(predict,sfm) S3method(predict,tsfm) S3method(predict,tsfmUpDn) Added: pkg/FactorAnalytics/R/predict.ffm.R =================================================================== --- pkg/FactorAnalytics/R/predict.ffm.R (rev 0) +++ pkg/FactorAnalytics/R/predict.ffm.R 2016-05-09 12:31:39 UTC (rev 4021) @@ -0,0 +1,67 @@ +#' @title Predicts asset returns based on a fitted fundamental factor model +#' +#' @description S3 \code{predict} method for object of class \code{ffm}. +#' +#' @details The estimated factor returns and potentially new factor exposures +#' are used to predict the asset returns during all dates from the fitted +#' \code{ffm} object. For predictions based on estimated factor returns from a +#' specific period use the \code{pred.date} argument. +#' +#' @param object an object of class \code{ffm} produced by \code{fitFfm}. +#' @param newdata data.frame containing the variables \code{asset.var}, +#' \code{date.var} and the same exact \code{exposure.vars} used in the fitted +#' \code{ffm} object. If omitted, the predictions are based on the data used +#' for the fit. +#' @param pred.date character; unique date used to base the predictions. Should +#' be coercible to class \code{Date} and match one of the dates in the data used +#' in the fiited \code{object}. +#' @param ... optional arguments passed to \code{predict.lm} or +#' \code{predict.lmRob}. +#' +#' @return +#' \code{predict.ffm} produces a N x T matrix of predicted asset returns, where +#' T is the number of time periods and N is the number of assets. T=1 if +#' \code{pred.date} is specified. +#' +#' @author Sangeetha Srinivasan +#' +#' @seealso \code{\link{fitFfm}}, \code{\link{summary.ffm}}, +#' \code{\link[stats]{predict.lm}}, \code{\link[robust]{predict.lmRob}} +#' +#' @examples +#' +#' # Load fundamental and return data +#' data(Stock.df) +#' +#' # fit a fundamental factor model +#' exposure.vars <- c("BOOK2MARKET", "LOG.MARKETCAP") +#' fit <- fitFfm(data=stock, asset.var="TICKER", ret.var="RETURN", +#' date.var="DATE", exposure.vars=exposure.vars) +#' +#' # generate random data +#' newdata <- as.data.frame(unique(stock$TICKER)) +#' newdata$BOOK2MARKET <- rnorm(nrow(newdata)) +#' newdata$LOG.MARKETCAP <- rnorm(nrow(newdata)) +#' pred.fund <- predict(fit, newdata) +#' +#' @method predict ffm +#' @export +#' + +predict.ffm <- function(object, newdata=NULL, pred.date=NULL, ...){ + + if (!is.null(pred.date) && !(pred.date %in% names(object$factor.fit))) { + stop("Invalid args: pred.date must be a character string that matches one + of the dates used in the fit") + } + + if (!is.null(newdata)) { + newdata <- checkData(newdata, method="data.frame") + } + + if (!is.null(pred.date)) { + as.matrix(predict(object$factor.fit[[pred.date]], newdata, ...)) + } else { + sapply(object$factor.fit, predict, newdata, ...) + } +} Modified: pkg/FactorAnalytics/R/predict.tsfm.r =================================================================== --- pkg/FactorAnalytics/R/predict.tsfm.r 2016-05-09 09:00:26 UTC (rev 4020) +++ pkg/FactorAnalytics/R/predict.tsfm.r 2016-05-09 12:31:39 UTC (rev 4021) @@ -12,7 +12,9 @@ #' \code{\link[lars]{predict.lars}} such as \code{mode}. #' #' @return -#' \code{predict.tsfm} produces a vector or a matrix of predictions. +#' \code{predict.tsfm} produces a matrix of return predictions, if all assets +#' have equal history. If not, a list of predicted return vectors of unequal +#' length is produced. #' #' @author Yi-An Chen and Sangeetha Srinivasan #' @@ -34,12 +36,10 @@ #' @export #' -predict.tsfm <- function(object, newdata = NULL, ...){ +predict.tsfm <- function(object, newdata=NULL, ...){ if (missing(newdata) || is.null(newdata)) { - lapply(object$asset.fit, predict, ...) - } else { - newdata <- checkData(newdata, method = "data.frame") - lapply(object$asset.fit, predict, newdata, ...) - } + newdata <- checkData(newdata, method="data.frame") + } + sapply(object$asset.fit, predict, newdata, ...) } Added: pkg/FactorAnalytics/man/predict.ffm.Rd =================================================================== --- pkg/FactorAnalytics/man/predict.ffm.Rd (rev 0) +++ pkg/FactorAnalytics/man/predict.ffm.Rd 2016-05-09 12:31:39 UTC (rev 4021) @@ -0,0 +1,62 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/predict.ffm.R +\name{predict.ffm} +\alias{predict.ffm} +\title{Predicts asset returns based on a fitted fundamental factor model} +\usage{ +\method{predict}{ffm}(object, newdata = NULL, pred.date = NULL, ...) +} +\arguments{ +\item{object}{an object of class \code{ffm} produced by \code{fitFfm}.} + +\item{newdata}{data.frame containing the variables \code{asset.var}, +\code{date.var} and the same exact \code{exposure.vars} used in the fitted +\code{ffm} object. If omitted, the predictions are based on the data used +for the fit.} + +\item{pred.date}{character; unique date used to base the predictions. Should +be coercible to class \code{Date} and match one of the dates in the data used +in the fiited \code{object}.} + +\item{...}{optional arguments passed to \code{predict.lm} or +\code{predict.lmRob}.} +} +\value{ +\code{predict.ffm} produces a N x T matrix of predicted asset returns, where +T is the number of time periods and N is the number of assets. T=1 if +\code{pred.date} is specified. +} +\description{ +S3 \code{predict} method for object of class \code{ffm}. +} +\details{ +The estimated factor returns and potentially new factor exposures +are used to predict the asset returns during all dates from the fitted +\code{ffm} object. For predictions based on estimated factor returns from a +specific period use the \code{pred.date} argument. +} +\examples{ + +# Load fundamental and return data +data(Stock.df) + +# fit a fundamental factor model +exposure.vars <- c("BOOK2MARKET", "LOG.MARKETCAP") +fit <- fitFfm(data=stock, asset.var="TICKER", ret.var="RETURN", + date.var="DATE", exposure.vars=exposure.vars) + +# generate random data +newdata <- as.data.frame(unique(stock$TICKER)) +newdata$BOOK2MARKET <- rnorm(nrow(newdata)) +newdata$LOG.MARKETCAP <- rnorm(nrow(newdata)) +pred.fund <- predict(fit, newdata) + +} +\author{ +Sangeetha Srinivasan +} +\seealso{ +\code{\link{fitFfm}}, \code{\link{summary.ffm}}, +\code{\link[stats]{predict.lm}}, \code{\link[robust]{predict.lmRob}} +} + Modified: pkg/FactorAnalytics/man/predict.tsfm.Rd =================================================================== --- pkg/FactorAnalytics/man/predict.tsfm.Rd 2016-05-09 09:00:26 UTC (rev 4020) +++ pkg/FactorAnalytics/man/predict.tsfm.Rd 2016-05-09 12:31:39 UTC (rev 4021) @@ -17,7 +17,9 @@ \code{\link[lars]{predict.lars}} such as \code{mode}.} } \value{ -\code{predict.tsfm} produces a vector or a matrix of predictions. +\code{predict.tsfm} produces a matrix of return predictions, if all assets +have equal history. If not, a list of predicted return vectors of unequal +length is produced. } \description{ S3 \code{predict} method for object of class \code{tsfm}. It From noreply at r-forge.r-project.org Wed May 11 02:46:53 2016 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Wed, 11 May 2016 02:46:53 +0200 (CEST) Subject: [Returnanalytics-commits] r4022 - in pkg/FactorAnalytics: . R Message-ID: <20160511004653.339C71800AD@r-forge.r-project.org> Author: pragnya Date: 2016-05-11 02:46:52 +0200 (Wed, 11 May 2016) New Revision: 4022 Modified: pkg/FactorAnalytics/DESCRIPTION pkg/FactorAnalytics/R/predict.ffm.R pkg/FactorAnalytics/R/predict.tsfm.r Log: Fixed bug in predict methods Modified: pkg/FactorAnalytics/DESCRIPTION =================================================================== --- pkg/FactorAnalytics/DESCRIPTION 2016-05-09 12:31:39 UTC (rev 4021) +++ pkg/FactorAnalytics/DESCRIPTION 2016-05-11 00:46:52 UTC (rev 4022) @@ -1,8 +1,8 @@ Package: factorAnalytics Type: Package Title: Factor Analytics -Version: 2.0.31 -Date: 2016-05-09 +Version: 2.0.32 +Date: 2016-05-10 Author: Eric Zivot, Sangeetha Srinivasan and Yi-An Chen Maintainer: Sangeetha Srinivasan Description: Linear factor model fitting for asset returns (three major types- Modified: pkg/FactorAnalytics/R/predict.ffm.R =================================================================== --- pkg/FactorAnalytics/R/predict.ffm.R 2016-05-09 12:31:39 UTC (rev 4021) +++ pkg/FactorAnalytics/R/predict.ffm.R 2016-05-11 00:46:52 UTC (rev 4022) @@ -55,13 +55,14 @@ of the dates used in the fit") } - if (!is.null(newdata)) { + if (is.null(newdata)) { + sapply(object$factor.fit, predict, ...) + } else { newdata <- checkData(newdata, method="data.frame") + if (is.null(pred.date)) { + sapply(object$factor.fit, predict, newdata, ...) + } else { + as.matrix(predict(object$factor.fit[[pred.date]], newdata, ...)) + } } - - if (!is.null(pred.date)) { - as.matrix(predict(object$factor.fit[[pred.date]], newdata, ...)) - } else { - sapply(object$factor.fit, predict, newdata, ...) - } } Modified: pkg/FactorAnalytics/R/predict.tsfm.r =================================================================== --- pkg/FactorAnalytics/R/predict.tsfm.r 2016-05-09 12:31:39 UTC (rev 4021) +++ pkg/FactorAnalytics/R/predict.tsfm.r 2016-05-11 00:46:52 UTC (rev 4022) @@ -38,8 +38,10 @@ predict.tsfm <- function(object, newdata=NULL, ...){ - if (missing(newdata) || is.null(newdata)) { + if (is.null(newdata)) { + sapply(object$asset.fit, predict, ...) + } else { newdata <- checkData(newdata, method="data.frame") + sapply(object$asset.fit, predict, newdata, ...) } - sapply(object$asset.fit, predict, newdata, ...) } From noreply at r-forge.r-project.org Mon May 23 16:17:41 2016 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 23 May 2016 16:17:41 +0200 (CEST) Subject: [Returnanalytics-commits] r4023 - in pkg/FactorAnalytics: . R Message-ID: <20160523141741.9BC84183B48@r-forge.r-project.org> Author: pragnya Date: 2016-05-23 16:17:41 +0200 (Mon, 23 May 2016) New Revision: 4023 Modified: pkg/FactorAnalytics/DESCRIPTION pkg/FactorAnalytics/R/fitFfm.R Log: Fixed bug in z.score functionality in fitFfm Modified: pkg/FactorAnalytics/DESCRIPTION =================================================================== --- pkg/FactorAnalytics/DESCRIPTION 2016-05-11 00:46:52 UTC (rev 4022) +++ pkg/FactorAnalytics/DESCRIPTION 2016-05-23 14:17:41 UTC (rev 4023) @@ -1,8 +1,8 @@ Package: factorAnalytics Type: Package Title: Factor Analytics -Version: 2.0.32 -Date: 2016-05-10 +Version: 2.0.33 +Date: 2016-05-23 Author: Eric Zivot, Sangeetha Srinivasan and Yi-An Chen Maintainer: Sangeetha Srinivasan Description: Linear factor model fitting for asset returns (three major types- Modified: pkg/FactorAnalytics/R/fitFfm.R =================================================================== --- pkg/FactorAnalytics/R/fitFfm.R 2016-05-11 00:46:52 UTC (rev 4022) +++ pkg/FactorAnalytics/R/fitFfm.R 2016-05-23 14:17:41 UTC (rev 4023) @@ -383,7 +383,7 @@ x_bar <- mean(w*x[[i]]) n <- length(x[[i]]) # use equal weighted squared deviation about the weighted mean - (x[[i]] - x_bar)/sqrt((x[[i]]-x_bar)^2/(n-1)) + (x[[i]] - x_bar)/sqrt(sum((x[[i]]-x_bar)^2)/(n-1)) } }