library(fpp3)
## ── Attaching packages ──────────────────────────────────────────── fpp3 1.0.3 ──
## ✔ tibble 3.3.1 ✔ tsibble 1.2.0
## ✔ dplyr 1.2.1 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.2 ✔ ggtime 1.0.0
## ✔ lubridate 1.9.5 ✔ feasts 0.5.0
## ✔ ggplot2 4.0.3 ✔ fable 0.5.0
## ── 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()
Exploring the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, Demand from vic_elec.
data(aus_production, pelt, gafa_stock, vic_elec)
Use ? (or help()) to find out about the data in each series.
?aus_production
## starting httpd help server ... done
?pelt
?gafa_stock
?vic_elec
What is the time interval of each series?
aus_production: quarterly pelt: annually gafa_stock: daily vic_elec: half-hourly
Use autoplot() to produce a time plot of each series.
(p1 <- autoplot(aus_production, Bricks))
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).
(p2 <- autoplot(pelt, Lynx))
(p3 <- autoplot(gafa_stock, Close))
(p4 <- autoplot(vic_elec, Demand))
Regarding the last plot, modifying both the axis labels and title.
p4 +
labs(title = 'Electricity Demand Over Time for Victoria, Australia',
y = 'Total Demand (MWh)',
x = 'Time (Half-Hourly)')
Using filter() to find out days corresponding to the peak closing price for the four stocks in gafa_stock.
gafa_stock |>
group_by(Symbol) |>
filter(Close == max(Close)) |>
knitr::kable()
| Symbol | Date | Open | High | Low | Close | Adj_Close | Volume |
|---|---|---|---|---|---|---|---|
| AAPL | 2018-10-03 | 230.05 | 233.470 | 229.78 | 232.07 | 230.2755 | 28654800 |
| AMZN | 2018-09-04 | 2026.50 | 2050.500 | 2013.00 | 2039.51 | 2039.5100 | 5721100 |
| FB | 2018-07-25 | 215.72 | 218.620 | 214.27 | 217.50 | 217.5000 | 58954200 |
| GOOG | 2018-07-26 | 1251.00 | 1269.771 | 1249.02 | 1268.33 | 1268.3300 | 2405600 |
Obtained the file called tute1.csv from the course website, open it in MS Excel and review its contents. Locating four variable columns. Columns B through D containing quarterly series, named as Sales, AdBudget and GDP. Sales features the quarterly sales for a small company over the period 1981-2005. AdBudget stands for the advertising budget and finally GDP represents the gross domestic product. All series have been adjusted for inflation.
tute1 <- readr::read_csv("https://raw.githubusercontent.com/sturrm/class_files/refs/heads/main/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.
mytimeseries <- tute1 |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index = Quarter)
mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
facet_grid(name ~ ., scales = "free_y")
Checking status without including facet_grid().
Without facet grid(), each variable is shown alongside within the same grid and the same scale. Displaying these variables hand-in-hand does not create any value to this analysis, the individual trends reveal themselves nicely for each variable when they are shown on their own.
The USgas package prsents data regarding the usage of natural gas for the US.
library(USgas)
(us_total_ts <- as_tsibble(us_total, index=year, key=state))
## # A tsibble: 1,266 x 3 [1Y]
## # Key: state [53]
## year state y
## <int> <chr> <int>
## 1 1997 Alabama 324158
## 2 1998 Alabama 329134
## 3 1999 Alabama 337270
## 4 2000 Alabama 353614
## 5 2001 Alabama 332693
## 6 2002 Alabama 379343
## 7 2003 Alabama 350345
## 8 2004 Alabama 382367
## 9 2005 Alabama 353156
## 10 2006 Alabama 391093
## # ℹ 1,256 more rows
3> Showing the annual natural gas usage by state for the New England area (comprising the states of Maine, Vermont, New Hampshire, Massachusetts, Connecticut and Rhode Island).
new_england <- c('Maine', 'Vermont', 'New Hampshire', 'Massachusetts', 'Connecticut', 'Rhode Island')
us_total_ts |>
filter(state %in% new_england) |>
autoplot() +
labs(title = "Natural Gas Consumption for New England States (1997-2019)",
x = 'Year',
y = 'Gas Consumption (Million Cubic Feet)') +
scale_y_continuous(label = scales::comma)
## Plot variable not specified, automatically selected `.vars = y`
tourism_data <- read.csv('https://raw.githubusercontent.com/sturrm/class_files/refs/heads/main/tourism.csv')
head(tourism_data)
## Quarter Region State Purpose Trips
## 1 1998-01-01 Adelaide South Australia Business 135.0777
## 2 1998-04-01 Adelaide South Australia Business 109.9873
## 3 1998-07-01 Adelaide South Australia Business 166.0347
## 4 1998-10-01 Adelaide South Australia Business 127.1605
## 5 1999-01-01 Adelaide South Australia Business 137.4485
## 6 1999-04-01 Adelaide South Australia Business 199.9126
data(tourism)
head(tourism)
## # A tsibble: 6 x 5 [1Q]
## # Key: Region, State, Purpose [1]
## Quarter Region State Purpose Trips
## <qtr> <chr> <chr> <chr> <dbl>
## 1 1998 Q1 Adelaide South Australia Business 135.
## 2 1998 Q2 Adelaide South Australia Business 110.
## 3 1998 Q3 Adelaide South Australia Business 166.
## 4 1998 Q4 Adelaide South Australia Business 127.
## 5 1999 Q1 Adelaide South Australia Business 137.
## 6 1999 Q2 Adelaide South Australia Business 200.
keys = c('Region', 'State', 'Purpose')
tourism_ts <- tourism_data |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index=Quarter, key=all_of(keys))
identical(tourism, tourism_ts)
## [1] TRUE
tourism_data |>
group_by(Region, Purpose) |>
summarize(avg_trips = mean(Trips)) |>
arrange(desc(avg_trips)) |>
head(1) |>
knitr::kable()
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by Region and Purpose.
## ℹ Output is grouped by Region.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(Region, Purpose))` for per-operation grouping
## (`?dplyr::dplyr_by`) instead.
| Region | Purpose | avg_trips |
|---|---|---|
| Sydney | Visiting | 747.27 |
trips_by_state_ts <- tourism_data |>
group_by(Quarter, State) |>
summarize(total_trips = sum(Trips)) |>
mutate(Quarter = yearquarter(Quarter)) |>
as_tsibble(index=Quarter, key=State)
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by Quarter and State.
## ℹ Output is grouped by Quarter.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(Quarter, State))` for per-operation grouping
## (`?dplyr::dplyr_by`) instead.
head(trips_by_state_ts)
## # A tsibble: 6 x 3 [1Q]
## # Key: State [1]
## # Groups: @ Quarter [6]
## Quarter State total_trips
## <qtr> <chr> <dbl>
## 1 1998 Q1 ACT 551.
## 2 1998 Q2 ACT 416.
## 3 1998 Q3 ACT 436.
## 4 1998 Q4 ACT 450.
## 5 1999 Q1 ACT 379.
## 6 1999 Q2 ACT 558.
Utlizing the following graph functions: autoplot(), gg_season(), gg_subseries(), gg_lag(), ACF() and exploring themes from the following time series: “Total Private” Employed from us_employment, Bricks from aus_production, Hare from pelt, “H02” Cost from PBS, and Barrels from us_gasoline.
Employment
us_employment |>
filter(Title == 'Total Private') |>
autoplot(Employed) +
labs(title = 'Autoplot')
Bricks
aus_production |>
autoplot(Bricks) +
labs(title = 'Autoplot')
## Warning: Removed 20 rows containing missing values or values outside the scale range
## (`geom_line()`).
Hare
pelt |>
autoplot(Hare) +
labs(title = 'Autoplot')
Cost
PBS |>
filter(ATC2 == 'H02') |>
autoplot(Cost) +
labs(title = 'Autoplot')
Barrels
us_gasoline |>
autoplot(Barrels) +
labs(title = 'Autoplot')
us_gasoline |>
gg_season(Barrels) +
labs(title = 'Seasonal Decomposition')
us_gasoline |>
gg_subseries(Barrels) +
labs(title = 'Subseries Plot')
An upward trend can be spotted herein. In terms of seasonality apsect, any visible trend is not clearly manifested, still an uptick can be spotted for the weeks stating June and ending September.