|
6 | 6 | #' @param h Forecasting horizon |
7 | 7 | #' @param level Confidence level for prediction intervals |
8 | 8 | #' @param method forecasting method, either "mean", "median", or random walk ("rw") |
9 | | -#' @param type_pi type of prediction interval currently, "gaussian" or "bootstrap" |
10 | | -#' @param block_length length of block for circular block bootstrap (\code{type_pi == 'blockbootstrap'}) |
| 9 | +#' @param type_pi type of prediction interval currently, "gaussian", "bootstrap", |
| 10 | +#' "blockbootstrap" or "movingblockbootstrap" |
| 11 | +#' @param block_length length of block for (circular) "blockbootstrap" or "movingblockbootstrap" |
11 | 12 | #' @param seed reproducibility seed for \code{type_pi == 'bootstrap'} |
12 | 13 | #' @param B Number of bootstrap replications for \code{type_pi == 'bootstrap'} |
13 | 14 | #' |
|
52 | 53 | #' plot(res5, "Quotes") |
53 | 54 | #' plot(res5, "TV.advert") |
54 | 55 | #' |
| 56 | +#' |
| 57 | +#' # moving block bootstrap |
| 58 | +#' res6 <- ahead::basicf(fpp::insurance, h=10, |
| 59 | +#' type_pi = "movingblockbootstrap", B=10, |
| 60 | +#' block_length = 4, method = "rw") |
| 61 | +#' |
| 62 | +#' par(mfrow=c(1, 2)) |
| 63 | +#' plot(res6, "Quotes") |
| 64 | +#' plot(res6, "TV.advert") |
| 65 | +#' |
55 | 66 | basicf <- function(y, |
56 | 67 | h = 5, |
57 | 68 | level = 95, |
58 | 69 | method = c("mean", "median", "rw"), |
59 | | - type_pi = c("gaussian", "bootstrap", "blockbootstrap"), |
| 70 | + type_pi = c("gaussian", |
| 71 | + "bootstrap", |
| 72 | + "blockbootstrap", |
| 73 | + "movingblockbootstrap"), |
60 | 74 | block_length = NULL, |
61 | 75 | seed = 1, |
62 | 76 | B = 100) |
@@ -90,6 +104,7 @@ basicf <- function(y, |
90 | 104 | fits <- ts(t(replicate(n = n_inputs, expr = point_forecast)), |
91 | 105 | start = start_x, frequency = freq_x) |
92 | 106 | resids <- y - fits |
| 107 | + colnames(resids) <- colnames(y) |
93 | 108 |
|
94 | 109 | # out-of-sample |
95 | 110 | fcast <- ts(t(replicate(n = h, expr = point_forecast)), |
@@ -117,24 +132,45 @@ basicf <- function(y, |
117 | 132 | return(structure(out, class = "mtsforecast")) |
118 | 133 | } |
119 | 134 |
|
120 | | - if (type_pi %in% c("bootstrap", "blockbootstrap")) |
| 135 | + if(type_pi %in% c("blockbootstrap", "movingblockbootstrap")) |
| 136 | + { |
| 137 | + if (nrow(y) <= 2 * freq_x) |
| 138 | + freq_x <- 1L |
| 139 | + |
| 140 | + if (is.null(block_length)) { |
| 141 | + block_length <- ifelse(freq_x > 1, 2 * freq_x, min(8, floor(nrow(y) / 2))) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (type_pi %in% c("bootstrap", "blockbootstrap", "movingblockbootstrap")) |
121 | 146 | { |
122 | 147 | sims <- vector("list", length = B) |
123 | 148 | for (i in 1:B) |
124 | 149 | { |
125 | 150 | # sampling from the residuals |
126 | 151 | set.seed(seed + i*100 + nchar(method)) |
127 | 152 |
|
128 | | - idx <- switch(type_pi, |
129 | | - bootstrap = sample.int(n = nrow(resids), |
130 | | - size = h, replace = TRUE), |
131 | | - blockbootstrap = mbb( |
132 | | - r = resids, |
133 | | - n = h, |
134 | | - b = block_length, |
135 | | - return_indices = TRUE, |
136 | | - seed = seed + i*100 + nchar(method) |
137 | | - ) |
| 153 | + idx <- switch( |
| 154 | + type_pi, |
| 155 | + bootstrap = sample.int( |
| 156 | + n = nrow(resids), |
| 157 | + size = h, |
| 158 | + replace = TRUE |
| 159 | + ), |
| 160 | + blockbootstrap = mbb( |
| 161 | + r = resids, |
| 162 | + n = h, |
| 163 | + b = block_length, |
| 164 | + return_indices = TRUE, |
| 165 | + seed = seed + i * 100 + nchar(method) |
| 166 | + ), |
| 167 | + movingblockbootstrap = mbb2( |
| 168 | + r = resids, |
| 169 | + n = h, |
| 170 | + b = block_length, |
| 171 | + return_indices = TRUE, |
| 172 | + seed = seed + i * 100 + nchar(method) |
| 173 | + ) |
138 | 174 | ) |
139 | 175 |
|
140 | 176 | sims[[i]] <- ts(as.matrix(fcast) + as.matrix(resids[idx, ]), |
|
0 commit comments