ts represents regularly spaced time series using numeric time stamps.
ts(data, start = 1, frequency = 1)
day 0
zoo provides infrastructure for regularly and irregularly spaced time series using arbitrary classes for the time stamps.
zoo(x, order.by = index(x))
2004/02
xts extends the zoo
class but provides a mechanism to customize the object's meta-data.
xts(x, order.by = index(x))
2008/01
irts, fts, timeSeries, tis, and etc.
...
reference: XKCD on "standards"
The data structure that underlies these time series objects:
⎡⎢
⎢
⎢
⎢
⎢⎣X11X21⋯Xp1X12X22⋯Xp2⋮⋮⋱⋮X1TX2T⋯XpT⎤⎥
⎥
⎥
⎥
⎥⎦
where Xjt
represents series j
, for j=1,…,p
and 1≤t≤T
, in the form of a T×p
matrix.
The data structure that underlies these time series objects:
⎡⎢
⎢
⎢
⎢
⎢⎣X11X21⋯Xp1X12X22⋯Xp2⋮⋮⋱⋮X1TX2T⋯XpT⎤⎥
⎥
⎥
⎥
⎥⎦
where Xjt
represents series j
, for j=1,…,p
and 1≤t≤T
, in the form of a T×p
matrix.
This matrix structure assumes
It is model-centric rather than data-centric.
Too many defaults as if we live in an ideal data world
enquiry
#> # A tibble: 110,397 x 5#> date channel category service volume#> <date> <fct> <fct> <fct> <int>#> 1 2014-06-16 Email Built Structure Control Advert… 1#> 2 2014-06-17 Email Built Structure Control Advert… 1#> 3 2014-07-14 Email Built Structure Control Advert… 1#> 4 2014-07-21 Email Built Structure Control Advert… 1#> 5 2014-07-22 Email Built Structure Control Advert… 1#> 6 2014-07-30 Email Built Structure Control Advert… 3#> # ... with 1.104e+05 more rows
enquiry
#> # A tibble: 110,397 x 5#> date channel category service volume#> <date> <fct> <fct> <fct> <int>#> 1 2014-06-16 Email Built Structure Control Advert… 1#> 2 2014-06-17 Email Built Structure Control Advert… 1#> 3 2014-07-14 Email Built Structure Control Advert… 1#> 4 2014-07-21 Email Built Structure Control Advert… 1#> 5 2014-07-22 Email Built Structure Control Advert… 1#> 6 2014-07-30 Email Built Structure Control Advert… 3#> # ... with 1.104e+05 more rows
as_tsibble()
library(tsibble)enquiry_tsbl <- enquiry %>% as_tsibble( key = id(service | category, channel), index = date )enquiry_tsbl
#> # A tsibble: 110,397 x 5 [1DAY]#> # Key: service | category, channel [204]#> date channel category service volume#> <date> <fct> <fct> <fct> <int>#> 1 2014-06-16 Email Built Structure Control Advert… 1#> 2 2014-06-17 Email Built Structure Control Advert… 1#> 3 2014-07-14 Email Built Structure Control Advert… 1#> 4 2014-07-21 Email Built Structure Control Advert… 1#> 5 2014-07-22 Email Built Structure Control Advert… 1#> 6 2014-07-30 Email Built Structure Control Advert… 3#> # ... with 1.104e+05 more rows
as_tsibble()
tbl_ts
#> # A tsibble: 110,397 x 5 [1DAY]#> # Key: service | category, channel [204]#> date channel category service volume#> <date> <fct> <fct> <fct> <int>#> 1 2014-06-16 Email Built Structure Control Advert… 1#> 2 2014-06-17 Email Built Structure Control Advert… 1#> 3 2014-07-14 Email Built Structure Control Advert… 1#> 4 2014-07-21 Email Built Structure Control Advert… 1#> 5 2014-07-22 Email Built Structure Control Advert… 1#> 6 2014-07-30 Email Built Structure Control Advert… 3#> # ... with 1.104e+05 more rows
as_tsibble()
tbl_ts
fill_na()
enquiry_tsbl %>% fill_na()
#> # A tsibble: 237,821 x 5 [1DAY]#> # Key: service | category, channel [204]#> date channel category service volume#> <date> <fct> <fct> <fct> <int>#> 1 2014-06-16 Email Built Structure Control Advert… 1#> 2 2014-06-17 Email Built Structure Control Advert… 1#> 3 2014-06-18 Email Built Structure Control Advert… NA#> 4 2014-06-19 Email Built Structure Control Advert… NA#> 5 2014-06-20 Email Built Structure Control Advert… NA#> 6 2014-06-21 Email Built Structure Control Advert… NA#> # ... with 2.378e+05 more rows
as_tsibble()
tbl_ts
fill_na()
enquiry_full <- enquiry_tsbl %>% fill_na(volume = 0L)enquiry_full
#> # A tsibble: 237,821 x 5 [1DAY]#> # Key: service | category, channel [204]#> date channel category service volume#> <date> <fct> <fct> <fct> <int>#> 1 2014-06-16 Email Built Structure Control Advert… 1#> 2 2014-06-17 Email Built Structure Control Advert… 1#> 3 2014-06-18 Email Built Structure Control Advert… 0#> 4 2014-06-19 Email Built Structure Control Advert… 0#> 5 2014-06-20 Email Built Structure Control Advert… 0#> 6 2014-06-21 Email Built Structure Control Advert… 0#> # ... with 2.378e+05 more rows
as_tsibble()
tbl_ts
fill_na()
index_by()
library(lubridate)enquiry_full %>% group_by(channel, category) %>% index_by(year = year(date))
#> # A tsibble: 237,821 x 6 [1DAY]#> # Key: service | category, channel [204]#> # Groups: channel, category @ year [239]#> date channel category service volume year#> <date> <fct> <fct> <fct> <int> <dbl>#> 1 2014-06-16 Email Built Struc… Advertising… 1 2014#> 2 2014-06-17 Email Built Struc… Advertising… 1 2014#> 3 2014-06-18 Email Built Struc… Advertising… 0 2014#> 4 2014-06-19 Email Built Struc… Advertising… 0 2014#> 5 2014-06-20 Email Built Struc… Advertising… 0 2014#> 6 2014-06-21 Email Built Struc… Advertising… 0 2014#> # ... with 2.378e+05 more rows
as_tsibble()
tbl_ts
fill_na()
index_by()
index_by()
+ summarise()
enquiry_year <- enquiry_full %>% group_by(channel, category) %>% index_by(year = year(date)) %>% summarise(annual_volume = sum(volume))enquiry_year
#> # A tsibble: 239 x 4 [1YEAR]#> # Key: category, channel [48]#> # Groups: channel [4]#> channel category year annual_volume#> <fct> <fct> <dbl> <int>#> 1 Email Animal Control 2014 962#> 2 Email Animal Control 2015 2849#> 3 Email Animal Control 2016 3159#> 4 Email Animal Control 2017 3416#> 5 Email Animal Control 2018 860#> 6 Email Built Structure Control 2014 336#> # ... with 233 more rows
as_tsibble()
tbl_ts
fill_na()
index_by()
index_by()
+ summarise()
arrange()
, filter()
, slice()
mutate()
, transmute()
, select()
, rename()
, summarise()
/summarize()
*_join()
group_by()
, ungroup()
gather()
, spread()
, nest()
, unnest()
arrange()
, filter()
, slice()
mutate()
, transmute()
, select()
, rename()
, summarise()
/summarize()
*_join()
group_by()
, ungroup()
gather()
, spread()
, nest()
, unnest()
enquiry_sum <- enquiry_full %>% summarise(ttl_volume = sum(volume))enquiry_sum
#> # A tsibble: 1,551 x 2 [1DAY]#> date ttl_volume#> <date> <int>#> 1 2014-01-01 636#> 2 2014-01-02 2171#> 3 2014-01-03 1968#> 4 2014-01-04 559#> 5 2014-01-05 489#> 6 2014-01-06 3320#> # ... with 1,545 more rows
slide()
/slide2()
/pslide()
: sliding window with overlapping observationsslide()
/slide2()
/pslide()
: sliding window with overlapping observationstile()
/tile2()
/ptile()
: tiling window without overlapping observations
slide()
/slide2()
/pslide()
: sliding window with overlapping observationstile()
/tile2()
/ptile()
: tiling window without overlapping observationsstretch()
/stretch2()
/pstretch()
: fixing an initial window and expanding to include more observations
Type-stable: slide()
/tile()
/stretch()
(a list) other variants: *_dbl()
, *_int()
, *_lgl()
, *_chr()
enquiry_sum %>% mutate(ma = slide_dbl(ttl_volume, mean, .size = 7))
enquiry_sum %>% mutate(yrmth = yearmonth(date)) %>% nest(-yrmth) %>% mutate(ma = slide_dbl( data, ~ mean(bind_rows(.)$ttl_volume), .size = 2 )) %>% unnest(data)
ts represents regularly spaced time series using numeric time stamps.
ts(data, start = 1, frequency = 1)
day 0
zoo provides infrastructure for regularly and irregularly spaced time series using arbitrary classes for the time stamps.
zoo(x, order.by = index(x))
2004/02
xts extends the zoo
class but provides a mechanism to customize the object's meta-data.
xts(x, order.by = index(x))
2008/01
irts, fts, timeSeries, tis, and etc.
...
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |