DATA 624: HOMEWORK 1

Author

Pascal Hermann Kouogang Tafo

HOMEWORK 1

Exercise 2.1

The main objective in this exercise is to explore the following four time series: Bricks from aus_production, Lynx from pelt , Close from gafa_stock, and Demand from vic_elec.

Loading the fpp3 library

I will install the package in my console so that it will not have to install it anytime that i am rendering the file.

library(fpp3)
Warning: package 'fpp3' was built under R version 4.5.3
── Attaching packages ──────────────────────────────────────────── fpp3 1.0.3 ──
✔ tibble      3.3.1     ✔ tsibble     1.2.0
✔ dplyr       1.2.1     ✔ tsibbledata 0.4.1
✔ tidyr       1.3.2     ✔ ggtime      1.0.0
✔ lubridate   1.9.5     ✔ feasts      0.5.0
✔ ggplot2     4.0.3     ✔ fable       0.5.0
Warning: package 'tibble' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.3
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'lubridate' was built under R version 4.5.2
Warning: package 'ggplot2' was built under R version 4.5.3
Warning: package 'tsibble' was built under R version 4.5.3
Warning: package 'tsibbledata' was built under R version 4.5.3
Warning: package 'ggtime' was built under R version 4.5.3
Warning: package 'feasts' was built under R version 4.5.3
Warning: package 'fabletools' was built under R version 4.5.3
Warning: package 'fable' was built under R version 4.5.3
── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
✖ lubridate::date()    masks base::date()
✖ dplyr::filter()      masks stats::filter()
✖ tsibble::intersect() masks base::intersect()
✖ tsibble::interval()  masks lubridate::interval()
✖ dplyr::lag()         masks stats::lag()
✖ tsibble::setdiff()   masks base::setdiff()
✖ tsibble::union()     masks base::union()

Case 1: Let’s find out about the data in the aus_production time series.

aus_production
# A tsibble: 218 x 7 [1Q]
   Quarter  Beer Tobacco Bricks Cement Electricity   Gas
     <qtr> <dbl>   <dbl>  <dbl>  <dbl>       <dbl> <dbl>
 1 1956 Q1   284    5225    189    465        3923     5
 2 1956 Q2   213    5178    204    532        4436     6
 3 1956 Q3   227    5297    208    561        4806     7
 4 1956 Q4   308    5681    197    570        4418     6
 5 1957 Q1   262    5577    187    529        4339     5
 6 1957 Q2   228    5651    214    604        4811     7
 7 1957 Q3   236    5317    227    603        5259     7
 8 1957 Q4   320    6152    222    582        4735     6
 9 1958 Q1   272    5758    199    554        4608     5
10 1958 Q2   233    5641    229    620        5196     7
# ℹ 208 more rows
help("aus_production")
starting httpd help server ... done

Interpretation

  1. aus_production is a time series provided by the Australian Bureau of statistics that contains the production indicators of six selected products in Australia such as Beer, Tobacco, Bricks, Cement, electricity and Gas.

  2. The aus_production time interval is Quarterly meaning the data an estimate of the production each quarter.

Case 2: Let’s find out about the data in the pelt time series.

pelt
# A tsibble: 91 x 3 [1Y]
    Year  Hare  Lynx
   <dbl> <dbl> <dbl>
 1  1845 19580 30090
 2  1846 19600 45150
 3  1847 19610 49150
 4  1848 11990 39520
 5  1849 28040 21230
 6  1850 58000  8420
 7  1851 74600  5560
 8  1852 75090  5080
 9  1853 88480 10170
10  1854 61280 19600
# ℹ 81 more rows
help("pelt")

Interpretation

  1. pelt is a time series that contains the trading records from the Hudson Bay Company for Snowshoe Hare and Canadian Lynx fur from 1845 to 1935.

  2. The time interval of this time series in annual meaning the time series represents each year of the trade records from the company.

Case 3: Let’s find out about the data in the gafa_stock time series.

