[Returnanalytics-commits] r3475 - in pkg/FactorAnalytics: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jul 21 12:24:01 CEST 2014
Author: pragnya
Date: 2014-07-21 12:24:00 +0200 (Mon, 21 Jul 2014)
New Revision: 3475
Modified:
pkg/FactorAnalytics/R/fitTsfm.R
pkg/FactorAnalytics/R/fitTsfm.control.R
pkg/FactorAnalytics/man/fitTsfm.Rd
pkg/FactorAnalytics/man/fitTsfm.control.Rd
Log:
Fixed handling of NULL in fitTsfm.control, deleted the mode argument for lars, fixed typos in passing arguments to fitTsfm, added more examples to fitTsfm
Modified: pkg/FactorAnalytics/R/fitTsfm.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.R 2014-07-21 08:10:51 UTC (rev 3474)
+++ pkg/FactorAnalytics/R/fitTsfm.R 2014-07-21 10:24:00 UTC (rev 3475)
@@ -152,6 +152,18 @@
#' colnames(dataToPlot) <- c("Fitted","Actual")
#' chart.TimeSeries(dataToPlot, main="FM fit for HAM1",
#' colorset=c("black","blue"), legend.loc="bottomleft")
+#'
+#' # example using "subsets" variable selection
+#' fit.sub <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
+#' factor.names=colnames(managers[,(7:9)]),
+#' data=managers, variable.selection="subsets",
+#' method="exhaustive", subset.size=2)
+#'
+#' # example using "lars" variable selection and subtracting risk-free rate
+#' fit.lar <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
+#' factor.names=colnames(managers[,(7:9)]),
+#' rf.name="US 3m TR", data=managers,
+#' variable.selection="lars", lars.criterion="cv")
#'
#' @export
@@ -183,7 +195,7 @@
decay <- control$decay
subset.size <- control$subset.size
lars.criterion <- control$lars.criterion
- m1 <- match(c("weights","method","model","x","y","qr"),
+ m1 <- match(c("weights","model","x","y","qr"),
names(control), 0L)
lm.args <- control[m1, drop=TRUE]
m2 <- match(c("weights","model","x","y","nrep"),
@@ -198,7 +210,7 @@
m5 <- match(c("type","normalize","eps","max.steps","trace"),
names(control), 0L)
lars.args <- control[m5, drop=TRUE]
- m6 <- match(c("K","type","mode","normalize","eps","max.steps","trace"),
+ m6 <- match(c("K","type","normalize","eps","max.steps","trace"),
names(control), 0L)
cv.lars.args <- control[m6, drop=TRUE]
@@ -416,10 +428,11 @@
# convert to matrix
reg.mat <- as.matrix(reg.xts)
# fit lars regression model
- lars.fit <- do.call(lars, c(x=list(reg.mat[,-1],y=reg.mat[,i]),lars.args))
+ lars.fit <- do.call(lars, c(list(x=reg.mat[,-1],y=reg.mat[,i]),lars.args))
lars.sum <- summary(lars.fit)
- cv.error <- do.call(cv.lars, c(x=list(reg.mat[,-1],y=reg.mat[,i],
- plot.it=FALSE),cv.lars.args))
+ cv.error <-
+ do.call(cv.lars, c(list(x=reg.mat[,-1],y=reg.mat[,i],plot.it=FALSE,
+ mode="step"),cv.lars.args))
# get the step that minimizes the "Cp" statistic or
# the K-fold "cv" mean-squared prediction error
@@ -439,14 +452,14 @@
beta.names <- names(coef.lars$coefficients)
beta[i, beta.names] <- coef.lars$coefficients
r2[i] <- lars.fit$R2[s]
- resid.sd[i] <- lars.sum$Rss[s]/(nrow(reg.xts)-s)
+ resid.sd[i] <- sqrt(lars.sum$Rss[s]/(nrow(reg.xts)-s))
}
fitted.xts <- do.call(merge, fitted.list)
results.lars <- list(asset.fit=asset.fit, alpha=alpha, beta=beta, r2=r2,
resid.sd=resid.sd, fitted=fitted.xts)
# As a special case for variable.selection="lars", fitted values are also
- # returned by fitTsfm. Else, shrinkage s from the best fit is needed to get
+ # returned by fitTsfm. Else, step s from the best fit is needed to get
# fitted values & residuals.
}
Modified: pkg/FactorAnalytics/R/fitTsfm.control.R
===================================================================
--- pkg/FactorAnalytics/R/fitTsfm.control.R 2014-07-21 08:10:51 UTC (rev 3474)
+++ pkg/FactorAnalytics/R/fitTsfm.control.R 2014-07-21 10:24:00 UTC (rev 3475)
@@ -98,10 +98,6 @@
#' \code{"lars"} method; one of "Cp" or "cv". See details. Default is "Cp".
#' @param K number of folds for computing the K-fold cross-validated mean
#' squared prediction error for \code{"lars"}. Default is 10.
-#' @param mode This refers to the index that is used for cross-validation. The
-#' default is "fraction" for \code{type="lasso"} or
-#' \code{type="forward.stagewise"}. For \code{type="lar"} or
-#' \code{type="stepwise"} the default is "step".
#'
#' @return A list of the above components. This is only meant to be used by
#' \code{fitTsfm}.
@@ -122,7 +118,7 @@
#' @examples
#'
#' # check argument list passed by fitTsfm.control
-#' tsfm.ctrl <- fitTsfm.control(method="exhaustive", nbest=2)
+#' tsfm.ctrl <- fitTsfm.control(method="exhaustive", subset.size=2)
#' print(tsfm.ctrl)
#'
#' # used internally by fitTsfm
@@ -130,7 +126,7 @@
#' fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
#' factor.names=colnames(managers[,(7:9)]),
#' data=managers, variable.selection="subsets",
-#' method="exhaustive", nbest=2)
+#' method="exhaustive", subset.size=2)
#'
#' @export
@@ -140,14 +136,19 @@
force.in=NULL, force.out=NULL, method,
really.big=FALSE, subset.size=1, type,
normalize=TRUE, eps=.Machine$double.eps, max.steps,
- lars.criterion="Cp", K = 10, mode) {
+ lars.criterion="Cp", K = 10) {
# get the user-specified arguments (that have no defaults)
- c <- match.call()
+ call <- match.call()
m <- match(c("weights","scope","scale","direction","method","type",
- "max.steps","mode"), names(c), 0L)
+ "max.steps"), names(call), 0L)
+
# drop unused levels
- result <- as.list(c[m, drop=TRUE])
+ if (!is.null(call) && sum(m>0) == 0) {
+ args <- list()
+ } else {
+ args <- as.list(call[m, drop=TRUE])
+ }
# check input validity for some of the arguments
if (decay<=0 || decay>1) {
@@ -184,11 +185,11 @@
}
# return list of arguments with defaults if they are unspecified
- result <- c(result, list(decay=decay, model=model, x=x, y=y, qr=qr,
- nrep=nrep, trace=trace, steps=steps, k=k,
- nbest=nbest, nvmax=nvmax, force.in=force.in,
- force.out=force.out, really.big=really.big,
- subset.size=subset.size, normalize=normalize,
- eps=eps, lars.criterion=lars.criterion, K=K))
+ result <- c(args, list(decay=decay, model=model, x=x, y=y, qr=qr, nrep=nrep,
+ trace=trace, steps=steps, k=k, nbest=nbest,
+ nvmax=nvmax, force.in=force.in, force.out=force.out,
+ really.big=really.big, subset.size=subset.size,
+ normalize=normalize, eps=eps,
+ lars.criterion=lars.criterion, K=K))
return(result)
}
Modified: pkg/FactorAnalytics/man/fitTsfm.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTsfm.Rd 2014-07-21 08:10:51 UTC (rev 3474)
+++ pkg/FactorAnalytics/man/fitTsfm.Rd 2014-07-21 10:24:00 UTC (rev 3475)
@@ -158,6 +158,18 @@
colnames(dataToPlot) <- c("Fitted","Actual")
chart.TimeSeries(dataToPlot, main="FM fit for HAM1",
colorset=c("black","blue"), legend.loc="bottomleft")
+
+# example using "subsets" variable selection
+fit.sub <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
+ factor.names=colnames(managers[,(7:9)]),
+ data=managers, variable.selection="subsets",
+ method="exhaustive", subset.size=2)
+
+# example using "lars" variable selection and subtracting risk-free rate
+fit.lar <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
+ factor.names=colnames(managers[,(7:9)]),
+ rf.name="US 3m TR", data=managers,
+ variable.selection="lars", lars.criterion="cv")
}
\author{
Eric Zivot, Yi-An Chen and Sangeetha Srinivasan.
Modified: pkg/FactorAnalytics/man/fitTsfm.control.Rd
===================================================================
--- pkg/FactorAnalytics/man/fitTsfm.control.Rd 2014-07-21 08:10:51 UTC (rev 3474)
+++ pkg/FactorAnalytics/man/fitTsfm.control.Rd 2014-07-21 10:24:00 UTC (rev 3475)
@@ -8,7 +8,7 @@
trace = FALSE, steps = 1000, k = 2, nbest = 1, nvmax = 8,
force.in = NULL, force.out = NULL, method, really.big = FALSE,
subset.size = 1, type, normalize = TRUE, eps = .Machine$double.eps,
- max.steps, lars.criterion = "Cp", K = 10, mode)
+ max.steps, lars.criterion = "Cp", K = 10)
}
\arguments{
\item{decay}{a scalar in (0, 1] to specify the decay factor for "DLS".
@@ -107,11 +107,6 @@
\item{K}{number of folds for computing the K-fold cross-validated mean
squared prediction error for \code{"lars"}. Default is 10.}
-
-\item{mode}{This refers to the index that is used for cross-validation. The
-default is "fraction" for \code{type="lasso"} or
-\code{type="forward.stagewise"}. For \code{type="lar"} or
-\code{type="stepwise"} the default is "step".}
}
\value{
A list of the above components. This is only meant to be used by
@@ -145,7 +140,7 @@
}
\examples{
# check argument list passed by fitTsfm.control
-tsfm.ctrl <- fitTsfm.control(method="exhaustive", nbest=2)
+tsfm.ctrl <- fitTsfm.control(method="exhaustive", subset.size=2)
print(tsfm.ctrl)
# used internally by fitTsfm
@@ -153,7 +148,7 @@
fit <- fitTsfm(asset.names=colnames(managers[,(1:6)]),
factor.names=colnames(managers[,(7:9)]),
data=managers, variable.selection="subsets",
- method="exhaustive", nbest=2)
+ method="exhaustive", subset.size=2)
}
\author{
Sangeetha Srinivasan
More information about the Returnanalytics-commits
mailing list