Function to get imputations from methods compared by kssa

get_imputations(x_ts, methods = "all", seed = 1234)

Arguments

x_ts

A ts object with missing data to be imputed

methods

A string or string vector indicating the method or methods You can choose between the following:

  • "all" - get imputed values for all methods - Default

  • "auto.arima" - State space representation of an ARIMA model

  • "StructTS" - State space representation of a structural model

  • "seadec" - Seasonal decomposition with Kalman smoothing

  • "linear_i" - Linear interpolation

  • "spline_i" - Spline interpolation

  • "stine_i" - Stineman interpolation

  • "simple_ma" - Simple moving average

  • "linear_ma" - Linear moving average

  • "exponential_ma" - Exponential moving average

  • "locf" - Last observation carried forward

  • "stl" - Seasonal and trend decomposition with Loess

For further details on these imputation methods please check packages imputeTS and forecast

seed

Numeric. Any number

Value

A list of imputed time series with the selected methods

Examples

# \donttest{

# Example 1: Get imputed values for airgap_na_ts with the methods of

library("imputeTS")
library("kssa")

# Create 20% random missing data in tsAirgapComplete time series from imputeTS
airgap_na <- missMethods::delete_MCAR(as.data.frame(tsAirgapComplete), 0.2)

# Convert to time series object
airgap_na_ts <- ts(airgap_na, start = c(1959, 1), end = c(1997, 12), frequency = 12)

my_imputations <- get_imputations(airgap_na_ts, methods = "all")

# my_imputations contains the imputed time series with all methods.
# Access it and choose the one from the best method for your purposes

