Skip to content

Commit

Permalink
version 0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchelloharawild authored and cran-robot committed Sep 16, 2019
1 parent df7e425 commit 4d7de0e
Show file tree
Hide file tree
Showing 29 changed files with 329 additions and 169 deletions.
23 changes: 13 additions & 10 deletions DESCRIPTION
@@ -1,5 +1,5 @@
Package: fabletools
Version: 0.1.0
Version: 0.1.1
Title: Core Tools for Packages in the 'fable' Framework
Description: Provides tools, helpers and data structures for developing models and time series functions for 'fable' and extension packages. These tools support a consistent and tidy interface for time series modelling and analysis.
Authors@R:
Expand All @@ -15,15 +15,17 @@ Authors@R:
role = "aut"),
person(given = "Di",
family = "Cook",
role = "ctb"),
person(given = "George",
family = "Athanasopoulos",
role = "ctb"))
Depends: R (>= 3.1.3)
Imports: tsibble (>= 0.8.0), ggplot2 (>= 3.0.0), tidyselect, rlang (>=
0.2.0), stats, dplyr (>= 0.8.0), tidyr (>= 0.8.3), generics,
R6, utils
Imports: tsibble (>= 0.8.0), tibble (>= 1.4.1), ggplot2 (>= 3.0.0),
tidyselect, rlang (>= 0.2.0), stats, dplyr (>= 0.8.0), tidyr
(>= 0.8.3), generics, R6, utils
Suggests: colorspace, covr, crayon, digest, fable, furrr, knitr,
methods, tibble (>= 1.4.1), pillar (>= 1.0.1), feasts,
rmarkdown, scales, spelling, testthat, tsibbledata, lubridate,
SparseM
methods, pillar (>= 1.0.1), feasts, rmarkdown, scales,
spelling, testthat, tsibbledata, lubridate, SparseM
Additional_repositories: https://tidyverts.org/
ByteCompile: true
License: GPL-3
Expand All @@ -34,11 +36,12 @@ LazyData: true
RoxygenNote: 6.1.1
Language: en-GB
NeedsCompilation: no
Packaged: 2019-08-08 12:20:24 UTC; mitchell
Packaged: 2019-09-16 06:09:44 UTC; mitchell
Author: Mitchell O'Hara-Wild [aut, cre],
Rob Hyndman [aut],
Earo Wang [aut],
Di Cook [ctb]
Di Cook [ctb],
George Athanasopoulos [ctb]
Maintainer: Mitchell O'Hara-Wild <mail@mitchelloharawild.com>
Repository: CRAN
Date/Publication: 2019-08-08 14:30:02 UTC
Date/Publication: 2019-09-16 10:50:02 UTC
56 changes: 28 additions & 28 deletions MD5
@@ -1,21 +1,21 @@
aef79ed52a6ba943c86bfe3461858d82 *DESCRIPTION
3147508b85a30e2c9a3f0c9ddbc70d2a *NAMESPACE
b23960bae8c64af166f4b635d346d256 *NEWS.md
384270e8f900a9d59232ab846ffb15ae *R/accuracy.R
848803afbecbe7293f55925f93eda47e *R/aggregate.R
794f6ed2be57230d77fb6d7430034a81 *DESCRIPTION
9a35502a335413db2f84f027a1bdd42d *NAMESPACE
015ea07128ae6f66ac3f1cd6b73de9f2 *NEWS.md
29ded7fa5d8ac5976a28e85236aadff1 *R/accuracy.R
707c4a411424bba8b4b7415f146b4414 *R/aggregate.R
e9d5258c496dd3e45743862b3f055720 *R/box_cox.R
e819a87e6f28bf0fdce118c9d98c68d8 *R/broom.R
32d9a89e7faa5922dd16ca071d3b1a03 *R/broom.R
a723ca2744273ea172de4e1f71b73a87 *R/compat-purrr.R
4ad5bb0db3e0dfb9bd162957bfe03386 *R/components.R
db673942428329ade27d466e0d163bf8 *R/dable.R
aa9df79752b5a56f6d7bcd36a4604954 *R/definitions.R
c1297a9c8558a9b653fbd33e892116af *R/equation.R
779ccb9784c216a7da595a7b133cb197 *R/estimate.R
fbb1fb71c0c19507b94b02a160b0a60c *R/fable.R
c4f920cab4d93ba6d6df8eb3747377d9 *R/estimate.R
9f34cf2832e641f6d0f16db72347a4d5 *R/fable.R
25ca3039e3d6000cdde9ee8de1e0e3b1 *R/fabletools.R
655ca26827e3aef6d85148c94d352a8a *R/features.R
c332fc378fc403e4a711ceaeeb6c2c3b *R/features.R
7740f6849f4988fd72412b9d3a5b6243 *R/fitted.R
55798d8fcbc46c00be1742d6c004b396 *R/forecast.R
9cfe9d930f6c70143ee1bce3b1063aff *R/forecast.R
bf3d49fe78c304a56a28b9849cd02e8e *R/frequency.R
6960957c68f4f0039d36c1dddca69170 *R/generate.R
65b2f28b74a18689811fcd08fc631e0c *R/ggplot_geom.R
Expand All @@ -27,14 +27,14 @@ d0969bcc12ec583f85aa13abde9d4585 *R/guess.R
901fc03b949dc3c47bb3c228a3d44e25 *R/interpolate.R
3c1e694c131dd559d2fb65ed471b15b9 *R/lst_methods.R
607fd43926e1a099b43238b0bc26ccc6 *R/mable.R
44d933c6602eefb02dc12fb3c9673c31 *R/model.R
3a17c04d96a69d87a110c66ed6a6ce2c *R/model.R
95363e0e27a8c26e92262234ffbc343a *R/model_combination.R
2cf3e56b7d473fde52532273c29b1c24 *R/model_decomposition.R
2d080d1e2d7d4f11d21fa94cd4c338c9 *R/model_null.R
b9424a2ecb6df171fa5307651562c091 *R/parse.R
36014c820968dbb91edae3880edc7fb1 *R/plot.R
c6882c4b00c017d4f0549c6f24fa3ab9 *R/quantile.R
cf0c619ddc2c3df1ff3c9cec87f82342 *R/reconciliation.R
4a4912f57d96b0ea4be0d32366424d3c *R/parse.R
3c859d2143105136bbe7346140a61319 *R/plot.R
250005debfd1c3203a40ea425d6f26a5 *R/quantile.R
59ea8568c48bbfb5bc4e2c14f9482366 *R/reconciliation.R
78f05bbf89c27837777a919f3b6d6761 *R/reexports.R
8fc9781e263f3eef401fdfd165400165 *R/refit.R
b0723973bdcd706ed4ea903295e634e6 *R/report.R
Expand All @@ -43,13 +43,13 @@ b0723973bdcd706ed4ea903295e634e6 *R/report.R
299bb581f0915f0e9bdb332d501e5c50 *R/specials.R
c2ef7f87979818e534d047604ace9b59 *R/stream.R
cbdf350bf426c9729a4078546d47e95c *R/tbl_utils.R
e80f74cf55431211559bd632c0506023 *R/transform.R
fc5f777d65ed634b0e30f3558edac4ac *R/transform.R
2c2807619d68338517a09daedfa6835f *R/traverse.R
ca04dd8917eb4af03a4196ddccfc964c *R/utils.R
4f2dde3a0f5adf5ef7408941123158ba *R/utils.R
809820147c40af4c8da46d2f4b6b8e4e *R/zzz.R
81d52c7dd1c57813047670c6b74a75db *README.md
49b2f1d199ea02dcda4d438697bf5cee *build/fabletools.pdf
c08c41a07e4a3cca1dcebb6f8a2adde0 *inst/WORDLIST
b96b3cbd76e97353d0da9a8b30507948 *README.md
9be0401aefa131649acef08a663dc6ef *build/fabletools.pdf
79c8e9770ba89a3f3b0530b559ad3e64 *inst/WORDLIST
b9c294bfe946a57c3b8dd361ce6384de *man/accuracy.Rd
25474b2182544737b7e6914573855c0f *man/aggregate_key.Rd
323ddbd8e0a3a706c0d43c4536bfcf0e *man/as-dable.Rd
Expand All @@ -68,14 +68,14 @@ a1f05db0781ceaf79d2900d980473dfc *man/components.Rd
196c26f9ce894852d832bdf55e418abd *man/dable.Rd
16b0e4c25a5803d4a6bfb5135c160705 *man/decomposition_model.Rd
88583662902703d997b02deb3bf0a61a *man/definitions.Rd
675669dd1f044d461274fa312f34ac67 *man/distribution_accuracy_measures.Rd
85900380d9815d6db482ff5c9d3eec52 *man/distribution_accuracy_measures.Rd
9c997222638d0e8ae8ef4db51ca14a01 *man/distributions.Rd
a0fdb24d9d15c5c5630de61a05615a2e *man/estimate.Rd
0adabf14c45279b920c97d24f4bebde8 *man/fable.Rd
96350e1f1e9f9fc89209757a4a0a0759 *man/fabletools-package.Rd
3de07387dbbfbb49746aac90c07b359b *man/fabletools-package.Rd
a46642dcbcf190ed3fd631e22d62d6b2 *man/fcdist.Rd
07442fa8d93c538d6cf125091a7006db *man/feature_set.Rd
180a09412cd627f00c69a617607cdb4a *man/features.Rd
a5822db55226f558da4ee241579ae850 *man/features.Rd
8012d04e6cc0ebfd1aee0489e94f9146 *man/features_by_pkg.Rd
e9d950335b569185499416191d1337ef *man/features_by_tag.Rd
cdb7be9efaa25f3cca4cf33ade6e1394 *man/figures/README-example-1.png
Expand All @@ -97,7 +97,7 @@ e7315a386366939fde82a760fce20ddc *man/is_hilo.Rd
b942ade165ab9f0bf37e7c0988cf26b7 *man/is_mable.Rd
6de7cfcd8ffac71caae51b1c92bc871e *man/is_model.Rd
1f3409f54c32a96c3d045013a53e594c *man/mable.Rd
c59fbc81ebd50b89be95aa083c638f46 *man/min_trace.Rd
d6a93a606e2dc18c7ed07f2980ae15af *man/min_trace.Rd
7c31cc8e274fd78d7e832806fb3f9bf3 *man/model.Rd
1393b3cb5b12c9184a7e56bc397c088f *man/model_lhs.Rd
f0fda8a3f7df861905315f24d5799a65 *man/model_rhs.Rd
Expand All @@ -123,13 +123,13 @@ fe74782d2d7a04c98808abda763e18ff *man/report.Rd
1be775e28bac074b6af7a24a37aa8c9a *man/scale_level.Rd
86bb716b419bf17501c72dd438875c54 *man/scale_tsibble.Rd
70d386591cc9a237bae1b094fd463348 *man/stream.Rd
5d30b5791176c1bab143575823cc1cda *man/tidy.Rd
750aaaefa9aa2044f8b40f8a48bdecaf *man/tidy.Rd
55fba44415f37c5fb8438e07c44b1c3b *man/traverse.Rd
4bf7ba0f683d07e48d465d0a4a558d7f *man/validate_formula.Rd
8176e3fbb47046d5f7220e147a102483 *tests/testthat.R
e063ecec5a135d3f661c6f37f4351294 *tests/testthat/setup-data.R
b93b054b076aba90b60d8f5a0fd9198d *tests/testthat/setup-models.R
a2117d7ba4eb571dab81885da0c8ebac *tests/testthat/test-accuracy.R
c76f88af365cdd31201e943e5be7aa9b *tests/testthat/test-accuracy.R
ac5d1ff0bea6006ea3379e92d94109cc *tests/testthat/test-broom.R
ba25fb317eac7605c8a0dee7b03c26b4 *tests/testthat/test-combination.R
88e7b04d77f49678426b4b1393bebeb3 *tests/testthat/test-decomposition-model.R
Expand All @@ -142,8 +142,8 @@ ba25fb317eac7605c8a0dee7b03c26b4 *tests/testthat/test-combination.R
5c512358f60a3b13c0f15a11e67a83e4 *tests/testthat/test-interpolate.R
69037b48ff29e48e06e047ee7983f148 *tests/testthat/test-mable.R
8b86f1b5a6a146c4c276420289019d36 *tests/testthat/test-multivariate.R
51d1f51ece6a5a56b43b170efac4c9c0 *tests/testthat/test-parser.R
dc6868b99ddd1ab598cb2b17ce6e915f *tests/testthat/test-reconciliation.R
1a2c9dd2ca3e208177d07758899b881b *tests/testthat/test-parser.R
996c0dd737f6f2ee0e9376b9ee0f07ba *tests/testthat/test-reconciliation.R
9a5176496e53324c9cfb5bed43de4eb4 *tests/testthat/test-spelling.R
60f4ae0c80a670fce0d5e87435f9c438 *tests/testthat/test-transformations.R
880d0607b40a40f313f0ffa9df705b16 *tests/testthat/test-validate_model.R
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -39,6 +39,7 @@ S3method(c,fcdist)
S3method(c,hilo)
S3method(c,lst_mdl)
S3method(coef,mdl_df)
S3method(coef,mdl_ts)
S3method(common_periods,default)
S3method(common_periods,interval)
S3method(common_periods,tbl_ts)
Expand Down Expand Up @@ -148,6 +149,7 @@ S3method(unique,fcdist)
S3method(unique,hilo)
export("%>%")
export(ACF1)
export(CRPS)
export(GeomForecast)
export(MAE)
export(MAPE)
Expand Down Expand Up @@ -293,6 +295,7 @@ importFrom(stats,qnorm)
importFrom(stats,quantile)
importFrom(stats,residuals)
importFrom(stats,var)
importFrom(tibble,new_tibble)
importFrom(tidyr,gather)
importFrom(tidyr,nest)
importFrom(tidyr,spread)
Expand Down
19 changes: 19 additions & 0 deletions NEWS.md
@@ -1,3 +1,22 @@
# fabletools 0.1.1

