Preparation
library(tsibbledata)## Warning: package 'tsibbledata' was built under R version 4.0.4
library(tsibble)## Warning: package 'tsibble' was built under R version 4.0.4
library(DT)
library(ggplot2)## Warning: package 'ggplot2' was built under R version 4.0.4
library(fpp2)## Warning: package 'fpp2' was built under R version 4.0.4
## Warning: package 'forecast' was built under R version 4.0.4
## Warning: package 'fma' was built under R version 4.0.4
## Warning: package 'expsmooth' was built under R version 4.0.4
library(fpp3)## Warning: package 'fpp3' was built under R version 4.0.4
## Warning: package 'feasts' was built under R version 4.0.4
## Warning: package 'fabletools' was built under R version 4.0.4
## Warning: package 'fable' was built under R version 4.0.4
library(autoplotly)## Warning: package 'autoplotly' was built under R version 4.0.4
library(dplyr)
library(forecast)
library(feasts)
library(lubridate)
library(expsmooth)
library(fma)
library(fabletools)help("gafa_stock")## starting httpd help server ... done
help("PBS")
help("vic_elec")
help("pelt")paste("gafa_stock is a time series of Historical stock prices from 2014-2018 for Google, Amazon, Facebook and Apple. ")## [1] "gafa_stock is a time series of Historical stock prices from 2014-2018 for Google, Amazon, Facebook and Apple. "
datatable(gafa_stock,
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
htmltools::em('Table GAFA stock prices.')),
extensions = 'FixedColumns',
option = list(scrollX = TRUE, fixedColumns = TRUE)
)gafa_stock %>% autoplot(Open)PBS %>% autoplot(Scripts)vic_elec %>% autoplot(Demand)pelt %>% autoplot(Hare)interval(gafa_stock) ## Warning: tz(): Don't know how to compute timezone for object of class tbl_ts/
## tbl_df/tbl/data.frame; returning "UTC". This warning will become an error in the
## next major version of lubridate.
## <Interval[0]>
interval(PBS)## Warning: tz(): Don't know how to compute timezone for object of class tbl_ts/
## tbl_df/tbl/data.frame; returning "UTC". This warning will become an error in the
## next major version of lubridate.
## <Interval[0]>
interval(vic_elec)## Warning: tz(): Don't know how to compute timezone for object of class tbl_ts/
## tbl_df/tbl/data.frame; returning "UTC". This warning will become an error in the
## next major version of lubridate.
## <Interval[0]>
gafa_stock %>%
group_by(Symbol) %>%
filter(Close == max(Close))## # A tsibble: 4 x 8 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date Open High Low Close Adj_Close Volume
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2018-10-03 230. 233. 230. 232. 230. 28654800
## 2 AMZN 2018-09-04 2026. 2050. 2013 2040. 2040. 5721100
## 3 FB 2018-07-25 216. 219. 214. 218. 218. 58954200
## 4 GOOG 2018-07-26 1251 1270. 1249. 1268. 1268. 2405600
tute1 <- read.csv("tute1.csv", header = TRUE)
datatable(tute1,
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
htmltools::em('tute1')),
extensions = 'FixedColumns',
option = list(scrollX = TRUE, fixedColumns = TRUE))mytimeseries <- tute1 %>%
mutate(Quarter = yearmonth(Quarter)) %>%
as_tsibble(index = Quarter)mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
facet_grid(name ~ ., scales = "free_y")#install.packages("USgas")
library(USgas)## Warning: package 'USgas' was built under R version 4.0.4
us_total <- us_total %>% as_tsibble(index = year, key = state)new_england<- us_total %>%
group_by(state) %>%
filter(state %in% c('Maine', 'Vermont', 'New Hampshire', 'Massachusetts', 'Connecticut' ,'Rhode Island')) %>%
ungroup()
autoplot(new_england, y)tourism <-readxl::read_excel("tourism.xlsx")datatable(tourism,
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
htmltools::em('tourism')),
extensions = 'FixedColumns',
option = list(scrollX = TRUE, fixedColumns = TRUE))## Warning in instance$preRenderHook(instance): It seems your data is too big
## for client-side DataTables. You may consider server-side processing: https://
## rstudio.github.io/DT/server.html
tourism_tsibble <- tourism %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(key = c("Region", "State", "Purpose"),index = "Quarter")tourism_tsibble %>% group_by(Region, Purpose) %>%
summarise(Trips = mean(Trips)) %>%
ungroup() %>%
filter(Trips == max(Trips))## # A tsibble: 1 x 4 [1Q]
## # Key: Region, Purpose [1]
## Region Purpose Quarter Trips
## <chr> <chr> <qtr> <dbl>
## 1 Melbourne Visiting 2017 Q4 985.
tourism_tsibble %>%
group_by(State) %>%
summarise(Trips = sum(Trips)) %>%
ungroup()## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## State Quarter Trips
## <chr> <qtr> <dbl>
## 1 ACT 1998 Q1 551.
## 2 ACT 1998 Q2 416.
## 3 ACT 1998 Q3 436.
## 4 ACT 1998 Q4 450.
## 5 ACT 1999 Q1 379.
## 6 ACT 1999 Q2 558.
## 7 ACT 1999 Q3 449.
## 8 ACT 1999 Q4 595.
## 9 ACT 2000 Q1 600.
## 10 ACT 2000 Q2 557.
## # ... with 630 more rows
aus_production %>% autoplot(Bricks)## Warning: Removed 20 row(s) containing missing values (geom_path).
pelt %>% autoplot(Lynx)gafa_stock %>% autoplot(Close)vic_elec %>% autoplot(Demand)vic_elec %>% ggplot(aes(x= Date, y= Demand, group = Holiday)) +
geom_line(aes(col=Holiday)) +
facet_grid(Holiday ~ ., scales ="free" )datatable(aus_arrivals, caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
htmltools::em('aus arrivals')),
extensions = 'FixedColumns',
option = list(scrollX = TRUE, fixedColumns = TRUE))aus_arrivals %>% autoplot(Arrivals)aus_arrivals %>% gg_season(Arrivals)aus_arrivals %>% gg_subseries(Arrivals)set.seed(12345678)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))Explore your chosen retail time series using the following functions:
Can you spot any seasonality, cyclicity and trend? What do you learn about the series?
autoplot(myseries, Turnover)gg_season(myseries, Turnover)gg_subseries(myseries, Turnover)gg_lag(myseries, Turnover)myseries %>% ACF(Turnover) %>% autoplot()TP <- us_employment %>%
filter(Title == "Total Private")autoplot(TP,Employed)gg_season(TP,Employed)gg_subseries(TP,Employed)gg_lag(TP,Employed)TP %>% ACF(Employed) %>% autoplot()aus_production %>% autoplot(Bricks)## Warning: Removed 20 row(s) containing missing values (geom_path).
aus_production %>% gg_season(Bricks)## Warning: Removed 20 row(s) containing missing values (geom_path).
aus_production %>% gg_subseries(Bricks)## Warning: Removed 5 row(s) containing missing values (geom_path).
aus_production %>% gg_lag(Bricks)## Warning: Removed 20 rows containing missing values (gg_lag).
pelt %>% autoplot(Hare)pelt %>% gg_subseries(Hare)pelt %>% gg_lag(Hare)H02 <- PBS %>% filter(ATC2 == "H02")
H02 %>% autoplot(Cost)H02 %>% gg_season(Cost)H02 %>% gg_subseries(Cost)H02 %>% ACF(Cost)## # A tsibble: 92 x 6 [1M]
## # Key: Concession, Type, ATC1, ATC2 [4]
## Concession Type ATC1 ATC2 lag acf
## <chr> <chr> <chr> <chr> <lag> <dbl>
## 1 Concessional Co-payments H H02 1M 0.834
## 2 Concessional Co-payments H H02 2M 0.679
## 3 Concessional Co-payments H H02 3M 0.514
## 4 Concessional Co-payments H H02 4M 0.352
## 5 Concessional Co-payments H H02 5M 0.264
## 6 Concessional Co-payments H H02 6M 0.219
## 7 Concessional Co-payments H H02 7M 0.253
## 8 Concessional Co-payments H H02 8M 0.337
## 9 Concessional Co-payments H H02 9M 0.464
## 10 Concessional Co-payments H H02 10M 0.574
## # ... with 82 more rows
us_gasoline %>% autoplot(Barrels)us_gasoline %>% gg_season(Barrels)us_gasoline %>% gg_subseries(Barrels)us_gasoline %>% gg_lag(Barrels)VicPig<- aus_livestock %>%
filter(State == "Victoria",
Animal == "Pigs",
between(year(Month),1990,1995))
VicPig %>% ACF(Count) %>% autoplot()dgoog <- gafa_stock %>%
filter(Symbol == "GOOG", year(Date) >= 2018) %>%
mutate(trading_day = row_number()) %>%
update_tsibble(index = trading_day, regular = TRUE) %>%
mutate(diff = difference(Close))google_stock <- gafa_stock %>%
filter(Symbol == "GOOG") %>%
mutate(trading_day = row_number()) %>%
update_tsibble(index = trading_day, regular = TRUE)
google_stock %>%
ACF(difference(Close)) %>%
autoplot()5/100*30## [1] 1.5
Data series bukanlah white noise jika adal lebih dari 1,5 yang tertinggal di luar batas. Kita dapat melihat dari plot di atas bahwa ada tiga lags yang di luar batas, yang berarti bahwa data series bukan white noise.