Data 624 - Assignment 1

library(fpp3)
Registered S3 method overwritten by 'tsibble':
  method               from 
  as_tibble.grouped_df dplyr
── Attaching packages ──────────────────────────────────────────── fpp3 1.0.2 ──
✔ tibble      3.3.1     ✔ tsibble     1.1.6
✔ dplyr       1.1.4     ✔ tsibbledata 0.4.1
✔ tidyr       1.3.2     ✔ feasts      0.4.2
✔ lubridate   1.9.4     ✔ fable       0.5.0
✔ ggplot2     4.0.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()

Question 1

aus_production %>% autoplot(Bricks)
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).

pelt %>% autoplot(Lynx)

gafa_stock %>% autoplot(Close)

vic_elec %>% 
  autoplot(Demand) +
  labs(
    title = "Half-Hourly Electricity Demand",
    subtitle = "Victoria, Australia",
    y = "Demand (MW)",
    x = "Time (30-minute intervals)"
  )

Question 2

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.

Question 3

tute1 <- readr::read_csv("tute1.csv")
Rows: 100 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl  (3): Sales, AdBudget, GDP
date (1): Quarter

ℹ 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.
mytimeseries <- tute1 |>
  mutate(Quarter = yearquarter(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")

Question 4

4a

#install.packages("USgas")
library(USgas)
library(fpp3)

4b

us_gas_ts <- us_total |>
  as_tsibble(
    index = year, 
    key = state
  )

4c

new_england_states <- c("Maine", "Vermont", "New Hampshire", 
                        "Massachusetts", "Connecticut", "Rhode Island")

us_gas_ts |>
  filter(state %in% new_england_states) |>
  autoplot(y) +
  labs(
    title = "Annual Natural Gas Consumption: New England",
    y = "Gas Consumption (billion cubic feet)",
    x = "Year"
  )

5

5a

library(fpp3)
library(readxl)

tourism_df <- read_excel("tourism.xlsx")

5b

tourism_ts <- tourism_df |>
  mutate(Quarter = yearquarter(Quarter)) |>
  as_tsibble(index = Quarter, key = c(Region, State, Purpose))

5c

tourism_ts |>
  as_tibble() |>
  group_by(Region, Purpose) |>
  summarise(Average_Trips = mean(Trips), .groups = "drop") |>
  filter(Average_Trips == max(Average_Trips))
# A tibble: 1 × 3
  Region Purpose  Average_Trips
  <chr>  <chr>            <dbl>
1 Sydney Visiting          747.

8

total_private <- us_employment |> 
  filter(Title == "Total Private")

total_private |> autoplot(Employed)

total_private |> ACF(Employed) |> autoplot()

aus_production |> autoplot(Bricks)
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).

aus_production |> gg_season(Bricks)
Warning: `gg_season()` was deprecated in feasts 0.4.2.
ℹ Please use `ggtime::gg_season()` instead.
Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).

aus_production |> gg_subseries(Bricks)
Warning: `gg_subseries()` was deprecated in feasts 0.4.2.
ℹ Please use `ggtime::gg_subseries()` instead.
Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).

pelt |> autoplot(Hare)

pelt |> gg_lag(Hare, geom = "point")
Warning: `gg_lag()` was deprecated in feasts 0.4.2.
ℹ Please use `ggtime::gg_lag()` instead.

h02 <- PBS |> filter(ATC2 == "H02") |> summarise(Cost = sum(Cost))
h02 |> gg_season(Cost)

us_gasoline |> autoplot(Barrels)

us_gasoline |> gg_subseries(Barrels)