-
Notifications
You must be signed in to change notification settings - Fork 2
/
nonLinear_master.R
132 lines (111 loc) · 4.97 KB
/
nonLinear_master.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File: nonLinear_master.R
# Author: Tyler Pike
# Date: 7/30/2019
# Note(s): Master script to set common experiment enviroment and control what scripts are run
#reset space
rm(list = ls())
#set working directory
setwd('~/Research/CanMachinesBeatTheAverage/')
#load in libraries
library(policyPlot) # charting (FRB specific library)
library(forecast) # time series foreasting
library(caret) # ML estimation
library(glmnet) # Lasso estimation
library(pROC) # AUC
library(tsfeatures) # time series characteristics
library(readxl) # read excel files
library(tidyverse) # general cleaning
library(lubridate) # date cleaning
library(doParallel) # parallel backend
library(foreach) # parallel backend
# set random seed
set.seed(11161995)
# version number
version = '5'
# declare which experiments to run
# USA variables
createTSmodels_usa = TRUE
createFFORMA_usa = FALSE
createCombo_usa = FALSE
createAnalysis_usa = FALSE
createTsAppendix_usa = FALSE
# EU variables
createCombo_euro = FALSE
createAnalysis_euro = FALSE
#----------------------------------------
# create forecast version folder
#----------------------------------------
# instantiate version folder if missing
directory = paste0('./Data/Forecasts/US/version_',version)
if(!dir.exists(directory)){
dir.create(directory)
}
#----------------------------------------
# create data helper functions
# (must be run)
#----------------------------------------
# instantiate data functions
source('./Scripts/nonLinear_data_functions.R')
#----------------------------------------
# create time series forecasts
#----------------------------------------
# desciption: generates several deterministic time-series model forecasts of US macroeconomic variables
# notes: this should only need to be done once, as they are deterministic.
# all forecasts begin in 1970:Q1 with data starting in ~1965:Q3
# input: pulls source data from FRED to forecast
# output: produces a forecast csv in ./Data/Forecasts, per target
if(createTSmodels_usa == TRUE){
system('Rscript ./Scripts/nonLinear_timeSeries_forecasts.R')
}
#----------------------------------------
# train FFORMA machine
#----------------------------------------
# desciption: trains and saves an instance of the FFORMA machine on a subset of M4 competition data
# notes: this should only need to be done once; very computationally expensive
# input: pulls source data from FRED to forecast
# output: produces a forecast csv in ./Data/Forecasts, per target
if(createFFORMA_usa == TRUE){
source('./Scripts/nonLinear_FFORMA_training.R')
}
#----------------------------------------
# create forecast combinations
#----------------------------------------
# description: creates a time series of forecast combinations based on US macro series
# input: a forecast csv in ./Data/Forecasts, per target (see createTSmodels_usa)
# a pre-trained FFORMA model (see FFORMA_training.R)
# output: produces new file for each new version declared
# csv of weighted forecaasts in ./Data/Forecasts/USA/ called derivedForecasts,
if(createCombo_usa == TRUE){
source('./Scripts/nonLinear_metaLearner_functions_usa.R')
source('./Scripts/nonLinear_fforma_forecasts.R')
}
# description: creates a time series of forecast combinations based on Euro SPF
# input: takes in a csv of the euro spf individual forecasts, found in ./Data/EuroSPF/
# output: produces new file for each new version declared
# csv of weighted forecaasts in ./Data/Forecasts/Euro called derivedForecasts,
if(createCombo_euro == TRUE){
source('./Scripts/nonLinear_metaLearner_functions_euro.R')
}
#-----------------------------------------------
# analyze unconditional forecasts
#-----------------------------------------------
# description; generate tables and charts of forecast combination results for US macro series
# input: takes in a csv for the individual forecast combination techniques at each specific horizon
# output: a forecast combinations chart as /Evaluation/UsaComparison/ForecastCharts/usaForecastChart_shaded.pdf
# a series of result tables in /Evaluation/UsaComparison/
if(createAnalysis_usa == TRUE){
source('./Scripts/nonLinear_forecast_analysis_usa.R')
}
# description; generates a table of individual time series forecsating model results for US macro series
# input: takes in a csv for the time series forecasts at each specific horizon
# output: a single results table in /Evaluation/UsaComparison/
if(createTsAppendix_usa == TRUE){
source('./Scripts/nonLinear_timeSeriesforecast_appendix_usa.R')
}
# description; generate tables and charts of forecast combination results for Euro SPF series
# input: takes in a csv for the individual forecast combination techniques at each specific horizon
# output: a forecast combinations chart as /Evaluation/EuroComparison/ForecastCharts/euroForecastChart_shaded.pdf
# a series of result tables in /Evaluation/EuroComparison/
if(createAnalysis_euro == TRUE){
source('./Scripts/nonLinear_forecast_analysis_euro.R')
}