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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jul 22 21:56:01 CEST 2013


Author: chenyian
Date: 2013-07-22 21:56:00 +0200 (Mon, 22 Jul 2013)
New Revision: 2622

Modified:
   pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
   pkg/FactorAnalytics/R/plot.FundamentalFactorModel.r
   pkg/FactorAnalytics/R/plot.StatFactorModel.r
   pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r
   pkg/FactorAnalytics/R/predict.StatFactorModel.r
   pkg/FactorAnalytics/R/predict.TimeSeriesFactorModel.r
   pkg/FactorAnalytics/data/stat.fm.data.RData
   pkg/FactorAnalytics/man/plot.FundamentalFactorModel.Rd
   pkg/FactorAnalytics/man/predict.StatFactorModel.Rd
   pkg/FactorAnalytics/man/predict.TimeSeriesFactorModel.Rd
Log:
debug 

Modified: pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitFundamentalFactorModel.R	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/R/fitFundamentalFactorModel.R	2013-07-22 19:56:00 UTC (rev 2622)
@@ -35,6 +35,8 @@
 #' the data.
 #' @param assetvar A character string giving the name of the asset variable in
 #' the data.
+#' @param exposure.names A character string giving the name of the exposure variable in
+#' the data.
 #' @return an S3 object containing
 #' \itemize{
 #' \item returns.cov A "list" object contains covariance information for
@@ -418,7 +420,8 @@
                    beta = B.final,
                    datevar = datevar,
                    returnsvar = returnsvar,
-                   assetvar = assetvar)
+                   assetvar = assetvar,
+                   exposure.names = exposure.names)
     class(output) <- "FundamentalFactorModel"
     return(output)
 }
\ No newline at end of file

Modified: pkg/FactorAnalytics/R/plot.FundamentalFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/plot.FundamentalFactorModel.r	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/R/plot.FundamentalFactorModel.r	2013-07-22 19:56:00 UTC (rev 2622)
@@ -63,9 +63,9 @@
            
            "1L" = {
             factor.names <- colnames(fit.fund$factors)
-            nn <- length(factor.names)
-            par(mfrow=c(nn,1))
-            for (i in factor.names) {
+#             nn <- length(factor.names)
+            par(mfrow=c(n/2,2))
+            for (i in factor.names[1:n]) {
             plot(fit.fund$factors[,i],main=paste(i," Factor Returns",sep="") )
             }
             par(mfrow=c(1,1))

Modified: pkg/FactorAnalytics/R/plot.StatFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/plot.StatFactorModel.r	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/R/plot.StatFactorModel.r	2013-07-22 19:56:00 UTC (rev 2622)
@@ -135,9 +135,7 @@
       
       fit.lm = fit.stat$asset.fit[[asset.name]]
              
-    if (!(class(fit.lm) == "lm"))
-      stop("Must pass a valid lm object")
-    
+     
     ## exact information from lm object
     
     factorNames = colnames(fit.lm$model)[-1]
@@ -250,8 +248,8 @@
     )
     } else {  #apca method
       
-      dates <- rownames(fit.stat$factors) 
-       actual.z <- zoo(fit.stat$asset.ret,as.Date(dates))
+      dates <- names(fit.stat$data[,asset.name]) 
+       actual.z <- zoo(fit.stat$asset.ret[,asset.name],as.Date(dates))
        residuals.z <- zoo(fit.stat$residuals,as.Date(dates))
        fitted.z <- actual.z - residuals.z
       t <- length(dates)

Modified: pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r	2013-07-22 19:56:00 UTC (rev 2622)
@@ -13,24 +13,38 @@
 #' @export
 #' @author Yi-An Chen
 #' 
-predict.FundamentalFactorModel <- function(fit,newdata,new.assetvar,new.datevar){
+predict.FundamentalFactorModel <- function(fit.fund,newdata,new.assetvar,new.datevar){
  
   # if there is no newdata provided
   # calculate fitted values
-  datevar <- as.character(fit$call)[4]
-  assetvar <- as.character(fit$call)[6]
-  assets = unique(data[,assetvar])
-  timedates = as.Date(unique(data[,datevar]))
-  
+   datevar <- as.character(fit.fund$datevar)
+   assetvar <- as.character(fit.fund$assetvar)
+   assets = unique(fit.fund$data[,assetvar])
+   timedates = as.Date(unique(fit.fund$data[,datevar]))
+   exposure.names <- fit.fund$exposure.names
+   
   numTimePoints <- length(timedates)
   numExposures <- length(exposure.names)
   numAssets <- length(assets)
   
-  f <-  fit$factors # T X 3 
-  exposure.names <- colnames(f)[-1]
+  f <-  fit.fund$factors # T X 3 
+  
  
-  predictor <- function(data,datevar,assetvar) {
-    
+  predictor <- function(data) {
+    fitted <- rep(NA,numAssets)
+    for (i in 1:numTimePoints) {
+      fit.tmp <- fit.fund$beta %*% t(f[i,])
+      fitted <- rbind(fitted,t(fit.tmp))
+    }
+    fitted <- fitted[-1,]
+    colnames(fitted) <- assets
+    return(fitted)
+  } 
+
+  
+  
+  predictor.new <- 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")  
   
@@ -49,7 +63,7 @@
   }
   
   if (missing(newdata) || is.null(newdata)) {
-   ans <- predictor(fit$data,datevar,assetvar)
+   ans <- predictor(fit.fund$data)
  } 
   
   # predict returns by newdata
@@ -65,7 +79,7 @@
   } 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) 
+  ans <- predictor.new(newdata,new.datevar,new.assetvar) 
   }
  }
 

