Seasonal decompose of monthly data including NA in r

The imputeTS package is a R package solely dedicated to replacing missing values in time series. You can use a function of the package before performing your decomposition.

The na_seadec() and the na_kalman() are especially good for replacing missing data in seasonal time series. But there are also other advanced methods available (Link to imputeTS Paper).

You would use it like this for this problem:

library(imputeTS)
x <- decompose(na_seadec(yourTimeSeries))

or if you want to use the na_kalman method instead:

library(imputeTS)
x <- decompose(na_kalman(yourTimeSeries))

The X-13ARIMA-SEATS software, accessible by the R-package seasonal, handles missing values and seasonal decomposition in a single step:

library(seasonal)

# a monthly time series with some missing values
AirPassengersNA <- AirPassengers
AirPassengersNA[c(2, 24)] <- NA

m <- seas(AirPassengersNA, na.action = na.x13)
head(m$data)

        final  seasonal seasonaladj    trend irregular adjustfac
[1,] 122.5860 0.9029705    122.5860 122.6289 0.9996500 0.9136445
[2,] 123.8615 0.9492046    123.8615 123.8656 0.9999671 0.9408045
[3,] 125.0191 1.0701984    125.0191 125.3132 0.9976535 1.0558387
[4,] 127.4633 1.0028864    127.4633 126.6222 1.0066428 1.0120561
[5,] 127.2526 0.9494692    127.2526 126.8592 1.0031006 0.9508650
[6,] 126.0700 1.0771444    126.0700 126.1723 0.9991886 1.0708339

Try filling in the missing values using a seasonal Kalman filter by employing na.StructTS from the zoo package first:

library(zoo)
decompose(na.StructTS(ts.monthly))

zoo has many other na. functions as well: na.aggregate, na.approx, na.fill, na.locf, na.spline, na.StructTS, na.trim.


I struggled with this for a long time too.

Just use na.locf from the zoo package on your ts object. na.locf returns a ts object, so there are no worries about a changed object type.

Use:

    library(zoo)
    season_ts <- na.locf(season_ts)

where season_ts is your ts object.

Tags:

R

Time Series

Na