Skip to contents

Interpolation of a time series with missing values

Usage

ts_interpolate(s, method = c("airline", "average"))

Arguments

s

The original time series

method

airline: interpolation through an estimated airline model average: interpolation using the average of the previous and next non missing values

Value

The interpolated series

Examples

ts_interpolate(AirPassengers)
#>      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

x <- AirPassengers
x[50:60] <- NA
ts_interpolate(x)
#>           Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
#> 1949 112.0000 118.0000 132.0000 129.0000 121.0000 135.0000 148.0000 148.0000
#> 1950 115.0000 126.0000 141.0000 135.0000 125.0000 149.0000 170.0000 170.0000
#> 1951 145.0000 150.0000 178.0000 163.0000 172.0000 178.0000 199.0000 199.0000
#> 1952 171.0000 180.0000 193.0000 181.0000 183.0000 218.0000 230.0000 242.0000
#> 1953 196.0000 190.6534 218.7178 207.0350 209.5219 239.5192 263.3560 262.0165
#> 1954 204.0000 188.0000 235.0000 227.0000 234.0000 264.0000 302.0000 293.0000
#> 1955 242.0000 233.0000 267.0000 269.0000 270.0000 315.0000 364.0000 347.0000
#> 1956 284.0000 277.0000 317.0000 313.0000 318.0000 374.0000 413.0000 405.0000
#> 1957 315.0000 301.0000 356.0000 348.0000 355.0000 422.0000 465.0000 467.0000
#> 1958 340.0000 318.0000 362.0000 348.0000 363.0000 435.0000 491.0000 505.0000
#> 1959 360.0000 342.0000 406.0000 396.0000 420.0000 472.0000 548.0000 559.0000
#> 1960 417.0000 391.0000 419.0000 461.0000 472.0000 535.0000 622.0000 606.0000
#>           Sep      Oct      Nov      Dec
#> 1949 136.0000 119.0000 104.0000 118.0000
#> 1950 158.0000 133.0000 114.0000 140.0000
#> 1951 184.0000 162.0000 146.0000 166.0000
#> 1952 209.0000 191.0000 172.0000 194.0000
#> 1953 227.2095 200.6290 175.7723 198.3361
#> 1954 259.0000 229.0000 203.0000 229.0000
#> 1955 312.0000 274.0000 237.0000 278.0000
#> 1956 355.0000 306.0000 271.0000 306.0000
#> 1957 404.0000 347.0000 305.0000 336.0000
#> 1958 404.0000 359.0000 310.0000 337.0000
#> 1959 463.0000 407.0000 362.0000 405.0000
#> 1960 508.0000 461.0000 390.0000 432.0000