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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 25 22:22:57 CEST 2013


Author: chenyian
Date: 2013-06-25 22:22:57 +0200 (Tue, 25 Jun 2013)
New Revision: 2434

Added:
   pkg/FactorAnalytics/R/predict.StatFactorModel.r
   pkg/FactorAnalytics/man/predict.StatFactorModel.Rd
Modified:
   pkg/FactorAnalytics/R/fitStatisticalFactorModel.R
Log:
1. add predict.StatFactorModel.Rd and predict.StatFactorModel.r
2. debug fitStatisticalFactorModel.R

Modified: pkg/FactorAnalytics/R/fitStatisticalFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitStatisticalFactorModel.R	2013-06-25 18:26:00 UTC (rev 2433)
+++ pkg/FactorAnalytics/R/fitStatisticalFactorModel.R	2013-06-25 20:22:57 UTC (rev 2434)
@@ -86,10 +86,9 @@
 
 # check data 
 data.xts <- checkData(data,method=ckeckData.method) 
+data <- coredata(data.xts)
 
-# convert it to coredata
 
-
   
  # function of test
  mfactor.test <- function(data, method = "bn", refine = TRUE, check = FALSE, max.k = NULL, sig = 0.05){
@@ -209,6 +208,7 @@
 		dimnames(data) <- list(1:m, paste("V", 1:n, sep = "."))
 	}
 	data.names <- dimnames(data)[[2]]
+  # demean
 	xc <- t(t(data) - colMeans(data))
 	if(is.null(ret.cov)) {
 		ret.cov <- crossprod(xc)/m
@@ -221,25 +221,43 @@
 	tmp <- data - f %*% B
 	alpha <- colMeans(tmp)
   # compute residuals
-	tmp <- t(t(tmp) - alpha)
-	r2 <- (1 - colSums(tmp^2)/colSums(xc^2))
+	resid <- t(t(tmp) - alpha)
+	r2 <- (1 - colSums(resid^2)/colSums(xc^2))
 	ret.cov <- t(B) %*% var(f) %*% B
 	diag(ret.cov) <- diag(ret.cov) + colSums(tmp^2)/(m - k - 1)
 	dimnames(B) <- list(paste("F", 1:k, sep = "."), data.names)
 	dimnames(f) <- list(dimnames(data)[[1]], paste("F", 1:k, sep = "."))
 	dimnames(ret.cov) <- list(data.names, data.names)
 	names(alpha) <- data.names
+  
+  if (ckeckData.method == "xts" | ckeckData.method == "zoo" ) {
+    f <- xts(f,index(data.xts))
+    resid <- xts(resid,index(data.xts))
+    }
+  
+  
   # create lm list for plot
   reg.list = list()
-  for (i in data.names) {
-    reg.df = as.data.frame(cbind(data[,i],f))
+  if (ckeckData.method == "xts" | ckeckData.method == "zoo" ) {
+    for (i in data.names) {
+      reg.xts = merge(data.xts[,i],f)
+      colnames(reg.xts)[1] <- i
+      fm.formula = as.formula(paste(i,"~", ".", sep=" "))
+      fm.fit = lm(fm.formula, data=reg.xts)
+      reg.list[[i]] = fm.fit
+    }
+      } else {
+    for (i in data.names) {
+    reg.df = as.data.frame(cbind(data[,i],coredata(f)))
     colnames(reg.df)[1] <- i
     fm.formula = as.formula(paste(i,"~", ".", sep=" "))
     fm.fit = lm(fm.formula, data=reg.df)
     reg.list[[i]] = fm.fit
     }
+  }
+  
 	ans <-  list(factors = f, loadings = B, k = k, alpha = alpha, ret.cov = ret.cov,
-	            	r2 = r2, eigen = eigen.tmp$values, residuals=tmp, asset.ret = data,
+	            	r2 = r2, eigen = eigen.tmp$values, residuals=resid, asset.ret = data,
                asset.fit=reg.list)
  
   return(ans)

Added: pkg/FactorAnalytics/R/predict.StatFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/predict.StatFactorModel.r	                        (rev 0)
+++ pkg/FactorAnalytics/R/predict.StatFactorModel.r	2013-06-25 20:22:57 UTC (rev 2434)
@@ -0,0 +1,23 @@
+#' predict method for StatFactorModel object.
+#' 
+#' Generic function of predict method for fitStatisticalFactorModel. It utilizes
+#' function \code{predict.lm}.
+#' 
+#' @param fit "StatFactorModel" 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}
+#' @author Yi-An Chen.
+#' ' 
+#' @examples
+#' data(stat.fm.data)
+#'.fit <- fitStatisticalFactorModel(sfm.dat,k=2,
+#                                   ckeckData.method="data.frame")
+#' 
+#' predict(fit)
+#' @export
+#' 
+
+predict.StatFactorModel <- function(fit,...){
+  lapply(fit$asset.fit, predict,...)
+}
\ No newline at end of file

Added: pkg/FactorAnalytics/man/predict.StatFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/predict.StatFactorModel.Rd	                        (rev 0)
+++ pkg/FactorAnalytics/man/predict.StatFactorModel.Rd	2013-06-25 20:22:57 UTC (rev 2434)
@@ -0,0 +1,31 @@
+\name{predict.StatFactorModel}
+\alias{predict.StatFactorModel}
+\title{predict method for StatFactorModel object.}
+\usage{
+  predict.StatFactorModel(fit, ...)
+}
+\arguments{
+  \item{fit}{"StatFactorModel" 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}}
+}
+\description{
+  Generic function of predict method for
+  fitStatisticalFactorModel. It utilizes function
+  \code{predict.lm}.
+}
+\examples{
+data(stat.fm.data)
+.fit <- fitStatisticalFactorModel(sfm.dat,k=2,
+
+predict(fit)
+}
+\author{
+  Yi-An Chen. '
+}
+



More information about the Returnanalytics-commits mailing list