[Returnanalytics-commits] r2437 - in pkg/FactorAnalytics: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jun 26 03:25:55 CEST 2013
Author: chenyian
Date: 2013-06-26 03:25:55 +0200 (Wed, 26 Jun 2013)
New Revision: 2437
Added:
pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r
pkg/FactorAnalytics/R/summary.StatFactorModel.r
pkg/FactorAnalytics/man/predict.FundamentalFactorModel.Rd
pkg/FactorAnalytics/man/summary.StatFactorModel.Rd
Modified:
pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd
Log:
1. add summary.StatFactorModel.Rd and summary.StatFactorModel.r
2. add predict.FundamentalFactorModel.Rd and predict.FundamentalFactorModel.r
3. modify fitFundamentalFactorModel.R
Modified: pkg/FactorAnalytics/R/fitFundamentalFactorModel.R
===================================================================
--- pkg/FactorAnalytics/R/fitFundamentalFactorModel.R 2013-06-25 23:22:51 UTC (rev 2436)
+++ pkg/FactorAnalytics/R/fitFundamentalFactorModel.R 2013-06-26 01:25:55 UTC (rev 2437)
@@ -48,9 +48,9 @@
#' residuals for each asset. If "wls" is TRUE, these are the weights used in
#' the weighted least squares regressions. If "cov = robust" these values are
#' computed with "scale.tau". Otherwise they are computed with "var".
-#' \item factor.rets A "xts" object containing the times series of
+#' \item factors A "xts" object containing the times series of
#' estimated factor returns and intercepts.
-#' \item resids A "xts" object containing the time series of residuals
+#' \item residuals A "xts" object containing the time series of residuals
#' for each asset.
#' \item tstats A "xts" object containing the time series of t-statistics
#' for each exposure.
@@ -142,8 +142,7 @@
if (match(returnsvar, exposure.names, FALSE))
stop(paste(returnsvar, "cannot be used as an exposure."))
- assets = unique(data[,assetvar])
- timedates = as.Date(unique(data[,datevar]))
+
numTimePoints <- length(timedates)
numExposures <- length(exposure.names)
numAssets <- length(assets)
@@ -318,10 +317,12 @@
paste("t", c("(Intercept)", exposures.numeric), sep = "."),
assets)
}
+
+# create matrix for fit
FE.hat.mat <- matrix(NA, ncol = ncols, nrow = numTimePoints,
dimnames = list(as.character(as.Date(as.numeric(names(FE.hat)), origin = "1970-01-01")),
cnames))
- # give each element t names and PERMNO
+ # give each element t names
for (i in 1:length(FE.hat)) {
names(FE.hat[[i]])[1] <- "numCoefs"
nc <- FE.hat[[i]][1]
@@ -401,10 +402,11 @@
cov.factor = Cov.factors,
cov.resids = Cov.resids,
resid.variance = resid.vars,
- factor.rets = f.hat,
- resids = resids,
+ factors = f.hat,
+ residuals = resids,
tstats = tstats,
- call = this.call)
+ call = this.call,
+ data = data)
class(output) <- "FundamentalFactorModel"
return(output)
}
Added: pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r (rev 0)
+++ pkg/FactorAnalytics/R/predict.FundamentalFactorModel.r 2013-06-26 01:25:55 UTC (rev 2437)
@@ -0,0 +1,57 @@
+#' predict method for FundamentalFactorModel object
+#'
+#' Generic function of predict method for fitFundamentalFactorModel.
+#'
+#' @param fit "FundamentalFactorModel" object
+#' @export
+#' @author Yi-An Chen
+#'
+predict.FundamentalFactorModel <- function(fit,newdata){
+
+ # 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]))
+
+ numTimePoints <- length(timedates)
+ numExposures <- length(exposure.names)
+ numAssets <- length(assets)
+
+ f <- fit$factors # T X 3
+ exposure.names <- colnames(f)[-1]
+ beta.all <- data[,c(datevar,assetvar,exposure.names)] # (N * T ) X 4
+
+ 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))
+ }
+ fitted <- fitted[-1,]
+ colnames(fitted) <- assets
+
+ }
+
+ # 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")
+ } else {
+
+
+
+ }
+
+ }
+
+
+}
\ No newline at end of file
Added: pkg/FactorAnalytics/R/summary.StatFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/summary.StatFactorModel.r (rev 0)
+++ pkg/FactorAnalytics/R/summary.StatFactorModel.r 2013-06-26 01:25:55 UTC (rev 2437)
@@ -0,0 +1,24 @@
+#' summary method for StatFactorModel object.
+#'
+#' Generic function of summary method for fitStatisticalFactorModel. It utilizes
+#' function \code{summary.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{summary.lm}
+#' @author Yi-An Chen.
+#' '
+#' @examples
+#' data(stat.fm.data)
+#'.fit <- fitStatisticalFactorModel(sfm.dat,k=2,
+# ckeckData.method="data.frame")
+#'
+#' summary(fit)
+#' @export
+#'
+
+
+summary.StatFactorModel <- function(fit,...){
+ lapply(fit$asset.fit, summary,...)
+}
\ No newline at end of file
Modified: pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd 2013-06-25 23:22:51 UTC (rev 2436)
+++ pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd 2013-06-26 01:25:55 UTC (rev 2437)
@@ -57,9 +57,9 @@
each asset. If "wls" is TRUE, these are the weights used
in the weighted least squares regressions. If "cov =
robust" these values are computed with "scale.tau".
- Otherwise they are computed with "var". \item factor.rets
- A "xts" object containing the times series of estimated
- factor returns and intercepts. \item resids A "xts"
+ Otherwise they are computed with "var". \item factors A
+ "xts" object containing the times series of estimated
+ factor returns and intercepts. \item residuals A "xts"
object containing the time series of residuals for each
asset. \item tstats A "xts" object containing the time
series of t-statistics for each exposure. \item call
Added: pkg/FactorAnalytics/man/predict.FundamentalFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/predict.FundamentalFactorModel.Rd (rev 0)
+++ pkg/FactorAnalytics/man/predict.FundamentalFactorModel.Rd 2013-06-26 01:25:55 UTC (rev 2437)
@@ -0,0 +1,17 @@
+\name{predict.FundamentalFactorModel}
+\alias{predict.FundamentalFactorModel}
+\title{predict method for FundamentalFactorModel object}
+\usage{
+ predict.FundamentalFactorModel(fit, newdata)
+}
+\arguments{
+ \item{fit}{"FundamentalFactorModel" object}
+}
+\description{
+ Generic function of predict method for
+ fitFundamentalFactorModel.
+}
+\author{
+ Yi-An Chen
+}
+
Added: pkg/FactorAnalytics/man/summary.StatFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/summary.StatFactorModel.Rd (rev 0)
+++ pkg/FactorAnalytics/man/summary.StatFactorModel.Rd 2013-06-26 01:25:55 UTC (rev 2437)
@@ -0,0 +1,31 @@
+\name{summary.StatFactorModel}
+\alias{summary.StatFactorModel}
+\title{summary method for StatFactorModel object.}
+\usage{
+ summary.StatFactorModel(fit, ...)
+}
+\arguments{
+ \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{summary.lm}}
+}
+\description{
+ Generic function of summary method for
+ fitStatisticalFactorModel. It utilizes function
+ \code{summary.lm}.
+}
+\examples{
+data(stat.fm.data)
+.fit <- fitStatisticalFactorModel(sfm.dat,k=2,
+
+summary(fit)
+}
+\author{
+ Yi-An Chen. '
+}
+
More information about the Returnanalytics-commits
mailing list