EXERCISE #1

Create the appropriate tsibble time series and plot of your time series.

library(fpp3)
## Warning: package 'fpp3' was built under R version 4.4.3
## Warning: package 'ggplot2' was built under R version 4.4.3
## Warning: package 'tsibble' was built under R version 4.4.3
## Warning: package 'tsibbledata' was built under R version 4.4.3
## Warning: package 'feasts' was built under R version 4.4.3
## Warning: package 'fabletools' was built under R version 4.4.3
## Warning: package 'fable' was built under R version 4.4.3
library(tseries)
library(forecast)
## Warning: package 'forecast' was built under R version 4.4.3
NAQ_data <- NAQ %>%
  mutate(Month = yearmonth(date)) %>%
  select(-date) %>%
  as_tsibble(index = Month)

#View (NAQ_data)
NAQ_data%>%
  autoplot() +
  geom_smooth(method = "lm", se = FALSE, color = "blue", size = 0.5) + 
  labs(title = 'Gross Domestic Product (GDP) ',
       y = 'GDP (Millions of Dollars)',
       x = 'Year') +
  theme_bw()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

###Rechecking the seasonality with some years data

library(lubridate) 
NAQ_data %>%
  filter(year(Month) %in% 2010:2015) %>%  
  autoplot() +
  geom_smooth(method = "lm", se = FALSE, color = "blue", size = 0.5) + 
  labs(title = 'Gross Domestic Product (GDP) (2010-2015)',
       y = 'GDP (Millions of Dollars)',
       x = 'Year') +
  theme_bw()

Please comment on things like trend, seasonality, and the constancy of mean or variance.

(1) The plot shows a clear upward trend in the data over time.
(2) The plot shows seasonal patterns in the data.
(3) The mean is not constant in this series due to the trend.
(4) The variance appears relatively constant across the series.

EXERCISE #2

Please fit three ETS mdoels:

1. ETS(A,N,N)

NAQ1 <- NAQ_data %>%
  model (ETS(value ~ error("A") + trend("N") + season("N")))
report (NAQ1) 
## Series: value 
## Model: ETS(A,N,N) 
##   Smoothing parameters:
##     alpha = 0.8712888 
## 
##   Initial states:
##     l[0]
##  1529551
## 
##   sigma^2:  14895130683
## 
##      AIC     AICc      BIC 
## 3975.218 3975.394 3984.042

2. ETS(A,A,N)

NAQ2 <- NAQ_data %>%
  model (ETS(value ~ error("A") + trend("A") + season("N")))
report (NAQ2)
## Series: value 
## Model: ETS(A,A,N) 
##   Smoothing parameters:
##     alpha = 0.5270385 
##     beta  = 0.0550976 
## 
##   Initial states:
##     l[0]     b[0]
##  1439168 20535.81
## 
##   sigma^2:  10933518483
## 
##      AIC     AICc      BIC 
## 3933.886 3934.333 3948.594

3. ETS(A,A,A)

NAQ3 <- NAQ_data %>%
  model (ETS(value ~ error("A") + trend("A") + season("A")))
report (NAQ3)
## Series: value 
## Model: ETS(A,A,A) 
##   Smoothing parameters:
##     alpha = 0.8080029 
##     beta  = 0.0701025 
##     gamma = 0.1363629 
## 
##   Initial states:
##     l[0]     b[0]     s[0]   s[-1]    s[-2]     s[-3]
##  1459037 24581.54 67476.78 13180.7 591.5411 -81249.02
## 
##   sigma^2:  4927521941
## 
##      AIC     AICc      BIC 
## 3826.127 3827.511 3852.601
components(NAQ3)
## # A dable: 144 x 7 [3M]
## # Key:     .model [1]
## # :        value = lag(level, 1) + lag(slope, 1) + lag(season, 4) + remainder
##    .model                         Month   value   level  slope  season remainder
##    <chr>                          <mth>   <dbl>   <dbl>  <dbl>   <dbl>     <dbl>
##  1 "ETS(value ~ error(\"A\") … 1989 Jan      NA NA         NA  -81249.       NA 
##  2 "ETS(value ~ error(\"A\") … 1989 Apr      NA NA         NA     592.       NA 
##  3 "ETS(value ~ error(\"A\") … 1989 Jul      NA NA         NA   13181.       NA 
##  4 "ETS(value ~ error(\"A\") … 1989 Oct      NA  1.46e6 24582.  67477.       NA 
##  5 "ETS(value ~ error(\"A\") … 1990 Jan 1434375  1.51e6 26825. -76885.    32005.
##  6 "ETS(value ~ error(\"A\") … 1990 Apr 1490965  1.50e6 23605.  -5672.   -45931.
##  7 "ETS(value ~ error(\"A\") … 1990 Jul 1499694  1.49e6 21062.   8233.   -36284.
##  8 "ETS(value ~ error(\"A\") … 1990 Oct 1538100  1.48e6 17983.  61488.   -43918.
##  9 "ETS(value ~ error(\"A\") … 1991 Jan 1475270  1.54e6 21847. -69369.    55116.
## 10 "ETS(value ~ error(\"A\") … 1991 Apr 1532427  1.54e6 20072.  -9125.   -25321.
## # ℹ 134 more rows
Based on the AIC values ETS(A,A,A) is the best model because it has the smallest AIC.

