-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_cmodels.R
More file actions
291 lines (232 loc) · 7.92 KB
/
_cmodels.R
File metadata and controls
291 lines (232 loc) · 7.92 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
### ------------------------------------
### Functions to generate different candidate models (-> F-Signals)
### ------------------------------------
### Decision Trees
cm_dt <- function(x_train,
y_train,
x_pred,
vec_depth,
vec_active,
windows) {
# Set active variables if not provided
if (is.null(vec_active)) {
vec_active <- seq_len(ncol(x_train))
}
# Check for NA-Values
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
stop("DT: NA problem.")
}
# Ensure y_train has a column name "response"
colnames(y_train) <- "response"
# Result-Object: Forecasts
col_names <- apply(expand.grid(vec_depth, windows), 1,
function(x) paste0("TREE_W", x[2], "_D", x[1]))
pred <- matrix(NA, ncol = length(windows) * length(vec_depth), nrow = 1,
dimnames = list(NULL, col_names))
# Number of Observations
n_obs <- nrow(x_train)
# Counter for Models
i <- 1
# Convert Test-Data
test_df <- as.data.frame(x_pred[, vec_active, drop = FALSE])
# Loop over windows and depths to create forecasts
for (win in windows) {
# Expanding Window
win <- ifelse(win == 0, n_obs, win)
# Convert Train-Data
train_df <- as.data.frame(cbind(y_train[(n_obs - win + 1):n_obs, , drop = FALSE],
x_train[(n_obs - win + 1):n_obs, vec_active, drop = FALSE]))
# Loop over Depths
for (d in vec_depth) {
# Fit Model
model <- rpart::rpart(response ~ .,
data = train_df,
method = "anova",
maxdepth = d,
minsplit = 15)
# Predict
pred[i] <- predict(model, test_df)
# Update Counter
i <- i + 1
}
}
# Return
return(pred)
}
### ------------------------------------
### Elastic-Net / Ridge Regression
cm_eln <- function(x_train,
y_train,
x_pred,
vec_alpha,
vec_active,
windows,
ran_st) {
# Set active variables if not provided
if (is.null(vec_active)) {
vec_active <- seq_len(ncol(x_train))
}
# Check for NA-Values
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("ELN: NA problem.")
}
# Result-Object: Forecasts
col_names <- apply(expand.grid(vec_alpha, windows), 1,
function(x) paste0("ELN_W", x[2], "_A", x[1]))
pred <- matrix(NA, ncol = length(windows) * length(vec_alpha), nrow = 1,
dimnames = list(NULL, col_names))
# Number of Observations
n_obs <- nrow(x_train)
# Counter for Models
i <- 1
# Loop over windows and alphas to create forecasts
for (win in windows) {
# Expanding Window
win <- ifelse(win == 0, n_obs, win)
# Train Data
x_train_sub <- x_train[(n_obs - win + 1):n_obs, vec_active, drop = FALSE]
y_train_sub <- y_train[(n_obs - win + 1):n_obs, , drop = FALSE]
# Reproducibility: CV-Foldid
set.seed(ran_st)
foldid <- sample(rep(seq_len(5), length.out = nrow(x_train_sub)))
# Loop over different alphas
for (a in vec_alpha) {
# Fit Model
glm_model <- glmnet::cv.glmnet(x = x_train_sub,
y = y_train_sub,
nfold = 5,
alpha = a,
intercept = TRUE,
standardize = TRUE,
foldid = foldid)
# Predict
pred[i] <- predict(glm_model,
newx = x_pred[, vec_active, drop = FALSE],
s = "lambda.min")
# Update Counter
i <- i + 1
}
}
# Return
return(pred)
}
### ----------------------------------------
### GBM Volatility-Tree (Application: Finance)
cm_gbm <- function(x_train,
y_train,
x_pred,
folds,
ntrees,
learning_rate,
n_cores,
ran_st) {
# Check for NA-Values
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("GBM: NA problem.")
}
# Convert Data
dtrain <- lightgbm::lgb.Dataset(x_train, label = y_train)
# Set up GBM Parameters
param_lgb <- list(boosting = "gbdt",
objective = "regression",
metric = "mse",
num_threads = n_cores,
learning_rate = learning_rate,
force_row_wise = TRUE,
early_stopping = -1,
verbose = -1)
# Cross-Validation
set.seed(ran_st)
model_lgb.cv <- lightgbm::lgb.cv(params = param_lgb,
data = dtrain,
nfold = folds,
nrounds = ntrees,
showsd = FALSE)
# Train Final Model
model_lgb <- lightgbm::lgb.train(params = param_lgb,
data = dtrain,
nrounds = model_lgb.cv$best_iter)
# Predict
pred <- predict(model_lgb, x_pred)
# Return
return(pred)
}
### ------------------------------------
### Combination of DP- and TBL-Model (Application: Finance)
cm_dp_tbl <- function(x_train,
y_train,
x_pred) {
# Check for NA-Values
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("DP-TBL: NA problem.")
}
if (ncol(x_train) != 2) {
rlang::abort("DP-TBL: Number of predictors exceeds 2.")
}
# Add Intercept and Subset
x_train_dp <- cbind(int = 1, x_train[, c("dp"), drop = FALSE])
x_train_tbl <- cbind(int = 1, x_train[, c("tbl"), drop = FALSE])
# Fit Models
model_dp <- stats::.lm.fit(x_train_dp, y_train)
model_tbl <- stats::.lm.fit(x_train_tbl, y_train)
# Predict
pred_dp <- model_dp$coefficients %*% c(1, x_pred[, "dp"])
pred_tbl <- model_tbl$coefficients %*% c(1, x_pred[, "tbl"])
# Combine Predictions
pred <- (pred_dp + pred_tbl) / 2
# Return
return(pred)
}
### ------------------------------------
### Principal Component Regression (Application: Finance)
cm_pcr <- function(x_train,
y_train,
x_pred,
n_comp,
val) {
# Check for NA-Values
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("PCR: NA problem.")
}
# Check Dimensions
if (n_comp > ncol(x_train)) {
rlang::abort("PCR: Number of components exceeds number of predictors.")
}
# Perform PCA
pca_model <- stats::prcomp(x_train, scale. = TRUE, center = TRUE)
# Validate Number of Components with Adjusted R2
if (val) {
# Initialize
max_adj_r2 <- -Inf
best_idx <- 1
# Loop over Components
for (i in seq(n_comp)) {
# Fit Regression
model <- stats::.lm.fit(x = cbind(1, pca_model$x[, 1:i, drop = FALSE]),
y = y_train)
# Calculate adjusted R-squared
y_hat <- model$coefficients[1] +
pca_model$x[, 1:i, drop = FALSE] %*%
model$coefficients[-1]
r2 <- 1 - sum((y_train - y_hat)^2) / sum((y_train - mean(y_train))^2)
adj_r2 <- 1 - (1 - r2) * (nrow(y_train) - 1) / (nrow(y_train) - i - 1)
# Update best Model
if (adj_r2 > max_adj_r2) {
max_adj_r2 <- adj_r2
best_idx <- i
}
}
} else {
# If no validation take fix n_comp
best_idx <- n_comp
}
# Re-Fit Regression
model <- .lm.fit(x = cbind(1, pca_model$x[, 1:best_idx, drop = FALSE]),
y = y_train)
# Project Test Data
x_pred_pca <- predict(pca_model, newdata = x_pred)[, 1:best_idx]
# Predict
pred <- model$coefficients[1] + x_pred_pca %*% model$coefficients[-1]
# Return
return(pred)
}