library(lubridate)
library(tsibble)
## Warning: package 'tsibble' was built under R version 4.3.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
library(tidyverse)
library(fpp3)
## Warning: package 'fpp3' was built under R version 4.3.3
## Warning: package 'tsibbledata' was built under R version 4.3.3
## Warning: package 'feasts' was built under R version 4.3.3
## Warning: package 'fabletools' was built under R version 4.3.3
## Warning: package 'fable' was built under R version 4.3.3
library(forecast)
## Warning: package 'forecast' was built under R version 4.3.3
library(seasonal)
## Warning: package 'seasonal' was built under R version 4.3.3
global_economy <- global_economy %>%
mutate(GDP_per_capita = GDP/Population)
global_economy %>%
autoplot(GDP_per_capita, show.legend = FALSE) +
labs(title = "GDP per Capita", y = "$US")
## Warning: Removed 3242 rows containing missing values or values outside the scale range
## (`geom_line()`).
max_gdp_country <- global_economy %>%
filter(GDP_per_capita == max(GDP_per_capita, na.rm = TRUE)) %>%
select(Country, Year, GDP_per_capita)
max_gdp_country
## # A tsibble: 1 x 3 [1Y]
## # Key: Country [1]
## Country Year GDP_per_capita
## <fct> <dbl> <dbl>
## 1 Monaco 2014 185153.
global_economy %>%
filter(Country == "Monaco") %>%
mutate(GDP_per_capita = GDP / Population) %>%
autoplot(GDP_per_capita) +
labs(title = "GDP per Capita for Monaco")
## Warning: Removed 11 rows containing missing values or values outside the scale range
## (`geom_line()`).
global_economy %>%
filter(Country == "United States") %>%
ggplot(aes(x = Year, y = GDP)) +
geom_line() +
labs(title = "United States GDP", y = "$US") +
theme_minimal()
aus_livestock %>% filter(Animal == 'Bulls, bullocks and steers') %>%
filter(State == 'Victoria') %>%
autoplot(Count) +
labs(title = "Slaughter of Victorian Bulls, bullocks and steers", y = 'Count')
aus_livestock %>%
filter(State == "Victoria", Animal == "Bulls, bullocks and steers") %>%
mutate(Quarter = yearquarter(Month)) %>%
index_by(Quarter) %>%
summarise(Count = sum(Count)) %>%
autoplot(Count)
vic_elec %>% autoplot(Demand) +
labs(title = 'Victory Electricity Demands', y = 'MWh')
vic_elec %>%
index_by(Year = year(Date)) %>% # Convert the Date to yearly format
summarise(Demand = sum(Demand)) %>% # Sum the demand for each year
autoplot(Demand) + # Plot using autoplot
labs(title = "Victorian Electricity Demand (Yearly)",
x = "Year",
y = "Demand (MW)") +
theme_minimal()
aus_production %>% autoplot(Gas) +
labs(title = 'Gas Production')
lambda <- aus_production %>%
features(Gas, features = guerrero) %>%
pull(lambda_guerrero)
aus_production %>%
autoplot(box_cox(Gas, lambda)) +
labs(y = "",
title = paste("Transformed gas production with lambda = ", round(lambda,2)))
canadian_gas %>%
autoplot(Volume) +
labs(title= "Monthly Gas Production in Canada")
lambda <- canadian_gas %>%
features(Volume, features = guerrero) %>%
pull(lambda_guerrero)
canadian_gas %>%
autoplot(box_cox(Volume, lambda)) +
labs(y = "",
title =(paste0(
"Transformed gas production with lambda = ",
round(lambda,2))))
set.seed(2001)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
myseries %>% autoplot(Turnover) +
labs(title = "Retail Data ",
y = "$AUD (Millions)")
lambda <- myseries %>%
features(Turnover, features = guerrero) %>%
pull(lambda_guerrero)
myseries %>%
autoplot(box_cox(Turnover, lambda)) +
labs(y = "",
title =(paste0(
"Transformed food services turnover with lambda = ",
round(lambda,2))))
aus_production %>% autoplot(Tobacco) +
labs(title = "*Original* Tobacco Production")
## Warning: Removed 24 rows containing missing values or values outside the scale range
## (`geom_line()`).
lambda <- aus_production %>%
features(Tobacco, features = guerrero)%>%
pull(lambda_guerrero)
aus_production %>% autoplot(box_cox(Tobacco,lambda)) +
labs(title = paste("*Transformed* Tobacco Production with lamda =", round(lambda, 2)))
## Warning: Removed 24 rows containing missing values or values outside the scale range
## (`geom_line()`).
economy_pass <- ansett %>%
filter(Class == "Economy",
Airports == "MEL-SYD")
autoplot(economy_pass, Passengers)+
labs(title = "*Original* Economy class Passengers Between Melbourne and Sydney")
lambda <- economy_pass %>%
features(Passengers, features = guerrero) %>%
pull(lambda_guerrero)
economy_pass %>%
autoplot(box_cox(Passengers, lambda)) +
labs(title = paste("**Transformed** Economy Class Passengers Count with lamda =", round(lambda, 2)))
pedestrian %>% filter(Sensor =='Southern Cross Station') %>% autoplot(Count)+
labs(title = "*Original* Pedestrian Count Hourly")
pedestrian_week <- pedestrian %>%
mutate(Week = yearweek(Date)) %>%
index_by(Week) %>%
summarise(Count = sum(Count))
pedestrian_week %>% autoplot(Count)+
labs(title = "*Transformed 1* Pedestrian Count Weekly")
lambda <- pedestrian_week %>%
features(Count, features = guerrero) %>%
pull(lambda_guerrero)
pedestrian_week %>% autoplot(box_cox(Count,lambda)) +
labs(title = paste("*Transformed 2* Weekly Pedestrian Count with lamda =", round(lambda, 2)))
gas <- tail(aus_production, 5*4) |> select(Gas)
gas %>% autoplot(Gas) +
labs(title = "Australia Gas Production")
gas %>% model(classical_decomposition(Gas, type = "multiplicative")) %>%
components() %>%
autoplot()
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_line()`).
gas %>% model(classical_decomposition(Gas, type = "multiplicative")) %>%
components(gas_season) %>%
as_tsibble() %>%
autoplot(Gas, colour = "darkgray") +
geom_line(aes(y=season_adjust), colour = "darkred") +
labs(title = "Seasonally Adjusted Gas Production")
gas_withoutlier <- gas
gas_withoutlier$Gas[10] <- gas_withoutlier$Gas[10] + 300 # Add 300 to the 10th observation
gas_withoutlier %>%
model(classical_decomposition(Gas, type = "multiplicative")) %>%
components() %>%
as_tsibble() %>%
autoplot(Gas, colour = "darkgray") +
geom_line(aes(y=season_adjust), colour = "darkblue") +
labs(title = "Seasonally Adjusted Data with an Outlier")
gas_withoutlier_end <- gas
gas_withoutlier_end$Gas[nrow(gas)] <- gas_withoutlier_end$Gas[nrow(gas)] + 300
gas_withoutlier_end %>%
model(classical_decomposition(Gas, type = "multiplicative")) %>%
components() %>%
as_tsibble() %>%
autoplot(Gas, colour = "darkgray") +
geom_line(aes(y=season_adjust), colour = "darkgreen") +
labs(title = "Seasonally Adjusted Data with an Outlier")
myseries %>% #### myseries was created for excercise 4
model(x11 = X_13ARIMA_SEATS(Turnover ~ x11())) %>%
components() %>%
autoplot()+
labs(title = "X-11 Decomposition of Retail Time Series")
#### The trend is generally upward, with some flattening periods in the
middle (around 1990 to 2005) and a notable rise in recent years. There
are some irregularities in the early part of the series (1980s) and
around 2020, where larger spikes are observed. The unusual features in
2020 could be attributed to the COVID-19 pandemic, which impacted retail
activity in many regions.