library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(tsibbledata)
library(tsibble)
##
## Attaching package: 'tsibble'
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
library(fpp3)
## ── Attaching packages ──────────────────────────────────────────── fpp3 0.4.0 ──
## ✔ lubridate 1.8.0 ✔ fable 0.3.1
## ✔ feasts 0.2.2
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ lubridate::interval() masks tsibble::interval()
## ✖ dplyr::lag() masks stats::lag()
## ✖ tsibble::setdiff() masks base::setdiff()
## ✖ tsibble::union() masks base::union()
proof<-c(0.067, 0.133, 0.200, 0.200, 0.200, 0.133, 0.067)
gas <- tail(aus_production, 5*4) %>% dplyr::select(Gas)
head(gas)
## # A tsibble: 6 x 2 [1Q]
## Gas Quarter
## <dbl> <qtr>
## 1 221 2005 Q3
## 2 180 2005 Q4
## 3 171 2006 Q1
## 4 224 2006 Q2
## 5 233 2006 Q3
## 6 192 2006 Q4
gas %>%
autoplot()+
labs(title = " Gas Data")
## Plot variable not specified, automatically selected `.vars = Gas`
gas %>%
model(classical_decomposition(Gas,type = "multiplicative")) %>%
components() %>%
autoplot() +
ggtitle(" Gas Data")
## Warning: Removed 2 row(s) containing missing values (geom_path).
descom<-gas %>%
model(classical_decomposition(Gas,type = "multiplicative")) %>%
components()
a=mean(descom$seasonal)
b =mean(descom$trend,na.rm=TRUE)
cat(" The calculated seasonality is ", a, " and the trend is", b)
## The calculated seasonality is 1 and the trend is 216.3203
part 3: yes
gas_decom <- gas %>%
model(classical_decomposition(Gas,type = "multiplicative")) %>%
components()
gas_decom %>%
ggplot(aes(x = Quarter)) +
geom_line(aes(y = season_adjust,)) +
labs(y = "",
title = "Gas Data")
gas2 <- gas
gas2$Gas[15] <- gas2$Gas[11]+300
gas2 %>%
model(classical_decomposition(Gas,type = "multiplicative")) %>%
components() %>%
autoplot() +
ggtitle("Gas Data with outlier")
## Warning: Removed 2 row(s) containing missing values (geom_path).
gas2 %>%
model(classical_decomposition(Gas,type = "multiplicative")) %>%
components() %>%
ggplot(aes(x = Quarter)) +
geom_line(aes(y = season_adjust,))
title = ("Gas Data with 300 added ") +
scale_colour_manual(
values = c("gray"),
breaks = c( "Seasonally Adjusted"))
gas3 <- gas
gas3$Gas[20] <- gas3$Gas[10]+300
gas3 %>%
model(classical_decomposition(Gas,type = "multiplicative")) %>%
components() %>%
autoplot() +
ggtitle("Gas Data with outlier ")
## Warning: Removed 2 row(s) containing missing values (geom_path).
set.seed(23456789)
myseries <- aus_retail %>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
myseries %>%
model(classical_decomposition(Turnover,type = "multiplicative")) %>%
components() %>%
autoplot() +
ggtitle("Multiplicative decomposition of my time series")
## Warning: Removed 6 row(s) containing missing values (geom_path).
data<-ts(myseries$Turnover,start=c(1982,4), end=c(2000,12), frequency=12)
x11_dcmp <- myseries %>%
model(x11 = X_13ARIMA_SEATS(Turnover ~ x11())) %>%
components()
autoplot(x11_dcmp) +
labs(title =
"myseries using X-11.")
canadian_gas %>%
model(
STL(Volume ~ trend(window = 25) +
season(window = 10),
robust = TRUE)) %>%
components() %>%
autoplot()+
labs(title = "STL decomposition of Canadian Gas Production")
canadian_gas %>%
autoplot(Volume)+
labs(title = "Monthly Canadian Gas Production",
subtitle = "autoplot()",
y = "billionsr")
canadian_gas %>%
gg_subseries(Volume)+
labs(title = "Monthly Canadian Gas Production",
subtitle = "gg_subseries()",
y = "billions")
canadian_gas %>%
gg_season(Volume)+
labs(title = "Monthly Canadian Gas Production",
subtitle = "gg_season()",
y = "billions")
canadian_gas %>%
model(
STL(Volume ~ trend(window = 25) +
season(window = 10),
robust = TRUE)) %>%
components() %>%
autoplot()+
labs(title = "STL decomposition of Canadian Gas Production")
canadian_gas %>%
model(
STL(Volume ~ trend(window = 25) +
season(window = 10),
robust = TRUE)) %>%
components() %>%
ggplot(aes(x = Month)) +
geom_line(aes(y = season_adjust))
labs(title = "STL decomposition of Canadian Gas Production") +
scale_colour_manual(
values = c("gray"),
breaks = c( "Seasonally Adjusted"))
## NULL
canadian_gas %>%
model(x11 = X_13ARIMA_SEATS(Volume ~ x11())) %>%
components() %>%
autoplot()+
labs(title = "X-11 decomposition")
canadian_gas %>%
model(seats = X_13ARIMA_SEATS(Volume ~ seats())) %>%
components() %>%
autoplot() +
labs(title ="SEATS Decomposition of Canadian Gas Production")
they look almost the exact same except for more variation with SEATS
liquor<-aus_retail%>%
filter(Industry == "Liquor retailing" & year(Month)>= 2000)%>%
summarise(Turnover = sum(Turnover))
ggplot(data= liquor)+
geom_line(aes(x=Month, y=Turnover))
liquor <-liquor%>%
mutate(`12-MA` = slider::slide_dbl(Turnover, mean,
.before = 5, .after = 6, .complete = TRUE))%>%
mutate(`2x12-MA` = slider::slide_dbl(`12-MA`, mean,
.before = 1, .after = 0, .complete = TRUE))
liquor%>%
autoplot(Turnover) +
geom_line(aes(y =`2x12-MA`), colour = "#0072B2") +
labs(y = "Liquor retailing Turnover",
title = "Total AUS Liquor retailing Turnover")+
guides(colour = guide_legend(title = "series"))
## Warning: Removed 12 row(s) containing missing values (geom_path).
10.
Additive: 1. compute the trend cycle component 2. calculate the detrended series with yt-Tt 3. average the detrended values for the season to estimate seasonal component 4. make sure seasonal effects are normalized 5. subtract estimated season and trend components to find remainder
Multiplicative: 1. Compute the trend cycle compenent 2. calculate detrended series with yt/Tt 3.estimate seasonal compenent by averaging detrended values 4. make sure seasonal effects are normanlized 5. calculate the raminder component by dividing the estimated seasonal and trend cycle components