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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jun 26 20:38:20 CEST 2013


Author: chenyian
Date: 2013-06-26 20:38:19 +0200 (Wed, 26 Jun 2013)
New Revision: 2448

Modified:
   pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r
   pkg/FactorAnalytics/man/predict.FundamentalFactorModel.Rd
Log:


Modified: pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r	2013-06-26 17:37:39 UTC (rev 2447)
+++ pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r	2013-06-26 18:38:19 UTC (rev 2448)
@@ -1,12 +1,19 @@
 #' predict method for FundamentalFactorModel object
 #'
 #' Generic function of predict method for fitFundamentalFactorModel.
+#' 
+#' newdata must be data.frame and contians date variable, asset variable and exact
+#' exposures names that are used in fit object by \code{fitFundamentalFactorModel}  
 #'
 #' @param fit "FundamentalFactorModel" object 
+#' @param newdata An optional data frame in which to look for variables with which to predict. 
+#'                If omitted, the fitted values are used. 
+#' @param new.assetvar specify new asset variable in newdata if newdata is provided.
+#' @param new.datevar  speficy new date variable in newdata if newdata is provided.                
 #' @export
 #' @author Yi-An Chen
 #' 
-predict.FundamentalFactorModel <- function(fit,newdata){
+predict.FundamentalFactorModel <- function(fit,newdata,new.assetvar,new.datevar){
  
   # if there is no newdata provided
   # calculate fitted values
@@ -21,37 +28,46 @@
   
   f <-  fit$factors # T X 3 
   exposure.names <- colnames(f)[-1]
+ 
+  predictor <- function(data,datevar,assetvar) {
+    
   beta.all <- data[,c(datevar,assetvar,exposure.names)] #  (N * T ) X 4
+  names(beta.all)[1:2] <- c("time","assets.names")  
   
-  if (missing(newdata) || is.null(newdata)) {
-   #
   ### calculated fitted values
-  # 
-  
+   
   fitted <- rep(NA,numAssets)
   for (i in 1:numTimePoints) {
-  beta <- subset(beta.all, DATE == index(f)[i])[,exposure.names]
-  beta <- as.matrix(cbind(rep(1,numAssets),beta))
-  fit.tmp <- beta %*% t(f[i,])
-  fitted <- rbind(fitted,t(fit.tmp))
+    beta <- subset(beta.all, time == index(f)[i] & assets.names == assets)[,exposure.names]
+    beta <- as.matrix(cbind(rep(1,numAssets),beta))
+    fit.tmp <- beta %*% t(f[i,])
+    fitted <- rbind(fitted,t(fit.tmp))
   }
   fitted <- fitted[-1,]
   colnames(fitted) <- assets
+  return(fitted)
+  }
   
+  if (missing(newdata) || is.null(newdata)) {
+   ans <- predictor(fit$data,datevar,assetvar)
  } 
   
   # predict returns by newdata
  if (!missing(newdata) && !is.null(newdata))  {
-   # check if newdata has the same data points as beta
-  if (dim(newdata) != c(numAssets*numTimePoints,numExposures)) {
-    stop("Dimension of newdata has to match mAssets*numTimePoints,numExposures")
+  # check if newdata has the same datevar and assetvar 
+ if (class(newdata) != "data.frame"){
+   stop("newdata has to be data.frame.")
+ } else if ( length(setdiff(unique(newdata$new.assetvar),assets))!= 0 ){
+   stop("newAssetvar must have the same assets as assetvar")
+ }   # check if newdata has the same data points as beta
+else  if (dim(newdata)[1] != numAssets*numTimePoints ) {
+    stop("length of newdata has to match numAssets*numTimePoints")
+  } else if( length(setdiff(intersect(names(newdata),exposure.names),exposure.names))!=0 ) {
+  stop("newdata must have exact the same exposure.names")    
   } else {
-    
-    
-    
+  ans <- predictor(newdata,new.datevar,new.assetvar) 
   }
- 
  }
 
-  
+return(ans)  
 }
\ No newline at end of file

Modified: pkg/FactorAnalytics/man/predict.FundamentalFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/predict.FundamentalFactorModel.Rd	2013-06-26 17:37:39 UTC (rev 2447)
+++ pkg/FactorAnalytics/man/predict.FundamentalFactorModel.Rd	2013-06-26 18:38:19 UTC (rev 2448)
@@ -2,15 +2,31 @@
 \alias{predict.FundamentalFactorModel}
 \title{predict method for FundamentalFactorModel object}
 \usage{
-  predict.FundamentalFactorModel(fit, newdata)
+  predict.FundamentalFactorModel(fit, newdata,
+    new.assetvar, new.datevar)
 }
 \arguments{
   \item{fit}{"FundamentalFactorModel" object}
+
+  \item{newdata}{An optional data frame in which to look
+  for variables with which to predict.  If omitted, the
+  fitted values are used.}
+
+  \item{new.assetvar}{specify new asset variable in newdata
+  if newdata is provided.}
+
+  \item{new.datevar}{speficy new date variable in newdata
+  if newdata is provided.}
 }
 \description{
   Generic function of predict method for
   fitFundamentalFactorModel.
 }
+\details{
+  newdata must be data.frame and contians date variable,
+  asset variable and exact exposures names that are used in
+  fit object by \code{fitFundamentalFactorModel}
+}
 \author{
   Yi-An Chen
 }



More information about the Returnanalytics-commits mailing list