Movatterモバイル変換


[0]ホーム

URL:


Title:Tidy Tools for Forecasting
Version:0.2.6
Description: Tidies up the forecasting modeling and prediction work flow, extends the 'broom' package with 'sw_tidy', 'sw_glance', 'sw_augment', and 'sw_tidy_decomp' functions for various forecasting models, and enables converting 'forecast' objects to "tidy" data frames with 'sw_sweep'.
URL:https://business-science.github.io/sweep/,https://github.com/business-science/sweep
BugReports:https://github.com/business-science/sweep/issues
License:GPL (≥ 3)
Encoding:UTF-8
LazyData:true
Depends:R (≥ 3.3.0)
Imports:broom (≥ 0.5.6), dplyr (≥ 1.0.0), forecast (≥ 8.0), rlang,tibble (≥ 1.2), timetk (≥ 2.1.0)
Suggests:fracdiff, ggplot2, knitr, lubridate, purrr, readr, rmarkdown,scales, stringr, testthat (≥ 2.0.0), tidyr (≥ 1.0.0),tidyquant, zoo
RoxygenNote:7.3.2
VignetteBuilder:knitr
Config/testthat/edition:2
NeedsCompilation:no
Packaged:2025-08-28 15:15:39 UTC; mdancho
Author:Matt Dancho [aut, cre], Davis Vaughan [aut]
Maintainer:Matt Dancho <mdancho@business-science.io>
Repository:CRAN
Date/Publication:2025-08-28 19:00:02 UTC

sweep: Tidy Tools for Forecasting

Description

Thesweep package "tidies" up the modeling workflow of theforecast package.

Details

The model and forecast objects are not covered bythebroom package. It includes thesw_tidy(),sw_glance(),andsw_augment() functions that work in a similar capacity asbroom functions.In addition, it providessw_tidy_decomp() to tidy decomposition, andsw_sweep() to coerceforecast objects to "tibbles" for easy visualization withggplot2and manipulation withdplyr.

To learn more aboutsweep, start with the vignettes:browseVignettes(package = "sweep")

Author(s)

Maintainer: Matt Danchomdancho@business-science.io

Authors:

See Also

Useful links:


Adds a sequential index column to a data frame

Description

Adds a sequential index column to a data frame

Usage

add_index(ret, rename_index)

Arguments

ret

An object of class tibble

rename_index

A variable indicating the index name to be used in thetibble returned


Print the ARIMA model parameters

Description

Refer to forecast:::arima.string.forecast arima.R

Usage

arima_string(object, padding = FALSE)

Arguments

object

An object of class Arima

padding

Add padding to the name returned


Print the BATS model parameters

Description

Refer to forecast:::makeText.forecast bats.R

Usage

bats_string(object)

Arguments

object

An object of class bats


Fictional sales data for bike shops purchasing Cannondale bikes

Description

A dataset containing the fictional bicycle orders spanning 2011 through 2015.Hypothetically, thebike_sales data are similar to sales data mainatainedin a business' sales data base. The unit price and model names come fromdata provided by model for the bicycle manufacturer, Cannondale (2016).The customers (bicycle shops) including name, location, etc andthe orders including quantity purchased and order dates are fictional.The data is intended for implementing business analytics techniques(e.g. forecast, clustering, etc) to identify underlying trends.

Usage

bike_sales

Format

A data frame with 15644 rows and 17 variables:

order.date

Date the order was placed

order.id

A unique order identification number

order.line

The sequential identification number for products on and order

quantity

Number of units purchased

price

The unit price of the bicycle

price.ext

The extended price = price x quantity

customer.id

A unique customer identification number

bikeshop.name

The customer name

bikeshop.city

The city that the bike shop is located

bikeshop.state

The state that the bike shop is located

latitude

The geograhpic latitude of the customer location

longitude

The geograhpic longitude of the customer location

product.id

A unique product identification number

model

The model name of the bicycle

category.primary

The main bicycle category, either "Mountain" or "Road"

category.secondary

One of nine more specific bicycle categories

frame

The bicycle frame material, either "Carbon" or "Aluminum"

Source

The 2016 bicycle model names and prices originated fromhttps://www.cannondale.com/en-us


