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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon May 9 14:31:39 CEST 2016


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 



More information about the Returnanalytics-commits mailing list