Skip to content

Commit

Permalink
related to #43, use PerformanceAnalytics for rf, and fix issue with m…
Browse files Browse the repository at this point in the history
…erge.xts
  • Loading branch information
JustinMShea committed Aug 22, 2021
1 parent f0bae8b commit 87b91d5
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions R/fitTsfm.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,6 @@ fitTsfm <- function(asset.names, factor.names, mkt.name=NULL, rf.name=NULL,
if (missing(factor.names) && !is.null(mkt.name)) {
factor.names <- NULL
}
if (length(grep(" ", colnames(data))) > 0) {
stop("Please use syntactically valid column names for continuity with merge.xts.
See 'make.names' function and associated documentation as well as
https://stackoverflow.com/questions/9195718/variable-name-restrictions-in-r")
}

# extract arguments to pass to different fit and variable selection functions
decay <- control$decay
Expand Down Expand Up @@ -213,21 +208,19 @@ https://stackoverflow.com/questions/9195718/variable-name-restrictions-in-r")
time(data.xts) <- as.Date(time(data.xts))

# extract columns to be used in the time series regression
original_names <- c(colnames(data.xts[,asset.names]), colnames(data.xts[,factor.names]))
dat.xts <- merge(data.xts[,asset.names], data.xts[,factor.names])
### After merging xts objects, the spaces in names get converted to periods
colnames(dat.xts) <- original_names

# convert all asset and factor returns to excess return form if specified
if (!is.null(rf.name)) {
dat.xts <- "[<-"(dat.xts,,vapply(dat.xts, function(x) x-data.xts[,rf.name],
FUN.VALUE = numeric(nrow(dat.xts))))
# Note `Return.excess` will modify variable names, so change back
dat.xts.names <- colnames(dat.xts)
dat.xts <- PerformanceAnalytics::Return.excess(R = dat.xts,
Rf = data.xts[ ,rf.name])
colnames(dat.xts) <- dat.xts.names
}

# spaces get converted to periods in colnames of xts object after merge
asset.names <- gsub(" ",".", asset.names, fixed=TRUE)
factor.names <- gsub(" ",".", factor.names, fixed=TRUE)
mkt.name <- gsub(" ",".", mkt.name, fixed=TRUE)
rf.name <- gsub(" ",".", rf.name, fixed=TRUE)

# select procedure based on the variable.selection method
# returns a list of the fitted factor model for all assets
if (variable.selection == "none") {
Expand Down

0 comments on commit 87b91d5

Please sign in to comment.