2.1

Load library

library(fpp3)
## Registered S3 method overwritten by 'tsibble':
##   method               from 
##   as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.1 ──
## ✔ tibble      3.2.1     ✔ tsibble     1.1.5
## ✔ dplyr       1.1.4     ✔ tsibbledata 0.4.1
## ✔ tidyr       1.3.1     ✔ feasts      0.4.1
## ✔ lubridate   1.9.3     ✔ fable       0.4.1
## ✔ ggplot2     3.5.1
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date()    masks base::date()
## ✖ dplyr::filter()      masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval()  masks lubridate::interval()
## ✖ dplyr::lag()         masks stats::lag()
## ✖ tsibble::setdiff()   masks base::setdiff()
## ✖ tsibble::union()     masks base::union()
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0     ✔ readr   2.1.5
## ✔ purrr   1.0.2     ✔ stringr 1.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()     masks stats::filter()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag()        masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Identify the time interval for each series

Bricks (from aus_production): Quarterly

aus_production %>% filter(!is.na(Bricks)) %>% select(Quarter) %>% head()
## # A tsibble: 6 x 1 [1Q]
##   Quarter
##     <qtr>
## 1 1956 Q1
## 2 1956 Q2
## 3 1956 Q3
## 4 1956 Q4
## 5 1957 Q1
## 6 1957 Q2

Lynx (from pelt): Yearly

pelt %>% filter(!is.na(Lynx)) %>% select(Year) %>% head()
## # A tsibble: 6 x 1 [1Y]
##    Year
##   <dbl>
## 1  1845
## 2  1846
## 3  1847
## 4  1848
## 5  1849
## 6  1850

Close (from gafa_stock): Daily

gafa_stock %>% filter(!is.na(Close)) %>% select(Date) %>% head()
## # A tsibble: 6 x 2 [!]
## # Key:       Symbol [1]
##   Date       Symbol
##   <date>     <chr> 
## 1 2014-01-02 AAPL  
## 2 2014-01-03 AAPL  
## 3 2014-01-06 AAPL  
## 4 2014-01-07 AAPL  
## 5 2014-01-08 AAPL  
## 6 2014-01-09 AAPL

Demand (from vic_elec): Half-hourly

vic_elec %>% filter(!is.na(Demand)) %>% select(Time) %>% head()
## # A tsibble: 6 x 1 [30m] <Australia/Melbourne>
##   Time               
##   <dttm>             
## 1 2012-01-01 00:00:00
## 2 2012-01-01 00:30:00
## 3 2012-01-01 01:00:00
## 4 2012-01-01 01:30:00
## 5 2012-01-01 02:00:00
## 6 2012-01-01 02:30:00

Plot series using autoplot()

aus_Bricks <- na.omit(aus_production)

autoplot(aus_Bricks, Bricks) + ggtitle("Australian Brick Production")

autoplot(pelt, Lynx) + ggtitle("Lynx Pelts Trapped in Canada")

autoplot(gafa_stock, Close) + ggtitle("GAFA Stock Closing Prices")

autoplot(vic_elec, Demand) +
  ggtitle("Electricity Demand in Victoria") +
  xlab("Time") +
  ylab("Demand (MW)")

2.2

Find the days with the highest closing price for each stock

gafa_stock %>%
  group_by(Symbol) %>%
  filter(Close == max(Close)) %>%
  select(Symbol, Date, Close)
## # A tsibble: 4 x 3 [!]
## # Key:       Symbol [4]
## # Groups:    Symbol [4]
##   Symbol Date       Close
##   <chr>  <date>     <dbl>
## 1 AAPL   2018-10-03  232.
## 2 AMZN   2018-09-04 2040.
## 3 FB     2018-07-25  218.
## 4 GOOG   2018-07-26 1268.

2.3

Read file

tute1 <- read.csv("tute1.csv")
glimpse(tute1)
## Rows: 100
## Columns: 4
## $ Quarter  <chr> "1981-03-01", "1981-06-01", "1981-09-01", "1981-12-01", "1982…
## $ Sales    <dbl> 1020.2, 889.2, 795.0, 1003.9, 1057.7, 944.4, 778.5, 932.5, 99…
## $ AdBudget <dbl> 659.2, 589.0, 512.5, 614.1, 647.2, 602.0, 530.7, 608.4, 637.9…
## $ GDP      <dbl> 251.8, 290.9, 290.8, 292.4, 279.1, 254.0, 295.6, 271.7, 259.6…

