bt: bt

Description Usage Arguments Value Author(s) Examples

View source: R/backtest_svm.R

Description

Function to backtest SVM recession predictions.

Usage

1
bt(mydat, fmla, mylag, best_cost, flip = FALSE)

Arguments

mydat

data.table of rawdata

fmla

model formula used to forecast recessions

mylag

integer number of months to lag the rawdata to avoid look-ahead bias (12 in this example).

best_cost

integer best_cost or NULL, in which case the best_cost will be determined using k-fold cross-validation.

flip

boolean whether to invest with the prediction or inverse to it. Debault is to long the asset when fwdstate is one and not hold it otherwise. If flip = TRUE, go long when the fwdstate is zero and not hold when fwdstate is one.

Value

list containing the following outputs: *error* the percentage of months incorrectly classified, *best_cost* integer value of th best_cost as determined by the k-fold cross validation on the first 14 years of data, *mylag* the number of months the rawdata is lagged before being used by the model, *stats* data.frame of aggregate performance stats generated by PerformanceAnalytics, *calstats* data.frame of calendar-based performance stats generated by PerformanceAnalytics, and *ans* is the predictions, 1 for recession and -1 for no recession.

Author(s)

Roger J. Bos, roger.bos@gmail.com

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
library(svm.recession)
best_cost <- 1120
mylag <- 12
factors <- "vintage|fwdstate|payems|sp500|yld_curve|man_prod"
# Define `fwdstate`
dat[, fwdstate := ifelse(recession==1, 1, -1)]
fmla = formula(fwdstate ~ .)
cols <- grep(factors, names(dat), value = TRUE)
mydat <- dat[, ..cols]
b1 <- bt(mydat = mydat, fmla = fmla, mylag = mylag, best_cost = best_cost, flip = TRUE)
# Model error rate
b1$error
# Investment strategy preformance
b1$stats
# Model best cost
b1$best_cost
# First few predictions
tail(b1$ans)

rogerjbos/svm_recession documentation built on Feb. 23, 2020, 2:49 a.m.