library(fpp3)
library(tsibble)
library(cowplot)
2.1
a. & b.
  This PBS data appears to have a monthly interval.
PBS %>%
filter(ATC2 == "A10") %>%
select(Month, Concession, Type, Cost,Scripts) %>%
plot_grid(autoplot(.,Scripts),autoplot(.,Cost) ,ncol = 1)

  The gafa_stock is daily, and covers roughly six years worth of data.
plot_grid(autoplot(gafa_stock,Open),autoplot(gafa_stock,Adj_Close),autoplot(gafa_stock,High),autoplot(gafa_stock,Low) )

2.2
  The textbook asks you to use filter, but it makes more sense to use slice_max here. As shown below, it gives the max Close value for each group.
gafa_stock %>% group_by(Symbol) %>%
slice_max(Close)
2.3
a.
tute1 <- readr::read_csv("tute1.csv")
View(tute1)
b.
mytimeseries <- tute1 %>%
mutate(Quarter = yearmonth(Quarter)) %>%
as_tsibble(index = Quarter)
c.
  The function facet_grid splits our data into seperate plots based on a categorical variable.
mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
facet_grid(name ~ ., scales = "free_y")

2.4
library(USgas)
us_total_ts <- us_total %>%
as_tsibble(index = year,key =state)
us_total_ts %>% filter(state %in% c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island") ) %>%
ggplot(aes(x = year, y = y, colour = state)) +
geom_line() +
facet_grid(state ~ ., scales = "free_y")

2.5
a.
tor <- readxl::read_excel("tourism.xlsx")
b.
ts_tor <- tor %>%
mutate(Quarter= yearquarter(Quarter)) %>%
as_tsibble(index=Quarter,key=c(Region, State, Purpose))
c.
ts_tor %>% group_by( Region,Purpose) %>%
summarise(avg_trips = mean(Trips)) %>% ungroup() %>%
slice_max(avg_trips)
d.
tor %>%
group_by(Quarter,State) %>%
summarise(Trips_total = sum(Trips)) %>%
mutate(Quarter=yearquarter(Quarter)) %>%
as_tsibble(index = Quarter,key=State) %>%
ggplot(aes(x = Quarter, y = Trips_total, colour = State)) +
geom_line() +
facet_grid(State ~ ., scales = "free_y")

2.8
set.seed(1221)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
myseries
  There are many clear and obvious trends shown in the graphics below. We see a peak in the turnover rate going from November to December. This is likely a very busy season for retail copanies due to the big Holiday. Then around August we see another peak. This is likely for people going back to school at the time. January is clearly the lowest period where many people are likely looking to not shop after spending a lot during the Holiday. We see an immediate resurgence in sales beginning in February.
plot_grid(autoplot(myseries,Turnover),gg_season(myseries,Turnover ),gg_subseries(myseries,Turnover ),gg_lag(myseries,Turnover))
