Do exercises 5.1, 5.2, 5.3, 5.4 and 5.7 in the Hyndman book. Please submit your Rpubs link as well as your .rmd file with your code.

Load required library

library("fpp3")
library('fabletools')

5.1

Produce forecasts for the following series using whichever of NAIVE(y), SNAIVE(y) or RW(y ~ drift()) is more appropriate in each case:

Australian Population (global_economy) Bricks (aus_production) NSW Lambs (aus_livestock) Household wealth (hh_budget). Australian takeaway food turnover (aus_retail).

Australian Population (global_economy)

aus_population = global_economy %>% filter(Country=="Australia")
autoplot(aus_population, Population)+
  labs(title = "Australian Population")

aus_population_fit = aus_population %>% model(Drift = NAIVE(Population~drift()))

#generate forecast
aus_population_forcast = aus_population_fit %>%  
  forecast(h=10)

#plot forecast
aus_population_forcast %>% 
  autoplot(aus_population) + 
  labs(title="Drift Forecast of Australian Population", 
       subtitle = "10 Year Forecast", 
       xlab="Year", ylab="Population" )

RW(y ~ drift()) was most appropriate - shows upward trend.

Bricks (aus_production)

aus_br = aus_production %>% filter(!is.na(Bricks))

autoplot(aus_br, Bricks) + labs(title = "Australian Brick Production")

The plot doesn’t have upward trend but has seasonality. Hence SNAIVE(y) is most appropriate

brick_model = aus_br %>% model(snaive = SNAIVE(Bricks ~ lag("year")))

brick_forcast = brick_model %>% forecast(h=10)

#plot
brick_forcast %>% autoplot(aus_br) + labs(title = "SNAIVE 10 Years forecast of Brick production")

NSW Lambs (aus_livestock)

NSW_Lambs = aus_livestock %>% filter(Animal == "Lambs", State=="New South Wales")
autoplot(NSW_Lambs) + labs(title = "Count of Lamb in New South Wales")

The plot shows seasonality, hence SNAIVE(y) is the appropriate method

NSW_Lambs_model = NSW_Lambs %>% model(snaive = SNAIVE(Count ~ lag("year")))

nsw_forcast = NSW_Lambs_model %>% forecast(h=10)
nsw_forcast %>% autoplot(NSW_Lambs)

Household wealth (hh_budget)

household_wealth = hh_budget %>% select(Wealth) %>% filter(Country=='Australia')  %>% filter(!is.na(Wealth))

household_wealth_fit = household_wealth %>%
  model(
    
    `Naïve` = NAIVE(Wealth)
    
    
  )

household_wealth_fit %>% forecast(h = 14) %>% autoplot(household_wealth)

Australian takeaway food turnover (aus_retail)

aus_takeaway = aus_retail %>% filter(Industry=='Takeaway food services',State=='Australian Capital Territory') 

aus_takeaway_fit = aus_takeaway %>%
  model(
     `Seasonal Naïve` = SNAIVE(Turnover)
    
  )

aus_takeaway_fit %>% forecast(h = 36) %>% autoplot(aus_takeaway)

5.2

Use the Facebook stock price (data set gafa_stock) to do the following:

Produce a time plot of the series. Produce forecasts using the drift method and plot them. Show that the forecasts are identical to extending the line drawn between the first and last observations. Try using some of the other benchmark functions to forecast the same data set. Which do you think is best? Why?

Time Plot

fbStock = gafa_stock %>% filter(Symbol == "FB") %>%
  select(Close) %>% mutate(day=row_number())
head(fbStock)
## # A tsibble: 6 x 3 [!]
##   Close Date         day
##   <dbl> <date>     <int>
## 1  54.7 2014-01-02     1
## 2  54.6 2014-01-03     2
## 3  57.2 2014-01-06     3
## 4  57.9 2014-01-07     4
## 5  58.2 2014-01-08     5
## 6  57.2 2014-01-09     6
autoplot(fbStock)  

Produce forecasts

fb2015Data = fbStock %>% filter(year(Date) == 2015)

fb_model = fb2015Data %>% model(
      `Naïve` = NAIVE(Close),
    Drift = NAIVE(Close ~ drift())
)

fb2016Data = fbStock %>% filter(yearmonth(Date) == yearmonth("2016 Jan"))

fb_forcast = fb_model %>% forecast(new_data = fb2016Data)

fb_forcast %>% autoplot(fb2015Data, level = NULL) +
  autolayer(fb2016Data, Close)

5.3

Apply a seasonal naïve method to the quarterly Australian beer production data from 1992. Check if the residuals look like white noise, and plot the forecasts.

# Extract data of interest
recent_production <- aus_production |>
  filter(year(Quarter) >= 1992)
# Define and estimate a model
fit <- recent_production |> model(SNAIVE(Beer))
# Look at the residuals
fit |> gg_tsresiduals()

# Look a some forecasts
fit |> forecast() |> autoplot(recent_production)

There is one line that is below the blue dashed line, and another just above the bound. It is not considered to be white noise.

5.4

Repeat the previous exercise using the Australian Exports series from global_economy and the Bricks series from aus_production. Use whichever of NAIVE() or SNAIVE() is more appropriate in each case.

aus_p = global_economy %>% filter(Country == "Australia")
fit = aus_p %>% model(NAIVE(Exports))
fit %>% forecast() %>% autoplot(aus_p)