DATA 624: HOMEWORK 2

Author

Pascal Hermann Kouogang Tafo

HOMEWORK 2

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

Exercise 3.1

  1. ** Let’s visualize the global_economy dataset.**
head(global_economy,10)
# A tsibble: 10 x 9 [1Y]
# Key:       Country [1]
   Country     Code   Year         GDP Growth   CPI Imports Exports Population
   <fct>       <fct> <dbl>       <dbl>  <dbl> <dbl>   <dbl>   <dbl>      <dbl>
 1 Afghanistan AFG    1960  537777811.     NA    NA    7.02    4.13    8996351
 2 Afghanistan AFG    1961  548888896.     NA    NA    8.10    4.45    9166764
 3 Afghanistan AFG    1962  546666678.     NA    NA    9.35    4.88    9345868
 4 Afghanistan AFG    1963  751111191.     NA    NA   16.9     9.17    9533954
 5 Afghanistan AFG    1964  800000044.     NA    NA   18.1     8.89    9731361
 6 Afghanistan AFG    1965 1006666638.     NA    NA   21.4    11.3     9938414
 7 Afghanistan AFG    1966 1399999967.     NA    NA   18.6     8.57   10152331
 8 Afghanistan AFG    1967 1673333418.     NA    NA   14.2     6.77   10372630
 9 Afghanistan AFG    1968 1373333367.     NA    NA   15.2     8.90   10604346
10 Afghanistan AFG    1969 1408888922.     NA    NA   15.0    10.1    10854428
tail(global_economy,10)
# A tsibble: 10 x 9 [1Y]
# Key:       Country [1]
   Country  Code   Year         GDP  Growth   CPI Imports Exports Population
   <fct>    <fct> <dbl>       <dbl>   <dbl> <dbl>   <dbl>   <dbl>      <dbl>
 1 Zimbabwe ZWE    2008  4415702800 -17.7    NA      68.1    41.5   13558469
 2 Zimbabwe ZWE    2009  9665793300  12.0    97.1    42.3    19.5   13810599
 3 Zimbabwe ZWE    2010 12041655200  19.7   100      53.5    29.6   14086317
 4 Zimbabwe ZWE    2011 14101920300  14.2   103.     54.7    34.8   14386649
 5 Zimbabwe ZWE    2012 17114849900  16.7   107.     49.0    25.2   14710826
 6 Zimbabwe ZWE    2013 19091020000   1.99  109.     36.7    22.0   15054506
 7 Zimbabwe ZWE    2014 19495519600   2.38  109.     33.7    20.9   15411675
 8 Zimbabwe ZWE    2015 19963120600   1.78  106.     37.6    19.2   15777451
 9 Zimbabwe ZWE    2016 20548678100   0.756 105.     31.3    19.9   16150362
10 Zimbabwe ZWE    2017 22040902300   4.70  106.     30.4    19.7   16529904

b) Let’s Plot the GDP per capita for each country over time.

global_economy |>
  autoplot(GDP/Population) +
  labs(title= "GDP per capita in US", y = "GDP per capita by country") +
  theme(legend.position = "none")
Warning: Removed 3242 rows containing missing values or values outside the scale range
(`geom_line()`).

c) Let’s find the country with the highest GDP per capita.

highest_GDP_per_Capita <- global_economy |> 
  mutate(GDP_per_Capita=GDP/Population) |> # adding a column GDP_per_Capita
  arrange(desc(GDP_per_Capita)) |>
  select(Country,Year,GDP_per_Capita) # extracting only columns of interest for a better visibility
Warning: Current temporal ordering may yield unexpected results.
ℹ Suggest to sort by `Country`, `Year` first.
Current temporal ordering may yield unexpected results.
ℹ Suggest to sort by `Country`, `Year` first.
Current temporal ordering may yield unexpected results.
ℹ Suggest to sort by `Country`, `Year` first.
head(highest_GDP_per_Capita)
Warning: Current temporal ordering may yield unexpected results.
ℹ Suggest to sort by `Country`, `Year` first.
# A tsibble: 6 x 3 [1Y]
# Key:       Country [2]
  Country        Year GDP_per_Capita
  <fct>         <dbl>          <dbl>
