[Returnanalytics-commits] r3559 - in pkg/FactorAnalytics: . R inst/tests man vignettes
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sat Nov 22 18:58:57 CET 2014
Author: pragnya
Date: 2014-11-22 18:58:56 +0100 (Sat, 22 Nov 2014)
New Revision: 3559
Modified:
pkg/FactorAnalytics/NAMESPACE
pkg/FactorAnalytics/R/fitTsfm.R
pkg/FactorAnalytics/R/fitTsfm.control.R
pkg/FactorAnalytics/inst/tests/test-fitTSFM.r
pkg/FactorAnalytics/man/fitTsfm.control.Rd
pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw
pkg/FactorAnalytics/vignettes/fitTsfm_vignette.pdf
Log:
fitTsfm control internalized. Re-ordered beta in fitTsfm for compatibility
Modified: pkg/FactorAnalytics/NAMESPACE
===================================================================
--- pkg/FactorAnalytics/NAMESPACE 2014-11-21 14:40:20 UTC (rev 3558)
+++ pkg/FactorAnalytics/NAMESPACE 2014-11-22 17:58:56 UTC (rev 3559)
@@ -17,7 +17,6 @@
S3method(summary,tsfm)
export(dCornishFisher)
export(fitTsfm)
-export(fitTsfm.control)
export(fmCov)
export(fmEsDecomp)
export(fmSdDecomp)
Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R 2014-11-21 14:40:20 UTC (rev 3558)
+++ pkg/FactorAnalytics/R/fitTsfm.R 2014-11-22 17:58:56 UTC (rev 3559)
@@ -281,14 +281,18 @@
return(result)
}
- # extract the fitted factor models, coefficients, r2 values and residual vol
- # from returned factor model fits above
+ # extract coefficients from fitted factor models returned above
coef.mat <- makePaddedDataFrame(lapply(reg.list, coef))
alpha <- coef.mat[, 1, drop=FALSE]
# to get alpha of class numeric, do: aplha <- coef.mat[,1]
beta <- coef.mat[, -1, drop=FALSE]
- # reorder the columns to match factor names vector
- beta <- subset(beta, select=factor.names)
+ # reorder and expand columns of beta to match factor.names
+ tmp <- matrix(NA, length(asset.names), length(factor.names))
+ colnames(tmp) <- factor.names
+ rownames(tmp) <- asset.names
+ beta <- merge(beta, tmp, all.x=TRUE, sort=FALSE)[,factor.names]
+ rownames(beta) <- asset.names
+ # extract r2 and residual sd
r2 <- sapply(reg.list, function(x) summary(x)$r.squared)
resid.sd <- sapply(reg.list, function(x) summary(x)$sigma)
# create list of return values.
Modified: pkg/FactorAnalytics/R/fitTsfm.control.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.control.R 2014-11-21 14:40:20 UTC (rev 3558)
+++ pkg/FactorAnalytics/R/fitTsfm.control.R 2014-11-22 17:58:56 UTC (rev 3559)
@@ -114,19 +114,19 @@
#' \code{\link[lars]{cv.lars}}
#'
#' @examples
-#'
+#' \dontrun{
#' # check argument list passed by fitTsfm.control
#' tsfm.ctrl <- fitTsfm.control(method="exhaustive", nvmin=2)
#' print(tsfm.ctrl)
+#' }
#'
-#' # used internally by fitTsfm
+#' # used internally by fitTsfm in the example below
#' data(managers)
#' fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
#' factor.names=colnames(managers[,(7:9)]),
#' data=managers, variable.selection="subsets",
#' method="exhaustive", nvmin=2)
-#'
-#' @export
+#'
fitTsfm.control <- function(decay=0.95, weights, model=TRUE, x=FALSE, y=FALSE,
qr=TRUE, nrep=NULL, scope, scale, direction,
Modified: pkg/FactorAnalytics/inst/tests/test-fitTSFM.r
===================================================================
--- pkg/FactorAnalytics/inst/tests/test-fitTSFM.r 2014-11-21 14:40:20 UTC (rev 3558)
+++ pkg/FactorAnalytics/inst/tests/test-fitTSFM.r 2014-11-22 17:58:56 UTC (rev 3559)
@@ -2,7 +2,6 @@
test_that("fitTsfm is as expected", {
- # fit Carhart 4-factor model using lm
fpath <- system.file("extdata", "timeSeriesReturns.csv",
package="factorAnalytics")
returns.z <- read.zoo(file=fpath, header=TRUE, sep=",", as.is=TRUE,
@@ -11,6 +10,8 @@
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"))
@@ -20,11 +21,11 @@
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))
+ 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"))))
Modified: pkg/FactorAnalytics/man/fitTsfm.control.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTsfm.control.Rd 2014-11-21 14:40:20 UTC (rev 3558)
+++ pkg/FactorAnalytics/man/fitTsfm.control.Rd 2014-11-22 17:58:56 UTC (rev 3559)
@@ -138,11 +138,13 @@
\code{\link[lars]{cv.lars}}.
}
\examples{
+\dontrun{
# check argument list passed by fitTsfm.control
tsfm.ctrl <- fitTsfm.control(method="exhaustive", nvmin=2)
print(tsfm.ctrl)
+}
-# used internally by fitTsfm
+# used internally by fitTsfm in the example below
data(managers)
fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
factor.names=colnames(managers[,(7:9)]),
Modified: pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw
===================================================================
--- pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw 2014-11-21 14:40:20 UTC (rev 3558)
+++ pkg/FactorAnalytics/vignettes/fitTsfm_vignette.Rnw 2014-11-22 17:58:56 UTC (rev 3559)
@@ -164,7 +164,7 @@
\begin{itemize}
\item Variable selection methods \verb"stepwise" and \verb"subsets" can be combined with any of the fit methods, "OLS", "DLS" or "Robust".
\item If variable selection method selected is \verb"lars", \code{fit.method} will be ignored.
-\item Refer to the section on \code{fitTsfm.control} for more details on the control arguments to the different variable selection methods.
+\item Refer to the next section on \code{fitTsfm control} for more details on the control arguments that can be passed to the different variable selection methods.
\end{itemize}
The next example uses the \verb"lars" variable selection method. The default type and criterion used are \verb"lasso" and the \verb"Cp" statistic. The \verb"subsets" variable selection method is demonstrated next for comparison using the same set of factors. However, the best subset of size 4 for each asset is chosen. Figures 3 and 4 display the factor betas from the two fits.
@@ -192,15 +192,11 @@
@
\newpage
-\subsection{fitTsfm.control}
+\subsection{fitTsfm control}
Since \code{fitTsfm} calls many different regression fitting and variable selection methods, it made sense to collect all the optional controls for these functions and process them via \code{fitTsfm.control}. This function is meant to be used internally by \code{fitTsfm} when arguments are passed to it via the ellipsis. The use of control parameters was demonstrated with subset.size in the fit.sub example earlier.
-<<tidy=TRUE>>=
-args(fitTsfm.control)
-@
-
-Here's an ordered list of control parameters passed by \code{fitTsfm} matched with their respective functions for easy reference. See the corresponding help files for more details on each parameter.
+For easy reference, here's a classified list of control parameters accepted and passed by \code{fitTsfm} to their respective model fitting (or) model selection functions in other packages. See the corresponding help files for more details on each parameter.
\begin{itemize}
\item \verb"lm": "weights","model","x","y","qr"
\item \verb"lmRob": "weights","model","x","y","nrep"
@@ -210,13 +206,15 @@
\item \verb"cv.lars": "K","type","normalize","eps","max.steps","trace"
\end{itemize}
-There are 4 other arguments passed to \code{fitTsfm.control} that determine the type of factor model fit chosen.
+There are 3 other significant arguments that can be passed through the \code{...} argument to \code{fitTsfm}.
\begin{itemize}
\item \verb"decay": Determines the decay factor for \code{fit.method="DLS"}, which performs exponentially weighted least squares, with weights adding to unity.
\item \verb"nvmin": The lower limit for the range of subset sizes from which the best model (BIC) is found when performing \verb"subsets" selection. Note that the upper limit was already passed to \verb"regsubsets" function. By specifying \code{nvmin=nvmax}, users can obtain the best model of a particular size (meaningful to those who want a parsimonious model, or to compare with a different model of the same size, or perhaps to avoid over-fitting/ data dredging etc.).
\item \verb"lars.criterion": An option (one of "Cp" or "cv") to assess model selection for the \code{"lars"} variable selection method. "Cp" is Mallow's Cp statistic and "cv" is K-fold cross-validated mean squared prediction error.
\end{itemize}
+\newpage
+
\subsection{Summary, Predict, Coefficients, Fitted values and Residuals}
<<>>=
Modified: pkg/FactorAnalytics/vignettes/fitTsfm_vignette.pdf
===================================================================
(Binary files differ)
More information about the Returnanalytics-commits
mailing list