Skip to content

Commit

Permalink
version 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelloharawild authored and cran-robot committed Mar 16, 2021
1 parent bd3ee77 commit de49ed3
Show file tree
Hide file tree
Showing 23 changed files with 417 additions and 48 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
@@ -1,6 +1,6 @@
Package: fabletools
Title: Core Tools for Packages in the 'fable' Framework
Version: 0.3.0
Version: 0.3.1
Authors@R:
c(person(given = "Mitchell",
family = "O'Hara-Wild",
Expand Down Expand Up @@ -45,7 +45,7 @@ Language: en-GB
LazyData: true
RoxygenNote: 7.1.1
NeedsCompilation: no
Packaged: 2021-01-19 05:34:50 UTC; mitchell
Packaged: 2021-03-16 15:57:54 UTC; mitchell
Author: Mitchell O'Hara-Wild [aut, cre],
Rob Hyndman [aut],
Earo Wang [aut],
Expand All @@ -54,4 +54,4 @@ Author: Mitchell O'Hara-Wild [aut, cre],
David Holt [ctb]
Maintainer: Mitchell O'Hara-Wild <mail@mitchelloharawild.com>
Repository: CRAN
Date/Publication: 2021-01-19 07:20:05 UTC
Date/Publication: 2021-03-16 22:10:03 UTC
39 changes: 22 additions & 17 deletions MD5
@@ -1,24 +1,24 @@
d99d13dc46c8d007423a125aa97c856a *DESCRIPTION
de1390ccca004bca65a21fff06ff807c *NAMESPACE
a8c4679ab06337e81c3776eb7481ac8b *NEWS.md
711b1551fa11f01a6cd38859fe2efd0f *DESCRIPTION
5a5e08c49894bf24d168ce1936f60cde *NAMESPACE
af0ad9442cd0a283bc1d05e5d8c2ac9f *NEWS.md
b4be2f8c6db88d242e618e4c6266baa2 *R/accessors.R
045ffdb1efba8e6625b34a76a3257387 *R/accuracy.R
61c3121db97a901e34741fd9f0263974 *R/aggregate.R
43196b3a91f03002efa4e99f6a94ce8c *R/box_cox.R
29c4211bf71e15aad4d3edaa39618a3d *R/broom.R
1db81c79416e183d71b7778b6e64efe5 *R/broom.R
cbaef737d494327d79afb92b21ccf254 *R/compat-purrr.R
3ef1dbefefb9435a8d4622f7f72463ef *R/components.R
515d6b02af1d96e4e5a0889f59238567 *R/dable.R
8ef591a2fa72a053a7af2b70ef8a0222 *R/definitions.R
a28cc83ee3ccd2c8df47710b23882cde *R/dable.R
355cc3401ddea9c3460fa4acaf32caa3 *R/definitions.R
9ad0ddbab62e1b98e2505bc165ed6818 *R/dplyr-dable.R
1374c58ef646487f08897d728757ac7f *R/dplyr-fable.R
7252803d29321966bbf4e38726a39d8f *R/dplyr-mable.R
f0cfa99a08bf99c3f0b2f8b4ab386305 *R/equation.R
7098a40958f70fe046ac023fe5e08167 *R/estimate.R
f23b65a675c89a6b617e5bc183987900 *R/fable.R
64f37c3dd4a10e5294289295362de566 *R/fable.R
7d82bf977dc75cafa7afdc18ba21caa0 *R/fabletools-package.R
b730954e9524549c949a96bd57e68c44 *R/features.R
39143e673d8640597101e1c57620e3bd *R/fitted.R
b686e5ff453a1ee396fa6b1fe3d8df10 *R/features.R
c7e1cf6c64869618c861edf9977af576 *R/fitted.R
010effec27cc34eca53b76a7502b405f *R/forecast.R
b49a5f15d5bad65d8d8130ed36a4e9b9 *R/frequency.R
67e6a5d3ba54719dd492a1d41b6eef5e *R/generate.R
Expand All @@ -32,15 +32,16 @@ d5a36d1279e7fb7a372d9de1f7786d7a *R/model.R
4aaaf1626ce168def4a4b445af32b14d *R/model_combination.R
29956ecc10adc8b296dcb5c9d2e56755 *R/model_decomposition.R
df6f7d3d9bcc6fc1a989f7d821aef85c *R/model_null.R
200ccace4ef4ea1dd983923c1d85cf1a *R/outliers.R
9263b54db853b06611fdc2a70851e555 *R/parse.R
c8c67a0676e16771757a3248abd50343 *R/plot.R
5a7d2ebecaf9d47565f0a0fc0a413ae3 *R/reconciliation.R
54a4b199f0df9f37aab70ca8523e4a7a *R/reconciliation.R
563cc27af1b423c80650eb8508c58877 *R/reexports.R
fb03bdad6d773df26247ec8c684ab9fa *R/refit.R
2ca682e8231f7f02cae0fa4128a134bc *R/report.R
37902979ade9836ebf0184e100d1f5ea *R/residuals.R
fe30a66e973d94f152eeec90864b4a15 *R/response.R
299bb581f0915f0e9bdb332d501e5c50 *R/specials.R
3b97f5a5e9a75acc13c57789c1846c47 *R/specials.R
c8d51f09ab597e1c57ce666882e39544 *R/stream.R
cbdf350bf426c9729a4078546d47e95c *R/tbl_utils.R
c65cf167631341af48b62aee71a0bf3f *R/temporal_aggregation.R
Expand All @@ -50,14 +51,15 @@ c720c5ba58764e6f7a9f0a3ffae5274b *R/utils.R
729d8de2770d3f84fe73a05729ae0893 *R/vctrs-dable.R
dab5adb8761ae06ce0b0a77f179dbe2d *R/vctrs-fable.R
1382e6f97d0c5e050b539401080b1445 *R/vctrs-mable.R
429e331e5243dfe52ca88543986b6551 *R/xregs.R
9eed4b755fab99446a41a2bb44576600 *R/zzz.R
933964bc28a06198d85850d94cee794e *README.md
6319bf86b3108d6e1a35de6980b878cd *build/fabletools.pdf
ede9c0dfaa26943c7201004142c62154 *README.md
f51f7f88342e1c25fa1698cf144fccd2 *build/fabletools.pdf
10e18aae8aba14bc6c6ca44a71fdc68a *build/vignette.rds
6f2a90e451213e60197c308eacb8ab71 *inst/WORDLIST
7bab195f754cc0017bd80d6b93536606 *inst/doc/extension_models.R
5a159266a4d4a7fc70c5e5702e4ccd9c *inst/doc/extension_models.Rmd
97795391ae12c23e4561f786bab46f91 *inst/doc/extension_models.html
3b7ab31593d3e28ffcf168fa5afad682 *inst/doc/extension_models.html
cff23ee87902f0ba1c751ada29f20fc2 *man/MAAPE.Rd
2cb004482006e49a4ec956d90b70ba75 *man/accuracy.Rd
775252575ce852d15fe54f6006fb2720 *man/agg_vec.Rd
Expand All @@ -67,7 +69,7 @@ e968b46684b00b9b8bd4d19a68c26414 *man/aggregation-vctrs.Rd
c62dddfed7dc7cef64a9d94c8d9e2952 *man/as-dable.Rd
ea4a261519a8f938bbca31e2c82960ba *man/as-fable.Rd
551b9a81c42c40e09d806b815c9a3892 *man/as_mable.Rd
2e6e5ffae77da16ecf82d5a2eb366be4 *man/augment.Rd
66a2707f658afdf5a01f5e2ea3f99bce *man/augment.Rd
146fdeeb16a79170cae2137bb874835a *man/autoplot.dcmp_ts.Rd
37c1e2a9ecc19e127e3f57071ef31b85 *man/autoplot.fbl_ts.Rd
f2269ba3a524bc34aa69f39e3e9bf530 *man/autoplot.tbl_ts.Rd
Expand All @@ -76,6 +78,7 @@ f2269ba3a524bc34aa69f39e3e9bf530 *man/autoplot.tbl_ts.Rd
cab019073996dcc2d5aba968f04fb033 *man/box_cox.Rd
3c109b101def27d5a9504671d485be4b *man/combination_ensemble.Rd
cbd465b6b7674866572de9547b4fe6a5 *man/combination_model.Rd
ad3ec7083e2f5972ee6f1600a949705d *man/common_xregs.Rd
51dd35a03ae3539a0cbb0352b8622fd4 *man/components.Rd
44b374027aa1634b32e0fbcfa4431971 *man/construct_fc.Rd
311d01a7b31c53ed10632df3d81e6a37 *man/dable-vctrs.Rd
Expand Down Expand Up @@ -119,7 +122,7 @@ ef48c67bf01ee8ca6814a55ce87c3522 *man/is_model.Rd
ccbf937cd020d3fc33038ccbe2d6f72e *man/mable-vctrs.Rd
0901c7b645dce8757e70b8d393d70123 *man/mable.Rd
07dca109d2ea56ef5d4f8997c0f4748f *man/mable_vars.Rd
b59b5c5babc198e868d29800e57d3e7f *man/middle_out.Rd
301048bbd74fdbb5d5312063b5a13803 *man/middle_out.Rd
796365246bc1be40f6e63057bcf72187 *man/min_trace.Rd
fc1f352a34744f1cea8032c687771d32 *man/model.Rd
1393b3cb5b12c9184a7e56bc397c088f *man/model_lhs.Rd
Expand All @@ -129,6 +132,7 @@ b3786690676b4f812ab2640a36c24ed2 *man/model_sum.Rd
6ffde087115fdb01c82f379200f72c8b *man/new_specials.Rd
7329f8eacb995de61ff71d5274e321f7 *man/new_transformation.Rd
01724dedf396d39a6de14d0d280841c3 *man/null_model.Rd
fd11d262287dcffaedb4719e387c59ab *man/outliers.Rd
1084daec4cc235fd241dfc3d00b5ec31 *man/parse_model.Rd
bdc8cb57affb2b7ffc1f2e2b0d54ed11 *man/parse_model_lhs.Rd
3fd1c33a565c1fbfed2b04be25d993e7 *man/parse_model_rhs.Rd
Expand All @@ -143,14 +147,15 @@ fe74782d2d7a04c98808abda763e18ff *man/report.Rd
b55489638f729e2380e2d96f4716b1d6 *man/response_vars.Rd
a51f43537bddd5e20fc9206074bf59e0 *man/scenarios.Rd
f24d13481193aa4e4beaa2ada30ef98d *man/skill_score.Rd
0e9e1a771dc255c708fdf2717b0dcb04 *man/special_xreg.Rd
83b079e6ae9511eaf3088c462f9d75df *man/stream.Rd
750aaaefa9aa2044f8b40f8a48bdecaf *man/tidy.Rd
74b8186caf6db910744ce117f81e1e32 *man/top_down.Rd
9e68d7156db1ba73e94464d68fd2c308 *man/traverse.Rd
4980cb795255188f627f650622c4bf80 *man/unpack_hilo.Rd
4bf7ba0f683d07e48d465d0a4a558d7f *man/validate_formula.Rd
8176e3fbb47046d5f7220e147a102483 *tests/testthat.R
7ae4d1c96691939b3f95c253e51be7ac *tests/testthat/Rplots.pdf
cdd548370717b42a9738c73daee45d1d *tests/testthat/Rplots.pdf
d086dba90b013cec5dda344fb7938612 *tests/testthat/setup-data.R
b93b054b076aba90b60d8f5a0fd9198d *tests/testthat/setup-models.R
6b683cb35b10e316ea6f1d5bcbf69b40 *tests/testthat/test-accuracy.R
Expand Down
8 changes: 8 additions & 0 deletions NAMESPACE
Expand Up @@ -116,6 +116,8 @@ S3method(model_sum,default)
S3method(model_sum,mdl_ts)
S3method(model_sum,model_combination)
S3method(model_sum,null_mdl)
S3method(outliers,mdl_df)
S3method(outliers,mdl_ts)
S3method(pivot_longer,mdl_df)
S3method(print,transformation)
S3method(rbind,dcmp_ts)
Expand Down Expand Up @@ -237,6 +239,7 @@ export(box_cox)
export(combination_ensemble)
export(combination_model)
export(common_periods)
export(common_xregs)
export(components)
export(construct_fc)
export(dable)
Expand Down Expand Up @@ -282,6 +285,7 @@ export(new_model_definition)
export(new_specials)
export(new_transformation)
export(null_model)
export(outliers)
export(parse_model)
export(parse_model_lhs)
export(parse_model_rhs)
Expand All @@ -298,6 +302,7 @@ export(response_vars)
export(scaled_pinball_loss)
export(scenarios)
export(skill_score)
export(special_xreg)
export(stream)
export(tidy)
export(top_down)
Expand Down Expand Up @@ -367,8 +372,11 @@ importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(lifecycle,deprecate_warn)
importFrom(stats,fitted)
importFrom(stats,model.frame)
importFrom(stats,model.matrix)
importFrom(stats,quantile)
importFrom(stats,residuals)
importFrom(stats,terms)
importFrom(stats,var)
importFrom(tibble,new_tibble)
importFrom(tidyr,gather)
Expand Down
18 changes: 18 additions & 0 deletions NEWS.md
@@ -1,3 +1,21 @@
# fabletools 0.3.1

## New features

* Added `outliers()` generic for identifying the outliers of a fitted model.
* Added `special_xreg()` special generator, for producing a model matrix of
exogenous regressors. It supports an argument for controlling the default
inclusion of an intercept.
* Migrated `common_xregs` helper from fable to fabletools for providing a
common and consistent interface for common time series exogenous regressors.
* Added experimental support for passing the tsibble index to `features()`
functions if the `.index` argument is used in the function.

## Improvements

* Added transformation support for fallback `fitted(h > 1)` method (#302).
* Documentation improvements.

# fabletools 0.3.0

## New features
Expand Down
4 changes: 3 additions & 1 deletion R/broom.R
@@ -1,7 +1,9 @@
#' Augment a mable
#'
#' Uses a fitted model to augment the response variable with fitted values and
#' residuals.
#' residuals. Response residuals (back-transformed) are stored in the `.resid`
#' column, while innovation residuals (transformed) are stored in the `.innov`
#' column.
#'
#' @param x A mable.
#' @param ... Arguments for model methods.
Expand Down
7 changes: 5 additions & 2 deletions R/dable.R
Expand Up @@ -89,8 +89,11 @@ tbl_sum.dcmp_ts <- function(x){
method <- expr_text((x%@%"aliases")[[response]])
out <- NextMethod()
names(out)[1] <- "A dable"
append(out, set_names(paste(response, method, sep = " = "),
paste(x%@%"method", "Decomposition")))
if(!is.null(method)) {
out[[length(out) + 1]] <- set_names(paste(response, method, sep = " = "),
paste(x%@%"method", "Decomposition"))
}
out
}

#' @export
Expand Down
6 changes: 5 additions & 1 deletion R/definitions.R
Expand Up @@ -4,6 +4,7 @@ model_definition <- R6::R6Class(NULL,
specials = list(),
formula = NULL,
extra = NULL,
origin = NULL,
env = global_env(),
check = function(.data){
},
Expand All @@ -28,7 +29,6 @@ model_definition <- R6::R6Class(NULL,
xreg_env <- get_env(self$specials$xreg)
xreg_env$lag <- self$recall_lag


self$prepare(formula, ...)

self$extra <- list2(...)
Expand All @@ -49,6 +49,10 @@ model_definition <- R6::R6Class(NULL,
data = NULL,
add_data = function(.data){
self$check(.data)
# Add data origin if not yet known (fitting model)
if(is.null(self$origin)) {
self$origin <- .data[[index_var(.data)]][[1]]
}
self$data <- .data
},
remove_data = function(){
Expand Down
19 changes: 3 additions & 16 deletions R/fable.R
Expand Up @@ -194,31 +194,18 @@ hilo.fbl_ts <- function(x, level = c(80, 95), ...){
}

restore_fable <- function(data, template){
data <- as_tibble(data)
data_cols <- names(data)

# key_vars <- setdiff(key_vars(template), data_cols)
# key_data <- select(key_data(template), key_vars)
# if (vec_size(key_data) == 1) {
# template <- remove_key(template, setdiff(key_vars(template), key_vars))
# }

# Variables to keep
tsbl_vars <- setdiff(c(index_var(template), key_vars(template)), data_cols)
fbl_vars <- setdiff(distribution_var(template), data_cols)
res <- bind_cols(template[tsbl_vars], data, template[fbl_vars])

tsbl <- build_tsibble(res, !!key_vars(template),
index = !!index(template), index2 = !!index2(template),
ordered = is_ordered(template), interval = interval(template),
validate = FALSE)
res <- bind_cols(data, template[fbl_vars])

build_fable(tsbl, response = response_vars(template), distribution = !!distribution_var(template))
build_fable(data, response = response_vars(template), distribution = !!distribution_var(template))
}

#' @export
select.fbl_ts <- function (.data, ...){
res <- select(as_tibble(.data), ...)
res <- select(as_tsibble(.data), ...)
restore_fable(res, .data)
}

Expand Down
4 changes: 4 additions & 0 deletions R/features.R
Expand Up @@ -12,6 +12,7 @@ features_impl <- function(.tbl, .var, features, ...){

# Compute response
key_dt <- key_data(.tbl)
idx <- index_var(.tbl)
.tbl <- as_tibble(.tbl)
if(NCOL(key_dt) > 1){
.tbl <- dplyr::new_grouped_df(.tbl, key_dt)
Expand All @@ -26,6 +27,9 @@ features_impl <- function(.tbl, .var, features, ...){
fmls <- formals(fn)[-1]
fn_safe <- safely(fn, tibble(.rows = 1))
res <- transpose(map(key_dt[[".rows"]], function(i){
# Add index to inputs
dots$.index <- .tbl[[idx]][i]
# Evaluate feature
out <- do.call(fn_safe, c(list(x[i]), dots[intersect(names(fmls), names(dots))]))
if(is.null(names(out[["result"]])))
names(out[["result"]]) <- paste0("..?", seq_along(out[["result"]]))
Expand Down
12 changes: 10 additions & 2 deletions R/fitted.R
Expand Up @@ -52,12 +52,20 @@ hfitted.mdl_ts <- function(object, h, ...) {
fn <- tryCatch(utils::getS3method("hfitted", class(object[["fit"]])),
error = function(e) NULL)
if(is.null(fn)) {
dt <- object$data
resp <- response_vars(object)
n <- nrow(object$data)

# Undo transformations
bt <- lapply(object$transformation, invert_transformation)
mv <- match(measured_vars(dt), names(dt))
dt[mv] <- mapply(calc, bt, dt[measured_vars(dt)], SIMPLIFY = FALSE)
names(dt)[mv] <- resp

n <- nrow(dt)
fits <- rep(NA_real_, n)

for (i in seq_len(n-h)) {
mdl <- tryCatch(refit(object, vec_slice(object$data, seq_len(i))),
mdl <- tryCatch(refit(object, vec_slice(dt, seq_len(i))),
error = function(e) NULL)
if(is.null(mdl)) next
fits[i + h] <- mean(forecast(mdl, h = h, point_forecast = NULL)[[resp]][h])
Expand Down
29 changes: 29 additions & 0 deletions R/outliers.R
@@ -0,0 +1,29 @@
#' Identify outliers
#'
#' Return a table of outlying observations using a fitted model.
#'
#' @param object An object which can identify outliers.
#' @param ... Arguments for further methods.
#'
#' @rdname outliers
#' @export
outliers <- function(object, ...){
UseMethod("outliers")
}

#' @rdname outliers
#' @export
outliers.mdl_df <- function(object, ...){
mbl_vars <- mable_vars(object)
kv <- key_vars(object)
object <- mutate(as_tibble(object),
dplyr::across(all_of(mbl_vars), function(x) lapply(x, outliers, ...)))
object <- pivot_longer(object, mbl_vars, names_to = ".model", values_to = ".outliers")
unnest_tsbl(object, ".outliers", parent_key = c(kv, ".model"))
}

#' @rdname outliers
#' @export
outliers.mdl_ts <- function(object, ...){
object$data[outliers(object$fit, ...),]
}
4 changes: 2 additions & 2 deletions R/reconciliation.R
Expand Up @@ -87,7 +87,7 @@ forecast.lst_mint_mdl <- function(object, key_data,
res <- map(object, function(x, ...) residuals(x, ...), type = "response")
if(length(unique(map_dbl(res, nrow))) > 1){
# Join residuals by index #199
res <- unname(as.matrix(reduce(res, full_join, by = "date")[,-1]))
res <- unname(as.matrix(reduce(res, full_join, by = index_var(res[[1]]))[,-1]))
} else {
res <- matrix(invoke(c, map(res, `[[`, 2)), ncol = length(object))
}
Expand Down Expand Up @@ -348,7 +348,7 @@ forecast.lst_topdwn_mdl <- function(object, key_data,
#'
#' @seealso
#' [`reconcile()`], [`aggregate_key()`]
#' [*Forecasting: Principles and Practice* - Middle-out approach](https://otexts.com/fpp3/middle-out.html)
#' [*Forecasting: Principles and Practice* - Middle-out approach](https://otexts.com/fpp3/single-level.html#middle-out-approach)
#'
#' @export
middle_out <- function(models, split = 1){
Expand Down

0 comments on commit de49ed3

Please sign in to comment.