library(fpp3)
## ── Attaching packages ────────────────────────────────────────────── fpp3 0.5 ──
## ✔ tibble 3.2.1 ✔ tsibble 1.1.3
## ✔ dplyr 1.1.3 ✔ tsibbledata 0.4.1
## ✔ tidyr 1.3.0 ✔ feasts 0.3.1
## ✔ lubridate 1.9.2 ✔ fable 0.3.3
## ✔ ggplot2 3.4.3 ✔ fabletools 0.3.3
## ── 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()
library(tsibble)
library(tsibbledata)
library(ggplot2)
library(ggfortify)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.4
## ✔ purrr 1.0.2 ✔ stringr 1.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ tsibble::interval() masks lubridate::interval()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Quarterly estimates of selected indicators of manufacturing production in Australia. The time interval is quarterly.
autoplot(aus_production, Bricks) +
labs(title = 'Bricks Quarterly', x = 'Year Quarter')
## Warning: Removed 20 rows containing missing values (`geom_line()`).
Hudson Bay Company trading records for Snowshoe Hare and Canadian Lynx furs from 1845 to 1935. This data contains trade records for all areas of the company. The time interval is annually.
autoplot(pelt, Lynx) +
labs(title = 'Lynx Annually', x = 'Year')
Historical stock prices from 2014-2018 for Google, Amazon, Facebook and Apple. All prices are in $USD. The time interval is daily but displays the year.
autoplot(gafa_stock, Close) +
labs(title = 'Close Daily', x = 'Day')
vic_elec is a half-hourly of Demand: Total electricity demand in MWh. The time interval is every half hour.
autoplot(vic_elec, Demand) +
labs(title = 'Demand Every Half-Hour', x = 'Half-Hour [30m]')
gafa_stock %>%
group_by(Symbol) %>%
filter(Close==max(Close))
## # A tsibble: 4 x 8 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date Open High Low Close Adj_Close Volume
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2018-10-03 230. 233. 230. 232. 230. 28654800
## 2 AMZN 2018-09-04 2026. 2050. 2013 2040. 2040. 5721100
## 3 FB 2018-07-25 216. 219. 214. 218. 218. 58954200
## 4 GOOG 2018-07-26 1251 1270. 1249. 1268. 1268. 2405600
tute1 = read_csv('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')
mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line()
Install the USgas package. Create a tsibble from us_total with year as the index and state as the key.
library(USgas)
data("us_total")
timeseries = us_total %>%
as_tsibble(key = state, index = year)
Plot the annual natural gas consumption by state for the New England area (comprising the states of Maine, Vermont, New Hampshire, Massachusetts, Connecticut and Rhode Island).
timeseries %>%
filter(state == c('Maine', 'Vermont', 'New Hampshire', 'Massachusetts', 'Connecticut', 'Rhode Island')) %>%
ggplot(aes(x = year, y = y, colour = state)) +
geom_line() +
facet_grid(state ~., scales = 'free_y')
Download tourism.xlsx from the book website and read it into R using readxl::read_excel().
readxl::read_excel('tourism.xlsx')
## # A tibble: 24,320 × 5
## Quarter Region State Purpose Trips
## <chr> <chr> <chr> <chr> <dbl>
## 1 1998-01-01 Adelaide South Australia Business 135.
## 2 1998-04-01 Adelaide South Australia Business 110.
## 3 1998-07-01 Adelaide South Australia Business 166.
## 4 1998-10-01 Adelaide South Australia Business 127.
## 5 1999-01-01 Adelaide South Australia Business 137.
## 6 1999-04-01 Adelaide South Australia Business 200.
## 7 1999-07-01 Adelaide South Australia Business 169.
## 8 1999-10-01 Adelaide South Australia Business 134.
## 9 2000-01-01 Adelaide South Australia Business 154.
## 10 2000-04-01 Adelaide South Australia Business 169.
## # ℹ 24,310 more rows
Create a tsibble which is identical to the tourism tsibble from the tsibble package.
timeseries2 = tourism %>%
as_tsibble(key = c(Region, Purpose), index = Quarter)
Find what combination of Region and Purpose had the maximum number of overnight trips on average.
timeseries2 %>%
group_by(Region, Purpose) %>%
summarise(Trips = mean(Trips)) %>%
ungroup() %>%
filter(Trips==max(Trips))
## # A tsibble: 1 x 4 [1Q]
## # Key: Region, Purpose [1]
## Region Purpose Quarter Trips
## <chr> <chr> <qtr> <dbl>
## 1 Melbourne Visiting 2017 Q4 985.
Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
timeseries3 = tourism %>%
as_tsibble()
timeseries3 %>%
group_by(Purpose, Region) %>%
group_by(State) %>%
summarise(Trips = sum(Trips))
## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## State Quarter Trips
## <chr> <qtr> <dbl>
## 1 ACT 1998 Q1 551.
## 2 ACT 1998 Q2 416.
## 3 ACT 1998 Q3 436.
## 4 ACT 1998 Q4 450.
## 5 ACT 1999 Q1 379.
## 6 ACT 1999 Q2 558.
## 7 ACT 1999 Q3 449.
## 8 ACT 1999 Q4 595.
## 9 ACT 2000 Q1 600.
## 10 ACT 2000 Q2 557.
## # ℹ 630 more rows
autoplot(aus_production, Bricks)
## Warning: Removed 20 rows containing missing values (`geom_line()`).
gg_season(aus_production, Bricks)
## Warning: Removed 20 rows containing missing values (`geom_line()`).
gg_subseries(aus_production, Bricks)
## Warning: Removed 5 rows containing missing values (`geom_line()`).
gg_lag(aus_production, Bricks)
## Warning: Removed 20 rows containing missing values (gg_lag).
ACF(aus_production, Bricks) %>%
autoplot()
autoplot(us_gasoline, Barrels)
gg_season(us_gasoline, Barrels)
gg_subseries(us_gasoline, Barrels)
gg_lag(us_gasoline, Barrels)
ACF(us_gasoline, Barrels) %>%
autoplot()
We can see a seasonality in the Bricks from aus_production and some in the Barrels from us_gasoline. We can see a yearly cycle as the seasons change monthly. The summer months peak while the winter months plateau. As the years increase, the amount of materials increase for Barrels. The years with respect to Bricks is unusual as it increases over the years but then decreases. From this, it can be determined bricks were no longer a growing material in technological advancement. The series have strong positive correlations as shown in the lag_plots.