Movatterモバイル変換


[0]ホーム

URL:


Converting from tsibble

Thetsibble package iswhere the nameslide() originated. It contained originalimplementations ofslide() and friends, along withvariations liketile() andstretch(), all ofwhich have been superceded by slider. As of tsibble 1.0.0, thosefunctions have been completely removed in favor of using slider. Thegoal of this vignette is to explain how to transition from tsibble toslider.

slide()

tsibble’s.size and.align arguments areroughly equivalent to using.before and.afterin slider. In tsibble, you’d specify the full width of the window with.size, and then you’d specify how to construct that windowby.aligning yourself to the left, right, or center. Inslider, you always start at the “current” element, and then specify howmany elements.before and.after the currentelement that you want in the window. The width of the window in sliderterms could be computed as.after - .before + 1.

x<-1:3# The current element, and 1 before itslider::slide(x, identity,.before =1,.complete =TRUE)#> [[1]]#> NULL#>#> [[2]]#> [1] 1 2#>#> [[3]]#> [1] 2 3
# Window size of 2, assume the current element is the right side of the windowtsibble::slide(x, identity,.size =2,.align ="right")#> [[1]]#> [1] NA#>#> [[2]]#> [1] 1 2#>#> [[3]]#> [1] 2 3

We also have to set the.complete argument of slider’sslide() toTRUE, as by default slider allowspartial windows, but tsibble’s version does not. The equivalent argumentto this in tsibble is.partial (note that they areinterpreted inversely of each other).

There is no.fill equivalent in slider. slider alwaysuses the vctrs definition of a missing value (a typedNAfor most vectors, aNULL for lists). This is why the sliderresult above has aNULL, while the tsibble result used anNA (the default.fill value in tsibble).

Specifying windows using.before and.aftermight first feel a bit unnatural to a tsibble or zoo user, but it isgenerally more flexible. You can generate irregular windows that aren’tpossible with tsibble, like:

# The current element, along with 1 before and 3 after (if they exist)slider::slide(1:6, identity,.before =1,.after =3)#> [[1]]#> [1] 1 2 3 4#>#> [[2]]#> [1] 1 2 3 4 5#>#> [[3]]#> [1] 2 3 4 5 6#>#> [[4]]#> [1] 3 4 5 6#>#> [[5]]#> [1] 4 5 6#>#> [[6]]#> [1] 5 6

As you will see in the next section, expanding windows are easy tocreate by setting.before or.after toInf.

This syntax also translates naturally toslide_index(),where the bounds of the window are (by default) computed as.i - .before and.i + .after, which oftencannot be expressed by a single window size value.

tile()

Tiling uses non-overlapping windows. For example, this segmentsx into 4 non-overlapping buckets, where as many buckets aspossible have a window size of 3.

x<-1:10tsibble::tile(x, identity,.size =3)#> [[1]]#> [1] 1 2 3#>#> [[2]]#> [1] 4 5 6#>#> [[3]]#> [1] 7 8 9#>#> [[4]]#> [1] 10

There is no direct equivalent to this in slider, but you can getclose withslide().tile() seems to left-alignthe index, so we need the current element plus two.afterit. Since this is a non-overlapping window, we want to.step forward by the size of the window, three.

result<- slider::slide(x, identity,.after =2,.step =3)result#> [[1]]#> [1] 1 2 3#>#> [[2]]#> NULL#>#> [[3]]#> NULL#>#> [[4]]#> [1] 4 5 6#>#> [[5]]#> NULL#>#> [[6]]#> NULL#>#> [[7]]#> [1] 7 8 9#>#> [[8]]#> NULL#>#> [[9]]#> NULL#>#> [[10]]#> [1] 10

This isn’t exactly the same, asslide() is guaranteed tobe size-stable, returning an object with the same size as.x. However, if youpurrr::compact() theresult to drop theNULL values, then they areequivalent.

purrr::compact(result)#> [[1]]#> [1] 1 2 3#>#> [[2]]#> [1] 4 5 6#>#> [[3]]#> [1] 7 8 9#>#> [[4]]#> [1] 10

stretch()

To construct expanding windows with tsibble, you’ve probably usedstretch(). This fixes an initial window size, and thenexpands to add more observations without dropping any.

x<-1:4tsibble::stretch(x, identity)#> [[1]]#> [1] 1#>#> [[2]]#> [1] 1 2#>#> [[3]]#> [1] 1 2 3#>#> [[4]]#> [1] 1 2 3 4

With slider, you can set.before = Inf to select thecurrent element plus all elements before this one.

slider::slide(x, identity,.before =Inf)#> [[1]]#> [1] 1#>#> [[2]]#> [1] 1 2#>#> [[3]]#> [1] 1 2 3#>#> [[4]]#> [1] 1 2 3 4

stretch() allows you to set.init to fix aninitial minimum window size:

tsibble::stretch(x, identity,.init =3)#> [[1]]#> [1] NA#>#> [[2]]#> [1] NA#>#> [[3]]#> [1] 1 2 3#>#> [[4]]#> [1] 1 2 3 4

There isn’t a direct equivalent of this in slider, but your functioncould returnNULL if the current window size didn’t holdenough elements:

identity3<-function(x) {if (length(x)<3) {NULL  }else {    x  }}slider::slide(x, identity3,.before =Inf)#> [[1]]#> NULL#>#> [[2]]#> NULL#>#> [[3]]#> [1] 1 2 3#>#> [[4]]#> [1] 1 2 3 4

[8]ページ先頭

©2009-2025 Movatter.jp