Objects exported from other packages

Description

These objects are imported from other packages. Follow the linksbelow to see their documentation.

dplyr

%>%


Augment data according to a tidied model

Description

Given an R statistical model or other non-tidy object, add columns to theoriginal dataset such as predictions, residuals and cluster assignments.

Usage

sw_augment(x, ...)

Arguments

x

model or other R object to convert to data frame

...

other arguments passed to methods

Details

sw_augment() is a wrapper forbroom::augment(). The benefit ofsw_augmentis that it has methods for various time-series model classes such asHoltWinters,ets,Arima, etc.

For non-time series,sw_augment() defaults tobroom::augment().The only difference is that the return is a tibble.

Note that by convention the first argument is almost alwaysdata,which specifies the original data object. This is not part of the S3signature, partly because it prevents rowwise_df_tidiers fromtaking a column name as the first argument.

See Also

broom::augment()


Default augment method

Description

By default,sw_augment() usesbroom::augment() to convert its output.

Usage

## Default S3 method:sw_augment(x, ...)

Arguments

x

an object to be tidied

...

extra arguments passed tobroom::augment()

Value

A tibble generated bybroom::augment()


Augments data

Description

Augments data

Usage

sw_augment_columns(ret, data, rename_index, timetk_idx = FALSE)

Arguments

ret

An object of class tibble

data

Any time series data that is to be augmented

rename_index

A variable indicating the index name to be used in thetibble returned

timetk_idx

Uses the timetk index (irregular time index) if present.


Construct a single row summary "glance" of a model, fit, or otherobject

Description

Construct a single row summary "glance" of a model, fit, or otherobject

Usage

sw_glance(x, ...)

Arguments

x

model or other R object to convert to single-row data frame

...

other arguments passed to methods

Details

sw_glance() is a wrapper forbroom::glance(). The benefit ofsw_glanceis that it has methods for various time-series model classes such asHoltWinters,ets,Arima, etc.sw_glance methods always return either a one-row tibble orNULL.The single row includes summary statistics relevent to the model accuracy,which can be used to assess model fit and quality.

For non-time series,sw_glance() defaults tobroom::glance().The only difference is that the return is a tibble.

Value

single-row tibble with model summary information.

See Also

broom::glance()


Default glance method

Description

By default,sw_glance() usesbroom::glance() to convert its output.

Usage

## Default S3 method:sw_glance(x, ...)

Arguments

x

an object to be tidied

...

extra arguments passed tobroom::glance()

Value

A tibble generated bybroom::glance()


Tidy forecast objects

Description

Tidy forecast objects

Usage

sw_sweep(x, fitted = FALSE, timetk_idx = FALSE, rename_index = "index", ...)

Arguments

x

A time-series forecast of classforecast.

fitted

Whether or not to return the fitted values (model values) in the results.FALSE by default.

timetk_idx

If timetk index (non-regularized index) is present, uses itto develop forecast. Otherwise uses default index.

rename_index

Enables the index column to be renamed.

...

Additional arguments passed totimetk::tk_make_future_timeseries()

Details

sw_sweep is designedto coerceforecast objects from theforecast packageintotibble objects in a "tidy" format (long).The returned object contains both the actual valuesand the forecasted values including the point forecast and upper and lowerconfidence intervals.

Thetimetk_idx argument is used to modify the return format of the index.

The index column name can be changed using therename_index argument.

Value

Returns atibble object.

See Also

timetk::tk_make_future_timeseries()

Examples

library(forecast)library(dplyr)# ETS forecastsUSAccDeaths %>%    ets() %>%    forecast(level = c(80, 95, 99)) %>%    sw_sweep()

Tidy the result of a time-series model into a summary tibble

Description

Tidy the result of a time-series model into a summary tibble

Usage

sw_tidy(x, ...)

Arguments

x

An object to be converted into a tibble ("tidy" data.frame)

...

extra arguments

Details

sw_tidy() is a wrapper forbroom::tidy(). The main benefit ofsw_tidy()is that it has methods for various time-series model classes such asHoltWinters,ets,Arima, etc.sw_tidy() methods always returns a "tidy" tibble with model coefficient / parameters.

