pacman::p_load(fpp3,
               tidyverse,
               tsibble,
               fable,
               feasts,
               tsibbledata)

1 Use the help function to explore what the series gafa_stock, PBS, vic_elec and peltrepresent.

help("gafa_stock")
## starting httpd help server ... done
help("PBS")
help("vic_elec")
help("pelt")

1.1 Use autoplot() to plot some of the series in these data sets.

1.1.1 gafa_stock

gafa_stock %>% autoplot(Open)

gafa_stock adalah rangkaian waktu harga saham di $USD dari 2014-2018 untuk Apple, Amazon, Facebook, dan Google.

1.1.2 PBS

PBS %>% autoplot(Scripts)

PBS adalah rangkaian waktu medicare bulanan Australia dengan dua nilai yaitu Scripts and Cost.

1.1.3 vic_elec

vic_elec %>% autoplot(Demand)

vic_elec adalah rangkaian waktu permintaan listrik setengah jam untuk Victoria, Australia dengan tiga nilai yaitu Demand, Temperature, dan Holiday.

1.1.4 Pelt

pelt %>% autoplot(Hare)

Pelt adalah serangkaian waktu catatan perdagangan pelt Snowshoe Hare dan bulu Lynx Kanada dari 1845 hingga 1935.

1.2 What is the time interval of each series?

gafa_stock = 1 Day PBS = 1 Month vic_elec = 30 Minutes pelt = 1 Year ### gafa_stock The time interval of gafa_stock is 1 day.

1.2.1 PBS

PBS has no fix time interval.

1.2.2 vic_elec

The time interval of vic_elec is 30 minutes.

1.2.3 Pelt

The time interval of pelt is 1 year.

2 Use filter() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock.

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

3 Download the file tute1.csv here, open it in Excel (or some other spreadsheet application), and review its contents. You should find four columns of information. Columns B through D each contain a quarterly series, labelled Sales, AdBudget and GDP. Sales contains the quarterly sales for a small company over the period 1981-2005. AdBudget is the advertising budget and GDP is the gross domestic product. All series have been adjusted for inflation.

3.1 You can read the data into R with the following script:

library (DT)
tute1 <- read.csv("tute1.csv")
datatable(tute1,
            extensions = 'FixedColumns',
            option = list(scrollX = TRUE, fixedColumns = TRUE)
          )

3.2 Convert the data to time series

timeseries <- tute1 %>%
  mutate(Quarter = yearmonth(Quarter)) %>%
  as_tsibble(index = Quarter)

3.3 Construct time series plots of each of the three series

timeseries %>%
  pivot_longer(-Quarter) %>%
  ggplot(aes(x = Quarter, y = value, colour = name))+ 
  geom_line()+
  facet_grid(name ~ ., scales = "free_y")

3.4 Check what happens when you don’t include facet_grid().

timeseries %>%
  pivot_longer(-Quarter) %>%
  ggplot(aes(x = Quarter, y = value, colour = name))+ 
  geom_line()

sertakan facet_grid(), ketiga set diplot pada satu grafik daripada 3 yang berbeda dengan nilai sumbu x yang sama. Karena rentang nilai berbeda secara signifikan untuk setiap set, itu membuatnya sulit untuk melihat pola dalam setiap set. Semua grafik diplot pada satu pesawat, yang membuat membaca ketiganya sedikit sulit.

4 The USgas package contains data on the demand for natural gas in the US.

4.1 Install the USgas package.

#install.packages("USgas")
library(USgas)
## Warning: package 'USgas' was built under R version 4.0.5

4.2 Create a tsibble from us_total with year as the index and state as the key.

Gas <- us_total %>% as_tsibble(key = state, index = year)
Gas
## # A tsibble: 1,265 x 3 [1Y]
## # Key:       state [53]
##     year state        y
##    <int> <chr>    <int>
##  1  1997 Alabama 324158
##  2  1998 Alabama 329134
##  3  1999 Alabama 337270
##  4  2000 Alabama 353614
##  5  2001 Alabama 332693
##  6  2002 Alabama 379343
##  7  2003 Alabama 350345
##  8  2004 Alabama 382367
##  9  2005 Alabama 353156
## 10  2006 Alabama 391093
## # ... with 1,255 more rows

4.3 Plot the annual natural gas consumption by state for the New England area (comprising the states of Maine, Vermont, New Hampshire, Massachusetts, Connecticut and Rhode Island).

autoplot(Gas,y)+ylab("y")+ggtitle("Natural Gas in U.S.")

5 Follow the instructions below:

5.1 Download tourism.xlsx here and read it into R using readxl::read_excel().

tourism <- readxl::read_excel("tourism.xlsx")

5.2 Create a tsibble which is identical to the tourism tsibble from the tsibble package.

tsibble_tourism <- tourism %>% mutate(Quarter = yearquarter(Quarter) ) %>%
 as_tsibble(index = Quarter, key = c(Region, State, Purpose))
