Submit exercises 2.1, 2.2, 2.3, 2.4, 2.5 and 2.8 from the Hyndman online Forecasting book. (https://oteinsom/fpp3/)
library(fpp3)
Explore the following four time series: Bricks from aus_production, Lynx from pelt, Close from gafa_stock, Demand from vic_elec.
a.Use ? (or help()) to find out about the data in each series. b.What is the time interval of each series? c.Use autoplot() to produce a time plot of each series. d.For the last plot, modify the axis labels and title.
Part 1: Bricks from aus_production a.
invisible(?aus_production)
Bricks: Clay brick production in millions of bricks.
c.and d.
autoplot(aus_production, Bricks)+
labs(title = "Quarterly Brick Production in Australia",
y = "Bricks (millions)",
x = "Time (Year and Quarter 1)")
## Warning: Removed 20 rows containing missing values (`geom_line()`).
d.
Part 2:Lynx from pelt a.
invisible(?pelt)
Lynx: The number of Canadian Lynx pelts traded.
b.The time intervals are annual durations.
autoplot(pelt, Lynx)+
labs(title = "Canadian Lynx Pelts Traded",
x = "Time (Year)")
Part3:Close from gafa_stock a.
invisible(?gafa_stock)
Close: The closing price for the stock.
b.The time intervals are day durations.
autoplot(gafa_stock, Close)+
labs(title = "Closing Price of GAFA stock from 2014-2018",
y = "Close ($USD)",
x = "Time (Day)")
Part4: Demand from vic_elec a.
invisible(?vic_elec)
Demand: Total electricity demand in MWh.
b.The time intervals are half-hour durations. c. and d.
autoplot(vic_elec, Demand)+
labs(title = "Electricity Demand for Victoria, Australia",
y = "Demand (MWh)",
x = "Time (30mins)")
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))%>%
select(Symbol,Date)
## # A tsibble: 4 x 2 [!]
## # Key: Symbol [4]
## # Groups: Symbol [4]
## Symbol Date
## <chr> <date>
## 1 AAPL 2018-10-03
## 2 AMZN 2018-09-04
## 3 FB 2018-07-25
## 4 GOOG 2018-07-26
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("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)
b.Convert the data to time series
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()
Without the facet_grid() function, the categorical data is on the same
scale. This makes it a lot harder to see trends in the time series
between the 3 values.
The USgas package contains data on the demand for natural gas in the US.
a.Install the USgas package.
library(USgas)
## Warning: package 'USgas' was built under R version 4.2.3
invisible(?us_total)
b.Create a tsibble from us_total with year as the index and state as the key.
timeseries <- us_total %>%
as_tsibble(key = state ,index = year)
c.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 %in% c("Maine", "Vermont", "New Hampshire", "Massachusetts",
"Connecticut", "Rhode Island")) %>%
ggplot(aes(x = year, y = y, color=state)) +
labs(y= " Total Gas (Million cubic feet)")+
geom_line() +
facet_grid(state ~ ., scales="free_y")
a.Download tourism.xlsx from the book website and read it into R using readxl::read_excel().
tourism <- readxl::read_excel("tourism.xlsx")
b.Create a tsibble which is identical to the tourism tsibble from the tsibble package.
invisible(??tourism)
tourism_tsibble <- tourism %>%
mutate(Quarter = yearquarter(Quarter)) %>%
as_tsibble(key = c(Region, State, Purpose),
index = Quarter)
c.Find what combination of Region and Purpose had the maximum number of overnight trips on average.
(max_ave_trips <- tourism %>%
group_by(Region, Purpose) %>%
summarize(AverageOvernightTrips = mean(Trips)) %>%
arrange(desc(AverageOvernightTrips)))
## # A tibble: 304 × 3
## # Groups: Region [76]
## Region Purpose AverageOvernightTrips
## <chr> <chr> <dbl>
## 1 Sydney Visiting 747.
## 2 Melbourne Visiting 619.
## 3 Sydney Business 602.
## 4 North Coast NSW Holiday 588.
## 5 Sydney Holiday 550.
## 6 Gold Coast Holiday 528.
## 7 Melbourne Holiday 507.
## 8 South Coast Holiday 495.
## 9 Brisbane Visiting 493.
## 10 Melbourne Business 478.
## # ℹ 294 more rows
d.Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
(newtourism_tsibble <- tourism_tsibble %>%
group_by(State) %>%
summarize(TotalTrips = sum(Trips)) %>%
tsibble(index = Quarter,
key = State))
## # A tsibble: 640 x 3 [1Q]
## # Key: State [8]
## State Quarter TotalTrips
## <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
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.
Can you spot any seasonality, cyclicity and trend? What do you learn about the series? What can you say about the seasonal patterns? Can you identify any unusual years?
# Load the required time series datasets
data("us_employment", "aus_production", "pelt", "PBS", "us_gasoline")
us_employment %>% filter(Title == "Total Private") %>% autoplot(Employed)
us_employment %>% filter(Title == "Total Private") %>% gg_season(Employed)
us_employment %>% filter(Title == "Total Private") %>% gg_subseries(Employed)
us_employment %>% filter(Title == "Total Private") %>% gg_lag(Employed)
us_employment %>% filter(Title == "Total Private") %>% ACF(Employed)%>%autoplot()
The data exhibits a robust upward trend with noticeable
seasonality. There is no indication of any cyclic behavior in the
series. The employment levels consistently rise over time. The seasonal
patterns remain stable throughout the year, though there is a slight
upward curve in June, which diminishes after December. An identified
anomalous year appears to be 2008/2010.
2.Bricks from aus_production
autoplot(aus_production, Bricks)
gg_season(aus_production, Bricks)
gg_subseries(aus_production, Bricks)
gg_lag(aus_production, Bricks)
ACF(aus_production,Bricks)%>% autoplot()
The data displays strong seasonality within each year and
exhibits pronounced cyclic behavior with a period of 40 years. No
discernible trend is evident over this period. Brick production shows an
increase from quarter 1 to 3 and decreases in quarter 4. A notable
decline in brick production is observed around 1983.
autoplot(pelt, Hare)
#gg_season(pelt, Hare)
gg_subseries(pelt, Hare)
gg_lag(pelt, Hare)
ACF(pelt, Hare)%>% autoplot()
The data exhibits cyclical behavior. There is no discernible
trend or defined seasonality. The ACF plot illustrates a cyclic pattern
occurring every ten years.
4.“H02” Cost from PBS
PBS %>% filter(ATC2 == "H02") %>% autoplot(Cost)
PBS %>% filter(ATC2 == "H02") %>% gg_season(Cost)
PBS %>% filter(ATC2 == "H02") %>% gg_subseries(Cost)
#PBS %>% filter(ATC2 == "H02") %>% gg_lag(Cost)
PBS %>% filter(ATC2 == "H02") %>% ACF(Cost)%>%autoplot()
The data demonstrates strong seasonality within each year, as
well as pronounced cyclic behavior, with no discernible trend.
5.Barrels from us_gasoline
autoplot(us_gasoline, Barrels)
gg_season(us_gasoline, Barrels)
gg_subseries(us_gasoline, Barrels)
gg_lag(us_gasoline, Barrels)
ACF(us_gasoline, Barrels)%>% autoplot()
Lacks any trend, seasonality, or cyclic behavior. Random
fluctuations are present, appearing to be unpredictable, with no
distinct patterns that would facilitate the development of a forecasting
model.