For non-time series,sw_tidy() defaults tobroom::tidy().The only difference is that the return is a tibble.The output ofsw_tidy() is always a tibble with disposable row names. It istherefore suited for further manipulation by packages like dplyr andggplot2.

Value

a tibble

See Also

broom::tidy()

Examples

library(dplyr)library(forecast)WWWusage %>%    auto.arima() %>%    sw_tidy(conf.int = TRUE)

Default tidying method

Description

By default,sw_tidy() usesbroom::tidy() to convert its output.

Usage

## Default S3 method:sw_tidy(x, ...)

Arguments

x

an object to be tidied

...

extra arguments passed tobroom::tidy()

Value

A tibble generated bybroom::tidy()


Coerces decomposed time-series objects to tibble format.

Description

Coerces decomposed time-series objects to tibble format.

Usage

sw_tidy_decomp(x, timetk_idx = FALSE, rename_index = "index", ...)

Arguments

x

A time-series object of classstl,ets,decomposed.ts,HoltWinters,bats ortbats.

timetk_idx

WhenTRUE, uses a timetk index (irregular, typically date or datetime) if present.

rename_index

Enables the index column to be renamed.

...

Not used.

Details

sw_tidy_decomp is designedto coerce time-series objects with decompositions totibble objects.

A regularized time index is always constructed. If no time index isdetected, a sequential index is returned as a default.The index column name can be changed using therename_index argument.

Value

Returns atibble object.

Examples

library(dplyr)library(forecast)library(sweep)# Decompose ETS modelUSAccDeaths %>%    ets() %>%    sw_tidy_decomp()# Decompose STL objectUSAccDeaths %>%    stl(s.window = 'periodic') %>%    sw_tidy_decomp()

Print the TBATS model parameters

Description

Refer to forecast:::makeTextTBATS.forecast bats.R

Usage

tbats_string(object)

Arguments

object

An object of class bats or tbats


Tidying methods for HoltWinters modeling of time series

Description

These methods tidyHoltWinters models of univariate timeseries.

Usage

## S3 method for class 'HoltWinters'sw_tidy(x, ...)## S3 method for class 'HoltWinters'sw_glance(x, ...)## S3 method for class 'HoltWinters'sw_augment(x, data = NULL, rename_index = "index", timetk_idx = FALSE, ...)## S3 method for class 'HoltWinters'sw_tidy_decomp(x, timetk_idx = FALSE, rename_index = "index", ...)

Arguments

x

An object of class "HoltWinters"

...

Additional parameters (not used)

data

Used withsw_augment only.NULL by default which simply returns augmented columns only.User can supply the original data, which returns the data + augmented columns.

rename_index

Used withsw_augment only.A string representing the name of the index generated.

timetk_idx

Used withsw_augment andsw_tidy_decomp.WhenTRUE, uses a timetk index (irregular, typically date or datetime) if present.

Value

sw_tidy() returns one row for each model parameter,with two columns:

sw_glance() returns one row with the following columns:

sw_augment() returns a tibble with the following time series attributes:

sw_tidy_decomp() returns a tibble with the following time series attributes:

See Also

stats::HoltWinters()

Examples

library(dplyr)library(forecast)fit_hw <- USAccDeaths %>%    stats::HoltWinters()sw_tidy(fit_hw)sw_glance(fit_hw)sw_augment(fit_hw)sw_tidy_decomp(fit_hw)

Tidying methods for StructTS (Error, Trend, Seasonal) / exponential smoothingmodeling of time series

Description

These methods tidy the coefficients of StructTS models of univariate timeseries.

Usage

## S3 method for class 'StructTS'sw_tidy(x, ...)## S3 method for class 'StructTS'sw_glance(x, ...)## S3 method for class 'StructTS'sw_augment(x, data = NULL, timetk_idx = FALSE, rename_index = "index", ...)

Arguments

x

An object of class "StructTS"

...

Additional parameters (not used)

data

Used withsw_augment only.NULL by default which simply returns augmented columns only.User can supply the original data, which returns the data + augmented columns.

timetk_idx

Used withsw_augment only.Uses a irregular timetk index if present.

rename_index

Used withsw_augment only.A string representing the name of the index generated.

