###Question #1
# Time plots
# 1. Bricks (Quarterly)
aus_production |>
autoplot(Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

# 2. Lynx (Annual)
pelt |>
autoplot(Lynx)

# 3. Close price (Daily)
gafa_stock |>
autoplot(Close)

# 4. Electricity Demand (Half-hourly) with modified labels and title
vic_elec |>
autoplot(Demand) +
labs(
title = "Electricity Demand in Victoria",
x = "Time",
y = "Electricity Demand (MW)"
)

###Question # 2
gafa_stock |>
group_by(Symbol) |>
filter(Close == max(Close, na.rm = TRUE))
###Question # 3
tute1 <- readr::read_csv("/Users/aribarazzaq/Desktop/Data 624/Homework 1/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.
glimpse(tute1)
## Rows: 100
## Columns: 4
## $ Quarter <date> 1981-03-01, 1981-06-01, 1981-09-01, 1981-12-01, 1982-03-01, …
## $ 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…
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")

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

###What happens without facet_grid()?
#All three series are plotted on the same graph
#They share the same y-axis
#GDP dominates because it has much larger values
#Sales and AdBudget appear almost flat
#The plot becomes hard to interpret
###Question # 4
library(USgas)
# Create tsibble
us_gas_ts <- us_total %>%
as_tsibble(
key = state,
index = year
)
# New England states
new_england <- c(
"Maine", "Vermont", "New Hampshire",
"Massachusetts", "Connecticut", "Rhode Island"
)
# Plot gas consumption
us_gas_ts %>%
filter(state %in% new_england) %>%
autoplot(y)

###Question # 5
# Step 1: Read the Excel file
tourism_raw <- read_excel("/Users/aribarazzaq/Desktop/Data 624/Homework 1/tourism.xlsx")
# Step 2: Create tsibble identical to tsibble::tourism
tourism_ts <- tourism_raw %>%
mutate(Quarter = yearquarter(Quarter)) %>% # convert to yearquarter
arrange(Region, State, Purpose, Quarter) %>% # sort to avoid warnings
as_tsibble(
key = c(Region, State, Purpose),
index = Quarter
)
# Step 3: Find Region × Purpose combination with max average trips
max_avg_trips <- tourism_ts %>%
as_tibble() %>% # convert to tibble to avoid tsibble index issues
group_by(Region, Purpose) %>%
summarise(
avg_trips = mean(Trips, na.rm = TRUE),
.groups = "drop"
) %>%
arrange(desc(avg_trips)) %>%
slice(1)
# Print result
max_avg_trips
# Step 4: Create new tsibble with total trips by State only
state_tourism <- tourism_ts %>%
index_by(Quarter) %>% # aggregate by time
group_by(State) %>%
summarise(
Trips = sum(Trips, na.rm = TRUE)
)
# Print a sample
state_tourism
### Question #8
#Total Private Employment
total_private <- us_employment %>%
filter(Title == "Total Private")
total_private %>% autoplot(Employed)

total_private %>% gg_season(Employed)

total_private %>% gg_subseries(Employed)

total_private %>% ggtime::gg_lag(Employed)

total_private %>%
ACF(Employed) %>%
autoplot()

#Bricks Production
bricks_clean <- aus_production %>%
filter(!is.na(Bricks))
bricks_clean %>% autoplot(Bricks)

bricks_clean %>% gg_season(Bricks)

bricks_clean %>% gg_subseries(Bricks)

bricks_clean %>% ggtime::gg_lag(Bricks)

bricks_clean %>%
ACF(Bricks) %>%
autoplot()

#Hare Pelts (Annual)
pelt %>% autoplot(Hare)

pelt %>% ggtime::gg_lag(Hare)

pelt %>%
ACF(Hare) %>%
autoplot()

#PBS H02 Cost
pbs_h02_total <- PBS %>%
filter(ATC2 == "H02") %>%
index_by(Month) %>% # Month is the time index in PBS
summarise(Cost = sum(Cost, na.rm = TRUE)) %>%
as_tsibble(index = Month) # ensure single series (no key)
pbs_h02_total %>% autoplot(Cost)

pbs_h02_total %>% gg_season(Cost)

pbs_h02_total %>% gg_subseries(Cost)

pbs_h02_total %>% ggtime::gg_lag(Cost)

pbs_h02_total %>% ACF(Cost) %>% autoplot()

# US Gasoline Barrels
# --- Build a clean WEEKLY tsibble (for autoplot + ACF) ---
gas_weekly <- us_gasoline %>%
as_tibble() %>% # drop any weird structure safely
select(Week, Barrels) %>%
as_tsibble(index = Week)
stopifnot(tsibble::is_tsibble(gas_weekly)) # must be TRUE
# Weekly autoplot (tsibble)
gas_weekly %>%
autoplot(Barrels) +
labs(title = "US Gasoline Production (Weekly)", x = "Week", y = "Barrels")

# Weekly ACF (tsibble) ✅ this is where key_data was happening
gas_weekly %>%
ACF(Barrels) %>%
autoplot() +
labs(title = "ACF: US Gasoline Production (Weekly)")

# Weekly lag plot (CORRECT for ggtime::gg_lag)
gas_weekly %>%
ggtime::gg_lag(Barrels)

# --- Build a MONTHLY tsibble (for gg_season + gg_subseries) ---
gas_monthly <- gas_weekly %>%
mutate(Month = yearmonth(Week)) %>%
index_by(Month) %>%
summarise(Barrels = mean(Barrels, na.rm = TRUE)) %>%
as_tsibble(index = Month)
stopifnot(tsibble::is_tsibble(gas_monthly)) # must be TRUE
# Monthly seasonal plots (tsibble)
gas_monthly %>%
gg_season(Barrels) +
labs(title = "Seasonal Plot: US Gasoline Production (Monthly)")

gas_monthly %>%
gg_subseries(Barrels) +
labs(title = "Subseries Plot: US Gasoline Production (Monthly)")