Modified: pkg/FactorAnalytics/R/predict.StatFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/predict.StatFactorModel.r	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/R/predict.StatFactorModel.r	2013-07-22 19:56:00 UTC (rev 2622)
@@ -4,9 +4,7 @@
 #' function \code{predict.lm}.
 #' 
 #' @param fit "StatFactorModel" object created by fitStatisticalFactorModel.
-#' @param newdata An optional data frame in which to look for variables with which to predict. 
-#' If omitted, the fitted values are used.
-#' @param ... Any other arguments used in \code{predict.lm}
+#' @param ... Any other arguments used in \code{predict.lm}. For example like newdata and fit.se. 
 #' @author Yi-An Chen.
 #' ' 
 #' @examples

Modified: pkg/FactorAnalytics/R/predict.TimeSeriesFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/predict.TimeSeriesFactorModel.r	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/R/predict.TimeSeriesFactorModel.r	2013-07-22 19:56:00 UTC (rev 2622)
@@ -4,11 +4,9 @@
 #' function \code{predict.lm}.
 #' 
 #' @param fit "TimeSeriesFactorModel" object created by fitTimeSeiresFactorModel.
-#' @param newdata An optional data frame in which to look for variables with which to predict. 
-#' If omitted, the fitted values are used.
-#' @param ... Any other arguments used in \code{predict.lm}
+#' @param ... Any other arguments used in \code{predict.lm}. for example newdata and se.fit.
 #' @author Yi-An Chen.
-#' ' 
+#' 
 #' @examples
 #' 
 #' # load data from the database
@@ -25,15 +23,17 @@
 #' @export
 #' 
 
