library(dplyr)
library(feasts)
library(fpp3)
library(ggfortify)
library(httr)
library(readr)
library(readxl)
library(seasonal)
library(stats)
library(tsibble)
library(tsibbledata)
library(tidyr)
library(USgas)
  1. Consider the GDP information in global_economy. Plot the GDP per capita for each country over time. Which country has the highest GDP per capita? How has this changed over time?

Monaco. It has grown over time and has high GDP.

global_economy %>% select(Country,GDP,Population,Year) %>% autoplot(GDP/Population,show.legend=FALSE)
## Warning: Removed 3242 rows containing missing values (`geom_line()`).

global_economy %>%
  mutate(GDP_per_capita = GDP / Population) %>%
  filter(GDP_per_capita == max(GDP_per_capita, na.rm = TRUE)) %>%
  select(Country, GDP_per_capita)
## # A tsibble: 1 x 3 [1Y]
## # Key:       Country [1]
##   Country GDP_per_capita  Year
##   <fct>            <dbl> <dbl>
## 1 Monaco         185153.  2014
global_economy %>%
  filter(Country == "Monaco") %>%
  autoplot(GDP/Population) +
  labs(y = "$US")
## Warning: Removed 11 rows containing missing values (`geom_line()`).

2. For each of the following series, make a graph of the data. If transforming seems appropriate, do so and describe the effect.
United States GDP from global_economy.
Transformation did not seem to be needed much here. Growing the population did not affect the GDP.

global_economy %>%
  filter(Country == "United States") %>%
  autoplot(GDP / 10 ^ 12) +
  labs(y = "$US (in trillions)") 

Slaughter of Victorian “Bulls, bullocks and steers” in aus_livestock.
We can see the count decreases over time. No transformation.

aus_livestock %>% filter(Animal == "Bulls, bullocks and steers",
         State == "Victoria") %>%
  autoplot(Count)

Victorian Electricity Demand from vic_elec.
Transformation was done to show electricity demand measured by days. We can see there are seasonal changes in the electricity demands.

ve <- vic_elec %>%
  group_by(Date) %>%
  mutate(Demand = sum(Demand)) %>%
  distinct(Date, Demand)

ve %>% 
  as_tsibble(index = Date) %>%
  autoplot(Demand)

Gas production from aus_production.
Box cox is used here because the seasonality and trends are constant.

Non-transformed

aus_production %>%
  autoplot(Gas)

Box Cox

l <- aus_production %>%
  features(Gas, features = guerrero) %>%
  pull(lambda_guerrero)

aus_production %>%
  autoplot(box_cox(Gas, l))

  1. Why is a Box-Cox transformation unhelpful for the canadian_gas data?
    The Box-Cox transformation does allow for the variation to be constant, thus making it unhelpful. Variation increases and decreases at certain years.

Non-transformed

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

Box Cox

box_cox = canadian_gas %>%
  autoplot(box_cox(Volume, cg %>% features(Volume,features=guerrero) %>% pull(lambda_guerrero)))
  1. What Box-Cox transformation would you select for your retail data (from Exercise 8 in Section 2.10)?
    The Box-Cox that uses guerrero with the lambda.
set.seed(123456)
series <- aus_retail %>%
  filter(`Series ID` == sample(aus_retail$`Series ID`,1)) 

autoplot(series, Turnover)

l <- series %>%
  features(Turnover, features = guerrero) %>%
  pull(lambda_guerrero)

series %>%
  autoplot(box_cox(Turnover, l))

  1. For the following series, find an appropriate Box-Cox transformation in order to stabilise the variance. Tobacco from aus_production, Economy class passengers between Melbourne and Sydney from ansett, and Pedestrian counts at Southern Cross Station from pedestrian.

Tobacco from aus_production

autoplot(aus_production, Tobacco)
## Warning: Removed 24 rows containing missing values (`geom_line()`).

Transformed: Box Cox is not useful since there is a downward trend in data.

l <- aus_production %>%
  features(Tobacco, features = guerrero) %>%
  pull(lambda_guerrero)

aus_production %>%
  autoplot(box_cox(Tobacco, l))
## Warning: Removed 24 rows containing missing values (`geom_line()`).

Economy class passengers between Melbourne and Sydney.

autoplot(ansett %>%
  filter(Class == "Economy",
         Airports == "MEL-SYD"), Passengers)

Transformed
We see variaion better after the transformation with lambda

l <- ansett %>%
  filter(Class == "Economy",
         Airports == "MEL-SYD") %>%
  features(Passengers, features = guerrero) %>%
  pull(lambda_guerrero)

