library(tidyverse)
library(fpp3)
library(kableExtra)
library(ggplot2)Use the help function to explore what the series ’gafa_stock, ’PBS, ’vic_elec and ’pelt represent. (a) Use autoplot() to plot some of the series in these data sets. (b) What is the time interval of each series?
Explore the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, Demand from vic_elec.
Use ? (or help()) to find out about the data in each series. What is the time interval of each series? Use autoplot() to produce a time plot of each series. For the last plot, modify the axis labels and title.
help(aus_production)## starting httpd help server ... done
help(pelt)
help(gafa_stock)
help(vic_elec)autoplot(aus_production, Bricks)## Warning: Removed 20 rows containing missing values (`geom_line()`).
ggtitle("Quarterly production of selected commodities in Australia")## $title
## [1] "Quarterly production of selected commodities in Australia"
##
## attr(,"class")
## [1] "labels"
There is a zig-zag movement in the time series for Aus production
autoplot(pelt, Lynx) +
ggtitle("Canadaian Lync pelts traded 1845-1945")
Canadian Lynx furs rises and fall again.The peaks also seem to
alternate, where a higher peak is followed by a shorter peak the next
cycle.
autoplot(gafa_stock, Open) +
ggtitle("Daily Opening Price for Stocks Traded, 2014 - 2018")
The graph show a considerable steady increase in price over time
for Amazon and google. After which there was a small decline from the
peak.
PBS %>%
summarise(TotalC = sum(Cost)) %>%
autoplot(TotalC) +
labs(title = "Total Costs of Scripts in Australia, July 1991 - June 2008",
y = "Total Cost ($AUD)")The total costs of all Medicare scripts in Australia increased over time. There also seems to be a seasonality, where the total cost takes a dip and then rises again every year.
autoplot(vic_elec, Demand) +
ggtitle("Hald-hourly Electricity Demand for Victoria, Australia")
There seems to be a seasonality where electricity demands increase
greatly during the summers, followed by a decrease and then a small
increase midway into the year, around winter. That is followed by a
decrease until summer.
What is the time interval of each series?
gafa_stock: 1 day (excluding weekends) PBS: 1 month vic_elec: 1 half-hour pelt: 1 year
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)) %>%
select(Symbol, Date)## # A tsibble: 4 x 2 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date
## <chr> <date>
## 1 AAPL 2018-10-03
## 2 AMZN 2018-09-04
## 3 FB 2018-07-25
## 4 GOOG 2018-07-26
The peak closing prices were hit between late July to early October for each of the four stocks.
Download the file ’tute1.csv from the book website, 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.
2.3(a) Convert the data to time series 2.3(b) Construct time series plots of each of the three series 2.3(c) Check what happens when you don’t include facet_grid().
tute1 <- readr::read_csv("tute1.csv")## Rows: 100 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): Quarter
## dbl (3): Sales, AdBudget, GDP
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#View(tute1)
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") +
ggtitle("facet_grid")mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
ggtitle("No facet_grid")When you don’t include facet_grid, the horizontal scales are not aligned, nor, can you see the numbers on the scale properly. It creates a separate graph for each variable.
The USgas package contains data on the demand for natural gas in the US.
Install the USgas package.
Create a tsibble from us_total with year as the index and state as the key.
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).
#install.packages("USgas")
library(USgas)## Warning: package 'USgas' was built under R version 4.2.3
us_total <- us_total %>%
as_tibble(key = state,
index = year)us_total %>%
filter(state %in% c('Maine', 'Vermont', 'New Hampshire', 'Massachusetts', 'Connecticut', 'Rhode Island')) %>%
ggplot(aes(x = year, y = y, colour = state)) +
geom_line() +
facet_grid(state ~., scales = "free_y") +
labs(title = "Annual Natural Gas Consumption in New England",
y = "Consumption")The yearly natural has consumption follows an increasing trend for Connecticut, Massachusetts, and Vermont, and is decreasing in the remaining states.
tourism <- readxl::read_excel("tourism.xlsx")tourism_ts <- tourism %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(key = c(Region, State, Purpose),
index = Quarter)tourism_ts %>%
group_by(Region, Purpose) %>%
mutate(Avg_Trips = mean(Trips)) %>%
ungroup() %>%
filter(Avg_Trips == max(Avg_Trips)) %>%
distinct(Region, Purpose)## # A tibble: 1 × 2
## Region Purpose
## <chr> <chr>
## 1 Sydney Visiting
Syndey, Australia has the maximum number of overnight trips on average for Visiting.
tourism %>%
group_by(Quarter, State) %>%
mutate(Quarter = yearquarter(Quarter),
Total_Trips = sum(Trips)) %>%
select(Quarter, State, Total_Trips) %>%
distinct() %>%
as_tsibble(index = Quarter,
key = State)## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## # Groups: State @ Quarter [640]
## Quarter State Total_Trips
## <qtr> <chr> <dbl>
## 1 1998 Q1 ACT 551.
## 2 1998 Q2 ACT 416.
## 3 1998 Q3 ACT 436.
## 4 1998 Q4 ACT 450.
## 5 1999 Q1 ACT 379.
## 6 1999 Q2 ACT 558.
## 7 1999 Q3 ACT 449.
## 8 1999 Q4 ACT 595.
## 9 2000 Q1 ACT 600.
## 10 2000 Q2 ACT 557.
## # … with 630 more rows
Monthly Australian retail data is provided in aus_retail. Explore your chosen retail time series using the following functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() %>% autoplot().
set.seed(222)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
autoplot(myseries, Turnover)myseries %>% gg_season(Turnover)myseries %>% gg_subseries(Turnover)myseries %>% gg_lag(Turnover, geom = "point")myseries %>% ACF(Turnover) %>% autoplot()It is observe that there is a steady increase in the retail trade turnover,with a cyclicity of a year. Every year seems to have similar seasonality as the previous year. The turnover seems to increase from November to December and then decreasing to February. There is also a small increase from June to July and a small decrease from October to November. From the series, it can be assumed that sales increase around the holiday season and then decrease afterwards.