gafa_stock
# A tsibble: 5,032 x 8 [!]
# Key:       Symbol [4]
   Symbol Date        Open  High   Low Close Adj_Close    Volume
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>     <dbl>
 1 AAPL   2014-01-02  79.4  79.6  78.9  79.0      67.0  58671200
 2 AAPL   2014-01-03  79.0  79.1  77.2  77.3      65.5  98116900
 3 AAPL   2014-01-06  76.8  78.1  76.2  77.7      65.9 103152700
 4 AAPL   2014-01-07  77.8  78.0  76.8  77.1      65.4  79302300
 5 AAPL   2014-01-08  77.0  77.9  77.0  77.6      65.8  64632400
 6 AAPL   2014-01-09  78.1  78.1  76.5  76.6      65.0  69787200
 7 AAPL   2014-01-10  77.1  77.3  75.9  76.1      64.5  76244000
 8 AAPL   2014-01-13  75.7  77.5  75.7  76.5      64.9  94623200
 9 AAPL   2014-01-14  76.9  78.1  76.8  78.1      66.1  83140400
10 AAPL   2014-01-15  79.1  80.0  78.8  79.6      67.5  97909700
# ℹ 5,022 more rows
help("gafa_stock")

Interpretation

  1. The “gafa_stock” time series is composed of historical stock prices in US dollars of four selected companies including Google, facebook, Apple and Amazon from 2014 to 2018.

  2. Each stock price is recorded daily for the majority, but there are some missing days in the dataset, the time interval for the “gafa_stock” time series will be considered daily irregular.

Case 4 Let’s find out about the data in the vic_elec time series.

vic_elec
# A tsibble: 52,608 x 5 [30m] <Australia/Melbourne>
   Time                Demand Temperature Date       Holiday
   <dttm>               <dbl>       <dbl> <date>     <lgl>  
 1 2012-01-01 00:00:00  4383.        21.4 2012-01-01 TRUE   
 2 2012-01-01 00:30:00  4263.        21.0 2012-01-01 TRUE   
 3 2012-01-01 01:00:00  4049.        20.7 2012-01-01 TRUE   
 4 2012-01-01 01:30:00  3878.        20.6 2012-01-01 TRUE   
 5 2012-01-01 02:00:00  4036.        20.4 2012-01-01 TRUE   
 6 2012-01-01 02:30:00  3866.        20.2 2012-01-01 TRUE   
 7 2012-01-01 03:00:00  3694.        20.1 2012-01-01 TRUE   
 8 2012-01-01 03:30:00  3562.        19.6 2012-01-01 TRUE   
 9 2012-01-01 04:00:00  3433.        19.1 2012-01-01 TRUE   
10 2012-01-01 04:30:00  3359.        19.0 2012-01-01 TRUE   
# ℹ 52,598 more rows
help("vic_elec")

Interpretation

  1. The vic_elec time series covers the total electricity demand in Megawatt in Victoria,Australia alongside of the temperature of Melbourne.

  2. The data were recorded every 30 minutes of every single day of 2012. For that reason, the time interval of the Vic_elec series is half-hourly.

Let’s produce a time plot for each series.

To accomplish, we will use the autoplot() function which is very suitable to create time series plot.s

a) Let’s create a time plot for the aus_production series

# Let's create a time plot for the aus_production series with the bricks as variable

Bricks_plot <- aus_production %>% autoplot(Bricks)+
  labs(y="number of bricks in Million", title="Quarterly production of Bricks in Australia")
Bricks_plot
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).

# Let's create a time plot for the pelt series with Lynx as our variable

Lynx_plot <- pelt |> autoplot(Lynx) 
Lynx_plot

# Let's create a time plot for the gafa_stock series with Close as our variable

Close_plot <- gafa_stock|>autoplot(Close)
Close_plot

# Let's create a time plot for the vic_elec series with Demand as our variable

Demand_plot <- vic_elec %>% autoplot(Demand) +
     labs(y="Electricity Demand in MWh", title="Half-hourly electricity demand for Victoria")
Demand_plot

Interpretation

a) Bricks production in the aus_production series

We notice that the production of bricks have been generally increasing throughout the years with the highest peak in the early 1980. We also observe biggest decline in the first quarter of 1983.

b) Lynx furs traded in the pelt series

We observe a seasonal pattern in the the number of canadian Lynx furs traded throughout the years which is understandable because there are most likely more furs traded during cold season than summer.

c) Closing price in the gafa_stock series

We observe a long term significant increase in closing prices of Amazon and Google stocks. Facebook is generally steady with a slightly increase over time and a little decrease by the end of the year 2018.

d) Electricity Demand in the Vic_elec serie

We observe a half-hourly seasonal electricity demands in Victoria,Australia with the highest demand reaching over 8000 megawatt in the early 2014.

Exercise 2.2

