R Markdown

install.packages(“fpp3”) install.packages(“USgas”) install.packages(“rmarkdown”) install.packages(“tinytex”) tinytex::install_tinytex() library(fpp3)

#2.1 Exercises ?aus_production #quarterly ?pelt #yearly ?gafa_stock #daily ?vic_elec #half-hourly

bricks_plot <- aus_production %>%
  autoplot(Bricks) +
    labs(title = "Australian Bricks Production",
         subtitle = "For exercise 2.1",
         y = "Bricks (millions)",
         x = "Year") 
print(bricks_plot)

lynx_plot <- pelt %>%
  autoplot(Lynx) +
  labs(title = "Canadian Lynx Trappings",
       y = "Number of Lynx",
       x = "Year")
print(lynx_plot)


close_plot <- gafa_stock %>%
  autoplot(Close) +
  labs(title = "Stock Closing Price",
       y = "Closing Price ($)",
       x = "Date") 
print(close_plot)

demand_plot <- vic_elec %>%
  autoplot(Demand) +
  labs(title = "Victoria Electricity Demand",
       y = "Electricity Demand (MW)",
       x = "Time") 
print(demand_plot)

#2.2 Use filter() to find what days corresponded to the peak closing prices for each of the four stocks. peak_days <- gafa_stock %>% group_by(Symbol) %>% filter(Close == max(Close)) %>% select(Symbol, Date, Close) %>% arrange(Symbol, Date)

peak_days %>%
  mutate(Peak_Date = format(Date, "%Y-%m-%d"),
         Peak_Close = round(Close, 2)) %>%
  select(Symbol, Peak_Date, Peak_Close) %>%
  as.data.frame()

#2.3 tute1 <- readr::read_csv(“tute1.csv”) 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”)

#2.4 install.packages(“USgas”) install.packages(c(“USgas”, “tsibble”, “dplyr”, “ggplot2”)) library(USgas) usgas_tsibble <- us_total %>% as_tsibble(key = state, index=year) view(usgas_tsibble) new_england_states <- c(“Maine”, “Vermont”, “New Hampshire”, “Massachusetts”, “Connecticut”, “Rhode Island”) new_england_plot <- usgas_tsibble %>% filter(state %in% new_england_states) %>% ggplot(aes(x = year, y = y, color = state)) + labs(title = “Gas Consumption in New England States”, x = “Year”, y = “Natural Gas Consumption (million cubic feet)”, color = “State”) print(new_england_plot)

#2.5 library(fpp3) library(dplyr) data(package = “tsibbledata”) View(tourism_data) summary(tourism_data)

tourism = readxl::read_excel(“tourism.xlsx”) tourism_tsibble <- tourism %>% mutate(Quarter = yearquarter(Quarter)) %>% as_tsibble(key = c(Region, State, Purpose), index = Quarter)

max_avg <- tourism_tsibble %>% group_by(Region, Purpose) %>% summarize(avg_trips = mean(Trips), .groups = “drop”) %>% filter(avg_trips == max(avg_trips)) print(max_avg)

state_trips <- tourism_tsibble %>% index_by(Quarter) %>% # Use index_by for the time index group_by(State) %>% # Use group_by for the key variable summarize(Total_Trips = sum(Trips), .groups = “drop”) %>% as_tsibble(key = State, index = Quarter) print(state_trips)

#2.8 library(fpp3) install.packages(“gridExtra”) library(gridExtra)

total_private <- us_employment %>% filter(Title == “Total Private”) %>% select(-Title) p1 <- autoplot (total_private, Employed) + labs(title = “Private Employment”) p2 <- gg_season(total_private, Employed) + labs(title = “Seasonal”) p3 <- gg_subseries(total_private, Employed) + labs(title = “subseries”) p4 <- gg_lag(total_private, Employed) + labs(title = “Lag”) p5 <- ACF(total_private, Employed) %>% autoplot() + labs(title = “ACF”) grid.arrange(p1,p2,p3,p4,p5, ncol=2)

bricks <- aus_production %>% select(Bricks) p1 <- autoplot(bricks,Bricks) +labs(title = “bricks produciton”) p2 <- gg_season(bricks,Bricks) +labs(title = “seasonal”) p3 <- gg_subseries(bricks, Bricks) +labs(title = “subseries”) p4 <- gg_lag(bricks, Bricks) +labs(title = “Lag”) p5 <- ACF(bricks,Bricks) %>% autoplot() +labs(title = “ACF”) grid.arrange(p1,p2,p3,p4,p5, ncol=2)

hare <- pelt %>% select(Hare) p1 <- autoplot(hare, Hare) + labs(title = “hare”) p2 <- gg_season(hare, Hare) + labs(title = “season”) p3 <- gg_subseries(hare, Hare) + labs(title = “subseries”) p4 <- gg_lag(hare, Hare) + labs(title = “lag”) p5 <- ACF(hare, Hare) + autoplot() + labs(title = “acf”) grid.arrange(p1,p2,p3,p4,p5, ncol=2)

h02_cost <- PBS %>% filter(ATC2 == “H02”) %>% summarize(Cost = sum(Cost)) p1 <- autoplot(h02_cost, Cost) + labs(title = “H02 Drug Cost”) p2 <- gg_season(h02_cost, Cost) + labs(title = “Seasonal Plot”) p3 <- gg_subseries(h02_cost, Cost) + labs(title = “Subseries Plot”) p4 <- gg_lag(h02_cost, Cost) + labs(title = “Lag Plot”) p5 <- ACF(h02_cost, Cost) %>% autoplot() + labs(title = “ACF”) grid.arrange(p1, p2, p3, p4, p5, ncol = 2)

barrels <- us_gasoline %>% select(Barrels) p1 <- autoplot(barrels, Barrels) + labs(title = “Gasoline Production (Barrels)”) p2 <- gg_season(barrels, Barrels) + labs(title = “Seasonal Plot”) p3 <- gg_subseries(barrels, Barrels) + labs(title = “Subseries Plot”) p4 <- gg_lag(barrels, Barrels) + labs(title = “Lag Plot”) p5 <- ACF(barrels, Barrels) %>% autoplot() + labs(title = “ACF”) grid.arrange(p1, p2, p3, p4, p5, ncol = 2)

#2.8 Summary: # 1. “Total Private”, generally upward trend. Seasonality with higher trend in summer season and low in winter season. # Time plot and Lags plot shows a business cycle in about 7 years. # 2. Bricks, there was an upward trend until around 1978 and generally a down trend. Seasonality Q3 generally has higher production. # 1980 production has a significant decline. # 3. Hare, doesn’t seems there is a clear trend, or seasonality. Seems about 10 years cycle. # 4. ho2_cost, generally trending upward. Montly seasonality with peaks in Jan and Dec.  # 5. Barrels. generally going upward, weekly trend can ben seen in gg_season plot, each lines of weekly move in consistent pattern.