library(fpp3)
library(plotly)
library(datasets)
library(seasonal)
library(tsibble)

3.1

Consider the GDP information in global_economy.

globalEcon <- global_economy
globalEcon <- globalEcon %>% mutate(GDP_Per_Capita = GDP/Population)

Plot the GDP per capita for each country over time.

globalEcon %>% select(Country,GDP_Per_Capita,Year) %>% autoplot(GDP_Per_Capita,show.legend=FALSE)

Which country has the highest GDP per capita?

max <- which.max(globalEcon$GDP_Per_Capita)
paste0('Country: ',globalEcon$Country[max],', GDP Per Capita: ',round(globalEcon$GDP_Per_Capita[max],0))
## [1] "Country: Monaco, GDP Per Capita: 185153"

How has this changed over time?

globalEcon %>% filter(Country == globalEcon$Country[which.max(GDP_Per_Capita)]) %>% autoplot(GDP_Per_Capita)

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

gdp <- globalEcon %>% filter(Country == 'United States') %>% select(GDP)
gdp %>% autoplot()

gdplam <- gdp %>% features(GDP,features=guerrero) %>% pull(lambda_guerrero)
gdp %>%
  autoplot(box_cox(GDP, gdplam))

The Box-Cox transformation is appropriate for the US GDP series as the it smoothens the series.

Slaughter of Victorian “Bulls, bullocks and steers” in aus_livestock.

bbs <- aus_livestock %>% filter(State == 'Victoria', Animal == 'Bulls, bullocks and steers')
bbs %>% autoplot()

bbslam <- bbs %>% features(Count,features=guerrero) %>% pull(lambda_guerrero)
bbs %>%
  autoplot(box_cox(Count, bbslam))

The Box-Cox transformation is not appropriate for the slaughter of Victorian “Bulls, bullocks, and steers” series as it is not able to make the series stationary due to irregular trends and nonconstant variance in the data.

Victorian Electricity Demand from vic_elec.

demand <- vic_elec %>% select(Demand)
demand %>% autoplot()

dlam <- demand %>% features(Demand,features=guerrero) %>% pull(lambda_guerrero)
demand %>%
  autoplot(box_cox(Demand, dlam))

The Box-Cox transformation is not appropriate for the Victorian Electricity Demand series due to irregular trends and outliers in the data.

Gas production from aus_production.

gas_prod <- aus_production %>% select(Gas)
gas_prod %>% autoplot()

gplam <- gas_prod %>% features(Gas,features=guerrero) %>% pull(lambda_guerrero)
gas_prod %>%
  autoplot(box_cox(Gas, gplam))

The Box-Cox transformation is appropriate for the Gas production series. The transformation shows a steeper spike in gas trends around the beginning than the original and we a steady growth in gas trends towards the end.

3.3

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

cgas <- canadian_gas
autoplot(cgas)

Box-Cox transform

clam = cgas %>% features(Volume,features=guerrero) %>% pull(lambda_guerrero)
cgas %>%
  autoplot(box_cox(Volume, clam))

The data set has nonconstant variability, thus the Box-Cox transformation is not able to make the time series stationary.

3.4

What Box-Cox transformation would you select for your retail data (from Exercise 8 in Section 2.10)?

set.seed(246810)
myseries <- aus_retail %>%
  filter(`Series ID` == 'A3349849A')
myseries %>% autoplot()

alam <- myseries %>% features(Turnover,features=guerrero) %>% pull(lambda_guerrero)
 
myseries %>% autoplot(box_cox(Turnover, alam))

paste0("lambda = ",round(alam,2))
## [1] "lambda = 0.51"

The data is transformed using the Box-Cox transformation with the ‘guerrero’ feature, with lambda being 0.51.

3.5

For the following series, find an appropriate Box-Cox transformation in order to stabilize the variance.

Tobacco from aus_production

tobacco <- aus_production %>% select(Tobacco)
tlam <- tobacco %>% features(Tobacco,features=guerrero) %>% pull(lambda_guerrero)
tobacco %>% autoplot()

tobacco %>% autoplot(box_cox(Tobacco, tlam))

Economy class passengers between Melbourne and Sydney from ansett

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

econPass %>% autoplot(box_cox(Passengers, elam))

Pedestrian counts at Southern Cross Station from pedestrian.

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

ped_cnt %>% autoplot(box_cox(Count, plam))

3.7

Consider the last five years of the Gas data from aus_production.

gas <- tail(aus_production, 5*4) %>% select(Gas)

Plot the time series.

gas %>% autoplot()

Can you identify seasonal fluctuations and/or a trend-cycle?

There is a yearly cyclic trend that increases over time. During the yearly cycle we see an increase after the first quarter with a peak platform around the third quarter and then a decrease.

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

gas %>% 
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components() %>%
  autoplot()

Do the results support the graphical interpretation from part a?

The results support the interpretation. The trend shows an increase and seasonality as observed in part a.

Compute and plot the seasonally adjusted data.

gas %>%
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components() %>% select(season_adjust) %>%
  autoplot()

Change one observation to be an outlier (e.g., add 300 to one observation), and recompute the seasonally adjusted data.

go <- gas
go$Gas[10] <- go$Gas[10] ++ 500
go %>%
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components() %>% select(season_adjust) %>%
  autoplot()

What is the effect of the outlier?

There is a spike in the trend where the outlier is.

Does it make any difference if the outlier is near the end rather than in the middle of the time series?

gm <- gas
gm$Gas[10] <- gm$Gas[10] ++ 500
gm %>%
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components() %>% select(season_adjust) %>%
  autoplot()

ge <- gas
ge$Gas[18] <- ge$Gas[18] ++ 500
ge %>%
  model(classical_decomposition(Gas, type = "multiplicative")) %>%
  components() %>% select(season_adjust) %>%
  autoplot()

It does not make a difference if the outlier is in the middle or the end. There will be a spike in the trend at the position of the outlier.

3.8

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?

decomp <- myseries %>%
  model(x11 = X_13ARIMA_SEATS(Turnover ~ x11())) %>%
  components()
autoplot(decomp)

The series decomposition using x-11 does not show any irregularities that were not observed previously.

3.9

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.

Write about 3–5 sentences describing the results of the decomposition. Pay particular attention to the scales of the graphs in making your interpretation.

The trend component of the decomposition shows a smooth increasing trend. The seasonality component shows constant variability with a consistent minima and maxima. The remainder component shows outliers in the data. The most striking one being between 1991 and 1992 which is caused by the recession that occurred during the two years.

Is the recession of 1991/1992 visible in the estimated components?

The 1991/1992 recession is visible by the steep decrease in the remainder component of the estimated components.