Let’s find what days correspond to the peak closing price for each of the four stocks in gafa_stock.

# Let's find what day(s) correspond to the peak closing price for APPLE

AAPL_Max_Closing_Price <- gafa_stock |>
              filter(Symbol=="AAPL") |> # we filter only the AAPL stock
  select(Symbol,Date,Close) |> # we select only the column of interest
  filter(Close== max(Close)) # we filter the day with the highest closing price
  
AAPL_Max_Closing_Price
# A tsibble: 1 x 3 [!]
# Key:       Symbol [1]
  Symbol Date       Close
  <chr>  <date>     <dbl>
1 AAPL   2018-10-03  232.
# Let's find what day(s) correspond to the peak closing price for AMAZON

AMZN_Max_Closing_Price <- gafa_stock |>
              filter(Symbol=="AMZN") |> # we filter only the AMZN stock
  select(Symbol,Date,Close) |> # we select only the column of interest
  filter(Close== max(Close)) # we filter the day with the highest closing price
  
AMZN_Max_Closing_Price
# A tsibble: 1 x 3 [!]
# Key:       Symbol [1]
  Symbol Date       Close
  <chr>  <date>     <dbl>
1 AMZN   2018-09-04 2040.
# Let's find what day(s) correspond to the peak closing price for FACEBOOK

Facebook_Max_Closing_Price <- gafa_stock |>
              filter(Symbol=="FB") |> # we filter only the FACEBOOK stock
  select(Symbol,Date,Close) |> # we select only the column of interest
  filter(Close== max(Close)) # we filter the day with the highest closing price
  
Facebook_Max_Closing_Price
# A tsibble: 1 x 3 [!]
# Key:       Symbol [1]
  Symbol Date       Close
  <chr>  <date>     <dbl>
1 FB     2018-07-25  218.
# Let's find what day(s) correspond to the peak closing price for GOOGLE

Google_Max_Closing_Price <- gafa_stock |>
              filter(Symbol=="GOOG") |> # we filter only the GOOGLE stock
  select(Symbol,Date,Close) |> # we select only the column of interest
  filter(Close== max(Close)) # we filter the day with the highest closing price
  
Google_Max_Closing_Price
# A tsibble: 1 x 3 [!]
# Key:       Symbol [1]
  Symbol Date       Close
  <chr>  <date>     <dbl>
1 GOOG   2018-07-26 1268.

Exercise 2.3

a) Let’s read the tute1 csv file in R.

In order to read the file,i loaded the file into a public GitHub so that everybody could have access to the file.

#read csv

tute1 <- read.csv("https://raw.githubusercontent.com/PascalT2015/DATA624-Homework1/refs/heads/main/tute1.csv")

head(tute1)
     Quarter  Sales AdBudget   GDP
1 1981-03-01 1020.2    659.2 251.8
2 1981-06-01  889.2    589.0 290.9
3 1981-09-01  795.0    512.5 290.8
4 1981-12-01 1003.9    614.1 292.4
5 1982-03-01 1057.7    647.2 279.1
6 1982-06-01  944.4    602.0 254.0
tail(tute1)
       Quarter  Sales AdBudget   GDP
95  2004-09-01  866.6    519.1 290.2
96  2004-12-01 1018.7    634.9 284.0
97  2005-03-01 1112.5    663.1 270.9
98  2005-06-01  997.4    583.3 294.7
99  2005-09-01  826.8    508.6 292.2
100 2005-12-01  992.6    634.2 255.1

b) Convert the data to time series

The tute1 file is a table of 4 columns labelled Quarter,Sales, AdBudget and GDP where AdBudget is the advertising budget and GDP is the gross domestic product. It represents the quarterly sales for a small company over the period 1981-2005. Since, the sales are recorded quarterly, the index or time interval for the time series that we will create in quarter. For a better visibility, we will convert the month into quarter. For instance,1981-03-01 becomes 1981Quarter1(1981Q1), 1981-06-01 becomes 1981Quarter2(1981Q2),etc…

tute1_timeseries <- tute1 |>
  mutate(Quarter = yearquarter(Quarter)) |>
  as_tsibble(index = Quarter)

head(tute1_timeseries)
# A tsibble: 6 x 4 [1Q]
  Quarter Sales AdBudget   GDP
    <qtr> <dbl>    <dbl> <dbl>
