Skip to content

Commit 2a037f2

Browse files
committed
refactor: rename frequency to freq
1 parent ffb64b4 commit 2a037f2

File tree

11 files changed

+49
-85
lines changed

11 files changed

+49
-85
lines changed

R/TaskFcst.R

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
TaskFcst = R6Class("TaskFcst",
2626
inherit = TaskRegr,
2727
public = list(
28-
#' @field frequency (`character(1)`)\cr
28+
#' @field freq (`character(1)`)\cr
2929
#' The frequency of the time series.
30-
frequency = NULL,
30+
freq = NULL,
3131

3232
#' @description
3333
#' Creates a new instance of this [R6][R6::R6Class] class.
@@ -36,10 +36,10 @@ TaskFcst = R6Class("TaskFcst",
3636
#' @template param_target
3737
#' @template param_order
3838
#' @template param_key
39-
#' @template param_frequency
39+
#' @template param_freq
4040
#' @template param_label
4141
#' @template param_extra_args
42-
initialize = function(id, backend, target, order, key = NULL, frequency = NULL, label = NA_character_, extra_args = list()) { # nolint
42+
initialize = function(id, backend, target, order, key = NULL, freq = NULL, label = NA_character_, extra_args = list()) { # nolint
4343
super$initialize(
4444
id = id,
4545
backend = backend,
@@ -53,18 +53,17 @@ TaskFcst = R6Class("TaskFcst",
5353
self$set_col_roles(order, add = "order")
5454
self$set_col_roles(key, add = "key")
5555
assert_choice(
56-
frequency, c("daily", "weekly", "monthly", "quarterly", "yearly"),
57-
null.ok = TRUE
56+
freq, c("daily", "weekly", "monthly", "quarterly", "yearly"), null.ok = TRUE
5857
)
59-
self$frequency = frequency
58+
self$freq = freq
6059
},
6160

6261
#' @description
6362
#' Printer.
6463
#' @param ... (ignored).
6564
print = function(...) {
6665
super$print()
67-
catf(str_indent("* Frequency:", self$frequency))
66+
catf(str_indent("* Frequency:", self$freq))
6867
}
6968
),
7069

R/TaskFcstAirpassengers.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ load_task_airpassengers = function(id = "airpassengers") {
2424
backend = b,
2525
target = "passengers",
2626
order = "date",
27-
frequency = "monthly",
27+
freq = "monthly",
2828
label = "Monthly Airline Passenger Numbers 1949-1960"
2929
)
3030
b$hash = task$man = "mlr3forecast::mlr_tasks_airpassengers"

R/TaskFcstElectricty.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ load_task_electricty = function(id = "electricity") {
2929
backend = b,
3030
target = "demand",
3131
order = "date",
32-
frequency = "daily",
32+
freq = "daily",
3333
label = "Daily electricity demand for Victoria, Australia"
3434
)
3535
b$hash = task$man = "mlr3forecast::load_task_electricty"

R/TaskFcstLivestock.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ load_task_livestock = function(id = "livestock") {
2626
target = "count",
2727
order = "month",
2828
key = c("animal", "state"),
29-
frequency = "monthly",
29+
freq = "monthly",
3030
label = "Australian livestock slaughter"
3131
)
3232
b$hash = task$man = "mlr3forecast::mlr_tasks_livestock"

R/as_task_fcst.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#' @inheritParams mlr3::as_task_regr
1111
#' @template param_order
1212
#' @template param_key
13-
#' @template param_frequency
13+
#' @template param_freq
1414
#'
1515
#' @return [TaskFcst].
1616
#' @export
@@ -31,7 +31,7 @@ as_task_fcst.TaskFcst = function(x, clone = FALSE, ...) { # nolint
3131

3232
#' @rdname as_task_fcst
3333
#' @export
34-
as_task_fcst.DataBackend = function(x, target = NULL, order = character(), key = character(), frequency = NULL, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint
34+
as_task_fcst.DataBackend = function(x, target = NULL, order = character(), key = character(), freq = NULL, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint
3535
force(id)
3636

3737
cn = x$colnames
@@ -42,14 +42,14 @@ as_task_fcst.DataBackend = function(x, target = NULL, order = character(), key =
4242
}
4343

4444
task = TaskFcst$new(
45-
id = id, backend = x, target = target, order = order, key = key, frequency = frequency, label = label, ... # nolint
45+
id = id, backend = x, target = target, order = order, key = key, freq = freq, label = label, ... # nolint
4646
)
4747
task
4848
}
4949

5050
#' @rdname as_task_fcst
5151
#' @export
52-
as_task_fcst.data.frame = function(x, target = NULL, order = character(), key = character(), frequency = NULL, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint
52+
as_task_fcst.data.frame = function(x, target = NULL, order = character(), key = character(), freq = NULL, id = deparse1(substitute(x)), label = NA_character_, ...) { # nolint
5353
force(id)
5454

5555
assert_data_frame(x, min.rows = 1L, min.cols = 1L, col.names = "unique")
@@ -66,7 +66,7 @@ as_task_fcst.data.frame = function(x, target = NULL, order = character(), key =
6666
}
6767

6868
task = TaskFcst$new(
69-
id = id, backend = x, target = target, order = order, key = key, frequency = frequency, label = label, ... # nolint
69+
id = id, backend = x, target = target, order = order, key = key, freq = freq, label = label, ... # nolint
7070
)
7171
task
7272
}

R/utils.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ quantiles_to_level = function(x) {
1515

1616
#' @export
1717
as.ts.TaskFcst = function(x, ...) { # nolint
18-
freq = switch(x$frequency,
18+
freq = switch(x$freq,
1919
daily = 365.25,
2020
weekly = 52,
2121
monthly = 12,
2222
quarterly = 4,
2323
yearly = 1,
24-
stopf("Unknown frequency: %s", x$frequency)
24+
stopf("Unknown frequency: %s", x$freq)
2525
)
2626
stats::ts(x$truth(), freq = freq)
2727
}
@@ -37,7 +37,7 @@ generate_newdata = function(task, n = 1L) {
3737
order_cols = task$col_roles$order
3838
max_index = max(task$data(cols = order_cols)[[1L]])
3939

40-
unit = switch(task$frequency,
40+
unit = switch(task$freq,
4141
daily = "day",
4242
weekly = "week",
4343
monthly = "month",

README.Rmd

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ knitr::opts_chunk$set(
1212
1313
lgr::get_logger("mlr3")$set_threshold("warn")
1414
options(datatable.print.class = FALSE, datatable.print.keys = FALSE)
15-
library(checkmate)
1615
library(data.table)
1716
library(mlr3misc)
1817
```
@@ -60,7 +59,7 @@ still in flux and may change.
6059
Currently, we support native forecasting learners from the forecast package.
6160
In the future, we plan to support more forecasting learners.
6261

63-
```{r}
62+
```{r, message = FALSE}
6463
library(mlr3forecast)
6564
6665
task = tsk("airpassengers")
@@ -127,26 +126,11 @@ prediction$score(msr("regr.rmse"))
127126

128127
### Example: forecasting electricity demand
129128

130-
```{r}
129+
```{r, message = FALSE}
131130
library(mlr3learners)
132131
library(mlr3pipelines)
133132
134-
task = tsibbledata::vic_elec |>
135-
as.data.table() |>
136-
setnames(tolower) |>
137-
_[
138-
year(time) == 2014L,
139-
.(
140-
demand = sum(demand) / 1e3,
141-
temperature = max(temperature),
142-
holiday = any(holiday)
143-
),
144-
by = date
145-
] |>
146-
as_task_fcst(
147-
id = "vic_elec", target = "demand", order = "date", frequency = "daily"
148-
)
149-
133+
task = tsk("electricity")
150134
graph = ppl("convert_types", "Date", "POSIXct") %>>%
151135
po("datefeatures",
152136
param_vals = list(
@@ -185,7 +169,7 @@ task = tsibbledata::aus_livestock |>
185169
target = "count",
186170
order = "month",
187171
key = "state",
188-
frequency = "monthly"
172+
freq = "monthly"
189173
)
190174
191175
graph = ppl("convert_types", "Date", "POSIXct") %>>%

README.md

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,9 @@ package. In the future, we plan to support more forecasting learners.
4848

4949
``` r
5050
library(mlr3forecast)
51-
#> Loading required package: mlr3
5251

5352
task = tsk("airpassengers")
5453
learner = lrn("fcst.auto_arima")$train(task)
55-
#> Registered S3 method overwritten by 'quantmod':
56-
#> method from
57-
#> as.zoo.data.frame zoo
5854
prediction = learner$predict(task, 140:144)
5955
prediction$score(msr("regr.rmse"))
6056
#> regr.rmse
@@ -103,32 +99,32 @@ prediction = flrn$predict_newdata(newdata, task)
10399
prediction
104100
#> <PredictionRegr> for 3 observations:
105101
#> row_ids truth response
106-
#> 1 NA 434.9439
107-
#> 2 NA 439.1822
108-
#> 3 NA 458.6453
102+
#> 1 NA 436.1867
103+
#> 2 NA 437.4089
104+
#> 3 NA 456.8410
109105
prediction = flrn$predict(task, 142:144)
110106
prediction
111107
#> <PredictionRegr> for 3 observations:
112108
#> row_ids truth response
113-
#> 1 461 455.2556
114-
#> 2 390 411.4701
115-
#> 3 432 432.2286
109+
#> 1 461 459.1495
110+
#> 2 390 414.8433
111+
#> 3 432 430.2693
116112
prediction$score(msr("regr.rmse"))
117113
#> regr.rmse
118-
#> 12.83247
114+
#> 14.41767
119115

120116
flrn = ForecastLearner$new(lrn("regr.ranger"), 1:12)
121117
resampling = rsmp("forecast_holdout", ratio = 0.9)
122118
rr = resample(task, flrn, resampling)
123119
rr$aggregate(msr("regr.rmse"))
124120
#> regr.rmse
125-
#> 50.76601
121+
#> 48.97126
126122

127123
resampling = rsmp("forecast_cv")
128124
rr = resample(task, flrn, resampling)
129125
rr$aggregate(msr("regr.rmse"))
130126
#> regr.rmse
131-
#> 26.06017
127+
#> 25.19211
132128
```
133129

134130
Or with some feature engineering using mlr3pipelines:
@@ -150,7 +146,7 @@ glrn = as_learner(graph %>>% flrn)$train(task)
150146
prediction = glrn$predict(task, 142:144)
151147
prediction$score(msr("regr.rmse"))
152148
#> regr.rmse
153-
#> 15.6575
149+
#> 15.58057
154150
```
155151

156152
### Example: forecasting electricity demand
@@ -159,22 +155,7 @@ prediction$score(msr("regr.rmse"))
159155
library(mlr3learners)
160156
library(mlr3pipelines)
161157

162-
task = tsibbledata::vic_elec |>
163-
as.data.table() |>
164-
setnames(tolower) |>
165-
_[
166-
year(time) == 2014L,
167-
.(
168-
demand = sum(demand) / 1e3,
169-
temperature = max(temperature),
170-
holiday = any(holiday)
171-
),
172-
by = date
173-
] |>
174-
as_task_fcst(
175-
id = "vic_elec", target = "demand", order = "date", frequency = "daily"
176-
)
177-
158+
task = tsk("electricity")
178159
graph = ppl("convert_types", "Date", "POSIXct") %>>%
179160
po("datefeatures",
180161
param_vals = list(
@@ -195,13 +176,13 @@ prediction = glrn$predict_newdata(newdata, task)
195176
prediction
196177
#> <PredictionRegr> for 14 observations:
197178
#> row_ids truth response
198-
#> 1 NA 186.9592
199-
#> 2 NA 191.3908
200-
#> 3 NA 185.1543
179+
#> 1 NA 187595.7
180+
#> 2 NA 196608.6
181+
#> 3 NA 189152.0
201182
#> --- --- ---
202-
#> 12 NA 215.4734
203-
#> 13 NA 219.6907
204-
#> 14 NA 219.8203
183+
#> 12 NA 222400.3
184+
#> 13 NA 226494.8
185+
#> 14 NA 226568.4
205186
```
206187

207188
### Example: global forecasting (longitudinal data)
@@ -222,7 +203,7 @@ task = tsibbledata::aus_livestock |>
222203
target = "count",
223204
order = "month",
224205
key = "state",
225-
frequency = "monthly"
206+
freq = "monthly"
226207
)
227208

228209
graph = ppl("convert_types", "Date", "POSIXct") %>>%
@@ -239,14 +220,14 @@ flrn = ForecastLearner$new(lrn("regr.ranger"), 1:3)$train(task)
239220
prediction = flrn$predict(task, 4460:4464)
240221
prediction$score(msr("regr.rmse"))
241222
#> regr.rmse
242-
#> 22183.18
223+
#> 22604.48
243224

244225
flrn = ForecastLearner$new(lrn("regr.ranger"), 1:3)
245226
resampling = rsmp("forecast_holdout", ratio = 0.9)
246227
rr = resample(task, flrn, resampling)
247228
rr$aggregate(msr("regr.rmse"))
248229
#> regr.rmse
249-
#> 93061.55
230+
#> 92125.26
250231
```
251232

252233
### Example: global vs local forecasting
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#' @param frequency (`character(1)`)\cr
1+
#' @param freq (`character(1)`)\cr
22
#' Frequency of the time series.
33
#' One of `"daily"`, `"weekly"`, `"monthly"`, `"quarterly"`, `"yearly"`.

man/TaskFcst.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)