Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #417 #465

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export(arima_string)
export(as_label)
export(as_name)
export(assign_value)
export(auto_stationarize)
export(calibrate_and_plot)
export(chr_assign)
export(ci_hi)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ None
## New Features
1. Fix #459 - Add function `ts_growth_rate_vec()`
2. Fix #463 - Add function `ts_adf_test()`
3. Fix #417 - Add function `auto_stationarize()`

## Minor Fixes and Improvements
1. Fix #456 Fix boilerplate examples to set the `.true` param to `FALSE`
Expand Down
70 changes: 70 additions & 0 deletions R/util-auto-stationarize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' Automatically Stationarize Time Series Data
#'
#' @family Utility
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @description This function attempts to make a non-stationary time series stationary.
#' This function attempts to make a given time series stationary by applying transformations
#' such as differencing or logarithmic transformation. If the time series is already
#' stationary, it returns the original time series.
#'
#' @details
#' If the input time series is non-stationary (determined by the Augmented Dickey-Fuller test),
#' this function will try to make it stationary by applying a series of transformations:
#' 1. It checks if the time series is already stationary using the Augmented Dickey-Fuller test.
#' 2. If not stationary, it attempts a logarithmic transformation.
#' 3. If the logarithmic transformation doesn't work, it applies differencing.
#'
#' @param .time_series A time series object to be made stationary.
#'
#' @examples
#' # Example 1: Using the AirPassengers dataset
#' auto_stationarize(AirPassengers)
#'
#' # Example 2: Using the BJsales dataset
#' auto_stationarize(BJsales)
#'
#' @return
#' If the time series is already stationary, it returns the original time series.
#' If a transformation is applied to make it stationary, it returns a list with two elements:
#' - stationary_ts: The stationary time series.
#' - ndiffs: The order of differencing applied to make it stationary.
#'
#' @name auto_stationarize
NULL

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

# Check if the time series is already stationary
if (ts_adf_test(time_series)$p_value < 0.05) {
cat("The time series is already stationary via ts_adf_test().\n")
return(time_series)
} else {
cat("The time series is not stationary. Attempting to make it stationary...\n")
}

# Transformation (e.g., logarithmic)
if (ts_adf_test(log(time_series))$p_value < 0.05) {
cat("Logarithmic transformation made the time series stationary.")
return(log(time_series))
}

# Differencing
if (ts_adf_test(diff(time_series, 1))$p_value >= 0.05) {
diff_order <- forecast::ndiffs(time_series)
cat("Differencing of order", diff_order, "made the time series stationary.\n")
}

# Return
return(
list(
stationary_ts = diff(time_series, diff_order),
ndiffs = diff_order
)
)
}
65 changes: 65 additions & 0 deletions man/auto_stationarize.Rd

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

1 change: 1 addition & 0 deletions man/calibrate_and_plot.Rd

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

1 change: 1 addition & 0 deletions man/internal_ts_backward_event_tbl.Rd

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

1 change: 1 addition & 0 deletions man/internal_ts_both_event_tbl.Rd

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

1 change: 1 addition & 0 deletions man/internal_ts_forward_event_tbl.Rd

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

1 change: 1 addition & 0 deletions man/model_extraction_helper.Rd

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

1 change: 1 addition & 0 deletions man/ts_get_date_columns.Rd

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

1 change: 1 addition & 0 deletions man/ts_info_tbl.Rd

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

1 change: 1 addition & 0 deletions man/ts_is_date_class.Rd

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

1 change: 1 addition & 0 deletions man/ts_lag_correlation.Rd

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

1 change: 1 addition & 0 deletions man/ts_model_auto_tune.Rd

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

1 change: 1 addition & 0 deletions man/ts_model_compare.Rd

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

1 change: 1 addition & 0 deletions man/ts_model_rank_tbl.Rd

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

1 change: 1 addition & 0 deletions man/ts_model_spec_tune_template.Rd

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

1 change: 1 addition & 0 deletions man/ts_qq_plot.Rd

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

1 change: 1 addition & 0 deletions man/ts_scedacity_scatter_plot.Rd

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

1 change: 1 addition & 0 deletions man/ts_to_tbl.Rd

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