Homework 1 cahpter2

Author

Guibril Ramde

2.10 Exercises

2.1

library(fpp3)
Warning: package 'fpp3' was built under R version 4.5.2
── Attaching packages ──────────────────────────────────────────── fpp3 1.0.3 ──
✔ tibble      3.3.1     ✔ tsibble     1.2.0
✔ dplyr       1.2.0     ✔ tsibbledata 0.4.1
✔ tidyr       1.3.2     ✔ ggtime      1.0.0
✔ lubridate   1.9.4     ✔ feasts      0.5.0
✔ ggplot2     4.0.2     ✔ 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.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'ggplot2' was built under R version 4.5.2
Warning: package 'tsibble' was built under R version 4.5.2
Warning: package 'ggtime' was built under R version 4.5.2
Warning: package 'feasts' was built under R version 4.5.2
Warning: package 'fabletools' was built under R version 4.5.2
Warning: package 'fable' was built under R version 4.5.2
── 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()
library(tsibble)
help(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


help(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   

help(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
# ℹ 81 more rows

help(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
# ℹ 5,022 more rows

Bricks in aus_production is a quarterly data

Demand from vic_elec is half hourly data

Lynx from pelt is a yearly data

Close from gafa_stock is daily data

aus_production |>
  autoplot(Bricks, na.rm =TRUE)

vic_elec |>
  autoplot(Demand)

gafa_stock |>
  autoplot(Close)

pelt |>
  autoplot(Lynx)

Modify the last plot axis label and title

vic_elec |>
  autoplot(Demand) +
  labs(
    title = "Electricity Demand in Victoria",
    x = "Date and Time",
    y = "Electricity Demand"
  )

2.2

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

2.3

load_data <- read.csv("tute1.csv")
load_data
       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
7   1982-09-01  778.5    530.7 295.6
8   1982-12-01  932.5    608.4 271.7
9   1983-03-01  996.5    637.9 259.6
10  1983-06-01  907.7    582.4 280.5
11  1983-09-01  735.1    506.8 287.2
12  1983-12-01  958.1    606.7 278.0
13  1984-03-01 1034.1    658.7 256.8
14  1984-06-01  992.8    614.9 271.0
15  1984-09-01  791.7    489.9 300.9
16  1984-12-01  914.2    586.5 289.8
17  1985-03-01 1106.5    663.0 266.8
18  1985-06-01  985.1    591.7 273.7
19  1985-09-01  823.9    502.2 301.3
20  1985-12-01 1025.1    616.4 285.6
21  1986-03-01 1064.7    647.1 270.6
22  1986-06-01  981.9    615.5 274.6
23  1986-09-01  828.3    514.8 299.7
24  1986-12-01  940.7    609.1 275.9
25  1987-03-01  991.1    641.3 279.3
26  1987-06-01 1021.2    620.2 290.8
27  1987-09-01  796.7    511.2 295.6
28  1987-12-01  986.6    621.3 271.9
29  1988-03-01 1054.2    645.3 267.4
30  1988-06-01 1018.7    616.0 281.0
31  1988-09-01  815.6    503.2 309.0
32  1988-12-01 1010.6    617.5 266.7
33  1989-03-01 1071.5    642.8 283.5
34  1989-06-01  954.0    585.6 282.3
35  1989-09-01  822.9    520.6 289.2
36  1989-12-01  867.5    608.6 270.7
37  1990-03-01 1002.3    645.7 266.5
38  1990-06-01  972.0    597.4 287.9
39  1990-09-01  782.9    499.8 287.6
40  1990-12-01  972.8    601.8 283.4
41  1991-03-01 1108.0    650.8 266.4
42  1991-06-01  943.7    588.3 292.3
43  1991-09-01  806.1    491.6 330.6
44  1991-12-01  954.2    603.3 286.2
45  1992-03-01 1115.5    663.2 259.2
46  1992-06-01  927.1    614.0 263.7
47  1992-09-01  800.7    506.3 288.2
48  1992-12-01  955.7    606.2 274.1
49  1993-03-01 1049.8    639.5 287.1
50  1993-06-01  886.0    585.9 285.5
51  1993-09-01  786.4    492.2 303.7
52  1993-12-01  991.3    610.4 275.6
53  1994-03-01 1113.9    660.8 249.3
54  1994-06-01  924.5    612.2 272.9
55  1994-09-01  771.4    509.2 289.8
56  1994-12-01  949.8    612.1 269.2
57  1995-03-01  990.5    653.2 261.3
58  1995-06-01 1071.4    605.3 292.9
59  1995-09-01  854.1    506.6 304.6
60  1995-12-01  929.8    597.4 276.3
61  1996-03-01  959.6    635.2 268.2
62  1996-06-01  991.1    611.6 293.5
63  1996-09-01  832.9    503.8 311.1
64  1996-12-01 1006.1    609.9 273.7
65  1997-03-01 1040.7    645.2 267.5
66  1997-06-01 1026.3    609.8 271.9
67  1997-09-01  785.9    512.1 308.8
68  1997-12-01  997.6    603.7 282.9
69  1998-03-01 1055.0    639.4 268.4
70  1998-06-01  925.6    601.6 271.4
71  1998-09-01  805.6    497.0 292.1
72  1998-12-01  934.1    602.8 287.6
73  1999-03-01 1081.7    647.3 258.0
74  1999-06-01 1062.3    612.5 282.9
75  1999-09-01  798.8    492.2 295.0
76  1999-12-01 1014.3    610.8 271.2
77  2000-03-01 1049.5    646.5 275.4
78  2000-06-01  961.7    603.3 284.0
79  2000-09-01  793.4    503.8 300.9
80  2000-12-01  872.3    598.3 277.4
81  2001-03-01 1014.2    649.4 273.8
82  2001-06-01  952.6    620.2 288.4
83  2001-09-01  792.4    497.9 283.4
84  2001-12-01  922.3    609.2 273.4
85  2002-03-01 1055.9    665.9 271.5
86  2002-06-01  906.2    600.4 283.6
87  2002-09-01  811.2    502.3 290.6
88  2002-12-01 1005.8    605.6 289.1
89  2003-03-01 1013.8    647.6 282.2
90  2003-06-01  905.6    583.5 285.6
91  2003-09-01  957.3    502.5 304.0
92  2003-12-01 1059.5    625.9 271.5
93  2004-03-01 1090.6    648.7 263.9
94  2004-06-01  998.9    610.7 288.3
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

a. read the data into R

tute1 <- readr::read_csv("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)

b. convert the data to time series

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

c. construct time series plots of each of the three series

mytimeseries |>
  pivot_longer(-Quarter) |>
  ggplot(aes(x = Quarter, y = value, colour = name)) +
  geom_line() +
  facet_grid(name ~ ., scales = "free_y")

not including facet_grid the 03 name get store in the same value making it one plot, which might be confusing during interpretation.

2.4

library(USgas)
names(us_total)
[1] "year"  "state" "y"    
#install.packages("USgas")

us_total <- us_total |>
  as_tsibble(key = state, index =  year)
us_total
# A tsibble: 1,266 x 3 [1Y]
# Key:       state [53]
    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
# ℹ 1,256 more rows
is_tsibble(us_total)
[1] TRUE

c. Plot the annual natural gas

new_england <- c(
   "Maine",
  "Vermont",
  "New Hampshire",
  "Massachusetts",
  "Connecticut",
  "Rhode Island"
  
)
us_total |>
  filter(state %in% new_england) |>
  autoplot(y)

2.5 a. read tourism.xlsx

load_data_t <- readxl::read_excel("tourism.xlsx")
load_data_t
# A tibble: 24,320 × 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.
 7 1999-07-01 Adelaide South Australia Business  169.
 8 1999-10-01 Adelaide South Australia Business  134.
 9 2000-01-01 Adelaide South Australia Business  154.
10 2000-04-01 Adelaide South Australia Business  169.
# ℹ 24,310 more rows

b.create tsibble

tourism_data <- load_data_t |>
  mutate(Quarter = yearquarter(Quarter)) |>
  as_tsibble(key = c(Region,State, Purpose), index = Quarter)
tourism_data
# 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
#checking if it is identical 
tourism
# 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
key_vars(tourism)
[1] "Region"  "State"   "Purpose"
index_var(tourism)
[1] "Quarter"

c. Max overnight trip

max_trip <- tourism_data |>
  group_by(Region, Purpose) |>
  summarise(avg_trips = mean(Trips), .groups = "drop") |>
  filter(avg_trips == max(avg_trips))
  

max_trip
# A tsibble: 1 x 4 [1Q]
# Key:       Region, Purpose [1]
  Region    Purpose  Quarter avg_trips
  <chr>     <chr>      <qtr>     <dbl>
1 Melbourne Visiting 2017 Q4      985.

d.Create a new tsibble which combine region and purpose

tourism_new <- tourism_data |>
  group_by(State) |>
  index_by(Quarter) |>
  summarise(Trips = sum(Trips))

tourism_new
# A tsibble: 640 x 3 [1Q]
# Key:       State [8]
   State Quarter Trips
   <chr>   <qtr> <dbl>
 1 ACT   1998 Q1  551.
 2 ACT   1998 Q2  416.
 3 ACT   1998 Q3  436.
 4 ACT   1998 Q4  450.
 5 ACT   1999 Q1  379.
 6 ACT   1999 Q2  558.
 7 ACT   1999 Q3  449.
 8 ACT   1999 Q4  595.
 9 ACT   2000 Q1  600.
10 ACT   2000 Q2  557.
# ℹ 630 more rows

2.8 Use the following graphics functions

total_private <- us_employment |>
  filter(Title == "Total Private")

total_private |> autoplot(Employed)

total_private |> gg_season(Employed)

total_private |> gg_subseries(Employed)

total_private |> gg_lag(Employed)

total_private |> ACF(Employed) |> autoplot()

  • Observation: Trend: There is a strong increasing trend in private employment over time. More recent years have much higher employment levels than earlier years. Cause could be internet, technologies, immigration. Cyclicity: There are periods where employment decreases for some time before recovering and continuing its long-term growth, suggesting cyclical movements, the autoplot is better to visualize that cyclicity pattern.

  • Private employment has a strong upward trend over time, although there are some periods of decline and recovery. This due to school ending, summer jobs, entry - level jobs and also holidays part time jobs.

  • Employment generally increases from January to June, remains relatively stable during the summer, decreases slightly around September, and then slightly increases or remains stable toward the end of the year. This pattern repeats across many years.

  • Some years show larger decreases than the usual seasonal pattern. The time-series plot shows particularly noticeable downturns around 2008–2009 and 2020.

aus_production |>
  ACF(Bricks, na.rm = TRUE) |> autoplot()
Warning: The `...` argument of `PACF()` is deprecated as of feasts 0.2.2.
ℹ ACF variables should be passed to the `y` argument. If multiple variables are
  to be used, specify them using `vars(...)`.
Warning: ACF currently only supports one column, `Bricks` will be used.

aus_production |>
  autoplot(Bricks, na.rm = TRUE)

aus_production |>
  gg_subseries(Bricks, na.rm = TRUE) 

aus_production |>
  gg_season(Bricks, na.rm = TRUE)

aus_production |>
  gg_lag(Bricks, geom = "point")
Warning: Removed 20 rows containing missing values (gg_lag).

Observation:

  • Observation: Trend: There is a strong increasing trend on aus_productions ‘Bricks’ over time. More recent years have much higher Bricks production than earlier years. Cause could be population growth, immigration, housing, tourism. Cyclicity: There are periods where Bricks production decreases for some time before recovering and continuing its long-term growth, suggesting cyclical movements, the autoplot is better to visualize that cyclicity pattern.

  • Bricks production has a strong upward trend over time, although there are some periods of decline and recovery like around years 1980-1984 which we can observe a peak. the same pattern repeat over the time.

  • Bricks production generally increases from Q1 to Q2, remains relatively stable from Q2 to Q3 then decreases slightly from Q3 to Q4.

  • 1975 to 1985 are the years we see increase on Bricks production in Australia, then down during early 2000’s to 2005.

pelt |>
  autoplot(Hare, na.rm = TRUE)

pelt |>
  gg_lag(Hare, geom = "point")

 #pelt |>
  #gg_season(Hare, na.rm = TRUE)
 
# pelt |>
 # gg_subseries(Hare, na.rm = TRUE)
 
 pelt |>
  ACF(Hare, na.rm = TRUE) |> autoplot()
Warning: ACF currently only supports one column, `Hare` will be used.

  • Observation: Trend: There is a strong increasing and decrese trend in Hares population over time. Cyclicity: The pattern tend to repeat throughout the years, increase - decrease and not stable.

  • The series show how the Hares population are affected from 1800 to 1920 with a pick in 1865 right followed with a strong decrease and pattern is repeated.

  • For this Hare data we could not use a seasonal plot because it is reported yearly.

  • Some cycles have much larger peaks than others, particularly around the 1860s and 1880s. noticeable downturns around 1863, 1868– 1900 and 1920 also a upturn increase in years 1865 and 1885 .

h02 <- PBS |>
  filter(ATC2 == "H02") |>
  group_by(ATC2) |>
  index_by(Month) |>
  summarise(Cost = sum(Cost))

h02 |>
  gg_lag(Cost, geom = "point")

h02 |>
  gg_season(Cost)

h02 |>
  gg_subseries(Cost)

h02 |>
  autoplot(Cost)

 h02 |>
   ACF(Cost) |> autoplot()

  • Observation: Trend: There is a strong decrease from January to February, then slightly upward increase from February through December trend in Cost from PBS over time. The same pattern repeat throughout the years 1995 to 2005. Seasonality: There are periods where Cost decreases for some time before recovering and continuing its long-term growth, suggesting cyclical movements, the seasonal plot is better to visualize that cyclicity pattern.

  • Cost has a strong upward trend over time, although there are some periods of decline and recovery. This due to new year and holidays.

  • Cost generally increases relatively from February through December. This pattern repeats across many years.

  • unusual years I can identify is year 2005 where we can observe a decrease from February through March

us_gasoline |>
  gg_season(Barrels)

us_gasoline |>
  gg_subseries(Barrels)

us_gasoline |>
  gg_lag(Barrels, geom = "point")

us_gasoline |>
  autoplot(Barrels)

us_gasoline |>
  ACF(Barrels) |> autoplot()

  • Observation: Trend: There is a strong increasing trend in USA Barrels over time. More recent years have much higher number of Barrels than earlier years. Cyclicity: There are periods where the number decreases for some time before recovering vice versa, suggesting cyclical movements, the autoplot is better to visualize that cyclicity pattern.

  • Barrels number has a strong upward trend over time, although there are some periods of decline and recovery. This due to the USA importing Barrels and competition.

  • The number of Barrels has increase over time, observing the seasonal pattern we see an up and down.

  • The trends are almost the same through years 1995 to 2015 almost the same pattern occur every year.