1 1981 Q1 1020.     659.  252.
2 1981 Q2  889.     589   291.
3 1981 Q3  795      512.  291.
4 1981 Q4 1004.     614.  292.
5 1982 Q1 1058.     647.  279.
6 1982 Q2  944.     602   254 
tail(tute1_timeseries)
# A tsibble: 6 x 4 [1Q]
  Quarter Sales AdBudget   GDP
    <qtr> <dbl>    <dbl> <dbl>
1 2004 Q3  867.     519.  290.
2 2004 Q4 1019.     635.  284 
3 2005 Q1 1112.     663.  271.
4 2005 Q2  997.     583.  295.
5 2005 Q3  827.     509.  292.
6 2005 Q4  993.     634.  255.

c) Construct time series plots of each of the three series

tute1_timeseries_plot <- tute1_timeseries %>% autoplot(Sales) +
  labs(x="Quarter", y="Sales", title="AdBudget and GDP Quarterly Sales")
  
tute1_timeseries_plot

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

a) Let’s load the USgas library.

I will install the USgas package in my console so that it will not have to install it anytime that i am rendering the file.

library(USgas)
Warning: package 'USgas' was built under R version 4.5.3

b) Let’s create a tsibble from us_total with year as the index and state as the key.

# Let's first open the us_total table

head(us_total)
  year   state      y
1 1997 Alabama 324158
2 1998 Alabama 329134
3 1999 Alabama 337270
4 2000 Alabama 353614
5 2001 Alabama 332693
6 2002 Alabama 379343
tail(us_total)
     year   state      y
1261 2014 Wyoming 136796
1262 2015 Wyoming 119265
1263 2016 Wyoming 123351
1264 2017 Wyoming 149405
1265 2018 Wyoming 165384
1266 2019 Wyoming 154836
us_total_timeseries <- us_total |>
  as_tsibble(key=state,index = year)

head(us_total_timeseries,10)
# A tsibble: 10 x 3 [1Y]
# Key:       state [1]
    year state        y
   <int> <chr>    <int>
 1  1997 Alabama 324158
 2  1998 Alabama 329134
 3  1999 Alabama 337270
 4  2000 Alabama 353614
 5  2001 Alabama 332693
 6  2002 Alabama 379343
 7  2003 Alabama 350345
 8  2004 Alabama 382367
 9  2005 Alabama 353156
10  2006 Alabama 391093

c) Let’s Plot the annual natural gas consumption by state for the New England area

us_total_timeseries %>% 
  filter(state=="Maine" | state== "Vermont" | state== "New Hampshire" | state== "Massachusetts" | state== "Connecticut" | state== "Rhode Island") %>%
  autoplot(y) +
  labs(y="Quantity of Gas ", title="Annual natural Gas consumption by state for the New England area")

Exercise 2.5:

a) Let’s read the tourism excel file into R

# let's load the readxl library to 

library(readxl)
Warning: package 'readxl' was built under R version 4.5.3
# Let's read the tourism excel file

tourism <-read_excel("C:/Users/herma/OneDrive/Desktop/CUNY SPS GRAD SCHOOL/FALL 2026 CLASSES/DATA 624/HOMEWORKS/Homework 1/tourism.xlsx")
head(tourism)
# A tibble: 6 × 5
  Quarter    Region   State           Purpose  Trips
  <chr>      <chr>    <chr>           <chr>    <dbl>
1 1998-01-01 Adelaide South Australia Business  135.
2 1998-04-01 Adelaide South Australia Business  110.
3 1998-07-01 Adelaide South Australia Business  166.
4 1998-10-01 Adelaide South Australia Business  127.
5 1999-01-01 Adelaide South Australia Business  137.
6 1999-04-01 Adelaide South Australia Business  200.
tail(tourism)
# A tibble: 6 × 5
  Quarter    Region          State           Purpose  Trips
  <chr>      <chr>           <chr>           <chr>    <dbl>
1 2016-07-01 Yorke Peninsula South Australia Visiting  22.8
2 2016-10-01 Yorke Peninsula South Australia Visiting  33.7
3 2017-01-01 Yorke Peninsula South Australia Visiting  46.2
4 2017-04-01 Yorke Peninsula South Australia Visiting  50.6
5 2017-07-01 Yorke Peninsula South Australia Visiting  27.8
6 2017-10-01 Yorke Peninsula South Australia Visiting  46.3

b) Let’s create a tsibble which is identical to the tourism tsibble from the tsibble package.