EXERCISE 3

Using the optimal ETS model, forecast the value of your series for the next 8 time periods (i.e., two years of quarterly data).

# Forecast the next 8 time periods 
NQ <- NAQ3 %>%
  forecast(h = 8)
# View (NQ)

Please output the numerical values for the forecast

FNQ <- as.data.frame(NQ)
FNT <- FNQ [, c("Month", ".mean")]
print (FNT)
##      Month   .mean
## 1 2025 Jan 7401263
## 2 2025 Apr 7643093
## 3 2025 Jul 7769789
## 4 2025 Oct 7932682
## 5 2026 Jan 7779689
## 6 2026 Apr 8021519
## 7 2026 Jul 8148215
## 8 2026 Oct 8311108

Plot the forecast

NQ <- NAQ3 %>%
  forecast(h = 8)

# Plot with 95% interval and red dashed forecast line
 autoplot(NAQ_data, level = 95) +
  geom_line(aes(x = Month, y = .mean), data = NQ, color = "red", linetype = "dashed", size = 0.7) +
  labs(title = "GDP Forecast Using ETS(A,A,A)",
       x = "Year",
       y = "GDP (Millions of Dollars)") +
  theme_minimal()
## Warning in geom_line(...): Ignoring unknown parameters: `level`

EXERCISE #4

Use the model() function to have fable select the optimal ETS model.

NAM <- NAQ_data %>%
   model(ETS(value))
 report(NAM)
## Series: value 
## Model: ETS(M,A,M) 
##   Smoothing parameters:
##     alpha = 0.8010549 
##     beta  = 0.1168831 
##     gamma = 0.0001000155 
## 
##   Initial states:
##     l[0]     b[0]     s[0]    s[-1]    s[-2]     s[-3]
##  1447128 22347.93 1.019497 1.002744 1.000633 0.9771264
## 
##   sigma^2:  2e-04
## 
##      AIC     AICc      BIC 
## 3688.426 3689.811 3714.901
components(NAQ3)
## # A dable: 144 x 7 [3M]
## # Key:     .model [1]
## # :        value = lag(level, 1) + lag(slope, 1) + lag(season, 4) + remainder
##    .model                         Month   value   level  slope  season remainder
##    <chr>                          <mth>   <dbl>   <dbl>  <dbl>   <dbl>     <dbl>
##  1 "ETS(value ~ error(\"A\") … 1989 Jan      NA NA         NA  -81249.       NA 
##  2 "ETS(value ~ error(\"A\") … 1989 Apr      NA NA         NA     592.       NA 
##  3 "ETS(value ~ error(\"A\") … 1989 Jul      NA NA         NA   13181.       NA 
##  4 "ETS(value ~ error(\"A\") … 1989 Oct      NA  1.46e6 24582.  67477.       NA 
##  5 "ETS(value ~ error(\"A\") … 1990 Jan 1434375  1.51e6 26825. -76885.    32005.
##  6 "ETS(value ~ error(\"A\") … 1990 Apr 1490965  1.50e6 23605.  -5672.   -45931.
##  7 "ETS(value ~ error(\"A\") … 1990 Jul 1499694  1.49e6 21062.   8233.   -36284.
##  8 "ETS(value ~ error(\"A\") … 1990 Oct 1538100  1.48e6 17983.  61488.   -43918.
##  9 "ETS(value ~ error(\"A\") … 1991 Jan 1475270  1.54e6 21847. -69369.    55116.
## 10 "ETS(value ~ error(\"A\") … 1991 Apr 1532427  1.54e6 20072.  -9125.   -25321.
## # ℹ 134 more rows
# Forecast the next 8 time periods 
NQF <- NAM %>%
  forecast(h = 8)
# View (NQF)

The numerical values for the forecast

FNQT <- as.data.frame(NQF)

FNTT <- FNQT [, c("Month", ".mean")]
print (FNTT)
##      Month   .mean
## 1 2025 Jan 7344064
## 2 2025 Apr 7616793
## 3 2025 Jul 7729140
## 4 2025 Oct 7956139
## 5 2026 Jan 7719322
## 6 2026 Apr 8001077
## 7 2026 Jul 8114236
## 8 2026 Oct 8347668

Plot the forecast

NQF <- NAM %>%
  forecast(h = 8)

# Plot with 95% interval and red dashed forecast line
 autoplot(NAQ_data, level = 95) +
  geom_line(aes(x = Month, y = .mean), data = NQF, color = "red", linetype = "dashed", size = 0.7) +
  labs(title = "GDP Forecast Using ETS(value)",
       x = "Year",
       y = "GDP (Millions of Dollars)") +
  theme_minimal()
## Warning in geom_line(...): Ignoring unknown parameters: `level`

Is the optimal model what you expected? Please respond by referring to the appearance of the GDP time series.

Although, the optimal model from Exercise #2 is ETS(A,A,A) and fable function select ETS (M,A,M), however, the GDP series from the 2 models shows a clear and consistent increase trend over time and the seasonal pattern was visually striking which make the multiplicative error and seasonal model suggested my the fable function also appropriate.

EXERCISE #5

I would like you to use the data and parameter estimates from Exercise #2 to perform the forecasting-updated steps using Excel formulas.

YES, THE VALUES MATCHED.