1 Monaco         2014        185153.
2 Monaco         2008        180640.
3 Liechtenstein  2014        179308.
4 Liechtenstein  2013        173528.
5 Monaco         2013        172589.
6 Monaco         2016        168011.

The Country with the highest GDP per capita is MONACO in 2014 with a GDP per capita equivalent to 185152.5 US dollar.

c) Let’s plot the GDP per capita of MONACO over time.

highest_GDP_per_Capita |> 
  filter(Country=="Monaco") |>
  autoplot(GDP_per_Capita)
Warning: Removed 11 rows containing missing values or values outside the scale range
(`geom_line()`).

We observe that the GDP per capita in MONACO has been increasing over time in general with couple years where it declined. The sharpest drop was registered between 2008-2009.

Exercise 3.2

a) Let’s make a graph of the United States GDP from global_economy.

global_economy %>%
  filter(Country=="United States")|>
  autoplot(GDP)

We observe a strong positive trend in the United States GDP from the global_economy dataset.

  1. Let’s make a graph of Slaughter of Victorian “Bulls, bullocks and steers” in aus_livestock.
head(aus_livestock,10)
# A tsibble: 10 x 4 [1M]
# Key:       Animal, State [1]
      Month Animal                     State                        Count
      <mth> <fct>                      <fct>                        <dbl>
 1 1976 Jul Bulls, bullocks and steers Australian Capital Territory  2300
 2 1976 Aug Bulls, bullocks and steers Australian Capital Territory  2100
 3 1976 Sep Bulls, bullocks and steers Australian Capital Territory  2100
 4 1976 Oct Bulls, bullocks and steers Australian Capital Territory  1900
 5 1976 Nov Bulls, bullocks and steers Australian Capital Territory  2100
 6 1976 Dec Bulls, bullocks and steers Australian Capital Territory  1800
 7 1977 Jan Bulls, bullocks and steers Australian Capital Territory  1800
 8 1977 Feb Bulls, bullocks and steers Australian Capital Territory  1900
 9 1977 Mar Bulls, bullocks and steers Australian Capital Territory  2700
10 1977 Apr Bulls, bullocks and steers Australian Capital Territory  2300
aus_livestock |> 
  select(-State) |>
  filter(Animal=="Bulls,bullocks and steers") |>
  autoplot(Count)

c) Let’s make a graph of Victorian Electricity Demand from vic_elec

head(vic_elec)
# A tsibble: 6 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   
Electricity_Demand_plot <- vic_elec %>% autoplot(Demand) +
     labs(y="Electricity Demand in MWh", title="Half-hourly electricity demand for Victoria")
Electricity_Demand_plot

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

**d) Let’s make a graph of the Gas production from aus_production

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
Gas_production_plot <- aus_production %>% autoplot(Gas)+
  labs(y="Gas produced in Million", title="Quarterly production of Gas ")
Gas_production_plot

Exercise 3 :Why is a Box-Cox transformation unhelpful for the canadian_gas data?

canadian_gas
# A tsibble: 542 x 2 [1M]
      Month Volume
      <mth>  <dbl>
 1 1960 Jan  1.43 
 2 1960 Feb  1.31 
 3 1960 Mar  1.40 
 4 1960 Apr  1.17 
 5 1960 May  1.12 
 6 1960 Jun  1.01 
 7 1960 Jul  0.966
 8 1960 Aug  0.977
 9 1960 Sep  1.03 
10 1960 Oct  1.25 
# ℹ 532 more rows
canadian_gas |>autoplot()
Plot variable not specified, automatically selected `.vars = Volume`

lambda<- canadian_gas |>
  features(Volume, features = guerrero)

canadian_gas |>
  autoplot(box_cox(Volume, lambda))

We observe that the canadian_gas series has strong trend and seasonality but stable variance, so a Box-Cox transformation is unhelpful because it does not improve stationarity. In fact, the original plot and Box_Cox transformation plot is technically the same.