To reach our goal, we will need to construct a tsibble of five column with a time interval or index equivalent to a Quarter and 3 keys variables including region,State, Purpose and our one measured variable here which is the number trips.

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

tourism_ts
# A tsibble: 24,320 x 5 [1Q]
# Key:       Region, State, Purpose [304]
   Quarter Region   State           Purpose  Trips
     <qtr> <chr>    <chr>           <chr>    <dbl>
 1 1998 Q1 Adelaide South Australia Business  135.
 2 1998 Q2 Adelaide South Australia Business  110.
 3 1998 Q3 Adelaide South Australia Business  166.
 4 1998 Q4 Adelaide South Australia Business  127.
 5 1999 Q1 Adelaide South Australia Business  137.
 6 1999 Q2 Adelaide South Australia Business  200.
 7 1999 Q3 Adelaide South Australia Business  169.
 8 1999 Q4 Adelaide South Australia Business  134.
 9 2000 Q1 Adelaide South Australia Business  154.
10 2000 Q2 Adelaide South Australia Business  169.
# ℹ 24,310 more rows

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

max_trips <- tourism_ts |> 
  group_by(Region,Purpose) |>
  summarise(average_trips=mean(Trips)) |>
  filter(average_trips== max(average_trips))

max_trips
# A tsibble: 76 x 4 [1Q]
# Key:       Region, Purpose [76]
# Groups:    Region [76]
   Region                     Purpose  Quarter average_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
  1. Create a new tsibble which combines the Purposes and Regions, and just has total trips by State

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

To knightly solve this exercise, it will be better to do it case by case.

a) Let’s use the autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() graphics functions to explore the “Total Private”Employed form us_employment time series features.

# # Let's visualize the content of the us_employment time series

head(us_employment)
# A tsibble: 6 x 4 [1M]
# Key:       Series_ID [1]
     Month Series_ID     Title         Employed
     <mth> <chr>         <chr>            <dbl>
1 1939 Jan CEU0500000001 Total Private    25338
2 1939 Feb CEU0500000001 Total Private    25447
3 1939 Mar CEU0500000001 Total Private    25833
4 1939 Apr CEU0500000001 Total Private    25801
5 1939 May CEU0500000001 Total Private    26113
6 1939 Jun CEU0500000001 Total Private    26485
tail(us_employment)
# A tsibble: 6 x 4 [1M]
# Key:       Series_ID [1]
     Month Series_ID Title                                  Employed
     <mth> <chr>     <chr>                                     <dbl>
1 2019 Apr TEMPHELPN All Employees, Temporary Help Services    2969.
2 2019 May TEMPHELPN All Employees, Temporary Help Services    3010.
3 2019 Jun TEMPHELPN All Employees, Temporary Help Services    3026.
4 2019 Jul TEMPHELPN All Employees, Temporary Help Services    2966.
5 2019 Aug TEMPHELPN All Employees, Temporary Help Services    3040 
6 2019 Sep TEMPHELPN All Employees, Temporary Help Services    3109.
# Let's create a new tsibble that contains only Total private employment title.

Total_Private_empl <- us_employment %>%
  select(-Series_ID) %>% # we select only the column of interest
  filter(Title=="Total Private")

Total_Private_empl
# A tsibble: 969 x 4 [1M]
# Key:       Series_ID [1]
      Month Title         Employed Series_ID    
      <mth> <chr>            <dbl> <chr>        
 1 1939 Jan Total Private    25338 CEU0500000001
 2 1939 Feb Total Private    25447 CEU0500000001
 3 1939 Mar Total Private    25833 CEU0500000001
 4 1939 Apr Total Private    25801 CEU0500000001
 5 1939 May Total Private    26113 CEU0500000001
 6 1939 Jun Total Private    26485 CEU0500000001
 7 1939 Jul Total Private    26481 CEU0500000001
 8 1939 Aug Total Private    26848 CEU0500000001
 9 1939 Sep Total Private    27468 CEU0500000001
10 1939 Oct Total Private    27830 CEU0500000001
# ℹ 959 more rows
# Let's use the graphics functions to explore feature from Total_Private_empl tsibble.

autoplot(Total_Private_empl)
Plot variable not specified, automatically selected `.vars = Employed`

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

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

gg_lag(Total_Private_empl)
Plot variable not specified, automatically selected `y = Employed`

