spsanderson / healthyR.ts

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

differencing of log #487

Closed spsanderson closed 1 year ago

spsanderson commented 1 year ago

Function:

util_difflog_ts <- function(.time_series){

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

  # Diff of Log
  diff_order <- 1
  while (
    (min_x > 0) & 
    (ts_adf_test(diff(log(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(log(time_series), diff_order)
    return(
      list(
        stationary_ts = stationary_ts,
        ndiffs = diff_order,
        adf_stats = ts_adf_test(stationary_ts),
        trans_type = "diff_log",
        ret = TRUE
      )
    )
  } else {
    rlang::inform(
      message = "Data either had a minimum value less than or equal to 0, 
      or requires more than differencing than its frequency, trying 
      double differencing log.",
      use_cli_format = TRUE
    )
    return(list(ret = FALSE))
  }
}

Examples:

> util_difflog_ts(x)
Differencing of order 1 made the time series stationary
$stationary_ts
              Jan          Feb          Mar          Apr          May          Jun          Jul
1949               0.052185753  0.112117298 -0.022989518 -0.064021859  0.109484233  0.091937495
1950 -0.025752496  0.091349779  0.112477983 -0.043485112 -0.076961041  0.175632569  0.131852131
1951  0.035091320  0.033901552  0.171148256 -0.088033349  0.053744276  0.034289073  0.111521274
1952  0.029675768  0.051293294  0.069733338 -0.064193158  0.010989122  0.175008910  0.053584246
1953  0.010256500  0.000000000  0.185717146 -0.004246291 -0.025863511  0.059339440  0.082887660
1954  0.014815086 -0.081678031  0.223143551 -0.034635497  0.030371098  0.120627988  0.134477914
1955  0.055215723 -0.037899273  0.136210205  0.007462721  0.003710579  0.154150680  0.144581229
1956  0.021353124 -0.024956732  0.134884268 -0.012698583  0.015848192  0.162204415  0.099191796
1957  0.028987537 -0.045462374  0.167820466 -0.022728251  0.019915310  0.172887525  0.097032092
1958  0.011834458 -0.066894235  0.129592829 -0.039441732  0.042200354  0.180943197  0.121098097
1959  0.066021101 -0.051293294  0.171542423 -0.024938948  0.058840500  0.116724274  0.149296301
1960  0.029199155 -0.064378662  0.069163360  0.095527123  0.023580943  0.125287761  0.150673346
              Aug          Sep          Oct          Nov          Dec
1949  0.000000000 -0.084557388 -0.133531393 -0.134732594  0.126293725
1950  0.000000000 -0.073203404 -0.172245905 -0.154150680  0.205443974
1951  0.000000000 -0.078369067 -0.127339422 -0.103989714  0.128381167
1952  0.050858417 -0.146603474 -0.090060824 -0.104778951  0.120363682
1953  0.029852963 -0.137741925 -0.116202008 -0.158901283  0.110348057
1954 -0.030254408 -0.123344547 -0.123106058 -0.120516025  0.120516025
1955 -0.047829088 -0.106321592 -0.129875081 -0.145067965  0.159560973
1956 -0.019560526 -0.131769278 -0.148532688 -0.121466281  0.121466281
1957  0.004291852 -0.144914380 -0.152090098 -0.129013003  0.096799383
1958  0.028114301 -0.223143551 -0.118092489 -0.146750091  0.083510633
1959  0.019874186 -0.188422419 -0.128913869 -0.117168974  0.112242855
1960 -0.026060107 -0.176398538 -0.097083405 -0.167251304  0.102278849

$ndiffs
[1] 1

$adf_stats
$adf_stats$test_stat
[1] -7.318571

$adf_stats$p_value
[1] 0.01

$trans_type
[1] "diff_log"

> util_difflog_ts(BJsales)
Data either had a minimum value less than or equal to 0, or requires more than differencing than
it's frequency, trying double differencing log.
[1] FALSE
> util_difflog_ts(runif(150))
Differencing of order 1 made the time series stationary
$stationary_ts
  [1]  0.27133347 -1.22559893  0.18708816  1.65742570  0.31179009  0.14018459  0.02879337
  [8] -2.55262197  2.67714875 -1.56338386  0.06423803  0.76060342 -2.28912906  2.72456391
 [15] -1.07838168  0.02560533  0.87213331 -0.37791713  0.69956233 -0.96457389 -0.38033356
 [22] -1.12103765  0.40027013  1.67201389 -1.73019463  0.53736230 -1.11102332  2.52956955
 [29] -0.14192592  0.49682352 -3.16023098  2.68142253  0.02743911  0.04998727 -0.83621777
 [36]  1.14450835 -0.39479574  0.09400563  0.40286701 -0.23984683  0.19279200 -1.83998216
 [43]  0.58221723  0.44495109 -1.18429770  0.22032056 -0.17714417  1.96879047 -0.10704212
 [50] -2.21724754  1.16599050 -0.34930616  1.01326582 -1.74078517  2.11291862  0.03660031
 [57] -0.22188336  0.25503150 -0.09803958 -1.02059018  0.83489060 -1.27624491  0.82101901
 [64]  0.15489507  0.52219465 -0.22296852 -4.31864736  3.90050587 -0.84343107  1.05561525
 [71]  0.19346571 -0.37805071 -4.85367397  5.21827170 -0.23841863  0.58677066 -0.07977455
 [78] -0.92378664  0.60071778 -0.65584673  0.74695662  0.31656350 -0.43208813 -0.71191455
 [85]  0.79066079  0.36202580 -0.11104073 -0.06216923 -0.98791085  0.66512931 -0.88406881
 [92]  1.20355825 -4.05497082  3.83732342  0.05357662  0.20719627  0.11152522 -0.11547764
 [99] -3.58228934  3.08108718 -2.67441938  2.21490319  1.08425521 -0.53835374 -1.00097232
[106] -2.81526291  4.17019863 -0.07013923  0.25251450 -0.68785347 -0.71786570 -0.73232582
[113]  1.79987364 -3.10993302  3.41526232 -0.13874221 -1.03029424  0.96833749  0.26829677
[120] -0.40016689  0.09098532 -0.74758447  0.81183358  0.22566106 -0.35495367 -0.14087421
[127]  0.44344649 -0.30396322 -0.89079167  0.36518005 -0.16531124  0.81949104  0.01173234
[134] -0.82235596  0.70744222 -0.73379718 -2.09677093  3.08586381 -0.59064327  0.17544117
[141]  0.08848901  0.39847678  0.01420459 -3.31188515  2.44656563  0.18248861  0.28035781
[148] -0.38365644  0.61385119

$ndiffs
[1] 1

$adf_stats
$adf_stats$test_stat
[1] -5.306121

$adf_stats$p_value
[1] 0.01

$trans_type
[1] "diff_log"