my_imputations$seadec
#>           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
#> 1959 112.0000 118.0000 132.0000 123.3140 121.0000 135.0000 148.0000 148.0000
#> 1960 117.5446 122.3693 141.0000 135.0000 125.0000 149.0000 174.0325 182.3044
#> 1961 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 209.3107
#> 1962 171.0000 180.0000 195.2651 181.0000 183.0000 218.0000 230.0000 242.0000
#> 1963 196.0000 196.0000 236.0000 235.0000 229.0000 243.0000 264.0000 272.0000
#> 1964 204.0000 188.0000 235.0000 227.0000 234.0000 264.0000 302.0000 293.0000
#> 1965 242.0000 236.9482 267.0000 269.0000 276.5032 314.2229 344.5418 347.0000
#> 1966 284.0000 277.0000 314.2851 313.0000 318.0000 374.0000 413.0000 405.0000
#> 1967 301.7037 301.0000 356.0000 348.0000 355.0000 422.0000 465.0000 467.0000
#> 1968 340.0000 318.0000 362.0000 348.0000 363.0000 435.0000 491.0000 505.0000
#> 1969 360.0000 364.5280 406.0000 405.9319 412.6000 472.0000 561.6426 559.0000
#> 1970 417.0000 391.0000 419.0000 461.0000 472.0000 535.0000 622.0000 558.2764
#> 1971 112.0000 118.0000 132.0000 124.4343 121.0000 135.0000 148.0000 148.0000
#> 1972 117.3937 117.0038 141.0000 135.0000 125.0000 149.0000 182.7009 178.0074
#> 1973 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 211.8450
#> 1974 171.0000 180.0000 197.4672 181.0000 183.0000 218.0000 230.0000 242.0000
#> 1975 196.0000 196.0000 236.0000 235.0000 229.0000 243.0000 264.0000 272.0000
#> 1976 204.0000 188.0000 235.0000 227.0000 234.0000 264.0000 302.0000 293.0000
#> 1977 242.0000 234.5636 267.0000 269.0000 274.8815 308.6641 338.8843 347.0000
#> 1978 284.0000 277.0000 315.0898 313.0000 318.0000 374.0000 413.0000 405.0000
#> 1979 302.9266 301.0000 356.0000 348.0000 355.0000 422.0000 465.0000 467.0000
#> 1980 340.0000 318.0000 362.0000 348.0000 363.0000 435.0000 491.0000 505.0000
#> 1981 360.0000 362.9972 406.0000 407.4202 414.5970 472.0000 560.0068 559.0000
#> 1982 417.0000 391.0000 419.0000 461.0000 472.0000 535.0000 622.0000 561.7262
#> 1983 112.0000 118.0000 132.0000 124.8307 121.0000 135.0000 148.0000 148.0000
#> 1984 117.0876 115.8873 141.0000 135.0000 125.0000 149.0000 183.6562 181.5816
#> 1985 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 214.4466
#> 1986 171.0000 180.0000 197.4194 181.0000 183.0000 218.0000 230.0000 242.0000
#> 1987 196.0000 196.0000 236.0000 235.0000 229.0000 243.0000 264.0000 272.0000
#> 1988 204.0000 188.0000 235.0000 227.0000 234.0000 264.0000 302.0000 293.0000
#> 1989 242.0000 234.4993 267.0000 269.0000 275.3521 305.4432 338.1458 347.0000
#> 1990 284.0000 277.0000 314.5596 313.0000 318.0000 374.0000 413.0000 405.0000
#> 1991 302.5556 301.0000 356.0000 348.0000 355.0000 422.0000 465.0000 467.0000
#> 1992 340.0000 318.0000 362.0000 348.0000 363.0000 435.0000 491.0000 505.0000
#> 1993 360.0000 364.3636 406.0000 406.5984 415.4845 472.0000 539.1809 559.0000
#> 1994 417.0000 391.0000 419.0000 461.0000 472.0000 535.0000 622.0000 600.8330
#> 1995 112.0000 118.0000 132.0000 121.6007 121.0000 135.0000 148.0000 148.0000
#> 1996 117.8050 113.6489 141.0000 135.0000 125.0000 149.0000 207.3850 216.1285
#> 1997 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 225.6738
#>           Sep      Oct      Nov      Dec
#> 1959 136.0000 119.0000 104.0000 118.0000
#> 1960 158.0000 133.0000 114.0000 137.6432
#> 1961 184.0000 162.0000 142.2832 166.0000
#> 1962 209.0000 191.0000 172.0000 194.0000
#> 1963 237.0000 211.0000 180.0000 201.0000
#> 1964 259.0000 229.0000 203.0000 229.0000
#> 1965 312.0000 274.0000 237.0000 278.0000
#> 1966 350.8656 306.0000 271.5084 298.0186
#> 1967 404.0000 354.3791 305.0000 333.6535
#> 1968 404.0000 357.6272 310.0000 337.0000
#> 1969 463.0000 407.0000 362.0000 405.0000
#> 1970 514.1505 461.0000 313.2862 221.9326
#> 1971 136.0000 119.0000 104.0000 118.0000
#> 1972 158.0000 133.0000 114.0000 137.9340
#> 1973 184.0000 162.0000 141.6582 166.0000
#> 1974 209.0000 191.0000 172.0000 194.0000
#> 1975 237.0000 211.0000 180.0000 201.0000
#> 1976 259.0000 229.0000 203.0000 229.0000
#> 1977 312.0000 274.0000 237.0000 278.0000
#> 1978 345.8953 306.0000 270.1208 298.6790
#> 1979 404.0000 356.7502 305.0000 334.4100
#> 1980 404.0000 359.7614 310.0000 337.0000
#> 1981 463.0000 407.0000 362.0000 405.0000
#> 1982 512.8581 461.0000 311.9234 222.0011
#> 1983 136.0000 119.0000 104.0000 118.0000
#> 1984 158.0000 133.0000 114.0000 138.7056
#> 1985 184.0000 162.0000 140.4108 166.0000
#> 1986 209.0000 191.0000 172.0000 194.0000
#> 1987 237.0000 211.0000 180.0000 201.0000
#> 1988 259.0000 229.0000 203.0000 229.0000
#> 1989 312.0000 274.0000 237.0000 278.0000
#> 1990 344.6957 306.0000 268.6549 297.7722
#> 1991 404.0000 357.1482 305.0000 333.9822
#> 1992 404.0000 358.0985 310.0000 337.0000
#> 1993 463.0000 407.0000 362.0000 405.0000
#> 1994 516.3645 461.0000 310.6359 220.7086
#> 1995 136.0000 119.0000 104.0000 118.0000
#> 1996 158.0000 133.0000 114.0000 138.0728
#> 1997 184.0000 162.0000 136.6725 166.0000
plot.ts(my_imputations$seadec)