## Breaking changes

* Updated method names available for `min_trace()` reconciliation (@GeorgeAthana).

## Improvements

* Improved error messaging for failing features.
* Added Continuous Ranked Probability Score (`CRPS()`) accuracy measure.
* Transformations of features are now computed for separately for each key, allowing transformations such as `scale(value)` to be used.
* Added structural scaling method for MinT (`min_trace(method = "wls_struct")`) forecast reconciliation (@GeorgeAthana).
* Performance improvements.
* Documentation improvements.

## Bug fixes

* Added failure condition for disjoint reconciliation graphs.

# fabletools 0.1.0

* First release.
Expand Down
32 changes: 28 additions & 4 deletions R/accuracy.R
Expand Up @@ -122,11 +122,35 @@ percentile_score <- function(.dist, .actual, na.rm = TRUE, ...){
mean(na.rm = na.rm)
}

#' @rdname distribution_accuracy_measures
#' @export
CRPS <- function(.dist, .actual, n_quantiles = 1000, na.rm = TRUE, ...){
if(is_dist_normal(.dist)){
mean <- map_dbl(.dist, `[[`, "mean")
sd <- map_dbl(.dist, `[[`, "sd")
z <- (.actual-mean)/sd
z <- sd*(z*(2*stats::pnorm(z)-1)+2*stats::dnorm(z)-1/sqrt(pi))
mean(z, na.rm = na.rm)
}
else{
probs <- seq(0, 1, length.out = n_quantiles + 2)[seq_len(n_quantiles) + 1]
percentiles <- quantile(.dist, probs)
if(length(percentiles[[1]]) > 1) abort("Percentile scores are not supported for multivariate distributions.")
z <- map2_dbl(percentiles, probs, function(percentile, prob){
L <- ifelse(.actual < percentile[[1]], (1-prob), prob)*abs(percentile[[1]]-.actual)
mean(L, na.rm = na.rm)
})
2 * mean(z, na.rm = na.rm)
}
}

