Closed spsanderson closed 1 year ago
Function:
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)) } }
Examples:
> util_singlediff_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] "diff" > util_singlediff_ts(BJsales) Data requires more single differencing than it's frequency, trying double differencing [1] FALSE > util_singlediff_ts(runif(150)*-1) Differencing of order 1 made the time series stationary $stationary_ts [1] -0.199334020 -0.405116486 0.590357560 0.166843754 -0.163220006 -0.533997485 0.095366330 [8] -0.167604990 0.269771407 0.043851176 -0.238503639 0.545457803 -0.703349104 0.485737021 [15] 0.424492740 -0.720034091 0.247324778 0.464171128 -0.968713508 0.720220389 -0.550252271 [22] 0.255699790 0.362067311 -0.280077365 -0.377524094 0.735850255 -0.809971313 0.121800410 [29] 0.444487421 0.150259752 -0.607661907 0.257703898 0.016731712 -0.157938169 -0.237527736 [36] 0.768846860 -0.059804210 -0.259113024 -0.292553812 0.487359701 0.163451193 -0.516499517 [43] -0.312687791 0.860701943 -0.250419705 0.062004191 0.124676413 -0.212882027 -0.391927560 [50] 0.064205276 -0.280662073 0.758255146 -0.634289159 0.065316930 0.034516889 0.263628801 [57] -0.464274600 0.456768314 -0.041977592 0.035204496 0.247522469 0.012192849 -0.488957954 [64] 0.615057614 -0.642823332 0.286549293 -0.305455529 0.457831864 0.283263163 -0.781747269 [71] 0.100599769 -0.109366291 0.714104377 -0.637413358 0.362123290 0.057238544 -0.471831386 [78] 0.742285622 -0.815847773 0.259446705 -0.171638282 0.703008331 -0.527874797 0.619769761 [85] -0.809977247 0.285991152 -0.299418453 0.247889457 0.158268320 0.213583108 -0.073844956 [92] -0.184198547 0.272371987 0.094834895 0.057669017 -0.786592620 0.080256307 -0.214912091 [99] 0.548541406 -0.050912870 0.177355615 0.008136612 0.157661706 0.073309486 -0.931609492 [106] 0.454148983 -0.307877811 0.406538821 -0.538995945 0.674232300 -0.580408069 0.009849190 [113] 0.271019108 0.452756844 -0.413571892 -0.137050760 0.166092831 0.068262939 0.051889502 [120] -0.424266676 0.265518842 0.186085937 0.362844683 -0.911294385 -0.042765180 0.370250624 [127] -0.285321412 0.357269767 -0.220633768 0.596657596 -0.533908903 -0.245580622 0.081047537 [134] 0.689387710 -0.587924560 0.443573975 0.086234805 0.013907032 0.026974607 -0.628625843 [141] -0.005484627 0.732938970 0.029198175 -0.742391955 0.488160006 0.050056203 -0.580750523 [148] 0.641111367 -0.313284928 $ndiffs [1] 1 $adf_stats $adf_stats$test_stat [1] -5.76895 $adf_stats$p_value [1] 0.01 $trans_type [1] "diff"
Function:
Examples: