[Returnanalytics-commits] r2596 - in pkg/FactorAnalytics: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jul 19 00:07:06 CEST 2013
Author: chenyian
Date: 2013-07-19 00:07:06 +0200 (Fri, 19 Jul 2013)
New Revision: 2596
Modified:
pkg/FactorAnalytics/R/plot.TimeSeriesFactorModel.r
pkg/FactorAnalytics/man/factorModelCovariance.Rd
pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd
pkg/FactorAnalytics/man/fitTimeseriesFactorModel.Rd
pkg/FactorAnalytics/man/plot.TimeSeriesFactorModel.Rd
Log:
modify Rd. file
Modified: pkg/FactorAnalytics/R/plot.TimeSeriesFactorModel.r
===================================================================
--- pkg/FactorAnalytics/R/plot.TimeSeriesFactorModel.r 2013-07-18 21:32:31 UTC (rev 2595)
+++ pkg/FactorAnalytics/R/plot.TimeSeriesFactorModel.r 2013-07-18 22:07:06 UTC (rev 2596)
@@ -14,7 +14,7 @@
#' Contributions to VaR"
#' @param max.show Maximum assets to plot. Default is 6.
#' @param plot.single Plot a single asset of lm class. Defualt is FALSE.
-#' @param fundName Name of the asset to be plotted.
+#' @param asset.name Name of the asset to be plotted.
#' @param which.plot.single integer indicating which plot to create: "none"
#' will create a menu to choose. Defualt is none. 1 = time series plot of
#' actual and fitted values 2 = time series plot of residuals with standard
@@ -37,13 +37,13 @@
#' # plot of all assets and show only first 4 assets.
#' plot(fit.macro,max.show=4)
#' # single plot of HAM1 asset
-#' plot(fit.macro, plot.single=TRUE, fundName="HAM1")
+#' plot(fit.macro, plot.single=TRUE, asset.name="HAM1")
#' }
#'
plot.TimeSeriesFactorModel <-
function(fit.macro,colorset=c(1:12),legend.loc=NULL,
which.plot=c("none","1L","2L","3L","4L","5L","6L","7L"),max.show=6,
- plot.single=FALSE, fundName,which.plot.single=c("none","1L","2L","3L","4L","5L","6L",
+ plot.single=FALSE, asset.name,which.plot.single=c("none","1L","2L","3L","4L","5L","6L",
"7L","8L","9L","10L","11L","12L","13L")) {
require(zoo)
require(PerformanceAnalytics)
@@ -55,7 +55,7 @@
## fit.macro lm object summarizing factor model fit. It is assumed that
## time series date information is included in the names component
## of the residuals, fitted and model components of the object.
- ## fundName charater. The name of the single asset to be ploted.
+ ## asset.name charater. The name of the single asset to be ploted.
## which.plot.single integer indicating which plot to create:
## 1 time series plot of actual and fitted values
## 2 time series plot of residuals with standard error bands
@@ -71,7 +71,10 @@
## 12 CUSUM plot of recursive estimates relative to full sample estimates
## 13 rolling estimates over 24 month window
which.plot.single<-which.plot.single[1]
- fit.lm = fit.macro$asset.fit[[fundName]]
+ if (missing(asset.name) == TRUE) {
+ stop("Neet to specify an asset to plot if plot.single is TRUE.")
+ }
+ fit.lm = fit.macro$asset.fit[[asset.name]]
if (!(class(fit.lm) == "lm"))
stop("Must pass a valid lm object")
@@ -79,7 +82,7 @@
## extract information from lm object
factorNames = colnames(fit.lm$model)[-1]
- fit.formula = as.formula(paste(fundName,"~", paste(factorNames, collapse="+"), sep=" "))
+ fit.formula = as.formula(paste(asset.name,"~", paste(factorNames, collapse="+"), sep=" "))
residuals.z = zoo(residuals(fit.lm), as.Date(names(residuals(fit.lm))))
fitted.z = zoo(fitted(fit.lm), as.Date(names(fitted(fit.lm))))
actual.z = zoo(fit.lm$model[,1], as.Date(rownames(fit.lm$model)))
@@ -104,7 +107,7 @@
switch(which.plot.single,
"1L" = {
## time series plot of actual and fitted values
- plot(actual.z, main=fundName, ylab="Monthly performance", lwd=2, col="black")
+ plot(actual.z, main=asset.name, ylab="Monthly performance", lwd=2, col="black")
lines(fitted.z, lwd=2, col="blue")
abline(h=0)
legend(x="bottomleft", legend=c("Actual", "Fitted"), lwd=2, col=c("black","blue"))
@@ -112,7 +115,7 @@
"2L" = {
## time series plot of residuals with standard error bands
- plot(residuals.z, main=fundName, ylab="Monthly performance", lwd=2, col="black")
+ plot(residuals.z, main=asset.name, ylab="Monthly performance", lwd=2, col="black")
abline(h=0)
abline(h=2*tmp.summary$sigma, lwd=2, lty="dotted", col="red")
abline(h=-2*tmp.summary$sigma, lwd=2, lty="dotted", col="red")
@@ -121,41 +124,41 @@
},
"3L" = {
## time series plot of squared residuals
- plot(residuals.z^2, main=fundName, ylab="Squared residual", lwd=2, col="black")
+ plot(residuals.z^2, main=asset.name, ylab="Squared residual", lwd=2, col="black")
abline(h=0)
legend(x="topleft", legend="Squared Residuals", lwd=2, col="black")
},
"4L" = {
## time series plot of absolute residuals
- plot(abs(residuals.z), main=fundName, ylab="Absolute residual", lwd=2, col="black")
+ plot(abs(residuals.z), main=asset.name, ylab="Absolute residual", lwd=2, col="black")
abline(h=0)
legend(x="topleft", legend="Absolute Residuals", lwd=2, col="black")
},
"5L" = {
## SACF and PACF of residuals
- chart.ACFplus(residuals.z, main=paste("Residuals: ", fundName, sep=""))
+ chart.ACFplus(residuals.z, main=paste("Residuals: ", asset.name, sep=""))
},
"6L" = {
## SACF and PACF of squared residuals
- chart.ACFplus(residuals.z^2, main=paste("Residuals^2: ", fundName, sep=""))
+ chart.ACFplus(residuals.z^2, main=paste("Residuals^2: ", asset.name, sep=""))
},
"7L" = {
## SACF and PACF of absolute residuals
- chart.ACFplus(abs(residuals.z), main=paste("|Residuals|: ", fundName, sep=""))
+ chart.ACFplus(abs(residuals.z), main=paste("|Residuals|: ", asset.name, sep=""))
},
"8L" = {
## histogram of residuals with normal curve overlayed
- chart.Histogram(residuals.z, methods="add.normal", main=paste("Residuals: ", fundName, sep=""))
+ chart.Histogram(residuals.z, methods="add.normal", main=paste("Residuals: ", asset.name, sep=""))
},
"9L" = {
## normal qq-plot of residuals
- chart.QQPlot(residuals.z, envelope=0.95, main=paste("Residuals: ", fundName, sep=""))
+ chart.QQPlot(residuals.z, envelope=0.95, main=paste("Residuals: ", asset.name, sep=""))
},
"10L"= {
## CUSUM plot of recursive residuals
if (as.character(fit.macro$call["fit.method"]) == "OLS") {
cusum.rec = efp(fit.formula, type="Rec-CUSUM", data=fit.lm$model)
- plot(cusum.rec, sub=fundName)
+ plot(cusum.rec, sub=asset.name)
} else
stop("CUMSUM applies only on OLS method")
},
@@ -163,7 +166,7 @@
## CUSUM plot of OLS residuals
if (as.character(fit.macro$call["fit.method"]) == "OLS") {
cusum.ols = efp(fit.formula, type="OLS-CUSUM", data=fit.lm$model)
- plot(cusum.ols, sub=fundName)
+ plot(cusum.ols, sub=asset.name)
} else
stop("CUMSUM applies only on OLS method")
},
@@ -171,7 +174,7 @@
## CUSUM plot of recursive estimates relative to full sample estimates
if (as.character(fit.macro$call["fit.method"]) == "OLS") {
cusum.est = efp(fit.formula, type="fluctuation", data=fit.lm$model)
- plot(cusum.est, functional=NULL, sub=fundName)
+ plot(cusum.est, functional=NULL, sub=asset.name)
} else
stop("CUMSUM applies only on OLS method")
},
@@ -184,7 +187,7 @@
reg.z = zoo(fit.lm$model, as.Date(rownames(fit.lm$model)))
rollReg.z = rollapply(reg.z, FUN=rollReg, fit.formula, width=24, by.column = FALSE,
align="right")
- plot(rollReg.z, main=paste("24-month rolling regression estimates:", fundName, sep=" "))
+ plot(rollReg.z, main=paste("24-month rolling regression estimates:", asset.name, sep=" "))
} else if (as.character(fit.macro$call["fit.method"]) == "DLS") {
decay.factor <- as.numeric(as.character(fit.macro$call["decay.factor"]))
t.length <- 24
@@ -198,10 +201,10 @@
}
reg.z = zoo(fit.lm$model[-length(fit.lm$model)], as.Date(rownames(fit.lm$model)))
factorNames = colnames(fit.lm$model)[c(-1,-length(fit.lm$model))]
- fit.formula = as.formula(paste(fundName,"~", paste(factorNames, collapse="+"), sep=" "))
+ fit.formula = as.formula(paste(asset.name,"~", paste(factorNames, collapse="+"), sep=" "))
rollReg.z = rollapply(reg.z, FUN=rollReg, fit.formula,w, width=24, by.column = FALSE,
align="right")
- plot(rollReg.z, main=paste("24-month rolling regression estimates:", fundName, sep=" "))
+ plot(rollReg.z, main=paste("24-month rolling regression estimates:", asset.name, sep=" "))
}
},
invisible()
Modified: pkg/FactorAnalytics/man/factorModelCovariance.Rd
===================================================================
--- pkg/FactorAnalytics/man/factorModelCovariance.Rd 2013-07-18 21:32:31 UTC (rev 2595)
+++ pkg/FactorAnalytics/man/factorModelCovariance.Rd 2013-07-18 22:07:06 UTC (rev 2596)
@@ -59,6 +59,29 @@
factorModelCovariance(t(sfm.apca.fit$loadings),
var(sfm.apca.fit$factors),sfm.apca.fit$resid.variance)
+
+ # fundamental factor model example
+
+
+ data(stock)
+# there are 447 assets
+exposure.names <- c("BOOK2MARKET", "LOG.MARKETCAP")
+test.fit <- fitFundamentalFactorModel(data=data,exposure.names=exposure.names,
+ datevar = "DATE", returnsvar = "RETURN",
+ assetvar = "TICKER", wls = TRUE,
+ regression = "classic",
+ covariance = "classic", full.resid.cov = FALSE,
+ robust.scale = TRUE)
+
+ # compute return covariance
+ # take beta as latest date input
+ beta.mat.fundm <- subset(data,DATE == "2003-12-31")[,exposure.names]
+ beta.mat.fundm <- cbind(rep(1,447),beta.mat.fundm) # add intercept
+FM return covariance
+ret.cov.fundm <- factorModelCovariance(beta.mat.fundm,test.fit$factor.cov$cov,
+ test.fit$resid.variance)
+ # the result is exactly the same
+test.fit$returns.cov$cov == ret.cov.fundm
}
\author{
Eric Zivot and Yi-An Chen.
Modified: pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd 2013-07-18 21:32:31 UTC (rev 2595)
+++ pkg/FactorAnalytics/man/fitFundamentalFactorModel.Rd 2013-07-18 22:07:06 UTC (rev 2596)
@@ -5,7 +5,7 @@
fitFundamentalFactorModel(data, exposure.names, datevar,
returnsvar, assetvar, wls = TRUE,
regression = "classic", covariance = "classic",
- full.resid.cov = TRUE, robust.scale = FALSE)
+ full.resid.cov = FALSE, robust.scale = FALSE)
}
\arguments{
\item{data}{data.frame, data must have \emph{assetvar},
@@ -44,26 +44,26 @@
asset variable in the data.}
}
\value{
- an S3 object containing \itemize{ \item cov.returns A
+ an S3 object containing \itemize{ \item returns.cov A
"list" object contains covariance information for asset
- returns, includes covariance, mean and eigenvalus. \item
- cov.factor Anobject of class "cov" or "covRob" which
- contains the covariance matrix of the factor returns
- (including intercept). \item cov.resids An object of
- class "cov" or "covRob" which contains the covariance
- matrix of the residuals, if "full.resid.cov" is TRUE.
- NULL if "full.resid.cov" is FALSE. \item resid.variance A
- vector of variances estimated from the OLS 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 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
- function call }
+ returns, includes covariance, mean and eigenvalus. Beta
+ of taken as latest date input. \item factor.cov An object
+ of class "cov" or "covRob" which contains the covariance
+ matrix of the factor returns (including intercept). \item
+ resids.cov An object of class "cov" or "covRob" which
+ contains the covariance matrix of the residuals, if
+ "full.resid.cov" is TRUE. NULL if "full.resid.cov" is
+ FALSE. \item resid.variance A vector of variances
+ estimated from the OLS 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 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 function call }
}
\description{
fit fundamental factor model or cross-sectional time
@@ -98,10 +98,10 @@
robust.scale = TRUE)
names(test.fit)
-test.fit$cov.returns
-test.fit$cov.resids
+test.fit$returns.cov
+test.fit$resids.cov
names(test.fit$cov.factor)
-test.fit$cov.factor$cov
+test.fit$factor.cov$cov
test.fit$factor
test.fit$resid.variance
test.fit$resids
Modified: pkg/FactorAnalytics/man/fitTimeseriesFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTimeseriesFactorModel.Rd 2013-07-18 21:32:31 UTC (rev 2595)
+++ pkg/FactorAnalytics/man/fitTimeseriesFactorModel.Rd 2013-07-18 22:07:06 UTC (rev 2596)
@@ -8,7 +8,7 @@
variable.selection = "none", decay.factor = 0.95,
nvmax = 8, force.in = NULL,
subsets.method = c("exhaustive", "backward", "forward", "seqrep"),
- lars.criteria = c("Cp", "cv"))
+ lars.criteria = "Cp")
}
\arguments{
\item{assets.names}{names of assets returns.}
@@ -57,7 +57,7 @@
\item{lars.criteria}{either choose minimum "Cp": unbiased
estimator of the true rist or "cv" 10 folds
- cross-validation. See detail.}
+ cross-validation. Default is "Cp". See detail.}
}
\value{
an S3 object containing \itemize{ \item{asset.fit}{Fit
@@ -83,11 +83,9 @@
\dontrun{
# load data from the database
data(managers.df)
-ret.assets = managers.df[,(1:6)]
-factors = managers.df[,(7:9)]
-# fit the factor model with OLS
-fit <- fitTimeseriesFactorModel(ret.assets,factors,fit.method="OLS",
- variable.selection="all subsets")
+fit <- fitTimeseriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
+ factors.names=c("EDHEC.LS.EQ","SP500.TR"),
+ data=managers.df,fit.method="OLS")
# summary of HAM1
summary(fit$asset.fit$HAM1)
# plot actual vs. fitted over time for HAM1
Modified: pkg/FactorAnalytics/man/plot.TimeSeriesFactorModel.Rd
===================================================================
--- pkg/FactorAnalytics/man/plot.TimeSeriesFactorModel.Rd 2013-07-18 21:32:31 UTC (rev 2595)
+++ pkg/FactorAnalytics/man/plot.TimeSeriesFactorModel.Rd 2013-07-18 22:07:06 UTC (rev 2596)
@@ -5,7 +5,7 @@
plot.TimeSeriesFactorModel(fit.macro, colorset = c(1:12),
legend.loc = NULL,
which.plot = c("none", "1L", "2L", "3L", "4L", "5L", "6L", "7L"),
- max.show = 6, plot.single = FALSE, fundName,
+ max.show = 6, plot.single = FALSE, asset.name,
which.plot.single = c("none", "1L", "2L", "3L", "4L", "5L", "6L", "7L", "8L", "9L", "10L", "11L", "12L", "13L"))
}
\arguments{
@@ -29,7 +29,7 @@
\item{plot.single}{Plot a single asset of lm class.
Defualt is FALSE.}
- \item{fundName}{Name of the asset to be plotted.}
+ \item{asset.name}{Name of the asset to be plotted.}
\item{which.plot.single}{integer indicating which plot to
create: "none" will create a menu to choose. Defualt is
@@ -54,15 +54,13 @@
\dontrun{
# load data from the database
data(managers.df)
-ret.assets = managers.df[,(1:6)]
-factors = managers.df[,(7:9)]
-# fit the factor model with OLS
-fit <- fitTimeSeriesFactorModel(ret.assets,factors,fit.method="OLS",
- variable.selection="all subsets")
+fit.macro <- fitTimeseriesFactorModel(assets.names=colnames(managers.df[,(1:6)]),
+ factors.names=c("EDHEC.LS.EQ","SP500.TR"),
+ data=managers.df,fit.method="OLS")
# plot of all assets and show only first 4 assets.
plot(fit.macro,max.show=4)
# single plot of HAM1 asset
-plot(fit.macro, plot.single=TRUE, fundName="HAM1")
+plot(fit.macro, plot.single=TRUE, asset.name="HAM1")
}
}
\author{
More information about the Returnanalytics-commits
mailing list