# }



# Example 2: Get imputed values for airgap_na_ts using only a subset of algorithms

library("imputeTS")
library("kssa")

# Create 20% random missing data in tsAirgapComplete time series from imputeTS
airgap_na <- missMethods::delete_MCAR(as.data.frame(tsAirgapComplete), 0.2)

# Convert to time series object
airgap_na_ts <- ts(airgap_na, start = c(1959, 1), end = c(1997, 12), frequency = 12)

my_imputations <- get_imputations(airgap_na_ts, methods = c("linear_i", "locf"))

# my_imputations contains the imputed time series with all applied
# methods (locf and linear interpolation).
# Access it and choose the one from the best method for your purposes

my_imputations$locf
#>      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
#> 1959 112 118 132 132 132 135 148 148 136 119 104 118
#> 1960 115 115 141 135 125 149 170 170 170 133 114 140
#> 1961 145 150 178 178 172 178 199 199 184 162 146 166
#> 1962 171 180 193 193 183 218 230 242 209 191 191 194
#> 1963 196 196 236 235 229 243 264 264 237 211 180 201
#> 1964 204 204 235 227 234 234 234 293 259 259 203 229
#> 1965 242 233 267 269 270 315 315 315 312 274 237 237
#> 1966 284 277 317 313 318 318 413 405 405 306 306 306
#> 1967 315 315 356 356 356 422 422 467 404 347 305 336
#> 1968 340 318 318 348 363 435 491 491 404 359 310 337
#> 1969 360 342 406 396 420 420 548 548 463 407 362 362
#> 1970 362 391 419 461 461 535 622 606 508 461 390 432
#> 1971 112 118 132 132 132 135 148 148 136 119 104 118
#> 1972 115 115 141 135 125 149 170 170 170 133 114 140
#> 1973 145 150 178 178 172 178 199 199 184 162 146 166
#> 1974 171 180 193 193 183 218 230 242 209 191 191 194
#> 1975 196 196 236 235 229 243 264 264 237 211 180 201
#> 1976 204 204 235 227 234 234 234 293 259 259 203 229
#> 1977 242 233 267 269 270 315 315 315 312 274 237 237
#> 1978 284 277 317 313 318 318 413 405 405 306 306 306
#> 1979 315 315 356 356 356 422 422 467 404 347 305 336
#> 1980 340 318 318 348 363 435 491 491 404 359 310 337
#> 1981 360 342 406 396 420 420 548 548 463 407 362 362
#> 1982 362 391 419 461 461 535 622 606 508 461 390 432
#> 1983 112 118 132 132 132 135 148 148 136 119 104 118
#> 1984 115 115 141 135 125 149 170 170 170 133 114 140
#> 1985 145 150 178 178 172 178 199 199 184 162 146 166
#> 1986 171 180 193 193 183 218 230 242 209 191 191 194
#> 1987 196 196 236 235 229 243 264 264 237 211 180 201
#> 1988 204 204 235 227 234 234 234 293 259 259 203 229
#> 1989 242 233 267 269 270 315 315 315 312 274 237 237
#> 1990 284 277 317 313 318 318 413 405 405 306 306 306
#> 1991 315 315 356 356 356 422 422 467 404 347 305 336
#> 1992 340 318 318 348 363 435 491 491 404 359 310 337
#> 1993 360 342 406 396 420 420 548 548 463 407 362 362
#> 1994 362 391 419 461 461 535 622 606 508 461 390 432
#> 1995 112 118 132 132 132 135 148 148 136 119 104 118
#> 1996 115 115 141 135 125 149 170 170 170 133 114 140
#> 1997 145 150 178 178 172 178 199 199 184 162 146 166
plot.ts(my_imputations$locf)