#' Distribution accuracy measures
#'
#' @inheritParams interval_accuracy_measures
#' @param n_quantiles The number of quantiles to use in approximating CRPS when an exact solution is not available.
#'
#' @export
distribution_accuracy_measures <- list(percentile = percentile_score)
distribution_accuracy_measures <- list(percentile = percentile_score, CRPS = CRPS)

#' Evaluate accuracy of a forecast or model
#'
Expand Down Expand Up @@ -282,21 +306,21 @@ accuracy.fbl_ts <- function(object, data, measures = point_accuracy_measures, ..
}

grp <- c(syms(by), groups(object))
by <- union(expr_text(index(object)), by)
by <- union(index_var(object), by)


if(!(".model" %in% by)){
warn('Accuracy measures should be computed separately for each model, have you forgotten to add ".model" to your `by` argument?')
}

if(NROW(missing_test <- suppressWarnings(anti_join(object, data, by = intersect(colnames(data), by)))) > 0){
n_miss <- length(unique(missing_test[[as_string(index(missing_test))]]))
n_miss <- length(unique(missing_test[[index_var(missing_test)]]))
warn(sprintf(
"The future dataset is incomplete, incomplete out-of-sample data will be treated as missing.
%i %s %s",
n_miss,
ifelse(n_miss==1, "observation is missing at", "observations are missing between"),
paste(unique(range(missing_test[[expr_text(index(missing_test))]])), collapse = " and ")
paste(unique(range(missing_test[[index_var(missing_test)]])), collapse = " and ")
))
}