tsibble_tourism
## # A tsibble: 24,320 x 5 [1Q]
## # Key:       Region, State, Purpose [304]
##    Quarter Region   State           Purpose  Trips
##      <qtr> <chr>    <chr>           <chr>    <dbl>
##  1 1998 Q1 Adelaide South Australia Business  135.
##  2 1998 Q2 Adelaide South Australia Business  110.
##  3 1998 Q3 Adelaide South Australia Business  166.
##  4 1998 Q4 Adelaide South Australia Business  127.
##  5 1999 Q1 Adelaide South Australia Business  137.
##  6 1999 Q2 Adelaide South Australia Business  200.
##  7 1999 Q3 Adelaide South Australia Business  169.
##  8 1999 Q4 Adelaide South Australia Business  134.
##  9 2000 Q1 Adelaide South Australia Business  154.
## 10 2000 Q2 Adelaide South Australia Business  169.
## # ... with 24,310 more rows

5.3 Find what combination of Region and Purpose had the maximum number of overnight trips on average.

tsibble_tourism %>% 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.

5.4 Create a new tsibble which combines the Purposes and Regions, and just has total trips by State

new_tsibble <- tsibble_tourism %>%
 group_by(State) %>% summarise(Trips = sum(Trips))%>%
 ungroup()
new_tsibble
## # 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

6 Create time plots of the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, Demand from vic_elec.

aus_production

aus_production %>% autoplot(Bricks)
## Warning: Removed 20 row(s) containing missing values (geom_path).

pelt

pelt %>% autoplot(Lynx)

gafa_stock

gafa_stock %>% autoplot(Close)

vic_elec

vic_elec %>% autoplot(Demand)

6.1 Use ? (or help()) to find out about the data in each series.

6.2 For the last plot, modify the axis labels and title.(Demand from vic_elec)

vic_elec %>% ggplot(aes(x= Date, y= Demand, group = Holiday)) +
 geom_line(aes(col=Holiday)) +
 facet_grid(Holiday ~ ., scales ="free" )

7 The aus_arrivals data set comprises quarterly international arrivals to Australia from Japan, New Zealand, UK and the US.

datatable(aus_arrivals,
          caption = htmltools::tags$caption(
            style = 'caption-side: bottom; text-align: center;',
            htmltools::em('data set comprises quarterly international arrivals to Australia from Japan, New Zealand, UK and the US')),
            extensions = 'FixedColumns',
            option = list(scrollX = TRUE, fixedColumns = TRUE)
          )

7.1 Use autoplot(), gg_season() and gg_subseries() to compare the differences between the arrivals from these four countries.

7.1.1 autoplot()

aus_arrivals %>% autoplot(Arrivals)

7.1.2 gg_season()

aus_arrivals %>% gg_season(Arrivals)

7.1.3 gg_subseries()

aus_arrivals %>% gg_subseries(Arrivals)

7.2 Can you identify any unusual observations?

Dari informasi yang diberikan di atas, kita dapat melihat kedatangan Jepang ke Australia sangat menurun. Setelah menjelajahi beberapa informasi di google di wikipedia, apa yang saya dapatkan adalah pada Maret 2007 Australia dan Jepang menandatangani pakta keamanan bersama. Ruang lingkup kerjasama keamanan meliputi:

Penegakan hukum dalam memerangi kejahatan transnasional, termasuk perdagangan narkotika ilegal dan prekursor, penyelundupan dan perdagangan orang, pemalsuan mata uang dan penyelundupan senjata Keamanan Perbatasan Penanggulangan Terorisme Perlucutan Senjata dan Kontra-Proliferasi Senjata Pemusnah Massal dan sarana mereka pengiriman Operasi Perdamaian Pertukaran penilaian strategis dan informasi terkait Maritim dan keamanan penerbangan Operasi bantuan kemanusiaan, termasuk perencanaan Kontinjensi bantuan bencana.

8 Monthly Australian retail data is provided in aus_retail. Select one of the time series as follows (but choose your own seed value):