-predict.TimeSeriesFactorModel <- function(fit,newdata,...){
-  if (missing(newdata) || is.null(newdata)  ) {
-  lapply(fit$asset.fit, predict,...)
-  } 
+predict.TimeSeriesFactorModel <- function(fit.macro,...){
+#   if (missing(newdata) || is.null(newdata)  ) {
+  lapply(fit.macro$asset.fit, predict,...)
+#   } 
+  
+#   
 #   if (  !(missing(newdata) && !is.null(newdata) )) {
-#    numAssets <- length(names(fit$asset.fit))
+#    numAssets <- length(names(fit.macro$asset.fit))
 #    
-#    data <- fit$data
-#   factors <-   data[,fit$factors.names]
+#    data <- fit.macro$data
+#   factors <-   data[,fit.macro$factors.names]
 #    mu.factors <- apply(factors,2,mean)
 #    cov.factors <- cov(factors)
 #    
@@ -53,6 +53,6 @@
 #     
 #     
 #   }
-#   
   
+  
 }
\ No newline at end of file

Modified: pkg/FactorAnalytics/data/stat.fm.data.RData
===================================================================
(Binary files differ)

Modified: pkg/FactorAnalytics/man/plot.FundamentalFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/plot.FundamentalFactorModel.Rd	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/man/plot.FundamentalFactorModel.Rd	2013-07-22 19:56:00 UTC (rev 2622)
@@ -1,41 +1,46 @@
-\name{plot.FundamentalFactorModel}
-\alias{plot.FundamentalFactorModel}
-\title{plot FundamentalFactorModel object.}
-\usage{
-  plot.FundamentalFactorModel(fund.fit,
-    which.plot = c("none", "1L", "2L", "3L", "4L"),
-    max.show = 12)
-}
-\arguments{
-  \item{fund.fit}{fit object created by
-  fitFundamentalFactorModel.}
-
-  \item{which.plot}{integer indicating which plot to
-  create: "none" will create a menu to choose. Defualt is
-  none. 1 = "factor returns", 2 = "R square", 3 = "Variance
-  of Residuals", 4 = "FM Correlation",}
-
-  \item{max.show}{Maximum assets to plot. Default is 12.}
-}
-\description{
-  Generic function of plot method for
-  fitFundamentalFactorModel.
-}
-\examples{
-\dontrun{
-# BARRA type factor model
-# there are 447 assets
-data(stock)
-assets = unique(fulldata[,"PERMNO"])
-timedates = as.Date(unique(fulldata[,"DATE"]))
-exposures <- exposures.names <- c("BOOK2MARKET", "LOG.MARKETCAP")
-fund.fit <- fitFundamentalFactorModel(fulldata=fulldata, timedates=timedates, exposures=exposures,covariance="classic", assets=assets,full.resid.cov=TRUE,
-                                      regression="classic",wls=TRUE)
-
-plot(fund.fit)
-}
-}
-\author{
-  Eric Zivot and Yi-An Chen.
-}
-
+\name{plot.FundamentalFactorModel}
+\alias{plot.FundamentalFactorModel}
+\title{plot FundamentalFactorModel object.}
+\usage{
+  plot.FundamentalFactorModel(fit.fund,
+    which.plot = c("none", "1L", "2L", "3L", "4L"),
+    max.show = 10)
+}
+\arguments{
+  \item{fit.fund}{fit object created by
+  fitFundamentalFactorModel.}
+
+  \item{which.plot}{integer indicating which plot to
+  create: "none" will create a menu to choose. Defualt is
+  none. 1 = "factor returns", 2 = "R square", 3 = "Variance
+  of Residuals", 4 = "FM Correlation",}
+
+  \item{max.show}{Maximum assets to plot. Default is 12.}
+}
+\description{
+  Generic function of plot method for
+  fitFundamentalFactorModel.
+}
+\examples{
+\dontrun{
+# BARRA type factor model
+# there are 447 assets
+data(stock)
+# BARRA type factor model
+data(stock)
+# there are 447 assets
+exposure.names <- c("BOOK2MARKET", "LOG.MARKETCAP")
+fit.fund <- fitFundamentalFactorModel(data=data,exposure.names=exposure.names,
+                                       datevar = "DATE", returnsvar = "RETURN",
+                                       assetvar = "TICKER", wls = TRUE,
+                                       regression = "classic",
+                                       covariance = "classic", full.resid.cov = TRUE,
+                                       robust.scale = TRUE)
+
+plot(fit.fund)
+}
+}
+\author{
+  Eric Zivot and Yi-An Chen.
+}
+

Modified: pkg/FactorAnalytics/man/predict.StatFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/predict.StatFactorModel.Rd	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/man/predict.StatFactorModel.Rd	2013-07-22 19:56:00 UTC (rev 2622)
@@ -8,11 +8,8 @@
   \item{fit}{"StatFactorModel" object created by
   fitStatisticalFactorModel.}
 
-  \item{newdata}{An optional data frame in which to look
-  for variables with which to predict. If omitted, the
-  fitted values are used.}
-
-  \item{...}{Any other arguments used in \code{predict.lm}}
+  \item{...}{Any other arguments used in \code{predict.lm}.
+  For example like newdata and fit.se.}
 }
 \description{
   Generic function of predict method for

Modified: pkg/FactorAnalytics/man/predict.TimeSeriesFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/predict.TimeSeriesFactorModel.Rd	2013-07-22 19:36:44 UTC (rev 2621)
+++ pkg/FactorAnalytics/man/predict.TimeSeriesFactorModel.Rd	2013-07-22 19:56:00 UTC (rev 2622)
@@ -2,17 +2,14 @@
 \alias{predict.TimeSeriesFactorModel}
 \title{predict method for TimeSeriesModel object.}
 \usage{
-  predict.TimeSeriesFactorModel(fit, newdata, ...)
+  predict.TimeSeriesFactorModel(fit.macro, ...)
 }
 \arguments{
   \item{fit}{"TimeSeriesFactorModel" object created by
   fitTimeSeiresFactorModel.}
 
-  \item{newdata}{An optional data frame in which to look
-  for variables with which to predict. If omitted, the
-  fitted values are used.}
-
-  \item{...}{Any other arguments used in \code{predict.lm}}
+  \item{...}{Any other arguments used in \code{predict.lm}.
+  for example newdata and se.fit.}
 }
 \description{
   Generic function of predict method for
@@ -32,6 +29,6 @@
 predict(fit,newdata,interval="confidence")
 }
 \author{
-  Yi-An Chen. '
+  Yi-An Chen.
 }
 



More information about the Returnanalytics-commits mailing list