Convert to time series

mytimeseries <- tute1 |>
  mutate(Quarter = yearquarter(Quarter)) |>
  as_tsibble(index = Quarter)

Construct time series plots

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

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

Without including facet_grid(), the plots lack section separation and the column names are not labeled on the right side. This diminishes the plot’s comprehensibility for the audience.

2.4

Load library

library(USgas)

Create a tsibble from us_total

us_gas_tsibble <- as_tsibble(us_total, key = state, index = year)
us_gas_tsibble <- us_gas_tsibble %>%
  rename(Consumption = y)
print(us_gas_tsibble)
## # A tsibble: 1,266 x 3 [1Y]
## # Key:       state [53]
##     year state   Consumption
##    <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
## # ℹ 1,256 more rows

Plot the annual natural gas consumption by state for the New England area

us_gas_tsibble %>%
  filter(state %in% c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island")) %>%
  ggplot(aes(x = year, y = Consumption, color = state)) +
  geom_line() +
  ggtitle("Annual Natural Gas Consumption in New England States") +
  xlab("Year") +
  ylab("Consumption (Billion Cubic Feet)") +
  theme_minimal()

2.5

Read file

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

Create tsibble

tourism_tsibble <- tourism %>%
  mutate(Quarter = yearquarter(Quarter)) %>%
  as_tsibble(key = c(Region, State, Purpose), index = Quarter)

print(tourism_tsibble)
## # 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.
## # ℹ 24,310 more rows

Find the Region & Purpose with Maximum Average Overnight Trips

max_avg_trips <- tourism_tsibble %>%
  index_by() %>%
  group_by(Region, Purpose) %>%
  summarise(avg_trips = mean(Trips, na.rm = TRUE), .groups = "drop") %>%
  arrange(desc(avg_trips)) %>%
  slice(1)

print(max_avg_trips)
## # A tsibble: 1 x 4 [1Q]
## # Key:       Region, Purpose [1]
##   Region    Purpose  Quarter avg_trips
##   <chr>     <chr>      <qtr>     <dbl>
## 1 Melbourne Visiting 2017 Q4      985.

Create a New tsibble with Total Trips by State

state_total_tsibble <- tourism_tsibble %>%
  index_by(Quarter) %>%  
  group_by(State) %>% 
  summarise(Total_Trips = sum(Trips, na.rm = TRUE), .groups = "drop") %>%
  as_tsibble(index = Quarter, key = State)  

print(state_total_tsibble)
## # A tsibble: 640 x 3 [1Q]
## # Key:       State [8]
##    State Quarter Total_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.
## # ℹ 630 more rows

2.8

Load library

library(forecast)
library(feasts)
library(zoo)

Create time series

us_employment_selected <- us_employment %>%
  filter(Title == "Total Private")
aus_production_selected <- aus_production %>%
  select(Quarter, Bricks)

aus_production_selected <- na.omit(aus_production_selected)
PBS_selected <- PBS %>%
  filter(ATC2 == "H02") %>%
  select(Month, Cost)

autoplot()

autoplot(us_employment_selected, Employed) + ggtitle("Total Private Employment")

autoplot(aus_production_selected, Bricks) + ggtitle("Bricks Production")

autoplot(pelt, Hare) + ggtitle("Hare Pelts")

autoplot(PBS_selected, Cost) + ggtitle("H02 Cost") + 
  scale_color_manual(values = c("blue", "red", "darkgreen", "orange"),
                     labels = c("Concessional Co-payments", 
                                "General Co-payments", 
                                "Concessional Safety Net", 
                                "General Safety Net")) + 
  theme(legend.position = "bottom")

autoplot(us_gasoline, Barrels) + ggtitle("Barrels of Gasoline")

gg_season()

The Pelt time series includes annual data, making it difficult for gg_season to visualize a seasonal pattern due to the absence of a clear seasonal structure.

gg_season(us_employment_selected, Employed) + ggtitle("Seasonality of Total Private Employment")

gg_season(aus_production_selected, Bricks) + ggtitle("Seasonality of Bricks Production")

gg_season(PBS_selected, Cost) + ggtitle("Seasonality of H02 Cost")

gg_season(us_gasoline, Barrels) + ggtitle("Seasonality of Gasoline Barrels")

gg_subseries()

gg_subseries(us_employment_selected, Employed) + ggtitle("Subseries for Total Private Employment")

gg_subseries(aus_production_selected, Bricks) + ggtitle("Subseries for Bricks Production")

gg_subseries(pelt, Hare) + ggtitle("Subseries for Hare Pelts")

gg_subseries(PBS_selected, Cost) + ggtitle("Subseries for H02 Cost")

gg_subseries(us_gasoline, Barrels) + ggtitle("Subseries for Gasoline Barrels")

gg_lag()

gg_lag(us_employment_selected, Employed) + ggtitle("Lag Plot of Total Private Employment")

gg_lag(aus_production_selected, Bricks) + ggtitle("Lag Plot of Bricks Production")

gg_lag(pelt, Hare) + ggtitle("Lag Plot of Hare Pelts")

df_filtered <- PBS_selected %>%
  filter(Concession == "Concessional", Type == "Co-payments")
gg_lag(df_filtered, Cost) + ggtitle("Lag Plot of Concessional Co-payments H02 Cost")

df_filtered2 <- PBS_selected %>%
  filter(Concession == "Concessional", Type == "Safety net")
gg_lag(df_filtered2, Cost) + ggtitle("Lag Plot of Concessional Safety net H02 Cost")

df_filtered3 <- PBS_selected %>%
  filter(Concession == "General", Type == "Co-payments")
gg_lag(df_filtered3, Cost) + ggtitle("Lag Plot of General Co-payments H02 Cost")

df_filtered4 <- PBS_selected %>%
  filter(Concession == "General", Type == "Safety net")
gg_lag(df_filtered4, Cost) + ggtitle("Lag Plot of General Safety net H02 Cost")

gg_lag(us_gasoline, Barrels) + ggtitle("Lag Plot of Gasoline Barrels")

ACF()

Employed_ts <- zoo(us_employment_selected$Employed, order.by = us_employment_selected$Month)

acf(Employed_ts, main = "ACF of Total Private Employment")

Bricks_ts <- zoo(aus_production_selected$Bricks, order.by = aus_production_selected$Quarter)

acf(Bricks_ts, main = "ACF of Bricks Production")

Hare_ts <- zoo(pelt$Hare, order.by = pelt$Year)

acf(Hare_ts, main = "ACF of Hare Pelts")

Cost_ts <- zoo(df_filtered$Cost, order.by = df_filtered$Month)

acf(Cost_ts, main = "ACF of Concessional Co-payments H02 Cost")

Cost_ts2 <- zoo(df_filtered2$Cost, order.by = df_filtered2$Month)

acf(Cost_ts2, main = "ACF of Concessional Safety net H02 Cost")

Cost_ts3 <- zoo(df_filtered3$Cost, order.by = df_filtered3$Month)

acf(Cost_ts3, main = "ACF of General Co-payments H02 Cost")

Cost_ts4 <- zoo(df_filtered4$Cost, order.by = df_filtered4$Month)

acf(Cost_ts4, main = "ACF of General Safety net H02 Cost")

Barrels_ts <- zoo(us_gasoline$Barrels, order.by = us_gasoline$Week)

acf(Barrels_ts, main = "ACF of Gasoline Barrels")

Can you spot any seasonality, cyclicity and trend?

Upon analyzing several of the plots, I’ve noted pronounced seasonality patterns that are quite evident. For instance, the Autoplot() of H02 Cost showcases a recurring cycle, with fluctuations that align closely with seasonal trends. Additionally, the gg_season() plot further illustrates this phenomenon, revealing consistent patterns within individual years.

What do you learn about the series?

This tells me that the Concessional Safety net H02 Cost and the General Safety net H02 Cost have consistently adhered to a familiar pattern throughout the years. This stability suggests that the factors influencing these costs remain relatively unchanged

What can you say about the seasonal patterns?

This reflects a predictable trend in the financial allocations and resources needed to maintain this safety net. Over time, various economic conditions and policy adjustments have had minimal impact on the overall cost structure, allowing stakeholders to anticipate and plan for financial commitments associated with the H02 program with greater confidence.

Can you identify any unusual years?

There were no unusual years, as the pattern remains consistent with only minor increases or decreases.