Closed spsanderson closed 1 year ago
Function Update (want to keep original)
auto_stationarizev2 <- function(.time_series) {
# Variables
time_series <- .time_series
freq <- frequency(.time_series)
min_x <- min(.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)
ret <- util_log_ts(time_series)
if(ret$ret == TRUE){return(ret)}
# Single Differencing
ret <- util_singlediff_ts(time_series)
if (ret$ret == TRUE){return(ret)}
# Double Differencing
ret <- util_doublediff_ts(time_series)
if (ret$ret == TRUE){return(ret)}
# Diff of Log
ret <- util_difflog_ts(time_series)
if (ret$ret == TRUE){return(ret)}
# Double Diff Log
ret <- util_doubledifflog_ts(time_series)
if (ret$ret == TRUE){return(ret)}
}
Examples:
> auto_stationarizev2(AirPassengers)
The time series is already stationary via ts_adf_test().
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1949 112 118 132 129 121 135 148 148 136 119 104 118
1950 115 126 141 135 125 149 170 170 158 133 114 140
1951 145 150 178 163 172 178 199 199 184 162 146 166
1952 171 180 193 181 183 218 230 242 209 191 172 194
1953 196 196 236 235 229 243 264 272 237 211 180 201
1954 204 188 235 227 234 264 302 293 259 229 203 229
1955 242 233 267 269 270 315 364 347 312 274 237 278
1956 284 277 317 313 318 374 413 405 355 306 271 306
1957 315 301 356 348 355 422 465 467 404 347 305 336
1958 340 318 362 348 363 435 491 505 404 359 310 337
1959 360 342 406 396 420 472 548 559 463 407 362 405
1960 417 391 419 461 472 535 622 606 508 461 390 432
> auto_stationarizev2(BJsales)
The time series is not stationary. Attempting to make it stationary...
Logrithmic Transformation Failed.
Data requires more single differencing than it's frequency, trying double differencing
Double Differencing of order 1 made the time series stationary
$stationary_ts
Time Series:
Start = 3
End = 150
Frequency = 1
[1] 0.5 -0.4 0.6 1.1 -2.8 3.0 -1.1 0.6 -0.5 -0.5 0.1 2.0 -0.6 0.8 1.2 -3.4 -0.7 -0.3
[19] 1.7 3.0 -3.2 0.9 2.2 -2.5 -0.4 2.6 -4.3 2.0 -3.1 2.7 -2.1 0.1 2.1 -0.2 -2.2 0.6
[37] 1.0 -2.6 3.0 0.3 0.2 -0.8 1.0 0.0 3.2 -2.2 -4.7 1.2 0.8 -0.6 -0.4 0.6 1.0 -1.6
[55] -0.1 3.4 -0.9 -1.7 -0.5 0.8 2.4 -1.9 0.6 -2.2 2.6 -0.1 -2.7 1.7 -0.3 1.9 -2.7 1.1
[73] -0.6 0.9 0.0 1.8 -0.5 -0.4 -1.2 2.6 -1.8 1.7 -0.9 0.6 -0.4 3.0 -2.8 3.1 -2.3 -1.1
[91] 2.1 -0.3 -1.7 -0.8 -0.4 1.1 -1.5 0.3 1.4 -2.0 1.3 -0.3 0.4 -3.5 1.1 2.6 0.4 -1.3
[109] 2.0 -1.6 0.6 -0.1 -1.4 1.6 1.6 -3.4 1.7 -2.2 2.1 -2.0 -0.2 0.2 0.7 -1.4 1.8 -0.1
[127] -0.7 0.4 0.4 1.0 -2.4 1.0 -0.4 0.8 -1.0 1.4 -1.2 1.1 -0.9 0.5 1.9 -0.6 0.3 -1.4
[145] -0.9 -0.5 1.4 0.1
$ndiffs
[1] 1
$adf_stats
$adf_stats$test_stat
[1] -6.562008
$adf_stats$p_value
[1] 0.01
$trans_type
[1] "double_diff"
$ret
[1] TRUE
> auto_stationarizev2(BJsales.lead)
The time series is not stationary. Attempting to make it stationary...
Logrithmic Transformation Failed.
Differencing of order 1 made the time series stationary
$stationary_ts
Time Series:
Start = 2
End = 150
Frequency = 1
[1] 0.06 0.25 -0.57 0.58 -0.20 0.23 -0.04 -0.19 0.03 0.42 0.04 0.24 0.34 -0.46 -0.18
[16] -0.08 0.29 0.56 -0.37 0.20 0.54 -0.31 0.03 0.52 -0.70 0.35 -0.63 0.44 -0.38 -0.01
[31] 0.22 0.10 -0.50 0.01 0.30 -0.76 0.52 0.15 0.06 -0.10 0.21 -0.01 0.70 -0.22 -0.76
[46] 0.06 0.02 -0.17 -0.08 0.01 0.11 -0.39 0.01 0.50 -0.02 -0.37 -0.13 0.05 0.54 -0.46
[61] 0.25 -0.52 0.44 0.02 -0.47 0.11 0.06 0.25 -0.35 0.00 -0.06 0.21 -0.09 0.36 0.09
[76] -0.04 -0.20 0.44 -0.23 0.40 -0.01 0.17 0.08 0.58 -0.27 0.79 -0.21 0.02 0.30 0.28
[91] -0.27 -0.01 0.03 0.16 -0.28 0.15 0.26 -0.36 0.32 -0.11 0.22 -0.65 0.00 0.47 0.16
[106] -0.19 0.48 -0.26 0.21 0.00 -0.20 0.35 0.38 -0.48 0.20 -0.32 0.43 -0.50 0.12 -0.17
[121] 0.15 -0.36 0.35 -0.03 -0.18 0.16 0.07 0.21 -0.50 0.23 -0.13 0.14 -0.15 0.19 -0.24
[136] 0.26 -0.22 0.17 0.37 -0.06 0.29 -0.34 -0.12 -0.16 0.25 0.08 -0.07 0.26 -0.37
$ndiffs
[1] 1
$adf_stats
$adf_stats$test_stat
[1] -4.838625
$adf_stats$p_value
[1] 0.01
$trans_type
[1] "diff"
$ret
[1] TRUE
BJsales
BJsales stationarized
BJsales.lead
BJsales.lead stationarized
Change the ndiffs section of the auto_stationarize function to the following:
Example:
_We See It Actually Fails the ts_adftest()
New Examples
Plots:
Edit function to use internal helpers to break the different types out to make things a little easier to read.