ansett %>%
  filter(Class == "Economy",
         Airports == "MEL-SYD") %>%
  autoplot(box_cox(Passengers, l))

Pedestrian counts at Southern Cross Station from pedestrian

ap <- aus_production %>% select(Tobacco)
ap%>% autoplot()
## Plot variable not specified, automatically selected `.vars = Tobacco`
## Warning: Removed 24 rows containing missing values (`geom_line()`).

Transformed

alambda <- ap %>% features(Tobacco,features=guerrero) %>% pull(lambda_guerrero)
ap %>% autoplot(box_cox(Tobacco, alambda))
## Warning: Removed 24 rows containing missing values (`geom_line()`).

Ansett

ansett %>% filter(Class=='Economy',Airports=='MEL-SYD') %>% select(Passengers) %>% autoplot()
## Plot variable not specified, automatically selected `.vars = Passengers`

Transformed

ansett %>% filter(Class=='Economy',Airports=='MEL-SYD') %>% select(Passengers) %>% autoplot(box_cox(Passengers, ansett %>% filter(Class=='Economy',Airports=='MEL-SYD') %>% select(Passengers) %>% features(Passengers,features=guerrero) %>% pull(lambda_guerrero)))

Pedestrian

pedestrian %>% filter(Sensor=='Southern Cross Station') %>% select(Count) %>% autoplot()
## Plot variable not specified, automatically selected `.vars = Count`

Transformed

pedestrian %>% filter(Sensor=='Southern Cross Station') %>% select(Count) %>% autoplot(box_cox(Count, pedestrian %>% filter(Sensor=='Southern Cross Station') %>% select(Count) %>% features(Count,features=guerrero) %>% pull(lambda_guerrero)))

  1. Consider the last five years of the Gas data from aus_production.
    gas <- tail(aus_production, 5*4) |> select(Gas)
  1. Plot the time series. Can you identify seasonal fluctuations and/or a trend-cycle?
    There is constant variance through a set of 1 year cycles.
gas <- tail(aus_production, 5*4) %>% select(Gas)
autoplot(gas, Gas)

b. Use classical_decomposition with type=multiplicative to calculate the trend-cycle and seasonal indices.

gd <- gas %>% model(classical_decomposition(Gas, type = "multiplicative")) 
components(gd) %>% autoplot() 
## Warning: Removed 2 rows containing missing values (`geom_line()`).

c. Do the results support the graphical interpretation from part a?
Yes, there is an upward trend where seasonality increases after the first quarter but goes back down after third quarter.
d. Compute and plot the seasonally adjusted data.

components(gd) %>%
  as_tsibble() %>%
  autoplot(Gas, colour = "black") +
  geom_line(aes(y=season_adjust), colour = "blue")

e. Change one observation to be an outlier (e.g., add 300 to one observation), and recompute the seasonally adjusted data. What is the effect of the outlier?
The outlier leads to a greater increase in one particular area and then goes back previous seasonality.

gas %>%
  mutate(Gas = ifelse(Gas == 245, Gas + 300, Gas)) %>%
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components() %>%
  as_tsibble() %>%
  autoplot(Gas, colour = "black") +
  geom_line(aes(y=season_adjust), colour = "blue")

  1. Does it make any difference if the outlier is near the end rather than in the middle of the time series?
    No because there is still a substantial increase where the outlier is.
gas %>%
  mutate(Gas = ifelse(Gas == 236, Gas + 300, Gas)) %>%
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components() %>%
  as_tsibble() %>%
  autoplot(Gas, colour = "black") +
  geom_line(aes(y=season_adjust), colour = "blue")

  1. Recall your retail time series data (from Exercise 8 in Section 2.10). Decompose the series using X-11. Does it reveal any outliers, or unusual features that you had not noticed previously?
    Seasonality appears to be more constant with X-11. Variability appears to decrease at the end of the data.
autoplot(series %>%
  model(x11 = X_13ARIMA_SEATS(Turnover ~ x11())) %>%
  components())

  1. Figures 3.19 and 3.20 show the result of decomposing the number of persons in the civilian labour force in Australia each month from February 1978 to August 1995.
  1. Write about 3–5 sentences describing the results of the decomposition. Pay particular attention to the scales of the graphs in making your interpretation.
    This decomposition shows that there is an upward trend. The seasonality is constant but it is small compared to the remainder. They don’t however show major deviation in the trends.
  2. Is the recession of 1991/1992 visible in the estimated components?
    Yes. We can see this in the remainder component.