ACF(Total_Private_empl|> select(Employed))|> autoplot() # The autocorrelation function needs a numerical vector as an entry
Response variable not specified, automatically selected `var = Employed`

Interpretation

Overall,we observe a strong positive trend in the Total private employed from the US employment throughout the years with a sharp decrease around 2008 and 2009 which we believe was the consequence of the great recession. Moreover, the gg_seasonal function shows us a graphic where each year graphs overlap with a clear visible pattern the years. Consequently, we believe that there is a clear annual seasonality.

b) Let’s use the autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() graphics functions to explore the Bricks from aus_production time series features.

# Let's visualize the content of the aus_production time series.

head(aus_production)
# A tsibble: 6 x 7 [1Q]
  Quarter  Beer Tobacco Bricks Cement Electricity   Gas
    <qtr> <dbl>   <dbl>  <dbl>  <dbl>       <dbl> <dbl>
1 1956 Q1   284    5225    189    465        3923     5
2 1956 Q2   213    5178    204    532        4436     6
3 1956 Q3   227    5297    208    561        4806     7
4 1956 Q4   308    5681    197    570        4418     6
5 1957 Q1   262    5577    187    529        4339     5
6 1957 Q2   228    5651    214    604        4811     7
tail(aus_production)
# A tsibble: 6 x 7 [1Q]
  Quarter  Beer Tobacco Bricks Cement Electricity   Gas
    <qtr> <dbl>   <dbl>  <dbl>  <dbl>       <dbl> <dbl>
1 2009 Q1   415      NA     NA   1963       58368   196
2 2009 Q2   398      NA     NA   2160       57471   238
3 2009 Q3   419      NA     NA   2325       58394   252
4 2009 Q4   488      NA     NA   2273       57336   210
5 2010 Q1   414      NA     NA   1904       58309   205
6 2010 Q2   374      NA     NA   2401       58041   236
# Let's create a new tsibble that contains only the number of Bricks produced.

Bricks_tsibble <- aus_production %>%
  select(Quarter,Bricks) # We select only the columns of interest

Bricks_tsibble
# A tsibble: 218 x 2 [1Q]
   Quarter Bricks
     <qtr>  <dbl>
 1 1956 Q1    189
 2 1956 Q2    204
 3 1956 Q3    208
 4 1956 Q4    197
 5 1957 Q1    187
 6 1957 Q2    214
 7 1957 Q3    227
 8 1957 Q4    222
 9 1958 Q1    199
10 1958 Q2    229
# ℹ 208 more rows
# Let's use the graphics functions to explore the number of Bricks produced tsibble feature .

autoplot(Bricks_tsibble)
Plot variable not specified, automatically selected `.vars = Bricks`
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).

gg_season(Bricks_tsibble)
Plot variable not specified, automatically selected `y = Bricks`
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).

gg_subseries(Bricks_tsibble)
Plot variable not specified, automatically selected `y = Bricks`
Warning: Removed 20 rows containing missing values or values outside the scale range
(`geom_line()`).

gg_lag(Bricks_tsibble)
Plot variable not specified, automatically selected `y = Bricks`
Warning: Removed 20 rows containing missing values (gg_lag).

ACF(Bricks_tsibble|> select(Bricks))|>autoplot() # The autocorrelation function needs a numerical vector as an entry
Response variable not specified, automatically selected `var = Bricks`

Interpretation

We observe some similarities in the quarterly production of bricks with a sharp drop around 1983 and 1984. It is important to point out that the third quarter has the highest bricks production in average which we believe could due to a dryer weather that facilitate the productions of bricks. The production of bricks from the aus_production time series can be considered quarterly seasonal.

C) Let’s use the autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() graphics functions to explore the Hare from pelt time series features.

The pelt time series represents the annual trading records from the Hudson Bay Company for Snowshoe Hare from 1845 to 1935 with no seasonal structure, we will not be able to use both the gg_season() and gg_subseries() because those functions need at least a seasonal observation in the dataset.

# Let's visualize the content of the pelt time series.

head(pelt)
# A tsibble: 6 x 3 [1Y]
   Year  Hare  Lynx
  <dbl> <dbl> <dbl>
1  1845 19580 30090
2  1846 19600 45150
3  1847 19610 49150
4  1848 11990 39520
5  1849 28040 21230
6  1850 58000  8420
tail(pelt)
# A tsibble: 6 x 3 [1Y]
   Year  Hare  Lynx
  <dbl> <dbl> <dbl>
