class: left, top, title-slide .title[ # Predictive Analytics Unit 12: Time Series Graphics ] .author[ ### Ken Arnold
Calvin University ] --- ## Time Plots ```r ansett %>% filter(Airports == "MEL-SYD") %>% autoplot(Passengers) # This calls fabletools::autoplot.tbl_ts ``` <img src="slides12tsgraphics_files/figure-html/ansett-autoplot-1.png" width="90%" style="display: block; margin: auto;" /> --- ## Doing this "by hand" ```r # key_vars(ansett) will tell you what variables are "key" (used for labels) ansett %>% filter(Airports == "MEL-SYD") %>% ggplot(aes(x = Week, y = Passengers, color = Class)) + geom_line() ``` <img src="slides12tsgraphics_files/figure-html/ansett-by-hand-1.png" width="90%" style="display: block; margin: auto;" /> --- ## Making `tsibbles` ```r nycflights13::weather %>% select(origin, time_hour, temp, humid, precip) %>% * as_tsibble(key = origin, index = time_hour) %>% autoplot(temp) ``` <img src="slides12tsgraphics_files/figure-html/nyc-weather-1.png" width="90%" style="display: block; margin: auto;" /> --- ## Level of Granularity ```r vic_elec %>% autoplot(Demand) + labs(title="Half-hourly electrical demand") ``` <img src="slides12tsgraphics_files/figure-html/demand-raw-1.png" width="90%" style="display: block; margin: auto;" /> --- ## Level of Granularity ```r vic_elec %>% index_by(time = yearweek(Time)) %>% summarize(Demand = mean(Demand)) %>% # try changing this to `sum`! autoplot(Demand) + labs(title = "Weekly electrical demand") ``` <img src="slides12tsgraphics_files/figure-html/demand-week-mean-1.png" width="90%" style="display: block; margin: auto;" /> --- ## Seasonal Plots ```r vic_elec %>% gg_season(Demand, period = "week", alpha=.2) + theme(legend.position = "none") + # turn off legend labs(y = "MWh", title = "Victoria electricity demand") ``` <img src="slides12tsgraphics_files/figure-html/demand-week-1.png" width="90%" style="display: block; margin: auto;" /> --- ## Different periods ```r vic_elec %>% gg_season(Demand, period = "day", alpha=.2) + theme(legend.position = "none") + # turn off legend labs(y = "MWh", title = "Victoria electricity demand") ``` <img src="slides12tsgraphics_files/figure-html/demand-day-1.png" width="90%" style="display: block; margin: auto;" /> --- ## Autocorrelation .pull-left[ ```r set.seed(0) noise <- tsibble(sample = 1:50, value = rnorm(50), index = sample) noise %>% autoplot(value) ``` <img src="slides12tsgraphics_files/figure-html/no-correlation-1.png" width="90%" style="display: block; margin: auto;" /> ```r noise %>% ACF(value) ``` ``` # A tsibble: 16 x 2 [1] lag acf <lag> <dbl> 1 1 0.220 2 2 -0.0161 3 3 -0.202 4 4 -0.208 5 5 -0.102 6 6 -0.0183 # … with 10 more rows ``` ] .pull-right[ ```r trend <- tsibble( sample = 1:50, value = 1:50 + rnorm(50), index = sample) trend %>% autoplot(value) ``` <img src="slides12tsgraphics_files/figure-html/trend-1.png" width="90%" style="display: block; margin: auto;" /> ```r trend %>% ACF(value) ``` ``` # A tsibble: 16 x 2 [1] lag acf <lag> <dbl> 1 1 0.937 2 2 0.877 3 3 0.820 4 4 0.761 5 5 0.703 6 6 0.645 # … with 10 more rows ``` ] --- ## Autocorrelation Plot (correlogram) .pull-left[ ```r noise %>% ACF(value) %>% autoplot() ``` <img src="slides12tsgraphics_files/figure-html/no-correlation-acf-1.png" width="90%" style="display: block; margin: auto;" /> ] .pull-right[ ```r trend %>% ACF(value) %>% autoplot() ``` <img src="slides12tsgraphics_files/figure-html/trend-acf-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ## Trend and Seasonality Together ```r a10 <- PBS %>% filter(ATC2 == "A10") %>% select(Month, Concession, Type, Cost) %>% summarise(TotalC = sum(Cost)) %>% mutate(Cost = TotalC / 1e6) ``` .pull-left[ ```r a10 %>% autoplot(Cost) ``` <img src="slides12tsgraphics_files/figure-html/a10-timeplot-1.png" width="90%" style="display: block; margin: auto;" /> ] .pull-right[ ```r a10 %>% ACF(Cost, lag_max = 50) %>% autoplot() ``` <img src="slides12tsgraphics_files/figure-html/a10-acf-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ```r a10 %>% gg_season(Cost, labels = "both") ``` <img src="slides12tsgraphics_files/figure-html/a10-season-1.png" width="90%" style="display: block; margin: auto;" />