Expand Down
45 changes: 12 additions & 33 deletions R/aggregate.R
@@ -1,34 +1,3 @@
parse_aggregation <- function(spec){
if(!is_call(spec)){
return(as_string(spec))
}
eval_tidy(spec, env = env(
`*` = function(e1, e2) {
e1 <- parse_aggregation(enexpr(e1))
e2 <- parse_aggregation(enexpr(e2))
c(
e1, e2,
flatten(map(e1, function(x){
map(e2, function(y){
c(x, y)
})
}))
)
},
`/` = function(e1, e2) {
e1 <- enexpr(e1)
e2 <- enexpr(e2)
if(is_call(e1)) abort("Hierarchical structure must be specified for specific nodes. Try adding more parenthesis.")
e1 <- parse_aggregation(e1)
e2 <- parse_aggregation(e2)
c(
e1,
map(e2, function(x, y) c(y, x), e1)
)
}
))
}

#' Expand a dataset to include other levels of aggregation
#'
#' Uses the structural specification given in `.spec` to aggregate a time
Expand Down Expand Up @@ -71,9 +40,17 @@ aggregate_key.tbl_ts <- function(.data, .spec = NULL, ...){
}

# Key combinations
key_comb <- c(list(chr()), parse_aggregation(.spec))
tm <- stats::terms(new_formula(lhs = NULL, rhs = .spec), env = empty_env())
key_comb <- attr(tm, "factors")
key_vars <- rownames(key_comb)
key_comb <- map(split(key_comb, col(key_comb)), function(x) key_vars[x!=0])

