library(fpp3)
Explore 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)
?aus_production
?pelt
?gafa_stock
?vic_elec
aus_production: quarterlypelt: annuallygafa_stock: dailyvic_elec: half-hourly(p1 <- autoplot(aus_production, Bricks))
## Warning: Removed 20 rows containing missing values (`geom_line()`).
(p2 <- autoplot(pelt, Lynx))
(p3 <- autoplot(gafa_stock, Close))
(p4 <- autoplot(vic_elec, Demand))
p4 +
labs(title = 'Electricity Demand Over Time for Victoria, Australia',
y = 'Total Demand (MWh)',
x = 'Time (Half-Hourly)')
Use filter() to find what days corresponded to the peak
closing price for each of 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 |
Download the file tute1.csv from the book website, open
it in Excel (or some other spreadsheet application), and review its
contents. You should find four columns of information. Columns B through
D each contain a quarterly series, labelled Sales,
AdBudget and GDP. Sales contains
the quarterly sales for a small company over the period 1981-2005.
AdBudget is the advertising budget and GDP is
the gross domestic product. All series have been adjusted for
inflation.
tute1 <- readr::read_csv("https://raw.githubusercontent.com/ShanaFarber/cuny-sps/master/DATA_624/Homeworks/Data/tute1.csv")
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")
Check what happens when you don’t include
facet_grid().
mytimeseries |>
pivot_longer(-Quarter) |>
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line()
When you do not use facet grid(), each variable is
plotted together on the same grid with the same scale. Plotting these
variables together does not add any value to our analysis, and we can
more easily see the individual trends for each variable when they are
plotted on their own.
The USgas package contains data on the demand for
natural gas in the US.
USgas package.#install.packages('USgas')
library(USgas)
us_total with year
as the index and state as the key.(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
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.xlsx from the book website and read it
into R using readxl::read_excel().tourism_data <- read.csv('https://raw.githubusercontent.com/ShanaFarber/cuny-sps/master/DATA_624/Homeworks/Data/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
tourism
tsibble from the tsibble package.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
Region and
Purpose had the maximum number of overnight trips on
average.tourism_data |>
group_by(Region, Purpose) |>
summarize(avg_trips = mean(Trips)) |>
arrange(desc(avg_trips)) |>
head(1) |>
knitr::kable()
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
| 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 grouped output by 'Quarter'. You can override using the
## `.groups` argument.
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.
Use the following graphics functions: autoplot(),
gg_season(), gg_subseries(),
gg_lag(), ACF() and explore features 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.
us_employment |>
filter(Title == 'Total Private') |>
autoplot(Employed) +
labs(title = 'Autoplot')
us_employment |>
filter(Title == 'Total Private') |>
gg_season(Employed) +
labs(title = 'Seasonal Decomposition')
us_employment |>
filter(Title == 'Total Private') |>
gg_subseries(Employed) +
labs(title = 'Subseries Plot')
us_employment |>
filter(Title == 'Total Private') |>
gg_lag(Employed) +
labs(title = 'Lag Plot')
us_employment |>
filter(Title == 'Total Private') |>
ACF(Employed) |>
autoplot() +
labs(title = 'Autocorrelation')
From the plots, we can see that there is an apparent upward trend in employment and some seasonality. There appears to be a slight increase in the earlier months of the year, from January until about June, and then a leveling out of employment. There is a positive correlation across all lag plots.There are several dips in employment in the 70s, 80s, 90s, and early 2000s.
aus_production |>
autoplot(Bricks) +
labs(title = 'Autoplot')
## Warning: Removed 20 rows containing missing values (`geom_line()`).
aus_production |>
gg_season(Bricks) +
labs(title = 'Seasonal Decomposition')
## Warning: Removed 20 rows containing missing values (`geom_line()`).
aus_production |>
gg_subseries(Bricks) +
labs(title = 'Subseries Plot')
## Warning: Removed 5 rows containing missing values (`geom_line()`).
aus_production |>
gg_lag(Bricks, geom='point') +
labs(title = 'Lag Plot')
## Warning: Removed 20 rows containing missing values (gg_lag).
aus_production |>
ACF(Bricks) |>
autoplot() +
labs(title = 'Autocorrelation')
The Australian brick production data appears quite cyclical. It starts out with a clear upward trend until about the early to mid 70s and from then on there are severe dips and then increases. Seasonally, brick production seems to increase from the first to the third quarter and then decrease. The largest dips in production are in the mid 70s and early 80s.
pelt |>
autoplot(Hare) +
labs(title = 'Autoplot')
pelt |>
gg_lag(Hare, geom='point') +
labs(title = 'Lag Plot')
pelt |>
ACF(Hare) |>
autoplot() +
labs(title = 'Autocorrelation')
The hare data is very cyclic. Plotting the autocorrelation shows about a 5 year cycle (5 years decreasing and then five years increasing). The largest increases were in the 1860s and 1880s.
PBS |>
filter(ATC2 == 'H02') |>
autoplot(Cost) +
labs(title = 'Autoplot')
PBS |>
filter(ATC2 == 'H02') |>
gg_season(Cost) +
labs(title = 'Seasonal Decomposition')
Concessional co-payments seem to be at their highest in the middle of the year, from around March to August. Concessional safety net and general safety net both experience the opposite, where they are at their lowest in these months.
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')
There seems to be pretty much an upward trend. As far as seasonality goes, there does not seem to be such a clear trend, although there does seem to be some elevation in the weeks between June and September.