knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Setup Libraries

library(tswgewrapped)

Load Data

file = system.file("extdata", "USeconomic.csv", package = "tswgewrapped", mustWork = TRUE)
data = read.csv(file, header = TRUE, stringsAsFactors = FALSE, check.names = FALSE)
names(data) = gsub("[(|)]", "", colnames(data))

Get Model Recommendations

lag.max = 10

models = list("AIC None" = list(select = "aic", trend_type = "none", lag.max = lag.max),
              "AIC Trend"    = list(select = "aic", trend_type = "trend", lag.max = lag.max),
              "AIC Both" = list(select = "aic", trend_type = "both", lag.max = lag.max),
              "BIC None" = list(select = "bic", trend_type = "none", lag.max = lag.max),
              "BIC Trend"    = list(select = "bic", trend_type = "trend", lag.max = lag.max),
              "BIC Both" = list(select = "bic", trend_type = "both", lag.max = lag.max))

var_interest = 'logGNP'
mdl_build = ModelBuildMultivariateVAR$new(data = data, var_interest = var_interest,
                                          mdl_list = models, verbose = 1)
mdl_build$summarize_build()
mdl_build$get_recommendations()
mdl_build$build_recommended_models()
# Get only user defined models
# Other options are ony recommended models (subset = 'r') or all models (subset = 'a') 
models = mdl_build$get_final_models(subset = 'u')
names(models)

Setup Models to be compared

#### With sliding ASE = TRUE
for (name in names(models)){
  models[[name]][['sliding_ase']] = TRUE
}

batch_size = 38
n.ahead = 2

Initialize the ModelCompareMultivariateVAR object

#### With n_step.ahead = TRUE (Default)              
mdl_compare = ModelCompareMultivariateVAR$new(data = data, var_interest = var_interest,
                                              mdl_list = models, n.ahead = n.ahead, batch_size = batch_size, verbose = 1)

Compare the models

Get AIC and BIC values (trained on the complete data)

mdl_compare$get_xIC() 

Compare boxplot of ASE values

p = mdl_compare$plot_boxplot_ases()

Statistically Compare the models

mdl_compare$statistical_compare()  

Simple Forecasts (with various options)

p = mdl_compare$plot_simple_forecasts(zoom = 50)  ## Zoom into plot
p = mdl_compare$plot_simple_forecasts(lastn = TRUE, limits = FALSE, zoom = 50)
p = mdl_compare$plot_simple_forecasts(lastn = TRUE, limits = TRUE, zoom = 50)
p = mdl_compare$plot_simple_forecasts(lastn = FALSE, limits = FALSE, zoom = 50)
p = mdl_compare$plot_simple_forecasts(lastn = FALSE, limits = TRUE, zoom = 50)

Plot and compare the forecasts per batch

p = mdl_compare$plot_batch_forecasts(only_sliding = FALSE)

Plot and compare the ASEs per batch

p = mdl_compare$plot_batch_ases() 

Raw Data and Metrics

ASEs = mdl_compare$get_tabular_metrics(ases = TRUE)
print(ASEs)
forecasts = mdl_compare$get_tabular_metrics(ases = FALSE)
print(forecasts)


josephsdavid/tswgewrapped documentation built on July 31, 2020, 9:36 a.m.