Description Usage Arguments Examples
Core function of the package. Runs through a series of dates and evaluates one or several expressions with the data available.
1 | timemachine(..., dates, history, post_process = ts_attach)
|
... |
expressions to be evaluated. Expressions can be named, so the name will appear in the output |
dates |
Date or character. At which points in time should the expressions be evaluated? |
history |
a data frame with the publication history of the data.
Must have column names |
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 | library(tsbox)
library(timemachine)
library(dplyr)
library(tidyr)
# Pseudo History example ----------------------------------------------------
# Constructing pseudo history data. It is assumed that mdeath is available
# one month after the end of the period, but fdeath immediately.
history <-
bind_rows(
pseudo_history(mdeaths, "1 month"),
pseudo_history(fdeaths),
) %>%
check_history()
# Telling the time machine where to evaluate
dates = seq(as.Date("1978-01-01"), to = as.Date("1979-10-01"), by = "month")
# Wormhole without an argument makes the latest data available in the
# globalenv(). This is useful to build the models.
# Since version 0.0.2, 'history' is not set via options anymore, and must be
# provided all the time.
wormhole(history)
# latest() returns the latest data
latest(history)
### Bon Voyage
# evalating some models from the forecast package
library(forecast)
# Put each model in a (named) expression. You can construct
fct_data <- timemachine(
etf = {
m <- forecast(mdeaths, h = 3)
m$mean
},
arima = {
m <- forecast(auto.arima(mdeaths), h = 3)
m$mean
},
arimax = {
m <-
forecast(
auto.arima(mdeaths, xreg = window(fdeaths, end = end(mdeaths))),
xreg = window(fdeaths, start = tsp(mdeaths)[2] + 1/12),
h = 1
)
m$mean
},
randomwalk = {
h = 3
e <- tsp(mdeaths)[2]
f <- tsp(mdeaths)[3]
ts(mdeaths[length(mdeaths)], start = e + 1/f, end = e + h/f, f = f)
},
history = history,
dates = dates
)
# need to find a clever way to get 'reference data'
bench_data <-
latest(history) %>%
ts_pick("mdeaths") %>%
select(ref_date = time, ref_value = value)
errors <- fct_data %>%
left_join(bench_data, by = "ref_date") %>%
# add fct horizon
group_by(pub_date, expr) %>%
mutate(h = seq(n())) %>%
ungroup() %>%
mutate(error = value - ref_value)
# error stats
errors %>%
group_by(expr, h) %>%
summarize(rmse = sqrt(sum(error^2)), mae = (mean(abs(error))))
# scatter plots, by horizon
library(ggplot2)
errors %>%
ggplot() +
geom_point(aes(x = value, y = ref_value)) +
facet_grid(h ~ expr)
# Real time data ------------------------------------------------------------
# Set up history
# assuming EXP is available one period before
swiss_history2 <- swiss_history %>%
filter(id %in% c("EXP", "GDP.CH")) %>%
mutate(pub_date = if_else(
id == "EXP",
add_to_date(pub_date, "-1 quarter"),
pub_date
)) %>%
# pc rates
group_by(id, pub_date) %>%
mutate(value = log(value) - lag(log(value))) %>%
ungroup() %>%
filter(!is.na(value)) %>%
check_history()
# Simulation
# evalating some models from the forecast package
library(forecast)
# Put each model in a (named) expression. You can construct
fct_data <- timemachine(
etf = {
m <- forecast(GDP.CH, h = 3)
m$mean
},
arima = {
m <- forecast(auto.arima(GDP.CH), h = 3)
m$mean
},
arima_exp = {
m <- forecast(auto.arima(GDP.CH,
xreg = window(EXP, end = end(GDP.CH))),
xreg = window(EXP, start = tsp(GDP.CH)[2] + 1/12),
h = 1)
m$mean
},
randomwalk = {
h = 3
e <- tsp(GDP.CH)[2]
f <- tsp(GDP.CH)[3]
ts(GDP.CH[length(GDP.CH)], start = e + 1/f, end = e + h/f, f = f)
},
history = swiss_history2,
dates = seq(as.Date("2014-01-01"), to = as.Date("2015-10-01"), by = "quarter")
)
# Evaluation
# see example_annual_gdp.R for advanced use of benchmark data
bench_data <-
latest(swiss_history2) %>%
ts_pick("GDP.CH") %>%
select(ref_date = time, ref_value = value)
errors <- fct_data %>%
left_join(bench_data, by = "ref_date") %>%
# add fct horizon
group_by(pub_date, expr) %>%
mutate(h = seq(n())) %>%
ungroup() %>%
mutate(error = value - ref_value)
# error stats
errors %>%
group_by(expr, h) %>%
summarize(rmse = sqrt(sum(error^2)), mae = (mean(abs(error))))
# scatter plots, by horizon
library(ggplot2)
errors %>%
ggplot() +
geom_point(aes(x = ref_value, y = value)) +
facet_grid(h ~ expr)
# Advanced benchmarking -----------------------------------------------------
# annual gdp growth
history_gdp <-
swiss_history %>%
filter(id == "GDP.CH") %>%
ts_frequency("year", sum) %>%
ts_pc()
# 1st BFS value is available in Oct
bench_bfs <-
history_gdp %>%
filter(as.POSIXlt(pub_date)$mon + 1 == 7) %>%
arrange(pub_date) %>%
group_by(ref_date) %>%
slice(1) %>%
ungroup() %>%
select(ref_date, ref_value = value)
# 1st SECO value is the first available value
bench_seco <-
history_gdp %>%
arrange(pub_date) %>%
group_by(ref_date) %>%
slice(1) %>%
ungroup() %>%
select(ref_date, ref_value = value)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.