Value

sw_tidy() returns one row for each model parameter,with two columns:

sw_glance() returns one row with the columns

sw_augment() returns a tibble with the following time series attributes:

See Also

stats::StructTS()

Examples

library(dplyr)library(forecast)fit_StructTS <- WWWusage %>%    StructTS()sw_tidy(fit_StructTS)sw_glance(fit_StructTS)sw_augment(fit_StructTS)

Tidying methods for ARIMA modeling of time series

Description

These methods tidy the coefficients of ARIMA models of univariate timeseries.

Usage

## S3 method for class 'Arima'sw_tidy(x, ...)## S3 method for class 'Arima'sw_glance(x, ...)## S3 method for class 'Arima'sw_augment(x, data = NULL, rename_index = "index", timetk_idx = FALSE, ...)## S3 method for class 'stlm'sw_tidy(x, ...)

Arguments

x

An object of class "Arima"

...

Additional parameters (not used)

data

Used withsw_augment only.NULL by default which simply returns augmented columns only.User can supply the original data, which returns the data + augmented columns.

rename_index

Used withsw_augment only.A string representing the name of the index generated.

timetk_idx

Used withsw_augment only.Uses a irregular timetk index if present.

Value

sw_tidy() returns one row for each coefficient in the model,with five columns:

sw_glance() returns one row with the columns

sw_augment() returns a tibble with the following time series attributes:

sw_tidy() returns the underlying ETS or ARIMA model'ssw_tidy()one row for each coefficient in the model,with five columns:

See Also

stats::arima()

Examples

library(dplyr)library(forecast)fit_arima <- WWWusage %>%    auto.arima()sw_tidy(fit_arima)sw_glance(fit_arima)sw_augment(fit_arima)

Tidying methods for BATS and TBATS modeling of time series

Description

Tidying methods for BATS and TBATS modeling of time series

Usage

## S3 method for class 'bats'sw_tidy(x, ...)## S3 method for class 'bats'sw_glance(x, ...)## S3 method for class 'bats'sw_augment(x, data = NULL, rename_index = "index", timetk_idx = FALSE, ...)## S3 method for class 'bats'sw_tidy_decomp(x, timetk_idx = FALSE, rename_index = "index", ...)

Arguments

x

An object of class "bats" or "tbats"

...

Additional parameters (not used)

data

Used withsw_augment only.NULL by default which simply returns augmented columns only.User can supply the original data, which returns the data + augmented columns.

rename_index

Used withsw_augment only.A string representing the name of the index generated.

timetk_idx

Used withsw_augment andsw_tidy_decomp.WhenTRUE, uses a timetk index (irregular, typically date or datetime) if present.

Value

sw_tidy() returns one row for each model parameter,with two columns:

sw_glance() returns one row with the columns

sw_augment() returns a tibble with the following time series attributes:

sw_tidy_decomp() returns a tibble with the following time series attributes:

See Also

forecast::bats(),forecast::tbats()

Examples

library(dplyr)library(forecast)fit_bats <- WWWusage %>%    bats()sw_tidy(fit_bats)sw_glance(fit_bats)sw_augment(fit_bats)

Tidying methods for decomposed time series

Description

Tidying methods for decomposed time series

Usage

## S3 method for class 'decomposed.ts'sw_tidy_decomp(x, timetk_idx = FALSE, rename_index = "index", ...)

Arguments

x

An object of class "decomposed.ts"

timetk_idx

Used withsw_augment andsw_tidy_decomp.WhenTRUE, uses a timetk index (irregular, typically date or datetime) if present.

rename_index

Used withsw_augment andsw_tidy_decomp.A string representing the name of the index generated.

...

Not used.

Value

sw_tidy_decomp() returns a tibble with the following time series attributes:

See Also

decompose()

Examples

library(dplyr)library(forecast)fit_decomposed <- USAccDeaths %>%    decompose()sw_tidy_decomp(fit_decomposed)

Tidying methods for ETS (Error, Trend, Seasonal) exponential smoothingmodeling of time series

Description

Tidying methods for ETS (Error, Trend, Seasonal) exponential smoothingmodeling of time series

Usage