if(attr(tm, "intercept")){
key_comb <- c(list(chr()), key_comb)
}

idx <- index2(.data)
intvl <- interval(.data)
.data <- as_tibble(.data)

agg_dt <- bind_row_attrb(map(key_comb, function(x){
Expand All @@ -91,7 +68,9 @@ aggregate_key.tbl_ts <- function(.data, .spec = NULL, ...){
.data <- ungroup(.data)

# Return tsibble
build_tsibble_meta(.data, key_data = key_dt, index = as_string(idx), index2 = as_string(idx), ordered = TRUE) %>%
build_tsibble_meta(.data, key_data = key_dt, index = as_string(idx),
index2 = as_string(idx), ordered = TRUE,
interval = intvl) %>%
mutate(!!!set_names(map(kv, function(x) expr(agg_key(!!sym(x)))), kv))
}

Expand Down
8 changes: 7 additions & 1 deletion R/broom.R
Expand Up @@ -32,7 +32,7 @@ augment.mdl_df <- function(x, ...){
augment.mdl_ts <- function(x, ...){
tryCatch(augment(x[["fit"]], ...),
error = function(e){
idx <- as_string(index(x$data))
idx <- index_var(x$data)
resp <- x$response
if(length(resp) > 1){
response(x) %>%
Expand Down Expand Up @@ -130,3 +130,9 @@ coef.mdl_df <- function(object, ...){
tidy.mdl_ts <- function(x, ...){
tidy(x$fit, ...)
}

#' @rdname tidy
#' @export
coef.mdl_ts <- function(object, ...){
tidy(object, ...)
}
2 changes: 1 addition & 1 deletion R/estimate.R
Expand Up @@ -26,7 +26,7 @@ estimate.tbl_ts <- function(.data, .model, ...){
# Compute response data (as attributes shouldn't change, using this approach should be much faster)
.dt_attr <- attributes(.data)
resp <- map(parsed$expressions, eval_tidy, data = .data, env = .model$specials)
.data <- unclass(.data)[expr_text(index(.data))]
.data <- unclass(.data)[index_var(.data)]
.data[map_chr(parsed$expressions, expr_text)] <- resp
attributes(.data) <- c(attributes(.data), .dt_attr[setdiff(names(.dt_attr), names(attributes(.data)))])

Expand Down

0 comments on commit 4d7de0e

Please sign in to comment.