spsanderson / healthyR.ts

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

double differencing #486

Closed spsanderson closed 11 months ago

spsanderson commented 11 months ago

Function:

util_doublediff_ts <- function(.time_series){

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

  # Double Differencing
  diff_order <- 1
  while ((ts_adf_test(diff(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("Double Differencing of order "
                       , diff_order
                       , " made the time series stationary"),
      use_cli_format = TRUE
    )
    # Return
    stationary_ts <- diff(diff(time_series, diff_order))
    return(
      list(
        stationary_ts = stationary_ts,
        ndiffs = diff_order,
        adf_stats = ts_adf_test(stationary_ts),
        trans_type = "double_diff",
        ret = TRUE
      )
    )
  } else {
    rlang::inform(
      message = "Data requires more double differencing than its frequency, 
      trying differencing log.",
      use_cli_format = TRUE
    )
    return(list(ret = FALSE))
  }
}

Example:

> util_doublediff_ts(x)
Differencing of order 1 made the time series stationary
$stationary_ts
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1949         6   14   -3   -8   14   13    0  -12  -17  -15   14
1950   -3   11   15   -6  -10   24   21    0  -12  -25  -19   26
1951    5    5   28  -15    9    6   21    0  -15  -22  -16   20
1952    5    9   13  -12    2   35   12   12  -33  -18  -19   22
1953    2    0   40   -1   -6   14   21    8  -35  -26  -31   21
1954    3  -16   47   -8    7   30   38   -9  -34  -30  -26   26
1955   13   -9   34    2    1   45   49  -17  -35  -38  -37   41
1956    6   -7   40   -4    5   56   39   -8  -50  -49  -35   35
1957    9  -14   55   -8    7   67   43    2  -63  -57  -42   31
1958    4  -22   44  -14   15   72   56   14 -101  -45  -49   27
1959   23  -18   64  -10   24   52   76   11  -96  -56  -45   43
1960   12  -26   28   42   11   63   87  -16  -98  -47  -71   42

$ndiffs
[1] 1

$adf_stats
$adf_stats$test_stat
[1] -7.318571

$adf_stats$p_value
[1] 0.01

$trans_type
[1] "double_diff"

> util_doublediff_ts(BJsales)
Differencing of order 1 made the time series stationary
$stationary_ts
Time Series:
Start = 2 
End = 150 
Frequency = 1 
  [1] -0.6 -0.1 -0.5  0.1  1.2 -1.6  1.4  0.3  0.9  0.4 -0.1  0.0  2.0  1.4  2.2  3.4  0.0 -0.7
 [19] -1.0  0.7  3.7  0.5  1.4  3.6  1.1  0.7  3.3 -1.0  1.0 -2.1  0.6 -1.5 -1.4  0.7  0.5 -1.7
 [37] -1.1 -0.1 -2.7  0.3  0.6  0.8  0.0  1.0  1.0  4.2  2.0 -2.7 -1.5 -0.7 -1.3 -1.7 -1.1 -0.1
 [55] -1.7 -1.8  1.6  0.7 -1.0 -1.5 -0.7  1.7 -0.2  0.4 -1.8  0.8  0.7 -2.0 -0.3 -0.6  1.3 -1.4
 [73] -0.3 -0.9  0.0  0.0  1.8  1.3  0.9 -0.3  2.3  0.5  2.2  1.3  1.9  1.5  4.5  1.7  4.8  2.5
 [91]  1.4  3.5  3.2  1.5  0.7  0.3  1.4 -0.1  0.2  1.6 -0.4  0.9  0.6  1.0 -2.5 -1.4  1.2  1.6
[109]  0.3  2.3  0.7  1.3  1.2 -0.2  1.4  3.0 -0.4  1.3 -0.9  1.2 -0.8 -1.0 -0.8 -0.1 -1.5  0.3
[127]  0.2 -0.5 -0.1  0.3  1.3 -1.1 -0.1 -0.5  0.3 -0.7  0.7 -0.5  0.6 -0.3  0.2  2.1  1.5  1.8
[145]  0.4 -0.5 -1.0  0.4  0.5

$ndiffs
[1] 1

$adf_stats
$adf_stats$test_stat
[1] -2.110919

$adf_stats$p_value
[1] 0.5301832

$trans_type
[1] "double_diff"

> util_doublediff_ts(runif(150)*-1)
Differencing of order 1 made the time series stationary
$stationary_ts
  [1] -0.689333154  0.260159930  0.277309661  0.221469726 -0.123542700 -0.038014818 -0.002476170
  [8] -0.028581971 -0.067552302 -0.248127715  0.593783911 -0.518097477  0.165221553  0.373704780
 [15] -0.671486184  0.064742276  0.614037437 -0.295509911 -0.406259891  0.270850127 -0.161971189
 [22]  0.442062502 -0.657035755  0.175304845  0.597266248 -0.179320377 -0.046348170 -0.154263453
 [29]  0.398233210 -0.339818930 -0.474199458  0.026743063  0.189082395  0.452681023 -0.645661020
 [36]  0.360849404  0.400089158 -0.629729028  0.092889452  0.250838986 -0.365986224  0.085453524
 [43] -0.154238579  0.046660959 -0.138852333  0.176849493 -0.094087358  0.105939786 -0.258282681
 [50]  0.625417707 -0.351759415  0.427980996 -0.619669030  0.252898948  0.441128853 -0.234617770
 [57] -0.148070934  0.236253734 -0.243012355  0.353749325 -0.121613016 -0.406473351  0.617133764
 [64] -0.553476567  0.002602441 -0.130649057  0.730006680 -0.633157080  0.573926369 -0.675211066
 [71]  0.248724615  0.492853190 -0.384213782  0.418047597 -0.104070854 -0.192895524  0.121380806
 [78] -0.257208959 -0.392600230  0.726626471 -0.668900975  0.492013842 -0.393051865  0.109835488
 [85]  0.518080311 -0.433621148  0.063655874  0.189968794 -0.492744255  0.474772725 -0.150407062
 [92]  0.184299142 -0.681272978  0.502469075  0.431732712 -0.499589271 -0.204480565  0.137742559
 [99] -0.222191089 -0.147431914  0.903980240 -0.740417250 -0.190976294  0.039301247 -0.017045095
[106]  0.163258540  0.176852161  0.007079589  0.581815264 -0.882883749  0.455174983  0.159827258
[113] -0.415086999 -0.018173792  0.624411809 -0.450806658  0.174595037  0.091103450  0.164639386
[120] -0.208004421 -0.429606852  0.353566914  0.087987163 -0.566241152 -0.054499863 -0.025563455
[127]  0.150272018  0.087660756  0.041927404  0.246565716 -0.533904206  0.519691182  0.335520149
[134] -0.376228777 -0.407527806  0.201505567  0.510825467 -0.593377211  0.740135131 -0.197717555
[141] -0.556495546  0.162753598  0.045959117 -0.338688185  0.021939719  0.882526578 -0.415707064
[148] -0.070062517  0.150656416

$ndiffs
[1] 1

$adf_stats
$adf_stats$test_stat
[1] -4.464509

$adf_stats$p_value
[1] 0.01

$trans_type
[1] "double_diff"