library(fpp3) 
## Warning: package 'fpp3' was built under R version 4.4.3
## Registered S3 method overwritten by 'tsibble':
##   method               from 
##   as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.2 ──
## ✔ tibble      3.2.1     ✔ tsibble     1.1.6
## ✔ dplyr       1.1.4     ✔ tsibbledata 0.4.1
## ✔ tidyr       1.3.1     ✔ feasts      0.4.2
## ✔ lubridate   1.9.3     ✔ fable       0.4.1
## ✔ ggplot2     3.5.1
## Warning: package 'dplyr' was built under R version 4.4.3
## Warning: package 'ggplot2' was built under R version 4.4.3
## Warning: package 'tsibble' was built under R version 4.4.3
## Warning: package 'tsibbledata' was built under R version 4.4.3
## Warning: package 'feasts' was built under R version 4.4.3
## Warning: package 'fabletools' was built under R version 4.4.3
## Warning: package 'fable' was built under R version 4.4.3
## ── 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(USgas)
## Warning: package 'USgas' was built under R version 4.4.3
library(readxl)
library(tsibbledata)
library(dplyr)



# help(aus_production)
# help(pelt)
# help(gafa_stock)
# help(vic_elec)

interval(aus_production)  
## <interval[1]>
## [1] 1Q
interval(pelt)            
## <interval[1]>
## [1] 1Y
interval(gafa_stock)    
## <interval[1]>
## [1] !
interval(vic_elec)       
## <interval[1]>
## [1] 30m
autoplot(aus_production, Bricks) +
  labs(title = "Brick Production", y = "Number of Bricks")
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

autoplot(pelt, Lynx) +
  labs(title = "Canadian Lynx Pelts", y = "Number of Lynx Pelts")

gafa_stock |>
  autoplot(Close) +
  labs(title = "Stock Closing Price", y = "Price")

autoplot(vic_elec, Demand) +
  labs(
    title = "Electricity Demand in Victoria",
    y = "Demand in MW",
    x = "Date and Time"
  )

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.
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.
View(tute1)

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

Without the facet_grid the plots share a common y-axis which distorts each individual scale

usgas_ts <- us_total |>
  as_tsibble(index = year, key = state)
new_england <- c("Maine", "Vermont", "New Hampshire", 
                        "Massachusetts", "Connecticut", "Rhode Island")
usgas_ts <- usgas_ts |>
  rename(consumption = y)

usgas_ts |>
  filter(state %in% new_england) |>
  autoplot(consumption) +
  labs(
    title = "Natural Gas Consumption in New England",
    x = "Year",
    y = "Consumption :Billion Cubic Feet"
  )

tourism_data <- read_excel("tourism.xlsx")
tourism_ts <- tourism_data |>
  mutate(Quarter = yearquarter(Quarter)) |>
  as_tsibble(index = Quarter, key = c(Region, State, Purpose))

tourism_ts |>
  group_by(Region, Purpose) |>
  summarise(avg_trips = mean(Trips, na.rm = TRUE)) |>
  ungroup() |>
  slice_max(avg_trips, n = 1)
## # 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.
state_totals <- tourism_ts |>
  group_by(State) |>
  summarise(Total_Trips = sum(Trips, na.rm = TRUE))

print(state_totals)
## # 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
testing <- function(data, var,name) {

  autoplot(data, !!sym(var)) + 
    labs(title = paste("Time plot", name))
  
  # 2. Seasonal plot
  print(gg_season(data, !!sym(var)) + labs(title = paste("Seasonal plot of", name)))
  
  # 3. Subseries plot
  print(gg_subseries(data, !!sym(var)) + labs(title = paste("Subseries plot", name)))
  
  # 4. Lag plot
  print(gg_lag(data, !!sym(var)) + labs(title = paste("Lag plot", name)))
  
  # 5. ACF plot
  print(ACF(data, !!sym(var)) |> autoplot() + labs(title = paste("ACF", name)))
}
# testing(us_employment, "Total_Private", "Total Private Employed")
testing(aus_production, "Bricks", "Bricks Production")
## Warning: `gg_season()` was deprecated in feasts 0.4.2.
## ℹ Please use `ggtime::gg_season()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

## Warning: `gg_subseries()` was deprecated in feasts 0.4.2.
## ℹ Please use `ggtime::gg_subseries()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_line()`).

## Warning: `gg_lag()` was deprecated in feasts 0.4.2.
## ℹ Please use `ggtime::gg_lag()` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: Removed 20 rows containing missing values (gg_lag).

# testing(pelt, "Hare", "Hare Trappings")
# testing(PBS, "HO2", "HO2 Cost")
testing(us_gasoline, "Barrels", "US Gasoline")