## S3 method for class 'ets'sw_tidy(x, ...)## S3 method for class 'ets'sw_glance(x, ...)## S3 method for class 'ets'sw_augment(x, data = NULL, timetk_idx = FALSE, rename_index = "index", ...)## S3 method for class 'ets'sw_tidy_decomp(x, timetk_idx = FALSE, rename_index = "index", ...)

Arguments

x

An object of class "ets"

...

Not used.

data

Used withsw_augment only.NULL by default which simply returns augmented columns only.User can supply the original data, which returns the data + augmented columns.

timetk_idx

Used withsw_augment andsw_tidy_decomp.WhenTRUE, uses a timetk index (irregular, typically date or datetime) if present.

rename_index

Used withsw_augment andsw_tidy_decomp.A string representing the name of the index generated.

Value

sw_tidy() returns one row for each model parameter,with two columns:

sw_glance() returns one row with the columns

sw_augment() returns a tibble with the following time series attributes:

sw_tidy_decomp() returns a tibble with the following time series attributes:

See Also

forecast::ets()

Examples

library(dplyr)library(forecast)fit_ets <- WWWusage %>%    ets()sw_tidy(fit_ets)sw_glance(fit_ets)sw_augment(fit_ets)sw_tidy_decomp(fit_ets)

Tidying methods for Nural Network Time Series models

Description

These methods tidy the coefficients of NNETAR models of univariate timeseries.

Usage

## S3 method for class 'nnetar'sw_tidy(x, ...)## S3 method for class 'nnetar'sw_glance(x, ...)## S3 method for class 'nnetar'sw_augment(x, data = NULL, timetk_idx = FALSE, rename_index = "index", ...)

Arguments

x

An object of class "nnetar"

...

Additional parameters (not used)

data

Used withsw_augment only.NULL by default which simply returns augmented columns only.User can supply the original data, which returns the data + augmented columns.

timetk_idx

Used withsw_augment only.Uses a irregular timetk index if present.

rename_index

Used withsw_augment only.A string representing the name of the index generated.

Value

sw_tidy() returns one row for each model parameter,with two columns:

sw_glance() returns one row with the columns

sw_augment() returns a tibble with the following time series attributes:

See Also

forecast::nnetar()

Examples

library(dplyr)library(forecast)fit_nnetar <- lynx %>%    nnetar()sw_tidy(fit_nnetar)sw_glance(fit_nnetar)sw_augment(fit_nnetar)

Tidying methods for STL (Seasonal, Trend, Level) decomposition of time series

Description

Tidying methods for STL (Seasonal, Trend, Level) decomposition of time series

Usage

## S3 method for class 'stl'sw_tidy(x, ...)## S3 method for class 'stl'sw_tidy_decomp(x, timetk_idx = FALSE, rename_index = "index", ...)## S3 method for class 'stlm'sw_tidy_decomp(x, timetk_idx = FALSE, rename_index = "index", ...)## S3 method for class 'stlm'sw_glance(x, ...)## S3 method for class 'stlm'sw_augment(x, data = NULL, rename_index = "index", timetk_idx = FALSE, ...)

Arguments

x

An object of class "stl"

...

Not used.

timetk_idx

Used withsw_tidy_decomp.WhenTRUE, uses a timetk index (irregular, typically date or datetime) if present.

rename_index

Used withsw_tidy_decomp.A string representing the name of the index generated.

data

Used withsw_augment only.

Value

sw_tidy() wrapssw_tidy_decomp()

sw_tidy_decomp() returns a tibble with the following time series attributes:

sw_glance() returns the underlying ETS or ARIMA model'ssw_glance() results one row with the columns

sw_augment() returns a tibble with the following time series attributes:

See Also

stats::stl()

Examples

library(dplyr)library(forecast)library(sweep)fit_stl <- USAccDeaths %>%    stl(s.window = "periodic")sw_tidy_decomp(fit_stl)

Validates data frame has column named the same name as variable rename_index

Description

Validates data frame has column named the same name as variable rename_index

Usage

validate_index(ret, rename_index)

Arguments

ret

An object of class tibble

rename_index

A variable indicating the index name to be used in thetibble returned


[8]ページ先頭

©2009-2025 Movatter.jp