Skip to content

Commit

Permalink
Merge pull request #492 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #485
  • Loading branch information
spsanderson authored Oct 27, 2023
2 parents dcd3cff + 714834a commit cceb78b
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 18 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export(ts_wfs_svm_poly)
export(ts_wfs_svm_rbf)
export(ts_wfs_xgboost)
export(util_log_ts)
export(util_singlediff_ts)
importFrom(magrittr,"%>%")
importFrom(recipes,bake)
importFrom(recipes,prep)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ None

## New Features
1. Fix #484 - Add function `util_log_ts()`
2. Fix #485 - Add function `util_singlediff_ts()`

## Minor Fixes and Improvements
1. Fix #480 - Add attributes to output of `ts_growth_rate_vec()`
Expand Down
83 changes: 83 additions & 0 deletions R/utils-singlediff-stationary.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#' Single Differencing to Make Time Series Stationary
#'
#' @family Utility
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description This function attempts to make a non-stationary time series stationary by applying
#' single differencing. It iteratively increases the differencing order until stationarity is achieved.
#'
#' @details
#' The function calculates the frequency of the input time series using the `stats::frequency` function.
#' It then applies single differencing incrementally until the Augmented Dickey-Fuller test indicates
#' stationarity (p-value < 0.05) or until the differencing order reaches the frequency of the data.
#'
#' If single differencing successfully makes the time series stationary, it returns the stationary time series
#' and related information as a list with the following elements:
#' - stationary_ts: The stationary time series after differencing.
#' - ndiffs: The order of differencing applied to make it stationary.
#' - adf_stats: Augmented Dickey-Fuller test statistics on the stationary time series.
#' - trans_type: Transformation type, which is "diff" in this case.
#' - ret: TRUE to indicate a successful transformation.
#'
#' If the data requires more single differencing than its frequency allows, it informs the user and
#' returns a list with ret set to FALSE, indicating that double differencing may be needed.
#'
#' @param .time_series A time series object to be made stationary.
#'
#' @examples
#' # Example 1: Using a time series dataset
#' util_singlediff_ts(AirPassengers)
#'
#' # Example 2: Using a different time series dataset
#' util_singlediff_ts(BJsales)$ret
#'
#' @return
#' If the time series is already stationary or the single differencing is successful,
#' it returns a list as described in the details section. If additional differencing is required,
#' it informs the user and returns a list with ret set to FALSE.
#'
#' @name util_singlediff_ts
NULL

#' @export
#' @rdname util_singlediff_ts
util_singlediff_ts <- function(.time_series){

time_series <- .time_series
f <- stats::frequency(time_series)

# Single Differencing
diff_order <- 1
while ((ts_adf_test(diff(time_series, diff_order))$p_value >= 0.05) &
(diff_order <= f)){
diff_order <- diff_order + 1
}

if (diff_order <= f){
rlang::inform(
message = paste0("Differencing of order "
, diff_order
, " made the time series stationary"),
use_cli_format = TRUE
)
# Return
stationary_ts <- diff(time_series, diff_order)
return(
list(
stationary_ts = stationary_ts,
ndiffs = diff_order,
adf_stats = ts_adf_test(stationary_ts),
trans_type = "diff",
ret = TRUE
)
)
} else {
rlang::inform(
message = "Data requires more single differencing than its frequency,
trying double differencing",
use_cli_format = TRUE
)
return(list(ret = FALSE))
}
}
3 changes: 2 additions & 1 deletion man/auto_stationarize.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/calibrate_and_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/internal_ts_backward_event_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/internal_ts_both_event_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/internal_ts_forward_event_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/model_extraction_helper.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_get_date_columns.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_info_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_is_date_class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_lag_correlation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_model_auto_tune.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_model_compare.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_model_rank_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_model_spec_tune_template.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_qq_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_scedacity_scatter_plot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/ts_to_tbl.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/util_log_ts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions man/util_singlediff_ts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cceb78b

Please sign in to comment.