library(fpp3)

2.1) Explore the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, Demand from vic_elec.

a) Use ? (or help()) to find out about the data in each series.

?aus_production
?pelt
?gafa_stock
?vic_elec

b) What is the time interval of each series?

  1. aus_production is quarterly based on the head not the documentation.
  2. pelt is annually.
  3. gafa_stock is done irregular daily based on trading days.
  4. vic_elec is half hourly.

c) Use autoplot() to produce a time plot of each series.

  1. For the last plot, modify the axis labels and title.
library(fpp3)

autoplot(aus_production, Bricks) 

autoplot(pelt, Lynx)

autoplot(gafa_stock, Close)

autoplot(vic_elec, Demand) + ggtitle("Quarterly electricity demand for Victoria, Australia") + xlab("Time") + ylab ("Demand in MWh") 

2.2) Use filter() to find what days corresponded to the peak closing price for each of the four stocks in gafa_stock.

gafa_stock %>%
  group_by(Symbol) %>%
  filter(Close == max(Close)) %>%
  select(Symbol, Date)
## # A tsibble: 4 x 2 [!]
## # Key:       Symbol [4]
## # Groups:    Symbol [4]
##   Symbol Date      
##   <chr>  <date>    
## 1 AAPL   2018-10-03
## 2 AMZN   2018-09-04
## 3 FB     2018-07-25
## 4 GOOG   2018-07-26

2.3) Download the file tute1.csv from the book website, open it in Excel (or some other spreadsheet application), and review its contents. You should find four columns of information. Columns B through D each contain a quarterly series, labelled Sales, AdBudget and GDP. Sales contains the quarterly sales for a small company over the period 1981-2005. AdBudget is the advertising budget and GDP is the gross domestic product. All series have been adjusted for inflation.

tute1 <- readr::read_csv("C:\\Users\\John Ledesma\\Downloads\\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")

2.4) The USgas package contains data on the demand for natural gas in the US.

  1. Install the USgas package.
  2. Create a tsibble from us_total with year as the index and state as the key.
  3. Plot the annual natural gas consumption by state for the New England area (comprising the states of Maine, Vermont, New Hampshire, Massachusetts, Connecticut and Rhode Island).
library('USgas')
us_total_tsibble <- as_tsibble(us_total, key = state, index = year)
new_england_gas <- filter(us_total_tsibble, state %in% c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island"))
autoplot(new_england_gas) + ggtitle("New England Gas Consumption") + xlab("Year") + ylab( "Natural Gas")
## Plot variable not specified, automatically selected `.vars = y`

2.5) a)Download tourism.xlsx from the book website and read it into R using readxl::read_excel().

library(readxl)
tourism_xl <- read_excel("C:\\Users\\John Ledesma\\Downloads\\tourism.xlsx")
  1. Create a tsibble which is identical to the tourism tsibble from the tsibble package.
tourism_xl <- tourism_xl %>%
  mutate(Quarter = yearquarter(Quarter))

tourism_tsibble <- tourism_xl %>%
  as_tsibble(key = c(Region,State,Purpose), index = Quarter)
  1. Find what combination of Region and Purpose had the maximum number of overnight trips on average.
tourism_R_and_P <- tourism_xl %>% 
    group_by(Region, Purpose) %>%
    summarise(Total_trips = sum(Trips,na.rm=TRUE)) %>%
    arrange(desc(Total_trips))
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
head(tourism_R_and_P)
## # A tibble: 6 × 3
## # Groups:   Region [4]
##   Region          Purpose  Total_trips
##   <chr>           <chr>          <dbl>
## 1 Sydney          Visiting      59782.
## 2 Melbourne       Visiting      49512.
## 3 Sydney          Business      48164.
## 4 North Coast NSW Holiday       47032.
## 5 Sydney          Holiday       44026.
## 6 Gold Coast      Holiday       42267.

Sydney/Visiting is the leading Region/Purpose

  1. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
tourism_group <- tourism_xl %>%
  group_by(State) %>%
  summarise(Total_Trips = sum(Trips, na.rm = TRUE)) %>%
  arrange(desc(Total_Trips))

tourism_group
## # A tibble: 8 × 2
##   State              Total_Trips
##   <chr>                    <dbl>
## 1 New South Wales        557367.
## 2 Victoria               390463.
## 3 Queensland             386643.
## 4 Western Australia      147820.
## 5 South Australia        118151.
## 6 Tasmania                54137.
## 7 ACT                     41007.
## 8 Northern Territory      28614.

2.8) Use the following graphics functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() and explore features from the following time series: “Total Private” Employed from us_employment, Bricks from aus_production, Hare from pelt, “H02” Cost from PBS, and Barrels from us_gasoline. Can you spot any seasonality, cyclicity and trend? What do you learn about the series? What can you say about the seasonal patterns? Can you identify any unusual years?

US_Total_Private <- us_employment %>% filter(Title=="Total Private")
Bricks <- aus_production %>% select(Quarter, Bricks)
Hare <- pelt %>% select(Year, Hare)
H02 <- PBS %>% filter(ATC2 == 'H02')
autoplot(US_Total_Private)
## Plot variable not specified, automatically selected `.vars = Employed`

autoplot(Bricks)
## Plot variable not specified, automatically selected `.vars = Bricks`

autoplot(Hare)
## Plot variable not specified, automatically selected `.vars = Hare`

autoplot(H02)
## Plot variable not specified, automatically selected `.vars = Scripts`

gg_season(US_Total_Private)
## Plot variable not specified, automatically selected `y = Employed`

gg_season(Bricks)
## Plot variable not specified, automatically selected `y = Bricks`

gg_season(H02)
## Plot variable not specified, automatically selected `y = Scripts`

gg_subseries(US_Total_Private)
## Plot variable not specified, automatically selected `y = Employed`

gg_subseries(Bricks)
## Plot variable not specified, automatically selected `y = Bricks`

gg_subseries(Hare)
## Plot variable not specified, automatically selected `y = Hare`

gg_subseries(H02)
## Plot variable not specified, automatically selected `y = Scripts`

gg_lag(US_Total_Private, geom = 'point')
## Plot variable not specified, automatically selected `y = Employed`

gg_lag(Bricks, geom = 'point')
## Plot variable not specified, automatically selected `y = Bricks`

gg_lag(Hare, geom = 'point')
## Plot variable not specified, automatically selected `y = Hare`

Based on the graphs of the data, I can make a few observations. For US employment, we can see there is a general upward trend with slight dips in February and peaks in June and July. For Bricks there was a large dip in bricks in quarter 2 of 1980 and Quarter 3 of each year seems to produce more For Hare,