Exercises 2.1, 2.2, 2.3, 2.4, 2.5 and 2.8 from the Hyndman online Forecasting book.
Use the help function to explore what the series gafa_stock, PBS, vic_elec and pelt represent. Use autoplot() to plot some of the series in these data sets. What is the time interval of each series?
library(fpp3)
autoplot(gafa_stock, Close)PBS%>%
filter(ATC2=="A01")%>%
autoplot(Scripts)autoplot(vic_elec, Demand)autoplot(pelt, Hare)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, Close)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.
#Read Data
tute1 <- readr::read_csv("tute1.csv")
View(tute1)
#Convert the data to time series
mytimeseries<-tute1%>%
mutate(Quarter = yearquarter(Quarter))%>%
as_tsibble(index = Quarter)
#Construct time series plots of each of the three series
mytimeseries %>%
pivot_longer(-Quarter) %>%
ggplot(aes(x = Quarter, y = value, colour = name)) +
geom_line() +
facet_grid(name ~ ., scales = "free_y")All three series are in one plot without facet_grid().
The USgas package contains data on the demand for natural gas in the US.
library(USgas)
ts_us_total<-us_total%>%
as_tsibble(index = year, key= state)
ts_us_total%>%
filter(state == 'Maine' |
state == 'Vermont' |
state == 'New Hampshire' |
state == 'Massachusetts' |
state == 'Connecticut' |
state == 'Rhode Island')%>%
autoplot(y)+
labs( y = 'Million Cubic Feet',
title = 'Annual natural gas consumption by state',
subtitle = 'for the New England area')tour<-readxl::read_excel("tourism.xlsx")
tour<-tour%>%
mutate(Quarter=yearquarter(Quarter))%>%
as_tsibble(index=Quarter, key=c(Region, State, Purpose))
#Find what combination of Region and Purpose had the maximum number of overnight trips on average.
tour%>%
as.tibble%>%
group_by(Region, Purpose)%>%
summarise(avg_trips=mean(Trips))%>%
arrange(desc(avg_trips))#Create a new tsibble which combines the Purposes and Regions, and just has total trips by State.
State_only<-tour%>%
as.tibble%>%
select(-Region, -Purpose)%>%
unique()%>%
group_by(State, Quarter)%>%
summarise(Total_trips = sum(Trips))%>%
ungroup()%>%
as_tsibble(index=Quarter, key=State)
State_onlyThe highest maximum number of overnight trips on average is Visiting Sydney.
set.seed(12000)
myseries <- aus_retail %>%
mutate(Month=yearmonth(Month))%>%
filter(`Series ID` == sample(aus_retail$`Series ID`,1))
autoplot(myseries, Turnover)myseries%>%
gg_season(Turnover)myseries%>%
gg_subseries(Turnover)myseries%>%
gg_lag(Turnover)myseries%>%
ACF(Turnover)%>%
autoplot()Can you spot any seasonality, cyclicity and trend? What do you learn about the series? The trend is increasing along time. The seasonality is the turnover is higher at the end of each year.All lag plots are quite positive. The cyclicity is less obvious and can be found every decades.