forecast Package Crash Course

Author

Mario Annau

Published

November 21, 2016

Company Data (AMZN)

urls <- paste("http://www.wikinvest.com/stock/Amazon.com_(AMZN)/Data/Revenue", rep(2000:2015, each=4), sprintf("Q%d", 1:4), sep = "/")
vals <- sapply(urls, function(x) {
  tree <- htmlTreeParse(x, useInternalNodes = TRUE)
  xmlValue(getNodeSet(tree, "//div[@id='nv_value']")[[1]])
})
matchvals <- gregexpr("[0-9.]+", vals)
numvals <- as.numeric(sapply(1:length(vals), function(i) substr(vals[i], matchvals[[i]][1], attr(matchvals[[i]], "match.length")[1] + 1)))
numvals <- ifelse(grepl("billion", vals), numvals * 1000, numvals)
amzn.rev <- ts(numvals, start = c(2000, 1), frequency = 4)
plot(amzn.rev)

We can easily see that should 1. Use the log scale or 2. Differentiate the series.

amzn.rev.log <- log(amzn.rev)
plot(amzn.rev.log)

Now it’s easy to see the trend and the seasonal component. We are now lazy and use auto.arima again:

mod2 <- auto.arima(amzn.rev.log)
mod2
Series: amzn.rev.log 
ARIMA(1,0,0)(2,1,0)[4] with drift 

Coefficients:
         ar1     sar1     sar2   drift
      0.8721  -0.5769  -0.2558  0.0597
s.e.  0.0692   0.1473   0.1417  0.0058

sigma^2 = 0.002051:  log likelihood = 101.54
AIC=-193.09   AICc=-191.98   BIC=-182.61
plot(forecast(mod2, 12))

mod3 <- auto.arima(amzn.rev)
mod3
Series: amzn.rev 
ARIMA(0,1,0)(2,1,0)[4] 

Coefficients:
        sar1    sar2
      0.2688  0.5529
s.e.  0.1211  0.1226

sigma^2 = 149679:  log likelihood = -436.59
AIC=879.17   AICc=879.61   BIC=885.4
fc3 <- forecast(mod3, 12)
plot(fc3)

fc3
        Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
2016 Q1       28576.30 28080.49 29072.12 27818.03 29334.58
2016 Q2       29263.61 28562.42 29964.79 28191.24 30335.98
2016 Q3       31600.70 30741.93 32459.47 30287.32 32914.07
2016 Q4       42569.82 41578.19 43561.44 41053.26 44086.37
2017 Q1       34824.38 33324.80 36323.97 32530.96 37117.80
2017 Q2       36051.12 34176.50 37925.74 33184.14 38918.10
2017 Q3       38947.31 36761.07 41133.55 35603.75 42290.88
2017 Q4       50978.86 48520.18 53437.53 47218.64 54739.08
2018 Q1       42768.06 39557.90 45978.22 37858.54 47677.57
2018 Q2       44259.96 40443.53 48076.39 38423.23 50096.69
2018 Q3       47398.85 43060.04 51737.65 40763.22 54034.47
2018 Q4       60036.20 55231.49 64840.92 52688.03 67384.38

Reading Materials