-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_fmodels.R
More file actions
1006 lines (826 loc) · 29.9 KB
/
_fmodels.R
File metadata and controls
1006 lines (826 loc) · 29.9 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### ----------------------------------------
### Functions for Forecasting Models (-> Benchmark Models)
### ----------------------------------------
# (Prevailing) Historical Mean (PHM)
phm <- function(y, window_size, mean_type = "rolling") {
# Check NA
if (any(is.na(y))) {
rlang::abort("PHM: NA problem.")
}
# Calculate Rolling or Expanding Mean
if (mean_type == "rolling") {
preds <- dplyr::lag(roll::roll_mean(y, width = window_size, min_obs = 1), n = 1)
} else if (mean_type == "expanding") {
preds <- dplyr::lag(roll::roll_mean(y, width = nrow(y), min_obs = 1), n = 1)
} else {
stop("Invalid mean_type. Use 'rolling' or 'expanding'.")
}
# Rolling Variance
vari <- dplyr::lag(roll::roll_var(y, width = window_size, min_obs = 1), n = 1)
# Return
return(list(pred = preds, var = vari))
}
### ----------------------------------------
# AR-Model (OLS)
ar_model <- function(x_train,
y_train,
x_pred,
mlags) {
# Check NA
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("AR: NA problem.")
}
# Split Data
x_train_ar <- cbind(int = 1, x_train[, c(1:mlags), drop = FALSE])
x_pred_ar <- cbind(int = 1, x_pred[, c(1:mlags), drop = FALSE])
# Fit AR-Model
model_ar <- stats::.lm.fit(x_train_ar, y_train)
# Predict
pred_ar <- x_pred_ar %*% model_ar$coefficients
# Return
return(pred_ar)
}
### ----------------------------------------
# Relaxed Lasso
relasso_model <- function(x_train,
y_train,
x_pred,
folds,
ran_st) {
# Check NA
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("Relax: NA problem.")
}
# Reproducibility: CV-Fold-ID
set.seed(ran_st)
foldid <- sample(rep(seq_len(folds), length.out = nrow(x_train)))
# Cross-Validation for Lambda and Gamma
rela_model <- glmnet::cv.glmnet(x = x_train,
y = y_train,
nfold = folds,
alpha = 1,
intercept = TRUE,
standardize = TRUE,
relax = TRUE,
foldid = foldid)
# Predict
pred_rlass <- predict(rela_model,
newx = x_pred,
s = "lambda.min",
gamma = "gamma.min")
# Return
return(pred_rlass)
}
### ----------------------------------------
# XGBoost
xgb_model <- function(x_train,
y_train,
x_pred,
folds,
ntrees,
learning_rate,
target_number,
n_cores,
ran_st) {
# Check NA
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("XGB: NA problem.")
}
# Check Targeting
if (!is.null(target_number)) {
if (target_number > ncol(x_train)) {
rlang::abort("XGB: Target number exceeds number of predictors.")
}
}
# Targeting
if (!is.null(target_number)) {
# Fit Lasso-Model
model_lasso <- glmnet::glmnet(x = x_train,
y = y_train,
alpha = 1,
intercept = TRUE,
standardize = TRUE,
nlambda = 750)
# Get Target Lambda
target_lam_idx <- max(which(model_lasso$df <= target_number))
target_lam <- model_lasso$lambda[target_lam_idx]
# Get Target Predictors
target_preds <- which(as.vector(coef(model_lasso, s = target_lam))[-1] != 0)
if (length(target_preds) == 0 || is.null(target_preds)) {
rlang::abort("XGB: No valid target predictors found.")
}
} else {
target_preds <- seq_len(ncol(x_train))
}
# Convert Data
dtrain <- xgboost::xgb.DMatrix(x_train[, target_preds, drop = FALSE],
label = y_train)
# Set up XGBoost Parameters
param_xgb <- list(booster = "gbtree",
objective = "reg:squarederror",
eta = learning_rate,
nthread = n_cores)
# Cross-Validation
set.seed(ran_st)
model_xgb.cv <- xgboost::xgb.cv(params = param_xgb,
data = dtrain,
nfold = folds,
nrounds = ntrees,
early_stopping_rounds = floor(0.2 * ntrees),
verbose = 0,
showsd = FALSE)
# Train Final Model
model_xgb <- xgboost::xgb.train(params = param_xgb,
data = dtrain,
nrounds = model_xgb.cv$best_iter,
verbose = 0)
# Predict
pred_xgb <- predict(model_xgb, x_pred[, target_preds, drop = FALSE])
# Return
return(pred_xgb)
}
### ----------------------------------------
# Multiple Additive Regression Trees with Dropouts (LightGBM)
dart_model <- function(x_train,
y_train,
x_pred,
folds,
ntrees,
learning_rate,
drop_rate,
target_number,
n_cores,
ran_st) {
# Check NA
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("DART: NA problem.")
}
# Check Targeting
if (!is.null(target_number)) {
if (target_number > ncol(x_train)) {
rlang::abort("DART: Target number exceeds number of predictors.")
}
}
# Targeting
if (!is.null(target_number)) {
# Fit Lasso-Model
model_lasso <- glmnet::glmnet(x = x_train,
y = y_train,
alpha = 1,
intercept = TRUE,
standardize = TRUE,
nlambda = 750)
# Get Target Lambda
target_lam_idx <- max(which(model_lasso$df <= target_number))
target_lam <- model_lasso$lambda[target_lam_idx]
# Get Target Predictors
target_preds <- which(as.vector(coef(model_lasso, s = target_lam))[-1] != 0)
if (length(target_preds) == 0 || is.null(target_preds)) {
rlang::abort("DART: No valid target predictors found.")
}
} else {
target_preds <- seq_len(ncol(x_train))
}
# Convert Data
dtrain <- lightgbm::lgb.Dataset(x_train[, target_preds, drop = FALSE],
label = y_train)
# Set up GBM Parameters
param_lgb <- list(boosting = "dart",
objective = "regression",
metric = "mse",
data_sample_strategy = "bagging",
subsample_freq = 0,
subsample = 1.0,
feature_frac = 1.0,
enable_bundle = TRUE,
num_threads = n_cores,
learning_rate = learning_rate,
drop_rate = drop_rate,
early_stopping = 0,
num_leaves = 31,
max_depth = -1,
min_samples_leaf = 20,
force_row_wise = FALSE,
fore_col_wise = TRUE)
# Cross-Validation
set.seed(ran_st)
model_lgb.cv <- lightgbm::lgb.cv(params = param_lgb,
data = dtrain,
nfold = folds,
nrounds = ntrees,
showsd = FALSE,
verbose = -1)
# Train Final Model
model_lgb <- lightgbm::lgb.train(params = param_lgb,
data = dtrain,
nrounds = model_lgb.cv$best_iter,
verbose = -1)
# Predict
pred <- predict(model_lgb, x_pred[, target_preds, drop = FALSE])
# Return
return(pred)
}
### ----------------------------------------
# Targeted Random Forest
trf_model <- function(x_train,
y_train,
x_pred,
target_number,
ntrees,
max_depth,
n_cores,
ran_st) {
# Check
if (!is.null(target_number)) {
#if (target_number > nrow(x_train)) {
# rlang::abort("TRF: Target number exceeds number of observations.")
#}
if (target_number > ncol(x_train)) {
rlang::abort("TRF: Target number exceeds number of predictors.")
}
}
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("TRF: NA problem.")
}
# Targeting
if (!is.null(target_number)) {
# Fit Lasso-Model
model_lasso <- glmnet::glmnet(x = x_train,
y = y_train,
alpha = 1,
intercept = TRUE,
standardize = TRUE,
nlambda = 750)
# Get Target Lambda
target_lam_idx <- max(which(model_lasso$df <= target_number))
target_lam <- model_lasso$lambda[target_lam_idx]
# Get Target Predictors
target_preds <- which(as.vector(coef(model_lasso, s = target_lam))[-1] != 0)
if (length(target_preds) == 0 || is.null(target_preds)) {
rlang::abort("TRF: No valid target predictors found.")
}
} else {
target_preds <- seq_len(ncol(x_train))
}
# Train Model
model_trf <- ranger::ranger(y = y_train,
x = x_train[, target_preds, drop = FALSE],
num.trees = ntrees,
mtry = floor(sqrt(length(target_preds))),
min.node.size = 5,
min.bucket = 1,
max.depth = max_depth,
sample.fraction = 1,
oob.error = FALSE,
node.stats = FALSE,
num.threads = n_cores,
seed = ran_st)
# Predict
pred_trf <- predict(model_trf, x_pred[, target_preds, drop = FALSE])$predictions #nolint
# Return
return(pred_trf)
}
### ----------------------------------------
# (Targeted) Complete Subset
csr_model <- function(x_train,
y_train,
x_pred,
target_number,
subset_length,
upper_bound,
sampling) {
# Check
if (!is.null(target_number)) {
if (target_number < subset_length) {
rlang::abort("CSR: Target number below subset length.")
}
if (target_number > nrow(x_train)) {
rlang::abort("CSR: Target number exceeds number of observations.")
}
if (target_number > ncol(x_train)) {
rlang::abort("CSR: Target number exceeds number of predictors.")
}
if (!sampling && target_number > 20) {
rlang::abort("CSR: Sampling should be used for more than 20 predictors.")
}
}
if (any(is.na(x_train)) || any(is.na(y_train)) || any(is.na(x_pred))) {
rlang::abort("TRT: NA problem.")
}
if (sampling && is.null(upper_bound)) {
rlang::abort("CSR: No upper bound given for sampling.")
}
if (!sampling && is.null(target_number) && ncol(x_train) > 20) {
rlang::abort("CSR: Sampling should be used for more than 20 predictors.")
}
# Targeting
if (!is.null(target_number)) {
# Fit Lasso-Model
model_lasso <- glmnet::glmnet(x = x_train,
y = y_train,
alpha = 1,
intercept = TRUE,
standardize = TRUE,
nlambda = 750)
# Get Target Lambda
target_lam_idx <- max(which(model_lasso$df <= target_number))
target_lam <- model_lasso$lambda[target_lam_idx]
# Get Target Predictors
target_preds <- which(as.vector(coef(model_lasso, s = target_lam))[-1] != 0)
if (length(target_preds) == 0 || is.null(target_preds)) {
rlang::abort("CSR: No valid target predictors found.")
}
} else {
target_preds <- seq_len(ncol(x_train))
}
# If no Sampling
if (!sampling) {
# Get all Combinations of Length subset_length
comb <- combn(target_preds, subset_length)
# Set up Vector
pred_csr <- rep(NA, ncol(comb))
# Create Subset Forecasts
for (u in seq_len(ncol(comb))) {
# Select Predictors
x_train_ss <- cbind(int = 1, x_train[, comb[, u], drop = FALSE])
x_pred_ss <- cbind(int = 1, x_pred[, comb[, u], drop = FALSE])
# Fit Model
model_ss <- stats::.lm.fit(x_train_ss, y_train)
# Predict
pred_csr[u] <- x_pred_ss %*% model_ss$coefficients
}
# If Sampling
} else if (sampling) {
# Set up Vector
pred_csr <- rep(NA, upper_bound)
# Create Subset Forecasts
for (u in seq_len(upper_bound)) {
# Sample Predictors
set.seed(u)
idx <- sample(target_preds, subset_length)
# Select Predictors
x_train_ss <- cbind(int = 1, x_train[, idx, drop = FALSE])
x_pred_ss <- cbind(int = 1, x_pred[, idx, drop = FALSE])
# Fit Model
model_ss <- stats::.lm.fit(x_train_ss, y_train)
# Predict
pred_csr[u] <- model_ss$coefficients %*% x_pred_ss[,]
}
} else {
rlang::abort("CSR: Sampling problem.")
}
# Compute Aggregate Forecast
pred <- mean(pred_csr)
# Return
return(pred)
}
### ----------------------------------------
### STSC Variants
# STSC-Sx - Using LASSO so select X forecasts ###
stscsx <- function(y,
X,
Ext_F,
lambda_grid,
kappa_grid,
init,
bias,
n_cores,
window_size,
target_number) {
# TVC-Forecasts
results_tvc <- hdflex::tvc(y,
X,
Ext_F,
init,
lambda_grid,
kappa_grid,
bias)
# Assign Result
forecast_tvc <- results_tvc$Forecasts$Point_Forecasts
variance_tvc <- results_tvc$Forecasts$Variance_Forecasts
# Remove
rm(list = c("results_tvc"))
# Get first Non-Na-Row
first_complete <- which(complete.cases(forecast_tvc))[1]
non_na_idx <- seq(first_complete, nrow(forecast_tvc))
adj <- first_complete - 1
# Subset Data
y_sub <- y[non_na_idx, , drop = FALSE]
forecast_tvc_sub <- forecast_tvc[non_na_idx, , drop = FALSE]
variance_tvc_sub <- variance_tvc[non_na_idx, , drop = FALSE]
# Remove
rm(list = c("forecast_tvc", "variance_tvc"))
# Set Time-Sequence
win <- window_size - 1
t_start <- win
t_end <- nrow(y_sub) - 1
t_seq <- seq(t_start, t_end)
# Result-Object: Prediction-Variance-Matrix
res <- matrix(NA, nrow = nrow(y), ncol = 2,
dimnames = list(rownames(y), c("STSCSx", "STSCSx_VAR")))
# Open Parallel Backend
cl <- parallel::makeCluster(n_cores)
doParallel::registerDoParallel(cl)
# Parallel Loop over time
res[t_seq + adj + 1, ] <- foreach::foreach(t = t_seq,
.combine = "rbind",
.packages = c("glmnet")) %dopar% {
# Set Training-Data
x_train <- forecast_tvc_sub[(t - win + 1):t, , drop = FALSE]
y_train <- y_sub[(t - win + 1):t, , drop = FALSE]
# Step 1: Candidate Model Selection (Lasso)
model_lasso <- glmnet::glmnet(x = x_train,
y = y_train,
alpha = 1,
intercept = TRUE,
standardize = FALSE,
nlambda = 750)
# Get Target Lambda
target_lam_idx <- max(which(model_lasso$df <= target_number))
target_lam <- model_lasso$lambda[target_lam_idx]
# Get Target Predictors
target_preds <- which(as.vector(coef(model_lasso, s = target_lam))[-1] != 0)
n_target <- length(target_preds)
if (n_target == 0 || is.null(target_preds)) {
rlang::abort("STSC-Sx: No valid target predictors found.")
}
# Step 2: Equal Weight Combination (Logarithmic Combination)
weights <- rep(1.0 / n_target, n_target)
variance_agg <- 1.0 / sum(weights / variance_tvc_sub[t + 1, target_preds])
forecast_agg <- sum(weights * forecast_tvc_sub[t+1, target_preds] / variance_tvc_sub[t+1, target_preds]) * variance_agg #nolint
# Return
return(cbind(forecast_agg, variance_agg))
}
# Close Parallel Backend
parallel::stopCluster(cl)
# Return
return(res)
}
### ----------------------------------------
# STSC-SFLEX - Using LASSO to dynamically select forecasts ###
stscsflex <- function(y,
X,
Ext_F,
lambda_grid,
kappa_grid,
init,
bias,
n_cores,
window_size,
folds) {
# TVC-Forecasts
results_tvc <- hdflex::tvc(y,
X,
Ext_F,
init,
lambda_grid,
kappa_grid,
bias)
# Assign Result
forecast_tvc <- results_tvc$Forecasts$Point_Forecasts
variance_tvc <- results_tvc$Forecasts$Variance_Forecasts
# Remove
rm(list = c("results_tvc"))
# Get first Non-Na-Row
first_complete <- which(complete.cases(forecast_tvc))[1]
non_na_idx <- seq(first_complete, nrow(forecast_tvc))
adj <- first_complete - 1
# Subset Data
y_sub <- y[non_na_idx, , drop = FALSE]
forecast_tvc_sub <- forecast_tvc[non_na_idx, , drop = FALSE]
variance_tvc_sub <- variance_tvc[non_na_idx, , drop = FALSE]
# Remove
rm(list = c("forecast_tvc", "variance_tvc"))
# PHM-Model
results_phm <- hdflex::tvc(y,
matrix(rep(0, length(y))),
NULL,
init,
lambda_grid,
kappa_grid,
bias)
# Assign Result
forecast_phm_sub <- results_phm$Forecasts$Point_Forecasts[non_na_idx, , drop = FALSE]
variance_phm_sub <- results_phm$Forecasts$Variance_Forecasts[non_na_idx, , drop = FALSE]
# Remove
rm(list = c("results_phm"))
# Set Time-Sequence
win <- window_size - 1
t_start <- win
t_end <- nrow(y_sub) - 1
t_seq <- seq(t_start, t_end)
# Result-Object: Prediction-Variance-Matrix
res <- matrix(NA, nrow = nrow(y), ncol = 2,
dimnames = list(rownames(y), c("STSCSFLEX", "STSCSFLEX_VAR")))
# Open Parallel Backend
cl <- parallel::makeCluster(n_cores)
doParallel::registerDoParallel(cl)
# Parallel Loop over time
res[t_seq + adj + 1, ] <- foreach::foreach(t = t_seq,
.combine = "rbind",
.packages = c("glmnet")) %dopar% {
# Set Training-Data
x_train <- forecast_tvc_sub[(t - win + 1):t, , drop = FALSE]
y_train <- y_sub[(t - win + 1):t, , drop = FALSE]
# Reproducibility: CV-Foldid
set.seed(t)
foldid <- sample(rep(seq_len(folds), length.out = nrow(x_train)))
# Step1: Candidate Model Selection (Lasso)
glm_model <- glmnet::cv.glmnet(x = x_train,
y = y_train,
nfold = folds,
alpha = 1,
intercept = TRUE,
standardize = FALSE,
foldid = foldid)
# Step 2: Equal Weight Combination (Logarithmic Combination)
target_cf <- which(as.vector(coef(glm_model, s = "lambda.min"))[-1] != 0)
n_target <- length(target_cf)
if (n_target == 0) {
weights <- rep(1.0 / ncol(forecast_phm_sub), ncol(forecast_phm_sub))
variance_agg <- 1.0 / sum(weights / variance_phm_sub[t + 1, ])
forecast_agg <- sum(weights * forecast_phm_sub[t + 1, ] / variance_phm_sub[t + 1, ]) * variance_agg
} else if (n_target > 0) {
weights <- rep(1.0 / n_target, n_target)
variance_agg <- 1.0 / sum(weights / variance_tvc_sub[t + 1, target_cf])
forecast_agg <- sum(weights * forecast_tvc_sub[t+1, target_cf] / variance_tvc_sub[t+1, target_cf]) * variance_agg #nolint
} else {
rlang::abort("STSC-SFLEX: No valid candidate models found.")
}
# Return
return(cbind(forecast_agg, variance_agg))
}
# Close Parallel Backend
parallel::stopCluster(cl)
# Return
return(res)
}
### ----------------------------------------
# Prinipal Component Dynamic Model Averaging
pcdma <- function(y,
X,
Ext_F,
window_size,
n_comp,
alpha,
lambda,
kappa,
n_cores,
exl_pca = NULL) {
### Check
if (nrow(y) != nrow(X)) {
rlang::abort("PC-DMA: Number of observations in y and X do not match.")
}
if (window_size > nrow(y)) {
rlang::abort("PC-DMA: Window size exceeds number of observations.")
}
if (n_comp > window_size) {
rlang::abort("PC-DMA: Number of Components exceeds Window Size.")
}
# Response-Name
colnames(y) <- "response"
# Combine Signals and Remove NA-Values
S_sub <- na.omit(cbind(if (exists("X")) X,
if (exists("Ext_F")) Ext_F))
# Remove
rm(list = c("X", "Ext_F"))
### Check
if (ncol(S_sub) < 15) {
rlang::abort("PC-DMA: Simple DMA would be better suited.")
}
if (n_comp > ncol(S_sub)) {
rlang::abort("PC-DMA: Number of Components exceeds number of predictors.")
}
# Separate predictors for PCA and those to be added directly to DMA
if (!is.null(exl_pca)) {
S_pca <- S_sub[, -exl_pca, drop = FALSE]
S_exl <- S_sub[, exl_pca, drop = FALSE]
} else {
S_pca <- S_sub
S_exl <- NULL
}
# Get first non-NA-row
first_complete <- nrow(y) - nrow(S_sub) + 1
non_na_idx <- seq(first_complete, nrow(y))
# Remove
rm(list = c("S_sub"))
# Subset Response
y_sub <- y[non_na_idx, , drop = FALSE]
# Result-Object: Prinipal Components
res_pca_sub <- matrix(NA, ncol = n_comp, nrow = nrow(y_sub),
dimnames = list(rownames(y_sub),
paste0("PC", seq_len(n_comp))))
# Initial PCA-Projection
row_idx <- seq(window_size - 1)
pca_model <- stats::prcomp(S_pca[row_idx, , drop = FALSE],
center = TRUE,
scale. = TRUE)
res_pca_sub[row_idx, ] <- pca_model$x[, seq_len(n_comp), drop = FALSE]
# Remove
rm(list = c("pca_model", "row_idx"))
# Time Sequence
t_start <- window_size
t_end <- nrow(y_sub)
t_seq <- seq(t_start, t_end)
# Open Parallel Backend
cl <- parallel::makeCluster(n_cores)
doParallel::registerDoParallel(cl)
# Project Signals for every t
res_pca_sub[t_seq, ] <- foreach::foreach(t = t_seq,
.combine = "rbind",
.packages = c("stats")) %dopar% {
# Get Training-Data
s_train <- S_pca[(t - window_size + 1):t, , drop = FALSE]
# Fit PCA
pca_model <- stats::prcomp(s_train, center = TRUE, scale. = TRUE)
# Return
return(pca_model$x[window_size, seq_len(n_comp), drop = FALSE])
}
# Close Parallel Backend
parallel::stopCluster(cl)
# Combine PCA results with excluded predictors
if (!is.null(S_exl)) {
combined_data <- cbind(y_sub, S_exl, res_pca_sub)
} else {
combined_data <- cbind(y_sub, res_pca_sub)
}
# Remove
rm(list = c("res_pca_sub", "S_exl"))
# Set up DMA-Result Matrix
res_pcdma <- matrix(NA, ncol = 4, nrow = nrow(y),
dimnames = list(rownames(y),
c("PC_DMA_MU",
"PC_DMA_PLL",
"PC_DMA_VAR",
"PC_DMA_CRPS")))
# Fit DMA Model
model <- eDMA::DMA(formula = response ~ .,
data = combined_data,
dAlpha = alpha,
vDelta = lambda,
vKeep = NULL,
bZellnerPrior = FALSE,
dG = 100,
bParallelize = FALSE,
iCores = 1,
dBeta = kappa)
# Continuous-Ranked-Probability-Score
crps_score <- compute_crps(model, y_sub, lambda)
# Assign estimated Quantities
res_pcdma[non_na_idx[-1], 1] <- model@Est$vyhat[-1]
res_pcdma[non_na_idx[-1], 2] <- model@Est$vLpdfhat[-1]
res_pcdma[non_na_idx[-1], 3] <- model@Est$vtotal[-1]
res_pcdma[non_na_idx[-1], 4] <- crps_score
# Remove
rm(list = c("model", "crps_score", "y_sub"))
# Cut Init-Period
res_pcdma[non_na_idx[1:window_size], ] <- NA
# Return
return(res_pcdma)
}
### ----------------------------------------
# Dynamic Model Averaging
dma <- function(y,
X,
Ext_F,
alpha,
lambda,
kappa) {
### Check
if (nrow(y) != nrow(X)) {
rlang::abort("DMA: Number of observations in y and X do not match.")
}
# Response-Name
colnames(y) <- "response"
# Combine Signals and Remove NA-Values
S_sub <- na.omit(cbind(if (exists("X")) X,
if (exists("Ext_F")) Ext_F))
# Check
if (ncol(S_sub) > 20) {
rlang::abort("DMA: Use PC-DMA instead.")
}
# Get first non-NA-row
first_complete <- nrow(y) - nrow(S_sub) + 1
non_na_idx <- seq(first_complete, nrow(y))
# Subset Response
y_sub <- y[non_na_idx, , drop = FALSE]
# Set up Result Matrix
res_dma <- matrix(NA, ncol = 4, nrow = nrow(y),
dimnames = list(rownames(y),
c("PC_DMA_MU",
"PC_DMA_PLL",
"PC_DMA_VAR",
"PC_DMA_CRPS")))
# Fit DMA Model
model <- eDMA::DMA(formula = response ~ .,
data = cbind(y_sub, S_sub),
dAlpha = alpha,
vDelta = lambda,
vKeep = NULL,
bZellnerPrior = FALSE,
dG = 100,
bParallelize = FALSE,
iCores = 1,
dBeta = kappa)
# Continuous-Ranked-Probability-Score
crps_score <- compute_crps(model, y_sub, lambda)
# Assign estimated Quantities
res_dma[non_na_idx[-1], 1] <- model@Est$vyhat[-1]
res_dma[non_na_idx[-1], 2] <- model@Est$vLpdfhat[-1]
res_dma[non_na_idx[-1], 3] <- model@Est$vtotal[-1]
res_dma[non_na_idx[-1], 4] <- crps_score
# Remove
rm(list = c("model", "crps_score", "y_sub"))
# Return
return(res_dma)
}
######## ----------------------------------------
### Continuous Ranked Probability Score (CRPS) Function
# Function to compute the CRPS
crps <- function(obs, mu, sig, df, dis) {
"
For reference see:
http://journals.ametsoc.org/doi/pdf/10.1175/MWR2904.1
https://CRAN.R-project.org/package=scoringRules
"
# Check Distribution
if (dis == "t") {
if (is.null(df)) {
rlang::abort("Invalid Degrees of Freedom")
}
}
# Convert
obs <- as.numeric(obs)
mu <- as.numeric(mu)
sig <- as.numeric(sig)
# Check Dimensions
if (length(obs) != length(mu) || length(obs) != length(sig)) {
rlang::abort("Invalid Dimensions")
}
# Normal or Students t-Distribution
if (dis == "normal") {
# Standardized obs
z <- (obs - mu) / sig
# PDF evaluated at the normalized predition error
pdf <- dnorm(z, 0.0, 1.0)
# CDF evaluated at the normalized predition error
cdf <- pnorm(z, 0.0, 1.0)
# Inverse of pi
pi_inv <- 1.0 / sqrt(pi)
# Compute Continuous Ranked Probability Score
crps <- sig * (z * (2.0 * cdf - 1.0) + 2.0 * pdf - pi_inv)
} else if (dis == "t") {
### For formula see: 10.32614/CRAN.package.scoringRules
# Scale Conversion
s <- sig * sqrt((df - 2.0) / df)
# Standardized obs
z <- (obs - mu) / s
# PDF evaluated at the normalized predition error
pdf <- dt(z, df)
# CDF evaluated at the normalized predition error
cdf <- pt(z, df)
# Beta-Function
bfrac <- beta(0.5, df - 0.5) / beta(0.5, 0.5 * df) ** 2.0
# Compute Continuous Ranked Probability Score
crps <- s * (
z * (2.0 * cdf - 1.0) +
2.0 / (df - 1.0) *
(pdf * (df + z ** 2.0) - sqrt(df) * bfrac)
)
} else {
stop("Invalid Distribution")
}
# Return
return(list(crps = crps, average_score = mean(crps)))
}
### ----------------------------------------
### Function for eDMA::DMA to calculate CRPS
compute_crps <- function(model, y_sub, lambda) {
# Continuous-Ranked-Probability-Score
Reduce("+", lapply(seq_along(lambda), function(i) {
# Set Degress of Freedom
dlm_n <- model@Est$vdfree[-1]
# Set DLM-Variance
dlm_Q <- model@Est$dlm_Q[[i]][-1, ]
# Set DLM-Forecast
dlm_f <- model@Est$dlm_f[[i]][-1, ]
# Set DLM-Weight
dlm_w <- model@Est$dlm_w[[i]][-1, ]
# Set Forgetting-Factor-Weight
d_w <- model@Est$mpmt[-1, i]
# Compute DLM-CRPS
dlm_crps <- do.call("cbind",
lapply(seq_len(ncol(dlm_f)),
function(i) {crps(y_sub[-1],
dlm_f[, i],
sqrt(dlm_Q[, i]),
dlm_n,
"t")$crps}))
# Compute DMA-CRPS
dma_crps <- rowSums(dlm_crps * dlm_w)
## Compute DMA-D-CRPS