set.seed(7777777)
seedseries <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`,1))

8.1 Explore your chosen retail time series using the following functions:

8.1.1 autoplot()

autoplot(seedseries, Turnover)

8.1.2 gg_season()

gg_season(seedseries, Turnover)

8.1.3 gg_subseries()

gg_subseries(seedseries, Turnover)

8.1.4 gg_lag()

gg_lag(seedseries, Turnover)

8.1.5 ACF() %>% autoplot()

seedseries %>% ACF(Turnover) %>% autoplot()

8.2 Can you spot any seasonality, cyclicity and trend? What do you learn about the series?

Dari autoplot, kita dapat melihat pola musiman atau siklik yang jelas dalam seri waktu, dan tren kenaikan.

Plot musiman menunjukkan bahwa memang ada pola musiman. Plot ini juga mengungkapkan bahwa ada lompatan besar khas setiap tahun pada bulan Desember, dan penurunan pada bulan Februari. Penjualan mulai meningkat pada musim gugur, memuncak antara November dan Desember, kemudian menurun setelah Januari, kemungkinan bertepatan dengan belanja liburan dan penjualan untuk Natal.

Subseri musiman menawarkan perspektif baru tentang musiman dengan menunjukkan nilai rata-rata bulanan. Kami melihat peningkatan besar dari November hingga Desember dan penurunan dari Desember hingga Februari, tetapi juga tren omset yang kecil dan menurun dari Januari hingga Juni dan peningkatan serupa dari Juli hingga November, sebelum lonjakan besar dari November hingga Desember.

9 Use the following graphics functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() and explore features from the following time series: “Total Private” Employed from us_employment, Bricks from aus_production, Hare from pelt, “H02” Cost from PBS, and us_gasoline.

Priv <- us_employment %>% 
        filter(Title == "Total Private")
Priv %>% autoplot(Employed)

Priv %>% gg_season(Employed)

Priv %>% gg_subseries(Employed)

Priv %>% gg_lag(Employed)

Priv %>% ACF(Employed)
## # A tsibble: 29 x 3 [1M]
## # Key:       Series_ID [1]
##    Series_ID       lag   acf
##    <chr>         <lag> <dbl>
##  1 CEU0500000001    1M 0.997
##  2 CEU0500000001    2M 0.993
##  3 CEU0500000001    3M 0.990
##  4 CEU0500000001    4M 0.986
##  5 CEU0500000001    5M 0.983
##  6 CEU0500000001    6M 0.980
##  7 CEU0500000001    7M 0.977
##  8 CEU0500000001    8M 0.974
##  9 CEU0500000001    9M 0.971
## 10 CEU0500000001   10M 0.968
## # ... with 19 more rows
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).

aus_production %>% ACF(Bricks)
## # A tsibble: 22 x 2 [1Q]
##      lag   acf
##    <lag> <dbl>
##  1    1Q 0.900
##  2    2Q 0.815
##  3    3Q 0.813
##  4    4Q 0.828
##  5    5Q 0.720
##  6    6Q 0.642
##  7    7Q 0.655
##  8    8Q 0.692
##  9    9Q 0.609
## 10   10Q 0.556
## # ... with 12 more rows
pelt %>% autoplot(Hare)

pelt %>% gg_subseries(Hare)

pelt %>% gg_lag(Hare)

pelt %>% ACF(Hare)
## # A tsibble: 19 x 2 [1Y]
##      lag     acf
##    <lag>   <dbl>
##  1    1Y  0.658 
##  2    2Y  0.214 
##  3    3Y -0.155 
##  4    4Y -0.401 
##  5    5Y -0.493 
##  6    6Y -0.401 
##  7    7Y -0.168 
##  8    8Y  0.113 
##  9    9Y  0.307 
## 10   10Y  0.340 
## 11   11Y  0.296 
## 12   12Y  0.206 
## 13   13Y  0.0372
## 14   14Y -0.153 
## 15   15Y -0.285 
## 16   16Y -0.295 
## 17   17Y -0.202 
## 18   18Y -0.0676
## 19   19Y  0.0956
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)

us_gasoline %>% ACF(Barrels)
## # A tsibble: 31 x 2 [1W]
##      lag   acf
##    <lag> <dbl>
##  1    1W 0.893
##  2    2W 0.882
##  3    3W 0.873
##  4    4W 0.866
##  5    5W 0.847
##  6    6W 0.844
##  7    7W 0.832
##  8    8W 0.831
##  9    9W 0.822
## 10   10W 0.808
## # ... with 21 more rows

10 The following time plots and ACF plots correspond to four different time series. Your task is to match each time plot in the first row with one of the ACF plots in the second row.

knitr::include_graphics("download.png")

+ 1 with B + 2 with A + 3 with D + 4 with C

11 The aus_livestock data contains the monthly total number of pigs slaughtered in Victoria, Australia, from Jul 1972 to Dec 2018. Use filter() to extract pig slaughters in Victoria between 1990 and 1995. Use autoplot() and ACF() for this data. How do they differ from white noise? If a longer period of data is used, what difference does it make to the ACF?

Victoria_Pig<- aus_livestock %>% 
                        filter(State == "Victoria", 
                               Animal == "Pigs", 
                               between(year(Month),1990,1995))
Victoria_Pig %>% ACF(Count) %>% autoplot()

12 Use the following code to compute the daily changes in Google closing stock prices.

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))

12.1 Why was it necessary to re-index the tsibble?

Karena interval untuk GOOG berdasarkan Tanggal akan kacau dan tidak sama untuk setiap baris ketika kita menggunakan filter untuk simbol GOOG, kita perlu mengubah indeks kita untuk menggunakan nomor urutan baris sehingga interval untuk setiap baris adalah sama.

12.2 Plot these differences and their ACF.

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()

12.3 Do the changes in the stock prices look like white noise?

5/100*30
## [1] 1.5

Seri data bukan suara putih jika ada lebih dari 1,5 tertinggal di luar batas.

Kita dapat melihat dari plot di atas bahwa ada tiga keterbesanan yang di luar batas, yang berarti bahwa seri data bukan kebisingan putih.