#2.1
# Install.packages(“fpp3”)
library(fpp3)
## Registered S3 method overwritten by 'tsibble':
## method from
## as_tibble.grouped_df dplyr
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.1 ──
## ✔ tibble 3.2.1 ✔ tsibble 1.1.6
## ✔ dplyr 1.1.4 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.1 ✔ feasts 0.4.1
## ✔ lubridate 1.9.4 ✔ fable 0.4.1
## ✔ ggplot2 3.5.1
## ── Conflicts ───────────────────────────────────────────────── fpp3_conflicts ──
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::intersect() masks base::intersect()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ✖ tsibble::setdiff() masks base::setdiff()
## ✖ tsibble::union() masks base::union()
?aus_production
## starting httpd help server ...
## done
data("aus_production")
?pelt
data("pelt")
?gafa_stock
data("gafa_stock")
?vic_elec
data("vic_elec")
library(dplyr)
aus_production %>%
autoplot(Bricks)
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

pelt %>%
autoplot(Lynx)

gafa_stock %>%
autoplot(Close)

vic_elec %>%
autoplot(Demand) +
labs(x = "Year[DUration 30 min", y = "Demand [ thousand]") +
ggtitle("Electricity Demand")

#2.2
library(dplyr)
data(gafa_stock)
gafa_stock %>% group_by(Symbol) %>%
filter(Close==max(Close)) %>%
select(Symbol, Date, Close)
## # A tsibble: 4 x 3 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date Close
## <chr> <date> <dbl>
## 1 AAPL 2018-10-03 232.
## 2 AMZN 2018-09-04 2040.
## 3 FB 2018-07-25 218.
## 4 GOOG 2018-07-26 1268.
#2.3
tute1 <- readr::read_csv("d:\\r_file\\tute1.csv")
## Rows: 100 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): Sales, AdBudget, GDP
## date (1): Quarter
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(tute1)
mytimeseries <- tute1 %>%
mutate(Quarter=yearquarter(Quarter)) %>%
as_tibble(index = Quarter)
mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
facet_grid(name ~ ., scales = "free_y")

mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line()

#2.4
#install.packages('USgas')
library(USgas)
data("us_total")
str(us_total)
## 'data.frame': 1266 obs. of 3 variables:
## $ year : int 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 ...
## $ state: chr "Alabama" "Alabama" "Alabama" "Alabama" ...
## $ y : int 324158 329134 337270 353614 332693 379343 350345 382367 353156 391093 ...
us_tot <- us_total %>%
rename(natural_gas_consumption_mcf = y)
us_total_tsib <- us_tot %>%
filter(state %in% c("Maine", "Vermont", "New Hampshire", "Massachusetts", "Connecticut", "Rhode Island")) %>%
as_tsibble(key = state, index = year)
us_total_tsib
## # A tsibble: 138 x 3 [1Y]
## # Key: state [6]
## year state natural_gas_consumption_mcf
## <int> <chr> <int>
## 1 1997 Connecticut 144708
## 2 1998 Connecticut 131497
## 3 1999 Connecticut 152237
## 4 2000 Connecticut 159712
## 5 2001 Connecticut 146278
## 6 2002 Connecticut 177587
## 7 2003 Connecticut 154075
## 8 2004 Connecticut 162642
## 9 2005 Connecticut 168067
## 10 2006 Connecticut 172682
## # ℹ 128 more rows
us_total_tsib %>% autoplot(natural_gas_consumption_mcf)

#2.5
#install.packages("readxl")
tourism <- readxl::read_excel("D:\\r_file\\tourism.xlsx")
tourism_tsi <- tourism %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(key = c(Region,
State,
Purpose),
index = Quarter)
tourism_tsi <- tourism %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(key = c(Region,
State,
Purpose),
index = Quarter)
tourism_tsic <- tourism_tsi %>%
group_by(Region,
Purpose) %>%
mutate(Avg_Trips = mean(Trips)) %>%
ungroup() %>%
filter(Avg_Trips == max(Avg_Trips)) %>%
distinct(Region,
Purpose)
max_avg_trips <- tourism %>%
group_by(Region, Purpose) %>%
summarise(avg_trips = mean(Trips)) %>%
arrange(desc(avg_trips))
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
#2.8
data("us_employment")
data("aus_production")
data("pelt")
data("PBS")
data("us_gasoline")
us_employment %>%
filter(Title == "Total Private") %>%
autoplot(Employed) +
ggtitle("Employed Autoplot")

us_employment %>%filter(Title == "Total Private") %>%
gg_season(Employed) +
ggtitle("Seasonal Decomposition Plot")

us_employment %>%
filter(Title == "Total Private") %>%
gg_subseries(Employed) +
ggtitle("Employed Subseries Plot")

us_employment %>%
filter(Title == "Total Private") %>%
gg_lag(Employed) +
ggtitle("Employed Lag Plot")

us_employment %>%
filter(Title == "Total Private") %>%
ACF(Employed) %>%
autoplot() +
ggtitle("Autocorrelation Function")

# aus_production
aus_production %>%
autoplot(Bricks) +
ggtitle("Bricks Autoplot")
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

aus_production %>%
gg_season(Bricks) +
ggtitle("Seasonal Decomposition for aus_production")
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

aus_production %>%
gg_subseries(Bricks) + ggtitle("Subseries Plot")
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_line()`).

aus_production %>%
gg_lag(Bricks) +
ggtitle("Lag Plot")
## Warning: Removed 20 rows containing missing values (gg_lag).

aus_production %>%
ACF(Bricks) %>%
autoplot() + ggtitle("Autocorrelation Function")

#pelt
pelt %>%
autoplot(Hare) + ggtitle("Autoplot")

aus_production %>%
gg_season(Bricks) +
ggtitle("Seasonal Decomposition")
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).

pelt %>%
gg_subseries(Hare)+ ggtitle("Subseries Plot")

pelt %>%
gg_lag(Hare) + ggtitle("Lag Plot")

pelt %>%
ACF(Hare) %>%
autoplot() + ggtitle("Autocorrelation Function")

#PBS
PBS %>%
filter(ATC2 == "H02") %>%
autoplot(Cost) +
ggtitle("Autoplot")

#us_gasoline
us_gasoline %>%
autoplot() + ggtitle("Autoplot for us_gasoline")
## Plot variable not specified, automatically selected `.vars = Barrels`

us_gasoline %>%
gg_season() +
ggtitle("Seasonal Decomposition")
## Plot variable not specified, automatically selected `y = Barrels`

us_gasoline %>%
gg_subseries()+ ggtitle("Subseries Plot")
## Plot variable not specified, automatically selected `y = Barrels`

us_gasoline %>%
gg_lag() + ggtitle("Lag Plot for US gasoline")
## Plot variable not specified, automatically selected `y = Barrels`

us_gasoline %>%
ACF() %>%
autoplot() + ggtitle("Autocorrelation Function")
## Response variable not specified, automatically selected `var = Barrels`
