Created
March 12, 2020 16:35
-
-
Save ha0ye/d62b33b1d735b09610852a7113f6bda3 to your computer and use it in GitHub Desktop.
Forward forecasts using rEDM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rEDM) | |
#### Example 1: a single forward prediction | |
# Assume we have already done the analysis, finding that the best embedding | |
# dimension is 2. Our objective is to make multiple 1-step ahead forecasts | |
# using simplex and E = 2. | |
dat <- data.frame(yr = as.numeric(time(sunspot.year)), | |
sunspot_count = as.numeric(sunspot.year)) | |
E <- 3 | |
# setup data structures: | |
# 1) add empty row with incremented time, and empty variable of interest | |
dat <- rbind(dat, | |
data.frame(yr = tail(dat, 1)$yr + 1, | |
sunspot_count = NA)) | |
# 2) setup lib and pred: | |
# lib excludes the last row; | |
# pred includes the last row and E previous lags | |
lib <- c(1, NROW(dat) - 1) | |
pred <- c(NROW(dat) - E, NROW(dat)) | |
# 3) make prediction | |
simplex_output <- simplex(dat, lib = lib, pred = pred, E = E, | |
stats_only = FALSE) | |
# 4) replace the missing value in `dat` with the forecast, so that we can | |
# repeat the process | |
dat[NROW(dat), "sunspot_count"] <- simplex_output$model_output[[1]][E, "pred"] | |
#### Example 2: multiple forward predictions | |
# setup initial data and E as previously | |
dat <- data.frame(yr = as.numeric(time(sunspot.year)), | |
sunspot_count = as.numeric(sunspot.year)) | |
E <- 3 | |
# wrap up previous steps 1-4 in a loop | |
num_predictions <- 5 | |
for (i in seq(num_predictions)) | |
{ | |
dat <- rbind(dat, | |
data.frame(yr = tail(dat, 1)$yr + 1, | |
sunspot_count = NA)) | |
lib <- c(1, NROW(dat) - 1) | |
pred <- c(NROW(dat) - E, NROW(dat)) | |
simplex_output <- simplex(dat, lib = lib, pred = pred, E = E, | |
stats_only = FALSE) | |
dat[NROW(dat), "sunspot_count"] <- simplex_output$model_output[[1]][E, "pred"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment