2.1

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

?aus_production
## starting httpd help server ... done

The ‘aus_production’ time series represents the quarterly production of selected commodities in Australia. These commodities are; Beer, Tobacco, bricks, cement, electricity, and gas

aus_production %>%
  autoplot(Bricks) +
  ggtitle("Bricks Production in Australia") +
  xlab("Quartly over years") +
  ylab("Bricks in Million Units")
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

?pelt

The ‘pelt’ time series shows Hudson Bay Company trading records for Snowshoe Hare and Canadian Lynx Furs from 1845 to 1935.

pelt %>%
  autoplot(Lynx) +
  ggtitle("Lynx Pelts Traded in Canada") +
  xlab("Year") +
  ylab("Number of Pelts")

?gafa_stock

The ‘gafa_stock’ time series shows historical prices from 2014 to 2018 for Google, Amazon, Facebook and Apple in USD.

gafa_stock %>%
  autoplot(Close) +
  ggtitle("Closing Stock Prices of GAFA Companies") +
  xlab("Date") +
  ylab("Closing Price (USD)")

?vic_elec

The ‘vic_elec’ time series operational demand met by region for different unit types.

vic_elec %>%
  autoplot(Demand) +
  ggtitle("Quartly Production of selected commodoties in Australia")+
  xlab("Time") +
  ylab("Demand")

2.2

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

peak_closing_prices <- gafa_stock %>%
  group_by(Symbol) %>%
  filter(Close == max(Close)) 

# View the results
print(peak_closing_prices)
## # A tsibble: 4 x 8 [!]
## # Key:       Symbol [4]
## # Groups:    Symbol [4]
##   Symbol Date        Open  High   Low Close Adj_Close   Volume
##   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
## 1 AAPL   2018-10-03  230.  233.  230.  232.      230. 28654800
## 2 AMZN   2018-09-04 2026. 2050. 2013  2040.     2040.  5721100
## 3 FB     2018-07-25  216.  219.  214.  218.      218. 58954200
## 4 GOOG   2018-07-26 1251  1270. 1249. 1268.     1268.  2405600

2.3

tute1 <- read.csv("https://raw.githubusercontent.com/sokkarbishoy/DATA624-Public/main/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")

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

##2.4

library(USgas)
## Warning: package 'USgas' was built under R version 4.3.3
ustotal_tsibble <- us_total %>%
  as_tsibble( key = state, index = year) %>%
    rename( gas_consumption = y)


ustotal_tsibble %>%
  filter(state %in% c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island")) %>%
  autoplot(gas_consumption) +    
  facet_wrap(~state, scales = "free_y") +   
  ggtitle("Annual Natural Gas Consumption by State (New England)") +
  xlab("Year") +
  ylab("Natural Gas Consumption")

Plots without facet wrap

ustotal_tsibble %>%
  filter(state %in% c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island")) %>%
  autoplot(gas_consumption) +    
  facet_wrap(~state) +   
  ggtitle("Annual Natural Gas Consumption by State (New England)") +
  xlab("Year") +
  ylab("Natural Gas Consumption")

##2.5

Download tourism.xlsx from the book website and read it into R using readxl::read_excel(). Create a tsibble which is identical to the tourism tsibble from the tsibble package.

library("readxl")
## Warning: package 'readxl' was built under R version 4.3.3
url <- "https://otexts.com/fpp3/extrafiles/tourism.xlsx"
download.file(url, destfile = "tourism.xlsx", mode = "wb")
tourism <- read_excel("tourism.xlsx")

tourism_tsibble <- tourism %>%
  mutate(Quarter = yearquarter(Quarter))%>%
  as_tsibble(index = Quarter, key = c("Region","State","Purpose"))

Find what combination of Region and Purpose had the maximum number of overnight trips on average.

max_avg_trips <- tourism_tsibble %>%
  group_by(Region, Purpose) %>%          
  summarise(avg_trips = mean(Trips, na.rm = TRUE)) %>%
  filter(avg_trips == max(avg_trips)) 
max_avg_trips
## # A tsibble: 76 x 4 [1Q]
## # Key:       Region, Purpose [76]
## # Groups:    Region [76]
##    Region                     Purpose  Quarter avg_trips
##    <chr>                      <chr>      <qtr>     <dbl>
##  1 Adelaide                   Visiting 2017 Q1     270. 
##  2 Adelaide Hills             Visiting 2002 Q4      81.1
##  3 Alice Springs              Holiday  1998 Q3      76.5
##  4 Australia's Coral Coast    Holiday  2014 Q3     198. 
##  5 Australia's Golden Outback Business 2017 Q3     174. 
##  6 Australia's North West     Business 2016 Q3     297. 
##  7 Australia's South West     Holiday  2016 Q1     612. 
##  8 Ballarat                   Visiting 2004 Q1     103. 
##  9 Barkly                     Holiday  1998 Q3      37.9
## 10 Barossa                    Holiday  2006 Q1      51.0
## # ℹ 66 more rows

##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.

Employed from us_employment

autoplot(us_employment %>% filter(Title == "Total Private")) + 
  ggtitle("Total Private Employed - Time Series Plot")
## Plot variable not specified, automatically selected `.vars = Employed`

# Seasonality Plot
us_employment %>%
  filter(Title == "Total Private") %>%
  gg_season(Employed) +
  ggtitle("Seasonality of Total Private Employed")

# Subseries Plot
us_employment %>%
  filter(Title == "Total Private") %>%
  gg_subseries(Employed) +
  ggtitle("Subseries Plot of Total Private Employed")

# Lag Plot
us_employment %>%
  filter(Title == "Total Private") %>%
  gg_lag(Employed) +
  ggtitle("Lag Plot of Total Private Employed")

us_employment %>%
  filter(Title == "Total Private") %>%
  ACF(Employed) %>%
  autoplot() +
  ggtitle("ACF of Total Private Employed")

Bricks from aus_production

#autoplot
aus_production %>%
  autoplot(Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

#ggseasion 
aus_production %>%
  gg_season(Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

aus_production %>%
  gg_subseries(Bricks) 
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_line()`).

aus_production %>%
  gg_lag(Bricks)
## Warning: Removed 20 rows containing missing values (gg_lag).

aus_production %>%
  ACF(Bricks) 
## # A tsibble: 22 x 2 [1Q]
##         lag   acf
##    <cf_lag> <dbl>
##  1       1Q 0.900
##  2       2Q 0.815
##  3       3Q 0.813
##  4       4Q 0.828
##  5       5Q 0.720
##  6       6Q 0.642
##  7       7Q 0.655
##  8       8Q 0.692
##  9       9Q 0.609
## 10      10Q 0.556
## # ℹ 12 more rows
autoplot(aus_production)
## Plot variable not specified, automatically selected `.vars = Beer`

Hare From ‘pelt’

pelt %>%
  autoplot(Hare)

pelt %>%
  gg_subseries(Hare)

pelt %>%
  gg_lag(Hare)

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

“H02” Cost from PBS

PBS %>%
  filter(ATC2 == "H02") %>%
  autoplot(Cost)

Barrels from us_gasoline.

us_gasoline %>%
  autoplot(Barrels)

us_gasoline %>%
  gg_season(Barrels)

us_gasoline %>%
  gg_subseries(Barrels)

us_gasoline %>%
  gg_lag(Barrels)

us_gasoline %>%
  ACF(Barrels) %>%
  autoplot()