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: | Mitchell O'Hara-Wild [aut, cre] , Rob Hyndman [aut], Earo Wang [aut] , Di Cook [ctb], George Athanasopoulos [ctb], David Holt [ctb] |
Maintainer: | Mitchell O'Hara-Wild <[email protected]> |
License: | GPL-3 |
Version: | 0.5.0 |
Built: | 2024-11-08 04:21:38 UTC |
Source: | https://github.com/tidyverts/fabletools |
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.
Maintainer: Mitchell O'Hara-Wild [email protected] (ORCID)
Authors:
Rob Hyndman
Earo Wang (ORCID)
Other contributors:
Di Cook [contributor]
George Athanasopoulos [contributor]
David Holt [contributor]
Useful links:
Report bugs at https://github.com/tidyverts/fabletools/issues
Summarise the performance of the model using accuracy measures. Accuracy measures can be computed directly from models as the one-step-ahead fitted residuals are available. When evaluating accuracy on forecasts, you will need to provide a complete dataset that includes the future data and data used to train the model.
## S3 method for class 'mdl_df' accuracy(object, measures = point_accuracy_measures, ...) ## S3 method for class 'mdl_ts' accuracy(object, measures = point_accuracy_measures, ...) ## S3 method for class 'fbl_ts' accuracy(object, data, measures = point_accuracy_measures, ..., by = NULL)
## S3 method for class 'mdl_df' accuracy(object, measures = point_accuracy_measures, ...) ## S3 method for class 'mdl_ts' accuracy(object, measures = point_accuracy_measures, ...) ## S3 method for class 'fbl_ts' accuracy(object, data, measures = point_accuracy_measures, ..., by = NULL)
object |
A model or forecast object |
measures |
A list of accuracy measure functions to compute (such as |
... |
Additional arguments to be passed to measures that use it. |
data |
A dataset containing the complete model dataset (both training and test data). The training portion of the data will be used in the computation of some accuracy measures, and the test data is used to compute the forecast errors. |
by |
Variables over which the accuracy is computed (useful for computing across forecast horizons in cross-validation). If |
library(fable) library(tsibble) library(tsibbledata) library(dplyr) fit <- aus_production %>% filter(Quarter < yearquarter("2006 Q1")) %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) # In-sample training accuracy does not require extra data provided. accuracy(fit) # Out-of-sample forecast accuracy requires the future values to compare with. # All available future data will be used, and a warning will be given if some # data for the forecast window is unavailable. fc <- fit %>% forecast(h = "5 years") fc %>% accuracy(aus_production) # It is also possible to compute interval and distributional measures of # accuracy for models and forecasts which give forecast distributions. fc %>% accuracy( aus_production, measures = list(interval_accuracy_measures, distribution_accuracy_measures) )
library(fable) library(tsibble) library(tsibbledata) library(dplyr) fit <- aus_production %>% filter(Quarter < yearquarter("2006 Q1")) %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) # In-sample training accuracy does not require extra data provided. accuracy(fit) # Out-of-sample forecast accuracy requires the future values to compare with. # All available future data will be used, and a warning will be given if some # data for the forecast window is unavailable. fc <- fit %>% forecast(h = "5 years") fc %>% accuracy(aus_production) # It is also possible to compute interval and distributional measures of # accuracy for models and forecasts which give forecast distributions. fc %>% accuracy( aus_production, measures = list(interval_accuracy_measures, distribution_accuracy_measures) )
agg_vec(x = character(), aggregated = logical(vec_size(x)))
agg_vec(x = character(), aggregated = logical(vec_size(x)))
x |
The vector of values. |
aggregated |
A logical vector to identify which values are |
An aggregation vector extends usual vectors by adding <aggregated>
values.
These vectors are typically produced via the aggregate_key()
function,
however it can be useful to create them manually to produce more complicated
hierarchies (such as unbalanced hierarchies).
agg_vec( x = c(NA, "A", "B"), aggregated = c(TRUE, FALSE, FALSE) )
agg_vec( x = c(NA, "A", "B"), aggregated = c(TRUE, FALSE, FALSE) )
aggregate_index(.data, .window, ..., .offset = "end", .bin_size = NULL)
aggregate_index(.data, .window, ..., .offset = "end", .bin_size = NULL)
.data |
A tsibble. |
.window |
Temporal aggregations to include. The default (NULL) will automatically identify appropriate temporal aggregations. This can be specified in several ways (see details). |
... |
< The value can be:
Returning values with size 0 or >1 was
deprecated as of 1.1.0. Please use |
.offset |
Offset the temporal aggregation windows to align with the start or end of the data. If FALSE, no offset will be applied (giving common breakpoints for temporal bins.) |
.bin_size |
Temporary. Define the number of observations in each temporal bucket |
This feature is very experimental. It currently allows for temporal aggregation of daily data as a proof of concept.
The aggregation .window
can be specified in several ways:
A character string, containing one of "day", "week", "month", "quarter" or "year". This can optionally be preceded by a (positive or negative) integer and a space, or followed by "s".
A number, taken to be in days.
A difftime
object.
library(tsibble) pedestrian %>% # Currently only supports daily data index_by(Date) %>% dplyr::summarise(Count = sum(Count)) %>% # Compute weekly aggregates fabletools:::aggregate_index("1 week", Count = sum(Count))
library(tsibble) pedestrian %>% # Currently only supports daily data index_by(Date) %>% dplyr::summarise(Count = sum(Count)) %>% # Compute weekly aggregates fabletools:::aggregate_index("1 week", Count = sum(Count))
Uses the structural specification given in .spec
to aggregate a time
series. A grouped structure is specified using grp1 * grp2
, and a nested
structure is specified via parent / child
. Aggregating the key structure is
commonly used with forecast reconciliation to produce coherent forecasts over
some hierarchy.
aggregate_key(.data, .spec, ...)
aggregate_key(.data, .spec, ...)
.data |
A tsibble. |
.spec |
The specification of aggregation structure. |
... |
< The value can be:
Returning values with size 0 or >1 was
deprecated as of 1.1.0. Please use |
This function is experimental, and is subject to change in the future.
The way in which the measured variables are aggregated is specified in a
similar way to how [dplyr::summarise()]
is used.
library(tsibble) tourism %>% aggregate_key(Purpose * (State / Region), Trips = sum(Trips))
library(tsibble) tourism %>% aggregate_key(Purpose * (State / Region), Trips = sum(Trips))
Coerce to a dable object
as_dable(x, ...) ## S3 method for class 'tbl_df' as_dable(x, response, method = NULL, seasons = list(), aliases = list(), ...) ## S3 method for class 'tbl_ts' as_dable(x, response, method = NULL, seasons = list(), aliases = list(), ...)
as_dable(x, ...) ## S3 method for class 'tbl_df' as_dable(x, response, method = NULL, seasons = list(), aliases = list(), ...) ## S3 method for class 'tbl_ts' as_dable(x, response, method = NULL, seasons = list(), aliases = list(), ...)
x |
Object to be coerced to a dable ( |
... |
Additional arguments passed to methods |
response |
The character vector of response variable(s). |
method |
The name of the decomposition method. |
seasons |
A named list describing the structure of seasonal components
(such as |
aliases |
A named list of calls describing common aliases computed from components. |
Coerce to a fable object
as_fable(x, ...) ## S3 method for class 'tbl_ts' as_fable(x, response, distribution, ...) ## S3 method for class 'grouped_ts' as_fable(x, response, distribution, ...) ## S3 method for class 'tbl_df' as_fable(x, response, distribution, ...) ## S3 method for class 'fbl_ts' as_fable(x, response, distribution, ...) ## S3 method for class 'grouped_df' as_fable(x, response, distribution, ...) ## S3 method for class 'forecast' as_fable(x, ..., point_forecast = list(.mean = mean))
as_fable(x, ...) ## S3 method for class 'tbl_ts' as_fable(x, response, distribution, ...) ## S3 method for class 'grouped_ts' as_fable(x, response, distribution, ...) ## S3 method for class 'tbl_df' as_fable(x, response, distribution, ...) ## S3 method for class 'fbl_ts' as_fable(x, response, distribution, ...) ## S3 method for class 'grouped_df' as_fable(x, response, distribution, ...) ## S3 method for class 'forecast' as_fable(x, ..., point_forecast = list(.mean = mean))
x |
Object to be coerced to a fable ( |
... |
Additional arguments passed to methods |
response |
The character vector of response variable(s). |
distribution |
The name of the distribution column (can be provided using a bare expression). |
point_forecast |
The point forecast measure(s) which should be returned
in the resulting fable. Specified as a named list of functions which accept
a distribution and return a vector. To compute forecast medians, you can use
|
Coerce a dataset to a mable
as_mable(x, ...) ## S3 method for class 'data.frame' as_mable(x, key = NULL, model = NULL, ...)
as_mable(x, ...) ## S3 method for class 'data.frame' as_mable(x, key = NULL, model = NULL, ...)
x |
A dataset containing a list model column. |
... |
Additional arguments passed to other methods. |
key |
Structural variable(s) that identify each model. |
model |
Identifiers for the columns containing model(s). |
Uses a fitted model to augment the response variable with fitted values and
residuals. Response residuals (back-transformed) are stored in the .resid
column, while innovation residuals (transformed) are stored in the .innov
column.
## S3 method for class 'mdl_df' augment(x, ...) ## S3 method for class 'mdl_ts' augment(x, type = NULL, ...)
## S3 method for class 'mdl_df' augment(x, ...) ## S3 method for class 'mdl_ts' augment(x, type = NULL, ...)
x |
A mable. |
... |
Arguments for model methods. |
type |
Deprecated. |
library(fable) library(tsibbledata) # Forecasting with an ETS(M,Ad,A) model to Australian beer production aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>% augment()
library(fable) library(tsibbledata) # Forecasting with an ETS(M,Ad,A) model to Australian beer production aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>% augment()
Produces a faceted plot of the components used to build the response variable of the dable. Useful for visualising how the components contribute in a decomposition or model.
## S3 method for class 'dcmp_ts' autoplot(object, .vars = NULL, scale_bars = TRUE, level = c(80, 95), ...)
## S3 method for class 'dcmp_ts' autoplot(object, .vars = NULL, scale_bars = TRUE, level = c(80, 95), ...)
object |
A dable. |
.vars |
The column of the dable used to plot. By default, this will be the response variable of the decomposition. |
scale_bars |
If |
level |
If the decomposition contains distributions, which levels should be used to display intervals? |
... |
Further arguments passed to |
library(feasts) library(tsibbledata) aus_production %>% model(STL(Beer)) %>% components() %>% autoplot()
library(feasts) library(tsibbledata) aus_production %>% model(STL(Beer)) %>% components() %>% autoplot()
Produces a forecast plot from a fable. As the original data is not included
in the fable object, it will need to be specified via the data
argument.
The data
argument can be used to specify a shorter period of data, which is
useful to focus on the more recent observations.
## S3 method for class 'fbl_ts' autoplot(object, data = NULL, level = c(80, 95), show_gap = TRUE, ...) ## S3 method for class 'fbl_ts' autolayer( object, data = NULL, level = c(80, 95), point_forecast = list(mean = mean), show_gap = TRUE, ... )
## S3 method for class 'fbl_ts' autoplot(object, data = NULL, level = c(80, 95), show_gap = TRUE, ...) ## S3 method for class 'fbl_ts' autolayer( object, data = NULL, level = c(80, 95), point_forecast = list(mean = mean), show_gap = TRUE, ... )
object |
A fable. |
data |
A tsibble with the same key structure as the fable. |
level |
The confidence level(s) for the plotted intervals. |
show_gap |
Setting this to |
... |
Further arguments passed used to specify fixed aesthetics for the forecasts such as |
point_forecast |
The point forecast measure to be displayed in the plot. |
library(fable) library(tsibbledata) fc <- aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>% forecast(h = "3 years") fc %>% autoplot(aus_production) aus_production %>% autoplot(Beer) + autolayer(fc)
library(fable) library(tsibbledata) fc <- aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>% forecast(h = "3 years") fc %>% autoplot(aus_production) aus_production %>% autoplot(Beer) + autolayer(fc)
Produces a time series plot of one or more variables from a tsibble. If the tsibble contains a multiple keys, separate time series will be identified by colour.
## S3 method for class 'tbl_ts' autoplot(object, .vars = NULL, ...) ## S3 method for class 'tbl_ts' autolayer(object, .vars = NULL, ...)
## S3 method for class 'tbl_ts' autoplot(object, .vars = NULL, ...) ## S3 method for class 'tbl_ts' autolayer(object, .vars = NULL, ...)
object |
A tsibble. |
.vars |
A bare expression containing data you wish to plot. Multiple variables can be plotted using |
... |
Further arguments passed to |
library(fable) library(tsibbledata) library(tsibble) tsibbledata::gafa_stock %>% autoplot(vars(Close, log(Close)))
library(fable) library(tsibbledata) library(tsibble) tsibbledata::gafa_stock %>% autoplot(vars(Close, log(Close)))
bottom_up(models)
bottom_up(models)
models |
A column of models in a mable. |
Reconciles a hierarchy using the bottom up reconciliation method. The response variable of the hierarchy must be aggregated using sums. The forecasted time points must match for all series in the hierarchy.
box_cox()
returns a transformation of the input variable using a Box-Cox
transformation. inv_box_cox()
reverses the transformation.
box_cox(x, lambda) inv_box_cox(x, lambda)
box_cox(x, lambda) inv_box_cox(x, lambda)
x |
a numeric vector. |
lambda |
a numeric value for the transformation parameter. |
The Box-Cox transformation is given by
if . For
,
.
a transformed numeric vector of the same length as x.
Rob J Hyndman & Mitchell O'Hara-Wild
Box, G. E. P. and Cox, D. R. (1964) An analysis of transformations. JRSS B 26 211–246.
library(tsibble) library(dplyr) airmiles %>% as_tsibble() %>% mutate(box_cox = box_cox(value, lambda = 0.3))
library(tsibble) library(dplyr) airmiles %>% as_tsibble() %>% mutate(box_cox = box_cox(value, lambda = 0.3))
Ensemble combination
combination_ensemble(..., weights = c("equal", "inv_var"))
combination_ensemble(..., weights = c("equal", "inv_var"))
... |
Estimated models used in the ensemble. |
weights |
The method used to weight each model in the ensemble. |
Combines multiple model definitions (passed via ...
) to produce a model
combination definition using some combination function (cmbn_fn
). Currently
distributional forecasts are only supported for models producing normally
distributed forecasts.
combination_model(..., cmbn_fn = combination_ensemble, cmbn_args = list())
combination_model(..., cmbn_fn = combination_ensemble, cmbn_args = list())
... |
Model definitions used in the combination. |
cmbn_fn |
A function used to produce the combination. |
cmbn_args |
Additional arguments passed to |
A combination model can also be produced using mathematical operations.
library(fable) library(tsibble) library(tsibbledata) # cmbn1 and cmbn2 are equivalent and equally weighted. aus_production %>% model( cmbn1 = combination_model(SNAIVE(Beer), TSLM(Beer ~ trend() + season())), cmbn2 = (SNAIVE(Beer) + TSLM(Beer ~ trend() + season()))/2 ) # An inverse variance weighted ensemble. aus_production %>% model( cmbn1 = combination_model( SNAIVE(Beer), TSLM(Beer ~ trend() + season()), cmbn_args = list(weights = "inv_var") ) )
library(fable) library(tsibble) library(tsibbledata) # cmbn1 and cmbn2 are equivalent and equally weighted. aus_production %>% model( cmbn1 = combination_model(SNAIVE(Beer), TSLM(Beer ~ trend() + season())), cmbn2 = (SNAIVE(Beer) + TSLM(Beer ~ trend() + season()))/2 ) # An inverse variance weighted ensemble. aus_production %>% model( cmbn1 = combination_model( SNAIVE(Beer), TSLM(Beer ~ trend() + season()), cmbn_args = list(weights = "inv_var") ) )
Weighted combination
combination_weighted(..., weights = NULL)
combination_weighted(..., weights = NULL)
... |
Estimated models used in the ensemble. |
weights |
The numeric weights applied to each model in |
Extract frequencies for common seasonal periods
common_periods(x) ## Default S3 method: common_periods(x) ## S3 method for class 'tbl_ts' common_periods(x) ## S3 method for class 'interval' common_periods(x) get_frequencies(period, ...) ## S3 method for class 'numeric' get_frequencies(period, ...) ## S3 method for class ''NULL'' get_frequencies(period, data, ..., .auto = c("smallest", "largest", "all")) ## S3 method for class 'character' get_frequencies(period, data, ...) ## S3 method for class 'Period' get_frequencies(period, data, ...)
common_periods(x) ## Default S3 method: common_periods(x) ## S3 method for class 'tbl_ts' common_periods(x) ## S3 method for class 'interval' common_periods(x) get_frequencies(period, ...) ## S3 method for class 'numeric' get_frequencies(period, ...) ## S3 method for class ''NULL'' get_frequencies(period, data, ..., .auto = c("smallest", "largest", "all")) ## S3 method for class 'character' get_frequencies(period, data, ...) ## S3 method for class 'Period' get_frequencies(period, data, ...)
x |
An object containing temporal data (such as a |
period |
Specification of the time-series period |
... |
Other arguments to be passed on to methods |
data |
A tsibble |
.auto |
The method used to automatically select the appropriate seasonal periods |
A named vector of frequencies appropriate for the provided data.
https://robjhyndman.com/hyndsight/seasonal-periods/
common_periods(tsibble::pedestrian)
common_periods(tsibble::pedestrian)
These special functions provide interfaces to more complicated functions within the model formulae interface.
common_xregs
common_xregs
The trend
special includes common linear trend regressors in the model. It also supports piecewise linear trend via the knots
argument.
trend(knots = NULL, origin = NULL)
knots |
A vector of times (same class as the data's time index) identifying the position of knots for a piecewise linear trend. |
origin |
An optional time value to act as the starting time for the trend. |
The season
special includes seasonal dummy variables in the model.
season(period = NULL)
period |
The periodic nature of the seasonality. This can be either a number indicating the number of observations in each seasonal period, or text to indicate the duration of the seasonal window (for example, annual seasonality would be "1 year"). |
The fourier
special includes seasonal fourier terms in the model. The maximum order of the fourier terms must be specified using K
.
fourier(period = NULL, K, origin = NULL)
period |
The periodic nature of the seasonality. This can be either a number indicating the number of observations in each seasonal period, or text to indicate the duration of the seasonal window (for example, annual seasonality would be "1 year"). |
K |
The maximum order of the fourier terms. |
origin |
An optional time value to act as the starting time for the fourier series. |
Allows you to extract elements of interest from the model which can be useful in understanding how they contribute towards the overall fitted values.
## S3 method for class 'mdl_df' components(object, ...) ## S3 method for class 'mdl_ts' components(object, ...)
## S3 method for class 'mdl_df' components(object, ...) ## S3 method for class 'mdl_ts' components(object, ...)
object |
A mable. |
... |
Other arguments passed to methods. |
A dable will be returned, which will allow you to easily plot the components and see the way in which components are combined to give forecasts.
library(fable) library(tsibbledata) # Forecasting with an ETS(M,Ad,A) model to Australian beer production aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>% components() %>% autoplot()
library(fable) library(tsibbledata) # Forecasting with an ETS(M,Ad,A) model to Australian beer production aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>% components() %>% autoplot()
A dable (decomposition table) data class (dcmp_ts
) which is a tsibble-like
data structure for representing decompositions. This data class is useful for
representing decompositions, as its print method describes how its columns
can be combined to produce the original data, and has a more appropriate
autoplot()
method for displaying decompositions. Beyond this, a dable
(dcmp_ts
) behaves very similarly to a tsibble (tbl_ts
).
dable(..., response, method = NULL, seasons = list(), aliases = list())
dable(..., response, method = NULL, seasons = list(), aliases = list())
... |
Arguments passed to |
response |
The name of the response variable column. |
method |
The name of the decomposition method. |
seasons |
A named list describing the structure of seasonal components
(such as |
aliases |
A named list of calls describing common aliases computed from components. |
This function allows you to specify a decomposition combination model using
any additive decomposition. It works by first decomposing the data using the
decomposition method provided to dcmp_fn
with the given formula. Secondary
models are used to fit each of the components from the resulting
decomposition. These models are specified after the decomposition formula.
All non-seasonal decomposition components must be specified, and any
unspecified seasonal components will be forecasted using seasonal naive.
These component models will be combined according to the decomposition
method, giving a combination model for the response of the decomposition.
decomposition_model(dcmp, ...)
decomposition_model(dcmp, ...)
dcmp |
A model definition which supports extracting decomposed |
... |
Model definitions used to model the components |
Forecasting: Principles and Practice - Forecasting Decomposition
library(fable) library(feasts) library(tsibble) library(dplyr) vic_food <- tsibbledata::aus_retail %>% filter(State == "Victoria", Industry == "Food retailing") # Identify an appropriate decomposition vic_food %>% model(STL(log(Turnover) ~ season(window = Inf))) %>% components() %>% autoplot() # Use an ETS model to seasonally adjusted data, and SNAIVE to season_year # Any model can be used, and seasonal components will default to use SNAIVE. my_dcmp_spec <- decomposition_model( STL(log(Turnover) ~ season(window = Inf)), ETS(season_adjust ~ season("N")), SNAIVE(season_year) ) vic_food %>% model(my_dcmp_spec) %>% forecast(h="5 years") %>% autoplot(vic_food)
library(fable) library(feasts) library(tsibble) library(dplyr) vic_food <- tsibbledata::aus_retail %>% filter(State == "Victoria", Industry == "Food retailing") # Identify an appropriate decomposition vic_food %>% model(STL(log(Turnover) ~ season(window = Inf))) %>% components() %>% autoplot() # Use an ETS model to seasonally adjusted data, and SNAIVE to season_year # Any model can be used, and seasonal components will default to use SNAIVE. my_dcmp_spec <- decomposition_model( STL(log(Turnover) ~ season(window = Inf)), ETS(season_adjust ~ season("N")), SNAIVE(season_year) ) vic_food %>% model(my_dcmp_spec) %>% forecast(h="5 years") %>% autoplot(vic_food)
distribution_var()
returns a character vector of the distribution variable
in the data.
distribution_var(x)
distribution_var(x)
x |
A dataset containing a distribution variable (such as a fable). |
Estimate a model
estimate(.data, ...) ## S3 method for class 'tbl_ts' estimate(.data, .model, ...)
estimate(.data, ...) ## S3 method for class 'tbl_ts' estimate(.data, .model, ...)
.data |
A data structure suitable for the models (such as a |
... |
Further arguments passed to methods. |
.model |
Definition for the model to be used. |
A fable (forecast table) data class (fbl_ts
) which is a tsibble-like data
structure for representing forecasts. In extension to the key and index from
the tsibble (tbl_ts
) class, a fable (fbl_ts
) must also contain a single
distribution column that uses values from the distributional package.
fable(..., response, distribution)
fable(..., response, distribution)
... |
Arguments passed to |
response |
The character vector of response variable(s). |
distribution |
The name of the distribution column (can be provided using a bare expression). |
Construct a feature set from features available in currently loaded packages. Lists of available features can be found in the following pages:
feature_set(pkgs = NULL, tags = NULL)
feature_set(pkgs = NULL, tags = NULL)
pkgs |
The package(s) from which to search for features. If |
tags |
Tags used to identify similar groups of features. If |
Features can be registered for use with the feature_set()
function using
register_feature()
. This function allows you to register a feature along
with the tags associated with it. If the features are being registered from
within a package, this feature registration should happen at load time using
[.onLoad()]
.
Create scalar valued summary features for a dataset from feature functions.
features(.tbl, .var, features, ...) features_at(.tbl, .vars, features, ...) features_all(.tbl, features, ...) features_if(.tbl, .predicate, features, ...)
features(.tbl, .var, features, ...) features_at(.tbl, .vars, features, ...) features_all(.tbl, features, ...) features_if(.tbl, .predicate, features, ...)
.tbl |
A dataset |
.var |
An expression that produces a vector from which the features are computed. |
features |
A list of functions (or lambda expressions) for the features to compute. |
... |
Additional arguments to be passed to each feature. These arguments will only be passed to features which use it in their formal arguments ( |
.vars |
A tidyselect compatible selection of the column(s) to compute features on. |
.predicate |
A predicate function (or lambda expression) to be applied to the columns or a logical vector. The variables for which .predicate is or returns TRUE are selected. |
Lists of available features can be found in the following pages:
# Provide a set of functions as a named list to features. library(tsibble) tourism %>% features(Trips, features = list(mean = mean, sd = sd)) # Search and use useful features with `feature_set()`. library(feasts) tourism %>% features(Trips, features = feature_set(tags = "autocorrelation")) # Best practice is to use anonymous functions for additional arguments tourism %>% features(Trips, list(~ quantile(., probs=seq(0,1,by=0.2))))
# Provide a set of functions as a named list to features. library(tsibble) tourism %>% features(Trips, features = list(mean = mean, sd = sd)) # Search and use useful features with `feature_set()`. library(feasts) tourism %>% features(Trips, features = feature_set(tags = "autocorrelation")) # Best practice is to use anonymous functions for additional arguments tourism %>% features(Trips, list(~ quantile(., probs=seq(0,1,by=0.2))))
Extracts the fitted values from each of the models in a mable. A tsibble will be returned containing these fitted values. Fitted values will be automatically back-transformed if a transformation was specified.
## S3 method for class 'mdl_df' fitted(object, ...) ## S3 method for class 'mdl_ts' fitted(object, h = 1, ...)
## S3 method for class 'mdl_df' fitted(object, ...) ## S3 method for class 'mdl_ts' fitted(object, h = 1, ...)
object |
A mable or time series model. |
... |
Other arguments passed to the model method for |
h |
The number of steps ahead that these fitted values are computed from. |
The forecast function allows you to produce future predictions of a time series
from fitted models. If the response variable has been transformed in the
model formula, the transformation will be automatically back-transformed
(and bias adjusted if bias_adjust
is TRUE
). More details about
transformations in the fable framework can be found in
vignette("transformations", package = "fable")
.
## S3 method for class 'mdl_df' forecast( object, new_data = NULL, h = NULL, point_forecast = list(.mean = mean), ... ) ## S3 method for class 'mdl_ts' forecast( object, new_data = NULL, h = NULL, bias_adjust = NULL, simulate = FALSE, bootstrap = FALSE, times = 5000, point_forecast = list(.mean = mean), ... )
## S3 method for class 'mdl_df' forecast( object, new_data = NULL, h = NULL, point_forecast = list(.mean = mean), ... ) ## S3 method for class 'mdl_ts' forecast( object, new_data = NULL, h = NULL, bias_adjust = NULL, simulate = FALSE, bootstrap = FALSE, times = 5000, point_forecast = list(.mean = mean), ... )
object |
The time series model used to produce the forecasts |
new_data |
A |
h |
The forecast horison (can be used instead of |
point_forecast |
The point forecast measure(s) which should be returned
in the resulting fable. Specified as a named list of functions which accept
a distribution and return a vector. To compute forecast medians, you can use
|
... |
Additional arguments for forecast model methods. |
bias_adjust |
Deprecated. Please use |
simulate |
Should forecasts be based on simulated future paths instead of analytical results. |
bootstrap |
Should innovations from simulated forecasts be bootstrapped from the model's fitted residuals. This allows the forecast distribution to have a different underlying shape which could better represent the nature of your data. |
times |
The number of future paths for simulations if |
The forecasts returned contain both point forecasts and their distribution.
A specific forecast interval can be extracted from the distribution using the
hilo()
function, and multiple intervals can be obtained using report()
.
These intervals are stored in a single column using the hilo
class, to
extract the numerical upper and lower bounds you can use unpack_hilo()
.
A fable containing the following columns:
.model
: The name of the model used to obtain the forecast. Taken from
the column names of models in the provided mable.
The forecast distribution. The name of this column will be the same as the
dependent variable in the model(s). If multiple dependent variables exist,
it will be named .distribution
.
Point forecasts computed from the distribution using the functions in the
point_forecast
argument.
All columns in new_data
, excluding those whose names conflict with the
above.
library(fable) library(tsibble) library(tsibbledata) library(dplyr) library(tidyr) # Forecasting with an ETS(M,Ad,A) model to Australian beer production beer_fc <- aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>% forecast(h = "3 years") # Compute 80% and 95% forecast intervals beer_fc %>% hilo(level = c(80, 95)) beer_fc %>% autoplot(aus_production) # Forecasting with a seasonal naive and linear model to the monthly # "Food retailing" turnover for each Australian state/territory. library(dplyr) aus_retail %>% filter(Industry == "Food retailing") %>% model( snaive = SNAIVE(Turnover), ets = TSLM(log(Turnover) ~ trend() + season()), ) %>% forecast(h = "2 years 6 months") %>% autoplot(filter(aus_retail, Month >= yearmonth("2000 Jan")), level = 90) # Forecast GDP with a dynamic regression model on log(GDP) using population and # an automatically chosen ARIMA error structure. Assume that population is fixed # in the future. aus_economy <- global_economy %>% filter(Country == "Australia") fit <- aus_economy %>% model(lm = ARIMA(log(GDP) ~ Population)) future_aus <- new_data(aus_economy, n = 10) %>% mutate(Population = last(aus_economy$Population)) fit %>% forecast(new_data = future_aus) %>% autoplot(aus_economy)
library(fable) library(tsibble) library(tsibbledata) library(dplyr) library(tidyr) # Forecasting with an ETS(M,Ad,A) model to Australian beer production beer_fc <- aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) %>% forecast(h = "3 years") # Compute 80% and 95% forecast intervals beer_fc %>% hilo(level = c(80, 95)) beer_fc %>% autoplot(aus_production) # Forecasting with a seasonal naive and linear model to the monthly # "Food retailing" turnover for each Australian state/territory. library(dplyr) aus_retail %>% filter(Industry == "Food retailing") %>% model( snaive = SNAIVE(Turnover), ets = TSLM(log(Turnover) ~ trend() + season()), ) %>% forecast(h = "2 years 6 months") %>% autoplot(filter(aus_retail, Month >= yearmonth("2000 Jan")), level = 90) # Forecast GDP with a dynamic regression model on log(GDP) using population and # an automatically chosen ARIMA error structure. Assume that population is fixed # in the future. aus_economy <- global_economy %>% filter(Country == "Australia") fit <- aus_economy %>% model(lm = ARIMA(log(GDP) ~ Population)) future_aus <- new_data(aus_economy, n = 10) %>% mutate(Population = last(aus_economy$Population)) fit %>% forecast(new_data = future_aus) %>% autoplot(aus_economy)
Use a model's fitted distribution to simulate additional data with similar
behaviour to the response. This is a tidy implementation of
stats::simulate()
.
## S3 method for class 'mdl_df' generate(x, new_data = NULL, h = NULL, times = 1, seed = NULL, ...) ## S3 method for class 'mdl_ts' generate( x, new_data = NULL, h = NULL, times = 1, seed = NULL, bootstrap = FALSE, bootstrap_block_size = 1, ... )
## S3 method for class 'mdl_df' generate(x, new_data = NULL, h = NULL, times = 1, seed = NULL, ...) ## S3 method for class 'mdl_ts' generate( x, new_data = NULL, h = NULL, times = 1, seed = NULL, bootstrap = FALSE, bootstrap_block_size = 1, ... )
x |
A mable. |
new_data |
The data to be generated (time index and exogenous regressors) |
h |
The simulation horizon (can be used instead of |
times |
The number of replications. |
seed |
The seed for the random generation from distributions. |
... |
Additional arguments for individual simulation methods. |
bootstrap |
If TRUE, then forecast distributions are computed using simulation with resampled errors. |
bootstrap_block_size |
The bootstrap block size specifies the number of contiguous residuals to be taken in each bootstrap sample. |
Innovations are sampled by the model's assumed error distribution.
If bootstrap
is TRUE
, innovations will be sampled from the model's
residuals. If new_data
contains the .innov
column, those values will be
treated as innovations for the simulated paths.
library(fable) library(dplyr) UKLungDeaths <- as_tsibble(cbind(mdeaths, fdeaths), pivot_longer = FALSE) UKLungDeaths %>% model(lm = TSLM(mdeaths ~ fourier("year", K = 4) + fdeaths)) %>% generate(UKLungDeaths, times = 5)
library(fable) library(dplyr) UKLungDeaths <- as_tsibble(cbind(mdeaths, fdeaths), pivot_longer = FALSE) UKLungDeaths %>% model(lm = TSLM(mdeaths ~ fourier("year", K = 4) + fdeaths)) %>% generate(UKLungDeaths, times = 5)
Uses the models within a mable to produce a one row summary of their fits. This typically contains information about the residual variance, information criterion, and other relevant summary statistics. Each model will be represented with a row of output.
## S3 method for class 'mdl_df' glance(x, ...) ## S3 method for class 'mdl_ts' glance(x, ...)
## S3 method for class 'mdl_df' glance(x, ...) ## S3 method for class 'mdl_ts' glance(x, ...)
x |
A mable. |
... |
Arguments for model methods. |
library(fable) library(tsibbledata) olympic_running %>% model(lm = TSLM(log(Time) ~ trend())) %>% glance()
library(fable) library(tsibbledata) olympic_running %>% model(lm = TSLM(log(Time) ~ trend())) %>% glance()
This function will return the results of a hypothesis test for each model in the mable.
## S3 method for class 'mdl_df' hypothesize(x, ...) ## S3 method for class 'mdl_ts' hypothesize(x, tests = list(), ...)
## S3 method for class 'mdl_df' hypothesize(x, ...) ## S3 method for class 'mdl_ts' hypothesize(x, tests = list(), ...)
x |
A mable. |
... |
Arguments for model methods. |
tests |
a list of test functions to perform on the model |
library(fable) library(tsibbledata) olympic_running %>% model(lm = TSLM(log(Time) ~ trend())) %>% hypothesize()
library(fable) library(tsibbledata) olympic_running %>% model(lm = TSLM(log(Time) ~ trend())) %>% hypothesize()
Uses a fitted model to interpolate missing values from a dataset.
## S3 method for class 'mdl_df' interpolate(object, new_data, ...) ## S3 method for class 'mdl_ts' interpolate(object, new_data, ...)
## S3 method for class 'mdl_df' interpolate(object, new_data, ...) ## S3 method for class 'mdl_ts' interpolate(object, new_data, ...)
object |
A mable containing a single model column. |
new_data |
A dataset with the same structure as the data used to fit the model. |
... |
Other arguments passed to interpolate methods. |
library(fable) library(tsibbledata) # The fastest running times for the olympics are missing for years during # world wars as the olympics were not held. olympic_running olympic_running %>% model(TSLM(Time ~ trend())) %>% interpolate(olympic_running)
library(fable) library(tsibbledata) # The fastest running times for the olympics are missing for years during # world wars as the olympics were not held. olympic_running olympic_running %>% model(TSLM(Time ~ trend())) %>% interpolate(olympic_running)
This function calculates the impulse response function (IRF) of a time series model. The IRF describes how a model's variables react to external shocks over time.
IRF(x, ...)
IRF(x, ...)
x |
A fitted model object, such as from a VAR or ARIMA model. This model is used to compute the impulse response. |
... |
Additional arguments to be passed to lower-level functions. |
If new_data
contains the .impulse
column, those values will be
treated as impulses for the calculated impulse responses.
The impulse response function provides insight into the dynamic behaviour of a system in response to external shocks. It traces the effect of a one-unit change in the impulse variable on the response variable over a specified number of periods.
Is the element an aggregation of smaller data
is_aggregated(x)
is_aggregated(x)
x |
An object. |
Is the object a dable
is_dable(x)
is_dable(x)
x |
An object. |
Is the object a fable
is_fable(x)
is_fable(x)
x |
An object. |
Is the object a mable
is_mable(x)
is_mable(x)
x |
An object. |
Is the object a model
is_model(x)
is_model(x)
x |
An object. |
Mean Arctangent Absolute Percentage Error
MAAPE(.resid, .actual, na.rm = TRUE, ...)
MAAPE(.resid, .actual, na.rm = TRUE, ...)
.resid |
A vector of residuals from either the training (model accuracy) or test (forecast accuracy) data. |
.actual |
A vector of responses matching the fitted values
(for forecast accuracy, |
na.rm |
Remove the missing values before calculating the accuracy measure |
... |
Additional arguments for each measure. |
Kim, Sungil and Heeyoung Kim (2016) "A new metric of absolute percentage error for intermittent demand forecasts". International Journal of Forecasting, 32(3), 669-679.
A mable (model table) data class (mdl_df
) is a tibble-like data structure
for applying multiple models to a dataset. Each row of the mable refers to a
different time series from the data (identified by the key columns). A mable
must contain at least one column of time series models (mdl_ts
), where the
list column itself (lst_mdl
) describes how these models are related.
mable(..., key = NULL, model = NULL)
mable(..., key = NULL, model = NULL)
... |
A set of name-value pairs. |
key |
Structural variable(s) that identify each model. |
model |
Identifiers for the columns containing model(s). |
mable_vars()
returns a character vector of the model variables in the
object.
mable_vars(x)
mable_vars(x)
x |
A dataset containing models (such as a mable). |
A collection of accuracy measures based on the accuracy of the prediction's direction (say, increasing or decreasing).
MDA(.resid, .actual, na.rm = TRUE, reward = 1, penalty = 0, ...) MDV(.resid, .actual, na.rm = TRUE, ...) MDPV(.resid, .actual, na.rm = TRUE, ...) directional_accuracy_measures
MDA(.resid, .actual, na.rm = TRUE, reward = 1, penalty = 0, ...) MDV(.resid, .actual, na.rm = TRUE, ...) MDPV(.resid, .actual, na.rm = TRUE, ...) directional_accuracy_measures
.resid |
A vector of residuals from either the training (model accuracy) or test (forecast accuracy) data. |
.actual |
A vector of responses matching the fitted values
(for forecast accuracy, |
na.rm |
Remove the missing values before calculating the accuracy measure |
reward , penalty
|
The weights given to correct and incorrect predicted directions. |
... |
Additional arguments for each measure. |
An object of class list
of length 3.
MDA()
: Mean Directional Accuracy
MDV()
: Mean Directional Value
MDPV()
: Mean Directional Percentage Value
Blaskowitz and H. Herwartz (2011) "On economic evaluation of directional forecasts". International Journal of Forecasting, 27(4), 1058-1065.
Point estimate accuracy measures
ME(.resid, na.rm = TRUE, ...) MSE(.resid, na.rm = TRUE, ...) RMSE(.resid, na.rm = TRUE, ...) MAE(.resid, na.rm = TRUE, ...) MPE(.resid, .actual, na.rm = TRUE, ...) MAPE(.resid, .actual, na.rm = TRUE, ...) MASE( .resid, .train, demean = FALSE, na.rm = TRUE, .period, d = .period == 1, D = .period > 1, ... ) RMSSE( .resid, .train, demean = FALSE, na.rm = TRUE, .period, d = .period == 1, D = .period > 1, ... ) ACF1(.resid, na.action = stats::na.pass, demean = TRUE, ...) point_accuracy_measures
ME(.resid, na.rm = TRUE, ...) MSE(.resid, na.rm = TRUE, ...) RMSE(.resid, na.rm = TRUE, ...) MAE(.resid, na.rm = TRUE, ...) MPE(.resid, .actual, na.rm = TRUE, ...) MAPE(.resid, .actual, na.rm = TRUE, ...) MASE( .resid, .train, demean = FALSE, na.rm = TRUE, .period, d = .period == 1, D = .period > 1, ... ) RMSSE( .resid, .train, demean = FALSE, na.rm = TRUE, .period, d = .period == 1, D = .period > 1, ... ) ACF1(.resid, na.action = stats::na.pass, demean = TRUE, ...) point_accuracy_measures
.resid |
A vector of residuals from either the training (model accuracy) or test (forecast accuracy) data. |
na.rm |
Remove the missing values before calculating the accuracy measure |
... |
Additional arguments for each measure. |
.actual |
A vector of responses matching the fitted values
(for forecast accuracy, |
.train |
A vector of responses used to train the model
(for forecast accuracy, the |
demean |
Should the response be demeaned (MASE) |
.period |
The seasonal period of the data (defaulting to 'smallest' seasonal period). from a model, or forecasted values from the forecast. |
d |
Should the response model include a first difference? |
D |
Should the response model include a seasonal difference? |
na.action |
Function to handle missing values. |
An object of class list
of length 8.
middle_out(models, split = 1)
middle_out(models, split = 1)
models |
A column of models in a mable. |
split |
The middle level of the hierarchy from which the bottom-up and top-down approaches are used above and below respectively. |
Reconciles a hierarchy using the middle out reconciliation method. The response variable of the hierarchy must be aggregated using sums. The forecasted time points must match for all series in the hierarchy.
reconcile()
, aggregate_key()
Forecasting: Principles and Practice - Middle-out approach
Reconciles a hierarchy using the minimum trace combination method. The response variable of the hierarchy must be aggregated using sums. The forecasted time points must match for all series in the hierarchy (caution: this is not yet tested for beyond the series length).
min_trace( models, method = c("wls_var", "ols", "wls_struct", "mint_cov", "mint_shrink"), sparse = NULL )
min_trace( models, method = c("wls_var", "ols", "wls_struct", "mint_cov", "mint_shrink"), sparse = NULL )
models |
A column of models in a mable. |
method |
The reconciliation method to use. |
sparse |
If TRUE, the reconciliation will be computed using sparse matrix algebra? By default, sparse matrices will be used if the MatrixM package is installed. |
Wickramasuriya, S. L., Athanasopoulos, G., & Hyndman, R. J. (2019). Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization. Journal of the American Statistical Association, 1-45. https://doi.org/10.1080/01621459.2018.1448825
Trains specified model definition(s) to a dataset. This function will
estimate the a set of model definitions (passed via ...
) to each series
within .data
(as identified by the key structure). The result will be a
mable (a model table), which neatly stores the estimated models in a tabular
structure. Rows of the data identify different series within the data, and
each model column contains all models from that model definition. Each cell
in the mable identifies a single model.
model(.data, ...) ## S3 method for class 'tbl_ts' model(.data, ..., .safely = TRUE)
model(.data, ...) ## S3 method for class 'tbl_ts' model(.data, ..., .safely = TRUE)
.data |
A data structure suitable for the models (such as a |
... |
Definitions for the models to be used. All models must share the same response variable. |
.safely |
If a model encounters an error, rather than aborting the process a NULL model will be returned instead. This allows for an error to occur when computing many models, without losing the results of the successful models. |
It is possible to estimate models in parallel using the
future package. By specifying a
future::plan()
before estimating the models, they will be computed
according to that plan.
Progress on model estimation can be obtained by wrapping the code with
progressr::with_progress()
. Further customisation on how progress is
reported can be controlled using the progressr
package.
library(fable) library(tsibbledata) # Training an ETS(M,Ad,A) model to Australian beer production aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) # Training a seasonal naive and ETS(A,A,A) model to the monthly # "Food retailing" turnover for selected Australian states. library(dplyr) progressr::with_progress( aus_retail %>% filter( Industry == "Food retailing", State %in% c("Victoria", "New South Wales", "Queensland") ) %>% model( snaive = SNAIVE(Turnover), ets = ETS(log(Turnover) ~ error("A") + trend("A") + season("A")), ) )
library(fable) library(tsibbledata) # Training an ETS(M,Ad,A) model to Australian beer production aus_production %>% model(ets = ETS(log(Beer) ~ error("M") + trend("Ad") + season("A"))) # Training a seasonal naive and ETS(A,A,A) model to the monthly # "Food retailing" turnover for selected Australian states. library(dplyr) progressr::with_progress( aus_retail %>% filter( Industry == "Food retailing", State %in% c("Victoria", "New South Wales", "Queensland") ) %>% model( snaive = SNAIVE(Turnover), ets = ETS(log(Turnover) ~ error("A") + trend("A") + season("A")), ) )
Extract the left hand side of a model
model_lhs(model)
model_lhs(model)
model |
A formula |
Extract the right hand side of a model
model_rhs(model)
model_rhs(model)
model |
A formula |
Similarly to pillar's type_sum and obj_sum, model_sum is used to provide brief model summaries.
model_sum(x)
model_sum(x)
x |
The model to summarise |
Suitable for extension packages to create new models for fable.
new_model_class( model = "Unknown model", train = function(.data, formula, specials, ...) abort("This model has not defined a training method."), specials = new_specials(), check = function(.data) { }, prepare = function(...) { }, ..., .env = caller_env(), .inherit = model_definition ) new_model_definition(.class, formula, ..., .env = caller_env(n = 2))
new_model_class( model = "Unknown model", train = function(.data, formula, specials, ...) abort("This model has not defined a training method."), specials = new_specials(), check = function(.data) { }, prepare = function(...) { }, ..., .env = caller_env(), .inherit = model_definition ) new_model_definition(.class, formula, ..., .env = caller_env(n = 2))
model |
The name of the model |
train |
A function that trains the model to a dataset. |
specials |
Special functions produced using |
check |
A function that is used to check the data for suitability with the model. This can be used to check for missing values (both implicit and explicit), regularity of observations, ordered time index, and univariate responses. |
prepare |
This allows you to modify the model class according to user
inputs. |
... |
Further arguments to |
.env |
The environment from which functions should inherit from. |
.inherit |
A model class to inherit from. |
.class |
A model class (typically created with |
formula |
The user's model formula. |
This function produces a new R6 model definition. An understanding of R6 is
not required, however could be useful to provide more sophisticated model
interfaces. All functions have access to self
, allowing the functions for
training the model and evaluating specials to access the model class itself.
This can be useful to obtain elements set in the %TODO
Allows extension packages to make use of the formula parsing of specials.
new_specials(..., .required_specials = NULL, .xreg_specials = NULL)
new_specials(..., .required_specials = NULL, .xreg_specials = NULL)
... |
A named set of functions which used to parse formula inputs |
.required_specials |
The names of specials which must be provided (and if not, are included with no inputs). |
.xreg_specials |
The names of specials which will be only used as inputs to other specials (most commonly |
Produces a new transformation for fable modelling functions which will be used to transform, back-transform, and adjust forecasts.
new_transformation(transformation, inverse) invert_transformation(x, ...)
new_transformation(transformation, inverse) invert_transformation(x, ...)
transformation |
A function which transforms the data |
inverse |
A function which is the inverse of a transformation |
x |
A transformation (such as one created with |
... |
Further arguments passed to other methods. |
For more details about transformations, read the vignette:
vignette("transformations", package = "fable")
scaled_logit <- function(x, lower=0, upper=1){ log((x-lower)/(upper-x)) } inv_scaled_logit <- function(x, lower=0, upper=1){ (upper-lower)*exp(x)/(1+exp(x)) + lower } my_scaled_logit <- new_transformation(scaled_logit, inv_scaled_logit) t_vals <- my_scaled_logit(1:10, 0, 100) t_vals
scaled_logit <- function(x, lower=0, upper=1){ log((x-lower)/(upper-x)) } inv_scaled_logit <- function(x, lower=0, upper=1){ (upper-lower)*exp(x)/(1+exp(x)) + lower } my_scaled_logit <- new_transformation(scaled_logit, inv_scaled_logit) t_vals <- my_scaled_logit(1:10, 0, 100) t_vals
Return a table of outlying observations using a fitted model.
outliers(object, ...) ## S3 method for class 'mdl_df' outliers(object, ...) ## S3 method for class 'mdl_ts' outliers(object, ...)
outliers(object, ...) ## S3 method for class 'mdl_df' outliers(object, ...) ## S3 method for class 'mdl_ts' outliers(object, ...)
object |
An object which can identify outliers. |
... |
Arguments for further methods. |
These accuracy measures can be used to evaluate how accurately a forecast distribution predicts a given actual value.
percentile_score(.dist, .actual, na.rm = TRUE, ...) quantile_score( .dist, .actual, probs = c(0.05, 0.25, 0.5, 0.75, 0.95), na.rm = TRUE, ... ) CRPS(.dist, .actual, n_quantiles = 1000, na.rm = TRUE, ...) distribution_accuracy_measures
percentile_score(.dist, .actual, na.rm = TRUE, ...) quantile_score( .dist, .actual, probs = c(0.05, 0.25, 0.5, 0.75, 0.95), na.rm = TRUE, ... ) CRPS(.dist, .actual, n_quantiles = 1000, na.rm = TRUE, ...) distribution_accuracy_measures
.dist |
The distribution of fitted values from the model, or forecasted values from the forecast. |
.actual |
A vector of responses matching the fitted values
(for forecast accuracy, |
na.rm |
Remove the missing values before calculating the accuracy measure |
... |
Additional arguments for each measure. |
probs |
A vector of probabilities at which the metric is evaluated. |
n_quantiles |
The number of quantiles to use in approximating CRPS when an exact solution is not available. |
An object of class list
of length 2.
A quantile (or percentile) score evaluates how accurately a set of quantiles (or percentiles) from the distribution match the given actual value. This score uses a pinball loss function, and can be calculated via the average of the score function given below:
The score function is given by
if
, and
if
. Where
is the
quantile probability,
is the quantile with probability
, and
is the actual value.
The resulting accuracy measure will average this score over all predicted
points at all desired quantiles (defined via the probs
argument).
The percentile score is uses the same method with probs
set to all
percentiles probs = seq(0.01, 0.99, 0.01)
.
The continuous ranked probability score (CRPS) is the continuous analogue of the pinball loss quantile score defined above. Its value is twice the integral of the quantile score over all possible quantiles:
It can be computed directly from the distribution via:
For some forecast distribution and actual value
.
Calculating the CRPS accuracy measure is computationally difficult for many
distributions, however it can be computed quickly and exactly for Normal and
emperical (sample) distributions. For other distributions the CRPS is
approximated using the quantile score of many quantiles (using the number of
quantiles specified in the n_quantiles
argument).
This function allows you to specify the method used to reconcile forecasts in accordance with its key structure.
reconcile(.data, ...) ## S3 method for class 'mdl_df' reconcile(.data, ...)
reconcile(.data, ...) ## S3 method for class 'mdl_df' reconcile(.data, ...)
.data |
A mable. |
... |
Reconciliation methods applied to model columns within |
library(fable) lung_deaths_agg <- as_tsibble(cbind(mdeaths, fdeaths)) %>% aggregate_key(key, value = sum(value)) lung_deaths_agg %>% model(lm = TSLM(value ~ trend() + season())) %>% reconcile(lm = min_trace(lm)) %>% forecast()
library(fable) lung_deaths_agg <- as_tsibble(cbind(mdeaths, fdeaths)) %>% aggregate_key(key, value = sum(value)) lung_deaths_agg %>% model(lm = TSLM(value ~ trend() + season())) %>% reconcile(lm = min_trace(lm)) %>% forecast()
Applies a fitted model to a new dataset. For most methods this can be done with or without re-estimation of the parameters.
## S3 method for class 'mdl_df' refit(object, new_data, ...) ## S3 method for class 'mdl_ts' refit(object, new_data, ...)
## S3 method for class 'mdl_df' refit(object, new_data, ...) ## S3 method for class 'mdl_ts' refit(object, new_data, ...)
object |
A mable. |
new_data |
A tsibble dataset used to refit the model. |
... |
Additional optional arguments for refit methods. |
library(fable) fit <- as_tsibble(mdeaths) %>% model(ETS(value ~ error("M") + trend("A") + season("A"))) fit %>% report() fit %>% refit(as_tsibble(fdeaths)) %>% report(reinitialise = TRUE)
library(fable) fit <- as_tsibble(mdeaths) %>% model(ETS(value ~ error("M") + trend("A") + season("A"))) fit %>% report() fit %>% refit(as_tsibble(fdeaths)) %>% report(reinitialise = TRUE)
Allows users to find and use features from your package using feature_set()
.
If the features are being registered from within a package, this feature
registration should happen at load time using [.onLoad()]
.
register_feature(fn, tags)
register_feature(fn, tags)
fn |
The feature function |
tags |
Identifying tags |
## Not run: tukey_five <- function(x){ setNames(fivenum(x), c("min", "hinge_lwr", "med", "hinge_upr", "max")) } register_feature(tukey_five, tags = c("boxplot", "simple")) ## End(Not run)
## Not run: tukey_five <- function(x){ setNames(fivenum(x), c("min", "hinge_lwr", "med", "hinge_upr", "max")) } register_feature(tukey_five, tags = c("boxplot", "simple")) ## End(Not run)
Displays the object in a suitable format for reporting.
report(object, ...)
report(object, ...)
object |
The object to report |
... |
Additional options for the reporting function |
Extracts the residuals from each of the models in a mable. A tsibble will be returned containing these residuals.
## S3 method for class 'mdl_df' residuals(object, ...) ## S3 method for class 'mdl_ts' residuals(object, type = "innovation", ...)
## S3 method for class 'mdl_df' residuals(object, ...) ## S3 method for class 'mdl_ts' residuals(object, type = "innovation", ...)
object |
A mable or time series model. |
... |
Other arguments passed to the model method for |
type |
The type of residuals to compute. If |
Returns a tsibble containing only the response variable used in the fitting of a model.
response(object, ...)
response(object, ...)
object |
The object containing response data |
... |
Additional parameters passed on to other methods |
response_vars()
returns a character vector of the response variables in the
object.
response_vars(x)
response_vars(x)
x |
A dataset containing a response variable (such as a mable, fable, or dable). |
A set of future scenarios for forecasting
scenarios(..., names_to = ".scenario")
scenarios(..., names_to = ".scenario")
... |
Input data for each scenario |
names_to |
The column name used to identify each scenario |
This function converts other error metrics such as MSE
into a skill score.
The reference or benchmark forecasting method is the Naive method for
non-seasonal data, and the seasonal naive method for seasonal data.
When used within accuracy.fbl_ts
, it is important that the data
contains both the training and test data, as the training data is used to
compute the benchmark forecasts.
skill_score(measure)
skill_score(measure)
measure |
The accuracy measure to use in computing the skill score. |
skill_score(MSE) library(fable) library(tsibble) lung_deaths <- as_tsibble(cbind(mdeaths, fdeaths)) lung_deaths %>% dplyr::filter(index < yearmonth("1979 Jan")) %>% model( ets = ETS(value ~ error("M") + trend("A") + season("A")), lm = TSLM(value ~ trend() + season()) ) %>% forecast(h = "1 year") %>% accuracy(lung_deaths, measures = list(skill = skill_score(MSE)))
skill_score(MSE) library(fable) library(tsibble) lung_deaths <- as_tsibble(cbind(mdeaths, fdeaths)) lung_deaths %>% dplyr::filter(index < yearmonth("1979 Jan")) %>% model( ets = ETS(value ~ error("M") + trend("A") + season("A")), lm = TSLM(value ~ trend() + season()) ) %>% forecast(h = "1 year") %>% accuracy(lung_deaths, measures = list(skill = skill_score(MSE)))
Helper special for producing a model matrix of exogenous regressors
special_xreg(...)
special_xreg(...)
... |
Arguments for |
Currently the fable_xreg_matrix
helper supports a single argument named
default_intercept
. If this argument is TRUE (passed via ...
above), then
the intercept will be returned in the matrix if not specified (much like the
behaviour of lm()
). If FALSE, then the intercept will only be included if
explicitly requested via 1
in the formula.
Extend the length of data used to fit a model and update the parameters to suit this new data.
stream(object, ...) ## S3 method for class 'mdl_df' stream(object, new_data, ...)
stream(object, ...) ## S3 method for class 'mdl_df' stream(object, new_data, ...)
object |
An object (such as a model) which can be extended with additional data. |
... |
Additional arguments passed on to stream methods. |
new_data |
A dataset of the same structure as was used to fit the model. |
This function will obtain the coefficients (and associated statistics) for each model in the mable.
## S3 method for class 'mdl_df' tidy(x, ...) ## S3 method for class 'mdl_df' coef(object, ...) ## S3 method for class 'mdl_ts' tidy(x, ...) ## S3 method for class 'mdl_ts' coef(object, ...)
## S3 method for class 'mdl_df' tidy(x, ...) ## S3 method for class 'mdl_df' coef(object, ...) ## S3 method for class 'mdl_ts' tidy(x, ...) ## S3 method for class 'mdl_ts' coef(object, ...)
x , object
|
A mable. |
... |
Arguments for model methods. |
library(fable) library(tsibbledata) olympic_running %>% model(lm = TSLM(log(Time) ~ trend())) %>% tidy()
library(fable) library(tsibbledata) olympic_running %>% model(lm = TSLM(log(Time) ~ trend())) %>% tidy()
top_down( models, method = c("forecast_proportions", "average_proportions", "proportion_averages") )
top_down( models, method = c("forecast_proportions", "average_proportions", "proportion_averages") )
models |
A column of models in a mable. |
method |
The reconciliation method to use. |
Reconciles a hierarchy using the top down reconciliation method. The response variable of the hierarchy must be aggregated using sums. The forecasted time points must match for all series in the hierarchy.
Interval estimate accuracy measures
winkler_score(.dist, .actual, level = 95, na.rm = TRUE, ...) pinball_loss(.dist, .actual, level = 95, na.rm = TRUE, ...) scaled_pinball_loss( .dist, .actual, .train, level = 95, na.rm = TRUE, demean = FALSE, .period, d = .period == 1, D = .period > 1, ... ) interval_accuracy_measures
winkler_score(.dist, .actual, level = 95, na.rm = TRUE, ...) pinball_loss(.dist, .actual, level = 95, na.rm = TRUE, ...) scaled_pinball_loss( .dist, .actual, .train, level = 95, na.rm = TRUE, demean = FALSE, .period, d = .period == 1, D = .period > 1, ... ) interval_accuracy_measures
.dist |
The distribution of fitted values from the model, or forecasted values from the forecast. |
.actual |
A vector of responses matching the fitted values
(for forecast accuracy, |
level |
The level of the forecast interval. |
na.rm |
Remove the missing values before calculating the accuracy measure |
... |
Additional arguments for each measure. |
.train |
A vector of responses used to train the model
(for forecast accuracy, the |
demean |
Should the response be demeaned (MASE) |
.period |
The seasonal period of the data (defaulting to 'smallest' seasonal period). from a model, or forecasted values from the forecast. |
d |
Should the response model include a first difference? |
D |
Should the response model include a seasonal difference? |
An object of class list
of length 3.