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)
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))
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)))
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))
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)))
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")
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")
autoplot(series %>%
model(x11 = X_13ARIMA_SEATS(Turnover ~ x11())) %>%
components())