[Returnanalytics-commits] r3638 - in pkg/FactorAnalytics: R inst/tests man vignettes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Apr 24 19:40:40 CEST 2015
Author: pragnya
Date: 2015-04-24 19:40:39 +0200 (Fri, 24 Apr 2015)
New Revision: 3638
Added:
pkg/FactorAnalytics/inst/tests/test-fitTsfm.r
Removed:
pkg/FactorAnalytics/inst/tests/test-fitTSFM.r
Modified:
pkg/FactorAnalytics/R/plot.sfm.r
pkg/FactorAnalytics/R/plot.tsfm.r
pkg/FactorAnalytics/man/plot.sfm.Rd
pkg/FactorAnalytics/man/plot.tsfm.Rd
pkg/FactorAnalytics/vignettes/fitSfm_vignette.R
pkg/FactorAnalytics/vignettes/fitSfm_vignette.Rnw
Log:
Fixed minor bug in group plot factor selection using f.sub.
Modified: pkg/FactorAnalytics/R/plot.sfm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.sfm.r 2015-04-22 04:49:25 UTC (rev 3637)
+++ pkg/FactorAnalytics/R/plot.sfm.r 2015-04-24 17:40:39 UTC (rev 3638)
@@ -60,8 +60,10 @@
#' 16 = CUSUM test-LS residuals,\cr
#' 17 = Recursive estimates (RE) test of LS regression coefficients,\cr
#' 18 = Rolling regression over a 24-period observation window
-#' @param f.sub vector of indexes of factors to show for group plots. Default is 1:2.
-#' @param a.sub vector of indexes of assets to show for group plots. Default is 1:6.
+#' @param f.sub numeric/character vector; subset of indexes/names of factors to
+#' include for group plots. Default is 1:2.
+#' @param a.sub numeric/character vector; subset of indexes/names of assets to
+#' include for group plots. At least 2 assets must be selected. Default is 1:6.
#' @param n.top scalar; number of largest and smallest weights to display for
#' each factor mimicking portfolio. Default is 3.
#' @param plot.single logical; If \code{TRUE} plots the characteristics of an
@@ -342,18 +344,17 @@
} else { # start of group asset plots
n <- length(x$asset.names)
- if (n<=1) {
+ if (n<=1 || length(a.sub) < 2) {
stop("Error: Two or more assets required for group plots.")
}
+ if (!(all(a.sub %in% x$asset.names)) && !(all(a.sub %in% 1:n))) {
+ stop("Invalid argument: a.sub is not a valid subset of asset names.")
+ }
k <- x$k
f.names <- paste("F", 1:k, sep = ".")
-
- if (!(all(f.sub %in% f.names) || all(f.sub %in% 1:k))) {
+ if (!(all(f.sub %in% f.names)) && !(all(f.sub %in% 1:k))) {
stop("Invalid argument: f.sub is not a valid subset of factor names.")
}
- if (!(all(a.sub %in% x$asset.names) || all(a.sub %in% 1:n))) {
- stop("Invalid argument: a.sub is not a valid subset of asset names.")
- }
# plot selection
repeat {
@@ -400,10 +401,10 @@
},
"3L" = {
## Estimated factor loadings
- C <- coef(x)[a.sub,]
+ C <- x$loadings[a.sub,f.sub,drop=FALSE]
Y <- row(C, as.factor=T)
- X <- as.vector(C[,(f.sub+1)])
- Z <- col(C[,(f.sub+1)], as.factor=T)
+ X <- as.vector(as.matrix(C[,,drop=FALSE]))
+ Z <- col(C, as.factor=T)
plot(
barchart(Y~X|Z, main="Factor Loadings \n", xlab="", as.table=TRUE,
origin=0, col=colorset[1], scales=list(relation="free"), ...)
@@ -485,13 +486,17 @@
},
"12L" = {
## Factor mimicking portfolio weights - top long and short positions in each factor
- par(mfrow=c(ceiling(length(f.sub)/2),2))
+ if (length(f.sub) < 2) {
+ par(mfrow=c(1,1))
+ } else {
+ par(mfrow=c(ceiling(length(f.sub)/2),2))
+ }
for (i in f.sub) {
main=paste("Largest & smallest weights (%): ", colnames(x$loadings)[i])
s <- summary(x, n.top=n.top)$mimic.sum[[i]]
top <- 100*stack(s[,c(2,4)])$values
names.arg <- stack(s[,c(1,3)])$values
- barplot(top, main=main, names.arg=names.arg, col=colorset[1], las=2, ...)
+ barplot(top, main=main, names.arg=names.arg, col=colorset[1], las=2, cex.main=0.9, ...)
abline(h=0, lwd=1, lty=1, col=1)
}
par(mfrow=c(1,1))
Modified: pkg/FactorAnalytics/R/plot.tsfm.r
===================================================================
--- pkg/FactorAnalytics/R/plot.tsfm.r 2015-04-22 04:49:25 UTC (rev 3637)
+++ pkg/FactorAnalytics/R/plot.tsfm.r 2015-04-24 17:40:39 UTC (rev 3638)
@@ -72,10 +72,10 @@
#' 17 = Recursive estimates (RE) test of LS regression coefficients,\cr
#' 18 = Rolling regression over a 24-period observation window, \cr
#' 19 = Asset returns vs factor returns (single factor model)
-#' @param f.sub subset of indexes of factors to show for group plots.
-#' Default is 1:2.
-#' @param a.sub vector of indexes of assets to show for group plots.
-#' Default is 1:6.
+#' @param f.sub numeric/character vector; subset of indexes/names of factors to
+#' include for group plots. Default is 1:2.
+#' @param a.sub numeric/character vector; subset of indexes/names of assets to
+#' include for group plots. At least 2 assets must be selected. Default is 1:6.
#' @param plot.single logical; If \code{TRUE} plots the characteristics of an
#' individual asset's factor model. The type of plot is given by
#' \code{which}. Default is \code{FALSE}.
@@ -397,16 +397,17 @@
} else { # start of group asset plots
n <- length(x$asset.names)
- if (n<=1) {
+ if (n<=1 || length(a.sub) < 2) {
stop("Error: Two or more assets required for group plots.")
}
- if (!(all(a.sub %in% 1:n)) || length(a.sub) < 2) {
- stop("Invalid argument: a.sub is not a valid subset.")
+ if (!(all(a.sub %in% x$asset.names)) && !(all(a.sub %in% 1:n))) {
+ stop("Invalid argument: a.sub is not a valid subset of asset names.")
}
- k <- ncol(coef(x))-1
- if (!(all(f.sub %in% 1:k))) {
- stop("Invalid argument: f.sub is not a valid subset.")
+ k <- ncol(x$beta)
+ if (!(all(f.sub %in% x$factor.names)) && !(all(f.sub %in% 1:k))) {
+ stop("Invalid argument: f.sub is not a valid subset of factor names.")
}
+
# plot selection
repeat {
if (is.null(which)) {
@@ -434,32 +435,36 @@
plot(
barchart(as.matrix(x$alpha)[a.sub,], main="Factor model Alpha (Intercept)", xlab="", col=colorset[1], ...)
)
-# barplot(coef(x)[a.sub,1], main="Factor model Alpha (Intercept)",
-# names.arg=rownames(coef(x))[a.sub], col=colorset[1], las=2, ...)
-# abline(h=0, lwd=1, lty=1, col=1)
+ # barplot(coef(x)[a.sub,1], main="Factor model Alpha (Intercept)",
+ # names.arg=rownames(coef(x))[a.sub], col=colorset[1], las=2, ...)
+ # abline(h=0, lwd=1, lty=1, col=1)
},
"2L" = {
## Factor model coefficients: Betas
- C <- coef(x)[a.sub,]
+ C <- x$beta[a.sub,f.sub,drop=FALSE]
Y <- row(C, as.factor=T)
- X <- as.vector(as.matrix(C[,(f.sub+1)]))
- Z <- col(C[,(f.sub+1)], as.factor=T)
+ X <- as.vector(as.matrix(C[,,drop=FALSE]))
+ Z <- col(C, as.factor=T)
plot(
- barchart(Y~X|Z, main="Factor model Betas \n", xlab="", as.table=TRUE,
- origin=0, col=colorset[1], scales=list(relation="free"), ...)
+ barchart(Y~X|Z, main="Factor model Betas \n", xlab="", as.table=TRUE,
+ origin=0, col=colorset[1], scales=list(relation="free"), ...)
)
-# par(mfrow=c(ceiling(length(f.sub)/2),2))
-# for (i in f.sub) {
-# main=paste(colnames(coef(x))[i+1], "factor Betas")
-# barplot(coef(x)[,i+1], main=main, names.arg=rownames(coef(x)),
-# col=colorset[1], las=2, ...)
-# abline(h=0, lwd=1, lty=1, col=1)
-# }
-# par(mfrow=c(1,1))
+ # par(mfrow=c(ceiling(length(f.sub)/2),2))
+ # for (i in f.sub) {
+ # main=paste(colnames(coef(x))[i+1], "factor Betas")
+ # barplot(coef(x)[,i+1], main=main, names.arg=rownames(coef(x)),
+ # col=colorset[1], las=2, ...)
+ # abline(h=0, lwd=1, lty=1, col=1)
+ # }
+ # par(mfrow=c(1,1))
},
"3L" = {
## Actual and fitted asset returns
- par(mfrow=c(ceiling(length(a.sub)/2),2))
+ if (length(a.sub) < 5) {
+ par(mfrow=c(length(a.sub),1))
+ } else {
+ par(mfrow=c(ceiling(length(a.sub)/2),2))
+ }
for (i in a.sub) {
asset <- x$asset.names[i]
plotData <- merge.xts(x$data[,asset], fitted(x)[,asset])
@@ -537,7 +542,11 @@
if (length(x$factor.names)>1) {
stop("Error: This option is only for single factor models.")
}
- par(mfrow=c(ceiling(length(a.sub)/2),2))
+ if (length(a.sub) < 5) {
+ par(mfrow=c(length(a.sub),1))
+ } else {
+ par(mfrow=c(ceiling(length(a.sub)/2),2))
+ }
for (i in a.sub) {
fit <- x$asset.fit[[i]]
asset <- x$asset.names[i]
Deleted: pkg/FactorAnalytics/inst/tests/test-fitTSFM.r
===================================================================
--- pkg/FactorAnalytics/inst/tests/test-fitTSFM.r 2015-04-22 04:49:25 UTC (rev 3637)
+++ pkg/FactorAnalytics/inst/tests/test-fitTSFM.r 2015-04-24 17:40:39 UTC (rev 3638)
@@ -1,33 +0,0 @@
-context("Test fitTsfm")
-
-test_that("fitTsfm is as expected", {
-
- fpath <- system.file("extdata", "timeSeriesReturns.csv",
- package="factorAnalytics")
- returns.z <- read.zoo(file=fpath, header=TRUE, sep=",", as.is=TRUE,
- FUN=as.yearmon)
- returns.z <- window(returns.z, start="2008-01-01", end="2012-12-31")
- assets <- names(returns.z)[1:30]
- ex.rets <- returns.z[,assets]-returns.z$rf
- carhart <- returns.z[,c("mktrf","smb","hml","umd")]
-
- # fit Carhart 4-factor model using lm
- ff4 <- lm(ex.rets ~ carhart)
- sum4 = summary(ff4)
- rsq4 <- as.numeric(sapply(X = sum4, FUN = "[", "r.squared"))
- Sigma.F <- var(carhart)
- beta.hat <- coef(ff4)[-1,]
- Sigma.eps <- diag(as.numeric(sapply(X = sum4, FUN = "[", "sigma")))
- Sigma.R <- t(beta.hat) %*% Sigma.F %*% beta.hat + Sigma.eps^2
-
- # fit Carhart 4-factor mode via fitTsfm
- ff.mod <- fitTsfm(asset.names=assets,
- factor.names=c("mktrf","smb","hml","umd"),
- data=cbind(ex.rets,carhart))
-
- # compare beta and r2
- expect_that(as.matrix(ff.mod$beta),is_equivalent_to(t(coef(ff4)[-1,])))
- expect_that(as.numeric(ff.mod$r2),
- equals(as.numeric(sapply(X=sum4, FUN="[", "r.squared"))))
-
-})
Added: pkg/FactorAnalytics/inst/tests/test-fitTsfm.r
===================================================================
--- pkg/FactorAnalytics/inst/tests/test-fitTsfm.r (rev 0)
+++ pkg/FactorAnalytics/inst/tests/test-fitTsfm.r 2015-04-24 17:40:39 UTC (rev 3638)
@@ -0,0 +1,33 @@
+context("Test fitTsfm")
+
+test_that("fitTsfm is as expected", {
+
+ fpath <- system.file("extdata", "timeSeriesReturns.csv",
+ package="factorAnalytics")
+ returns.z <- read.zoo(file=fpath, header=TRUE, sep=",", as.is=TRUE,
+ FUN=as.yearmon)
+ returns.z <- window(returns.z, start="2008-01-01", end="2012-12-31")
+ assets <- names(returns.z)[1:30]
+ ex.rets <- returns.z[,assets]-returns.z$rf
+ carhart <- returns.z[,c("mktrf","smb","hml","umd")]
+
+ # fit Carhart 4-factor model using lm
+ ff4 <- lm(ex.rets ~ carhart)
+ sum4 = summary(ff4)
+ rsq4 <- as.numeric(sapply(X = sum4, FUN = "[", "r.squared"))
+ Sigma.F <- var(carhart)
+ beta.hat <- coef(ff4)[-1,]
+ Sigma.eps <- diag(as.numeric(sapply(X = sum4, FUN = "[", "sigma")))
+ Sigma.R <- t(beta.hat) %*% Sigma.F %*% beta.hat + Sigma.eps^2
+
+ # fit Carhart 4-factor mode via fitTsfm
+ ff.mod <- fitTsfm(asset.names=assets,
+ factor.names=c("mktrf","smb","hml","umd"),
+ data=cbind(ex.rets,carhart))
+
+ # compare beta and r2
+ expect_that(as.matrix(ff.mod$beta),is_equivalent_to(t(coef(ff4)[-1,])))
+ expect_that(as.numeric(ff.mod$r2),
+ equals(as.numeric(sapply(X=sum4, FUN="[", "r.squared"))))
+
+})
\ No newline at end of file
Modified: pkg/FactorAnalytics/man/plot.sfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/plot.sfm.Rd 2015-04-22 04:49:25 UTC (rev 3637)
+++ pkg/FactorAnalytics/man/plot.sfm.Rd 2015-04-24 17:40:39 UTC (rev 3638)
@@ -52,9 +52,11 @@
17 = Recursive estimates (RE) test of LS regression coefficients,\cr
18 = Rolling regression over a 24-period observation window}
-\item{f.sub}{vector of indexes of factors to show for group plots. Default is 1:2.}
+\item{f.sub}{numeric/character vector; subset of indexes/names of factors to
+include for group plots. Default is 1:2.}
-\item{a.sub}{vector of indexes of assets to show for group plots. Default is 1:6.}
+\item{a.sub}{numeric/character vector; subset of indexes/names of assets to
+include for group plots. At least 2 assets must be selected. Default is 1:6.}
\item{n.top}{scalar; number of largest and smallest weights to display for
each factor mimicking portfolio. Default is 3.}
Modified: pkg/FactorAnalytics/man/plot.tsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/plot.tsfm.Rd 2015-04-22 04:49:25 UTC (rev 3637)
+++ pkg/FactorAnalytics/man/plot.tsfm.Rd 2015-04-24 17:40:39 UTC (rev 3638)
@@ -51,11 +51,11 @@
18 = Rolling regression over a 24-period observation window, \cr
19 = Asset returns vs factor returns (single factor model)}
-\item{f.sub}{subset of indexes of factors to show for group plots.
-Default is 1:2.}
+\item{f.sub}{numeric/character vector; subset of indexes/names of factors to
+include for group plots. Default is 1:2.}
-\item{a.sub}{vector of indexes of assets to show for group plots.
-Default is 1:6.}
+\item{a.sub}{numeric/character vector; subset of indexes/names of assets to
+include for group plots. At least 2 assets must be selected. Default is 1:6.}
\item{plot.single}{logical; If \code{TRUE} plots the characteristics of an
individual asset's factor model. The type of plot is given by
Modified: pkg/FactorAnalytics/vignettes/fitSfm_vignette.R
===================================================================
--- pkg/FactorAnalytics/vignettes/fitSfm_vignette.R 2015-04-22 04:49:25 UTC (rev 3637)
+++ pkg/FactorAnalytics/vignettes/fitSfm_vignette.R 2015-04-24 17:40:39 UTC (rev 3638)
@@ -48,7 +48,7 @@
## ----fig.cap="Top 3 largest and smallest weights in the factor mimicking portfolios", fig.width=7, fig.height=4.5, fig.show='asis'----
# Factor mimicking portfolio weights from PCA fit
t(fit.pca$mimic)
-plot(fit.pca, which=12, n.top=3, cex.main=0.9)
+plot(fit.pca, which=12, n.top=3)
## ----fig.cap="Correlations between assets with the top 3 largest and smallest positions in the F.1's factor mimicking portfolio", fig.width=5, fig.height=5, fig.show='asis'----
plot(fit.pca, which=13, f.sub=1, n.top=3)
Modified: pkg/FactorAnalytics/vignettes/fitSfm_vignette.Rnw
===================================================================
--- pkg/FactorAnalytics/vignettes/fitSfm_vignette.Rnw 2015-04-22 04:49:25 UTC (rev 3637)
+++ pkg/FactorAnalytics/vignettes/fitSfm_vignette.Rnw 2015-04-24 17:40:39 UTC (rev 3638)
@@ -186,7 +186,7 @@
<<fig.cap="Top 3 largest and smallest weights in the factor mimicking portfolios", fig.width=7, fig.height=4.5, fig.show='asis'>>=
# Factor mimicking portfolio weights from PCA fit
t(fit.pca$mimic)
-plot(fit.pca, which=12, n.top=3, cex.main=0.9)
+plot(fit.pca, which=12, n.top=3)
@
\newpage
More information about the Returnanalytics-commits
mailing list