1  1930  4230  6980
2  1931 19520  8310
3  1932 82110 16010
4  1933 89760 24820
5  1934 81660 29700
6  1935 15760 35400
# Let's create a new tsibble that contains only the annual Hare trading record.

Hare_tsibble <- pelt %>%
  select(Year,Hare) # We select only the columns of interest

Hare_tsibble
# A tsibble: 91 x 2 [1Y]
    Year  Hare
   <dbl> <dbl>
 1  1845 19580
 2  1846 19600
 3  1847 19610
 4  1848 11990
 5  1849 28040
 6  1850 58000
 7  1851 74600
 8  1852 75090
 9  1853 88480
10  1854 61280
# ℹ 81 more rows
# Let's use the graphics functions to explore the annual Hare trading record tsibble feature.

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

gg_lag(Hare_tsibble)
Plot variable not specified, automatically selected `y = Hare`

ACF(Hare_tsibble|> select(Hare))|>autoplot() # The autocorrelation function needs a numerical vector as an entry
Response variable not specified, automatically selected `var = Hare`

Interpretation

There is not a clear visible repeated pattern in the snowshoe Hare trading record throughout the years. Therefore we can conclude that there is no annual seasonal cycle in the snowshoe Hare trading.

d) Let’s use the autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() graphics functions to explore the “H02” Cost from PBS time series features.

# Let's visualize the content of the PBS time series.

head(PBS)
# A tsibble: 6 x 9 [1M]
# Key:       Concession, Type, ATC1, ATC2 [1]
     Month Concession   Type       ATC1  ATC1_desc ATC2  ATC2_desc Scripts  Cost
     <mth> <chr>        <chr>      <chr> <chr>     <chr> <chr>       <dbl> <dbl>
1 1991 Jul Concessional Co-paymen… A     Alimenta… A01   STOMATOL…   18228 67877
2 1991 Aug Concessional Co-paymen… A     Alimenta… A01   STOMATOL…   15327 57011
3 1991 Sep Concessional Co-paymen… A     Alimenta… A01   STOMATOL…   14775 55020
4 1991 Oct Concessional Co-paymen… A     Alimenta… A01   STOMATOL…   15380 57222
5 1991 Nov Concessional Co-paymen… A     Alimenta… A01   STOMATOL…   14371 52120
6 1991 Dec Concessional Co-paymen… A     Alimenta… A01   STOMATOL…   15028 54299
tail(PBS)
# A tsibble: 6 x 9 [1M]
# Key:       Concession, Type, ATC1, ATC2 [1]
     Month Concession Type       ATC1  ATC1_desc ATC2  ATC2_desc Scripts  Cost
     <mth> <chr>      <chr>      <chr> <chr>     <chr> <chr>       <dbl> <dbl>
1 2008 Jan General    Safety net Z     <NA>      Z     Z             797  9604
2 2008 Feb General    Safety net Z     <NA>      Z     Z             135  1591
3 2008 Mar General    Safety net Z     <NA>      Z     Z              15   276
4 2008 Apr General    Safety net Z     <NA>      Z     Z              11   165
5 2008 May General    Safety net Z     <NA>      Z     Z              21   278
6 2008 Jun General    Safety net Z     <NA>      Z     Z              57   491
# Let's list the unique values in ATC2 column.

unique(PBS$ATC2)
 [1] "A01" "A02" "A03" "A04" "A05" "A06" "A07" "A09" "A10" "A11" "A12" "A14"
[13] "A15" "B01" "B02" "B03" "B05" "C01" "C02" "C03" "C04" "C05" "C07" "C08"
[25] "C09" "C10" "D"   "D01" "D02" "D04" "D05" "D06" "D07" "D08" "D10" "D11"
[37] "G01" "G02" "G03" "G04" "H01" "H02" "H03" "H04" "H05" "J01" "J02" "J04"
[49] "J05" "J06" "J07" "L01" "L02" "L03" "L04" "M01" "M02" "M03" "M04" "M05"
[61] "N02" "N03" "N04" "N05" "N06" "N07" "P01" "P02" "P03" "R"   "R01" "R03"
[73] "R05" "R06" "S"   "S01" "S02" "S03" "V01" "V03" "V04" "V06" "V07" "Z"  
# Let's create a new tsibble that contain only the H02 drug rows

