Skip to contents

Methods of rjd3toolkit::ts_adjust(), rjd3toolkit::ts_interpolate() and rjd3toolkit::differences() for tsibbles. Each numeric measured variable of each series (one per key) is converted to a ts, with the frequency deduced from the index, and transformed with the function of rjd3toolkit. To transform only some variables, select them first (e.g. with dplyr::select()).

Usage

# S3 method for class 'tbl_ts'
ts_adjust(s, method = c("LeapYear", "LengthOfPeriod"), reverse = FALSE)

# S3 method for class 'tbl_ts'
ts_interpolate(s, method = c("airline", "average"))

# S3 method for class 'tbl_ts'
differences(data, lags = 1, mean = TRUE)

Arguments

s, data

A tsibble.

method

For ts_adjust(): "LeapYear" (correction of the leap year effect, the default) or "LengthOfPeriod" (correction of the length of the period). For ts_interpolate(): "airline" (interpolation with an airline model, the default) or "average".

reverse

For ts_adjust(): if TRUE, the inverse correction is applied.

lags, mean

For differences(): lags of the differences and mean correction (see rjd3toolkit::differences()).

Value

A tsibble with the same rows, in which the numeric measured variables are transformed. For differences(), the first sum(lags) observations of each series are missing (as with tsibble::difference()).

Details

These generics are re-exported, so that library(fable.rjd3) is enough to use them.

As for X13(), the index must be of class yearmonth or yearquarter, with an interval dividing a year, and the series must have no implicit gaps (see tsibble::fill_gaps() to make the missing values explicit before ts_interpolate()).

Examples

air <- as_tsibble(AirPassengers)

# Leap year correction
ts_adjust(air)
#> # A tsibble: 144 x 2 [1M]
#>       index value
#>       <mth> <dbl>
#>  1 1949 Jan  112 
#>  2 1949 Feb  119.
#>  3 1949 Mar  132 
#>  4 1949 Apr  129 
#>  5 1949 May  121 
#>  6 1949 Jun  135 
#>  7 1949 Jul  148 
#>  8 1949 Aug  148 
#>  9 1949 Sep  136 
#> 10 1949 Oct  119 
#> # ℹ 134 more rows

# Interpolation of missing values
air_na <- air
air_na$value[c(5, 50)] <- NA
ts_interpolate(air_na)
#> # A tsibble: 144 x 2 [1M]
#>       index value
#>       <mth> <dbl>
#>  1 1949 Jan  112 
#>  2 1949 Feb  118 
#>  3 1949 Mar  132 
#>  4 1949 Apr  129 
#>  5 1949 May  115.
#>  6 1949 Jun  135 
#>  7 1949 Jul  148 
#>  8 1949 Aug  148 
#>  9 1949 Sep  136 
#> 10 1949 Oct  119 
#> # ℹ 134 more rows

# Regular and seasonal differences
differences(air, lags = c(1, 12))
#> # A tsibble: 144 x 2 [1M]
#>       index value
#>       <mth> <dbl>
#>  1 1949 Jan    NA
#>  2 1949 Feb    NA
#>  3 1949 Mar    NA
#>  4 1949 Apr    NA
#>  5 1949 May    NA
#>  6 1949 Jun    NA
#>  7 1949 Jul    NA
#>  8 1949 Aug    NA
#>  9 1949 Sep    NA
#> 10 1949 Oct    NA
#> # ℹ 134 more rows

# Several series
air_tbl <- tibble::as_tibble(air)
two <- dplyr::bind_rows(
  dplyr::mutate(air_tbl, id = "a"),
  dplyr::mutate(air_tbl, id = "b", value = value * 2)
)
two <- as_tsibble(two, key = id, index = index)
ts_adjust(two)
#> # A tsibble: 288 x 3 [1M]
#> # Key:       id [2]
#>       index value id   
#>       <mth> <dbl> <chr>
#>  1 1949 Jan  112  a    
#>  2 1949 Feb  119. a    
#>  3 1949 Mar  132  a    
#>  4 1949 Apr  129  a    
#>  5 1949 May  121  a    
#>  6 1949 Jun  135  a    
#>  7 1949 Jul  148  a    
#>  8 1949 Aug  148  a    
#>  9 1949 Sep  136  a    
#> 10 1949 Oct  119  a    
#> # ℹ 278 more rows