spsanderson / healthyR.ts

A time-series companion package to healthyR
https://www.spsanderson.com/healthyR.ts/
Other
19 stars 3 forks source link

log transform #484

Closed spsanderson closed 1 year ago

spsanderson commented 1 year ago

Function:

util_log_ts <- function(.time_series){

  time_series <- .time_series
  min_x <- min(time_series)

  if (min_x >= 0){
    if (ts_adf_test(log(time_series))$p_value < 0.05){
      rlang::inform(
        message = "Logrithmic transformation made the time series stationary",
        use_cli_format = TRUE
      )
      stationary_ts <- log(time_series)
      # Return
      return(
        list(
          stationary_ts = stationary_ts,
          ndiffs = NA,
          adf_stats = ts_adf_test(stationary_ts),
          trans_type = "log",
          ret = TRUE
        )
      )
    } else {
      rlang::inform(
        message = "Logrithmic Transformation Failed.",
        use_cli_format = TRUE
      )
      return(list(ret = FALSE))
    }
  } else {
    rlang::inform(
      message = "The minimum value of the time series is less than or equal to 0.",
      use_cli_format = TRUE
    )
    return(list(ret = FALSE))
  }
}

Examples:

> util_log_ts(x)
Logrithmic transformation made the time series stationary
$stationary_ts
          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug      Sep      Oct
1949 4.718499 4.770685 4.882802 4.859812 4.795791 4.905275 4.997212 4.997212 4.912655 4.779123
1950 4.744932 4.836282 4.948760 4.905275 4.828314 5.003946 5.135798 5.135798 5.062595 4.890349
1951 4.976734 5.010635 5.181784 5.093750 5.147494 5.181784 5.293305 5.293305 5.214936 5.087596
1952 5.141664 5.192957 5.262690 5.198497 5.209486 5.384495 5.438079 5.488938 5.342334 5.252273
1953 5.278115 5.278115 5.463832 5.459586 5.433722 5.493061 5.575949 5.605802 5.468060 5.351858
1954 5.318120 5.236442 5.459586 5.424950 5.455321 5.575949 5.710427 5.680173 5.556828 5.433722
1955 5.488938 5.451038 5.587249 5.594711 5.598422 5.752573 5.897154 5.849325 5.743003 5.613128
1956 5.648974 5.624018 5.758902 5.746203 5.762051 5.924256 6.023448 6.003887 5.872118 5.723585
1957 5.752573 5.707110 5.874931 5.852202 5.872118 6.045005 6.142037 6.146329 6.001415 5.849325
1958 5.828946 5.762051 5.891644 5.852202 5.894403 6.075346 6.196444 6.224558 6.001415 5.883322
1959 5.886104 5.834811 6.006353 5.981414 6.040255 6.156979 6.306275 6.326149 6.137727 6.008813
1960 6.033086 5.968708 6.037871 6.133398 6.156979 6.282267 6.432940 6.406880 6.230481 6.133398
          Nov      Dec
1949 4.644391 4.770685
1950 4.736198 4.941642
1951 4.983607 5.111988
1952 5.147494 5.267858
1953 5.192957 5.303305
1954 5.313206 5.433722
1955 5.468060 5.627621
1956 5.602119 5.723585
1957 5.720312 5.817111
1958 5.736572 5.820083
1959 5.891644 6.003887
1960 5.966147 6.068426

$ndiffs
[1] NA

$adf_stats
$adf_stats$test_stat
[1] -7.318571

$adf_stats$p_value
[1] 0.01

$trans_type
[1] "log"

> util_log_ts(BJsales)
Logrithmic Transformation Failed.
[1] FALSE
> util_log_ts(runif(150)*-1)
The minimum value of the time series is less than or equal to 0.