H02_tsibble <- PBS %>% 
     filter(ATC2=="H02") # we pull out only H02 rows
  
H02_tsibble
# A tsibble: 816 x 9 [1M]
# Key:       Concession, Type, ATC1, ATC2 [4]
      Month Concession   Type     ATC1  ATC1_desc ATC2  ATC2_desc Scripts   Cost
      <mth> <chr>        <chr>    <chr> <chr>     <chr> <chr>       <dbl>  <dbl>
 1 1991 Jul Concessional Co-paym… H     Systemic… H02   CORTICOS…   63261 317384
 2 1991 Aug Concessional Co-paym… H     Systemic… H02   CORTICOS…   53528 269891
 3 1991 Sep Concessional Co-paym… H     Systemic… H02   CORTICOS…   52822 269703
 4 1991 Oct Concessional Co-paym… H     Systemic… H02   CORTICOS…   54016 280418
 5 1991 Nov Concessional Co-paym… H     Systemic… H02   CORTICOS…   49281 268070
 6 1991 Dec Concessional Co-paym… H     Systemic… H02   CORTICOS…   51798 277139
 7 1992 Jan Concessional Co-paym… H     Systemic… H02   CORTICOS…   42436 221772
 8 1992 Feb Concessional Co-paym… H     Systemic… H02   CORTICOS…   52913 272345
 9 1992 Mar Concessional Co-paym… H     Systemic… H02   CORTICOS…   62908 325700
10 1992 Apr Concessional Co-paym… H     Systemic… H02   CORTICOS…   68499 349271
# ℹ 806 more rows
# Let's use the graphics functions to explore the “H02” Cost from PBS time series features.

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

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

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

ACF(H02_tsibble %>% select(Cost))|>autoplot()
Response variable not specified, automatically selected `var = Cost`

Interpretation

The graphics show us an overall repeated pattern of the Corticosteroids (H02)drug prices monthly over the years where each year graphs overlap with a clear underlying pattern visible monthly. We can safely believe that there is a monthly seasonality of the H02 cost throughout the years.

e) Let’s use the autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() graphics functions to explore the Barrels from us_gasoline. time series features.

# Let's visualize the content of the us_gasoline time series.


head(us_gasoline)
# A tsibble: 6 x 2 [1W]
      Week Barrels
    <week>   <dbl>
1 1991 W06    6.62
2 1991 W07    6.43
3 1991 W08    6.58
4 1991 W09    7.22
5 1991 W10    6.88
6 1991 W11    6.95
tail(us_gasoline)
# A tsibble: 6 x 2 [1W]
      Week Barrels
    <week>   <dbl>
1 2016 W50    9.27
2 2016 W51    9.28
3 2016 W52    8.46
4 2017 W01    8.47
5 2017 W02    8.07
6 2017 W03    8.04
# Let's create a new tsibble 

Barrels_tsibble <- us_gasoline %>%
  select(Week,Barrels) # We select only the columns of interest

Barrels_tsibble
# A tsibble: 1,355 x 2 [1W]
       Week Barrels
     <week>   <dbl>
 1 1991 W06    6.62
 2 1991 W07    6.43
 3 1991 W08    6.58
 4 1991 W09    7.22
 5 1991 W10    6.88
 6 1991 W11    6.95
 7 1991 W12    7.33
 8 1991 W13    6.78
 9 1991 W14    7.50
10 1991 W15    6.92
# ℹ 1,345 more rows
# Let's use the graphics functions to explore the Barrels from us_gasoline feature .

autoplot(Barrels_tsibble)
Plot variable not specified, automatically selected `.vars = Barrels`

gg_season(Barrels_tsibble)
Plot variable not specified, automatically selected `y = Barrels`

gg_subseries(Barrels_tsibble)
Plot variable not specified, automatically selected `y = Barrels`

gg_lag(Barrels_tsibble)
Plot variable not specified, automatically selected `y = Barrels`

ACF(Barrels_tsibble|> select(Barrels))|>autoplot() # The autocorrelation function needs a numerical vector as an entry
Response variable not specified, automatically selected `var = Barrels`

Interpretation

Looking at our first graph, we can clearly see a steady increases from 1990 to 1999,from 1999 to 2009 and from 2009 upward with a slight difference between 2007 and 2009 which can also be tight to the great recession that strengthen economics conditions. Therefore, we can conclude of an overall confirmation of a weekly seasonality.