Load Data

setwd("~/Documents/MSAE/Predictive Analytics")
AAPLdata <- read.csv("AAPL (1).csv")
MSFTdata <- read.csv("MSFT.csv")
AMBCdata <- read.csv("AMBC.csv")
BACdata <- read.csv("BAC.csv")

Data Manipulation

#change variable names for monthly adj. close
AAPLdata <- rename(AAPLdata, AAPL = Adj.Close)
AMBCdata <- rename(AMBCdata, AMBC = Adj.Close)
BACdata <- rename(BACdata, BAC = Adj.Close)
MSFTdata <- rename(MSFTdata, MSFT = Adj.Close)

#combine datasets
AAPLdata <- AAPLdata[c(1,6)]
AMBCdata <- AMBCdata[c(1,6)]
BACdata  <- BACdata [c(1,6)]
MSFTdata <- MSFTdata[c(1,6)]

stockdata <- merge(AAPLdata, AMBCdata, by = "Date")
stockdata <- merge(stockdata, BACdata, by = "Date")
stockdata <- merge(stockdata, MSFTdata, by = "Date")

stockdata$Date <- as.Date(stockdata$Date)
stockdata <- rename(stockdata, Month = Date)

#Creating Tsibble 
stockdata <- stockdata %>% 
  mutate(Month = yearmonth(Month)) %>% 
  tsibble(index = Month)

Building Training and Test Set

training <- stockdata[1:48,]
test <- stockdata[49:60,]


#Pivot Longer
temp <- stockdata %>% 
  pivot_longer(!c(Month), names_to='Stock', values_to='Value')
training <- training %>% 
  pivot_longer(!c(Month), names_to='Stock', values_to='Value')
test <- test %>% 
  pivot_longer(!c(Month), names_to='Stock', values_to='Value')

#Assign Sector
temp$Sector = rep('Tech', nrow(temp))
training$Sector = rep('Tech', nrow(training))
test$Sector = rep('Tech', nrow(test))


temp$Sector[temp$Stock=="AMBC"|temp$Stock=="BAC"]="Finance"
training$Sector[training$Stock=="AMBC"|training$Stock=="BAC"]="Finance"
test$Sector[test$Stock=="AMBC"|test$Stock=="BAC"]="Finance"

temp %>% autoplot()
## Plot variable not specified, automatically selected `.vars = Value`

Different aggregate groups

myagg <- temp %>% aggregate_key(Stock/Sector, Value = sum(Value))
myagg2 <- training %>% aggregate_key(Stock, Value = mean(Value))
myagg3 <- training %>% aggregate_key(Sector/Stock, Value = mean(Value))
agg_test <- test %>% aggregate_key(Sector/Stock, Value = mean(Value))

#aggregate plot 
myagg %>% 
  filter(is_aggregated(Sector)) %>% 
  autoplot(Value) +
  labs(y= "Value", title = "Value of Investment") +
  facet_wrap(vars(Stock), scales = "free_y", ncol = 3) +
  theme(axis.text.x = element_text (angle = 90, hjust = 1)) +
  theme(legend.position = "none")

Models

Bottoms up forecast

m1 <- myagg2 %>% model(ets = ETS(Value)) %>% reconcile(bu =bottom_up(ets))
f1 <- m1 %>% forecast(h=12)
f1 %>% autoplot(temp)

acc1 <- f1 %>% accuracy(test %>% aggregate_key(Stock, Value = mean(Value)))

Middle Out forecast

m2 <- myagg3 %>% model(ets2 = ETS(Value)) %>% reconcile(md =middle_out(ets2))
f2 <- m2 %>% forecast(h=12)
f2 %>% autoplot(temp %>% aggregate_key(Sector/Stock, Value = mean(Value))) +
  facet_wrap(~Stock + Sector) + 
  theme(axis.text.x = element_text (angle = 90, hjust = 1))

acc2 <- f2 %>% accuracy(test %>% aggregate_key(Sector/Stock, Value = mean(Value)))

Top Down forecast

m3 <- myagg3 %>% model(ets3 = ETS(Value)) %>% reconcile(td = top_down(ets3))
f3 <- m3 %>% forecast(h=12)
f3 %>% autoplot(temp %>% aggregate_key(Sector/Stock, Value = mean(Value))) + 
  facet_wrap(~Stock + Sector) +
  theme(axis.text.x = element_text (angle = 90, hjust = 1))

acc3 <- f3 %>% accuracy(test %>% aggregate_key(Sector/Stock, Value = mean(Value)))

MinT Forecast

m4 <- myagg3 %>% model(ets4 = ETS(Value)) %>% reconcile(mint = min_trace(ets4))
f4 <- m4 %>% forecast(h=12)
f4 %>% autoplot(temp %>% aggregate_key(Sector/Stock, Value = mean(Value))) + 
  facet_wrap(~Stock + Sector) +
  theme(axis.text.x = element_text (angle = 90, hjust = 1))

acc4 <- f4 %>% accuracy(test %>% aggregate_key(Sector/Stock, Value = mean(Value)))

acc4 %>% filter(is_aggregated(Sector))
## # A tibble: 2 × 12
##   .model Sector     Stock      .type     ME  RMSE   MAE    MPE  MAPE  MASE RMSSE
##   <chr>  <chr*>     <chr*>     <chr>  <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl>
## 1 ets4   <aggregat… <aggregat… Test   -8.76  13.6  11.8  -8.33  10.5   NaN   NaN
## 2 mint   <aggregat… <aggregat… Test  -68.0   68.7  68.0 -58.1   58.1   NaN   NaN
## # ℹ 1 more variable: ACF1 <dbl>

Accuracy metrics

fit <- myagg3 %>% 
  model(ets = ETS(Value)) %>% 
  reconcile(bottom_up = bottom_up(ets), 
            MinT = min_trace(ets), 
            top_down = top_down(ets),
            middle_out = middle_out(ets),
            )

fcst <- fit %>% forecast(h=12)

accuracy_table <- fcst %>% accuracy(test %>% aggregate_key(Sector/Stock, Value = mean(Value)))

kable(accuracy_table, 
      format = "markdown",
      caption = "Model Accuracy")
Model Accuracy
.model Sector Stock .type ME RMSE MAE MPE MAPE MASE RMSSE ACF1
MinT Finance AMBC Test 10.5847975 10.669803 10.584798 69.340092 69.340092 NaN NaN 0.5246184
MinT Finance BAC Test 13.5690845 13.895308 13.569085 42.222383 42.222383 NaN NaN 0.4097659
MinT Finance Test 0.6713416 1.973305 1.706177 2.232191 7.118058 NaN NaN 0.4295048
MinT Tech AAPL Test 98.5561469 100.968130 98.556147 61.013536 61.013536 NaN NaN 0.6962055
MinT Tech MSFT Test 170.8309234 173.964212 170.830923 61.620757 61.620757 NaN NaN 0.7694010
MinT Tech Test 51.8221220 57.260127 51.822122 22.789969 22.789969 NaN NaN 0.7403013
MinT Test -68.0302807 68.666505 68.030281 -58.058031 58.058031 NaN NaN 0.6207345
bottom_up Finance AMBC Test 3.6138914 3.856297 3.613891 23.146295 23.146295 NaN NaN 0.5218961
bottom_up Finance BAC Test -0.9006490 3.173922 2.674216 -3.781121 8.740353 NaN NaN 0.4280993
bottom_up Finance Test -20.7692981 20.857273 20.769298 -89.704067 89.704067 NaN NaN 0.4596980
bottom_up Tech AAPL Test -1.8925192 20.436583 17.530697 -2.778628 11.076385 NaN NaN 0.6790635
bottom_up Tech MSFT Test -23.4811672 37.999595 33.232276 -10.189793 13.095621 NaN NaN 0.7497382
bottom_up Tech Test -242.9386347 243.775623 242.938635 -114.840727 114.840727 NaN NaN 0.6838691
bottom_up Test -384.2316771 384.299233 384.231677 -323.536109 323.536109 NaN NaN 0.3388331
ets Finance AMBC Test 3.6138914 3.856297 3.613891 23.146295 23.146295 NaN NaN 0.5218961
ets Finance BAC Test -0.9006490 3.173922 2.674216 -3.781121 8.740353 NaN NaN 0.4280993
ets Finance Test 1.2869116 2.306127 1.915269 4.849127 7.829977 NaN NaN 0.4596980
ets Tech AAPL Test -1.8925192 20.436583 17.530697 -2.778628 11.076385 NaN NaN 0.6790635
ets Tech MSFT Test -23.4811672 37.999595 33.232276 -10.189793 13.095621 NaN NaN 0.7497382
ets Tech Test -19.9267478 29.795178 25.867061 -10.633164 12.877962 NaN NaN 0.7163744
ets Test -8.7556644 13.615830 11.807022 -8.333749 10.461152 NaN NaN 0.6720512
middle_out Finance AMBC Test 9.3945091 9.490396 9.394509 61.452080 61.452080 NaN NaN 0.5218961
middle_out Finance BAC Test 15.3749429 15.673273 15.374943 47.945954 47.945954 NaN NaN 0.4280993
middle_out Finance Test 1.2869116 2.306127 1.915269 4.849127 7.829977 NaN NaN 0.4596980
middle_out Tech AAPL Test 76.3695928 78.815333 76.369593 47.080026 47.080026 NaN NaN 0.6671447
middle_out Tech MSFT Test 121.2686077 125.676600 121.268608 43.254332 43.254332 NaN NaN 0.7701968
middle_out Tech Test -19.9267478 29.795178 25.867061 -10.633164 12.877962 NaN NaN 0.7163744
middle_out Test -139.1635806 139.379119 139.163581 -117.637585 117.637585 NaN NaN 0.4752063
top_down Finance AMBC Test 12.3158510 12.389158 12.315851 80.810630 80.810630 NaN NaN 0.5217012
top_down Finance BAC Test 23.6001136 23.796132 23.600114 74.085781 74.085781 NaN NaN 0.4296929
top_down Finance Test 12.4334241 12.580603 12.433424 52.631579 52.631579 NaN NaN 0.4620918
top_down Tech AAPL Test 118.1920083 119.851802 118.192008 73.658504 73.658504 NaN NaN 0.6728235
top_down Tech MSFT Test 198.7075960 202.028558 198.707596 71.753809 71.753809 NaN NaN 0.7840639
top_down Tech Test 99.3346559 102.623782 99.334656 44.930570 44.930570 NaN NaN 0.7512358
top_down Test -8.7556644 13.615830 11.807022 -8.333749 10.461152 NaN NaN 0.6720512

Comparison of Models

glance(m1)
## # A tibble: 10 × 10
##    Stock        .model  sigma2 log_lik   AIC  AICc   BIC    MSE   AMSE    MAE
##    <chr*>       <chr>    <dbl>   <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>  <dbl>
##  1 AAPL         ets    0.00961   -195.  396.  396.  401.  90.9  188.   0.0830
##  2 AAPL         bu     0.00961   -195.  396.  396.  401.  90.9  188.   0.0830
##  3 AMBC         ets    3.86      -124.  255.  255.  260.   3.70   6.08 1.41  
##  4 AMBC         bu     3.86      -124.  255.  255.  260.   3.70   6.08 1.41  
##  5 BAC          ets    0.00899   -141.  289.  289.  294.   7.68  15.6  0.0757
##  6 BAC          bu     0.00899   -141.  289.  289.  294.   7.68  15.6  0.0757
##  7 MSFT         ets    0.00369   -207.  424.  426.  434. 176.   324.   0.0466
##  8 MSFT         bu     0.00369   -207.  424.  426.  434. 176.   324.   0.0466
##  9 <aggregated> ets    0.00443   -172.  354.  355.  363.  33.9   63.0  0.0534
## 10 <aggregated> bu     0.00443   -172.  354.  355.  363.  33.9   63.0  0.0534
glance(m2)
## # A tibble: 14 × 11
##    Sector      Stock      .model  sigma2 log_lik   AIC  AICc   BIC    MSE   AMSE
##    <chr*>      <chr*>     <chr>    <dbl>   <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>
##  1 Finance     AMBC       ets2   3.86      -124.  255.  255.  260.   3.70   6.08
##  2 Finance     AMBC       md     3.86      -124.  255.  255.  260.   3.70   6.08
##  3 Finance     BAC        ets2   0.00899   -141.  289.  289.  294.   7.68  15.6 
##  4 Finance     BAC        md     0.00899   -141.  289.  289.  294.   7.68  15.6 
##  5 Finance     <aggregat… ets2   4.73      -129.  264.  265.  270.   4.53   8.53
##  6 Finance     <aggregat… md     4.73      -129.  264.  265.  270.   4.53   8.53
##  7 Tech        AAPL       ets2   0.00961   -195.  396.  396.  401.  90.9  188.  
##  8 Tech        AAPL       md     0.00961   -195.  396.  396.  401.  90.9  188.  
##  9 Tech        MSFT       ets2   0.00369   -207.  424.  426.  434. 176.   324.  
## 10 Tech        MSFT       md     0.00369   -207.  424.  426.  434. 176.   324.  
## 11 Tech        <aggregat… ets2   0.00451   -198.  405.  407.  415. 112.   207.  
## 12 Tech        <aggregat… md     0.00451   -198.  405.  407.  415. 112.   207.  
## 13 <aggregate… <aggregat… ets2   0.00443   -172.  354.  355.  363.  33.9   63.0 
## 14 <aggregate… <aggregat… md     0.00443   -172.  354.  355.  363.  33.9   63.0 
## # ℹ 1 more variable: MAE <dbl>
glance(m3)
## # A tibble: 14 × 11
##    Sector      Stock      .model  sigma2 log_lik   AIC  AICc   BIC    MSE   AMSE
##    <chr*>      <chr*>     <chr>    <dbl>   <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>
##  1 Finance     AMBC       ets3   3.86      -124.  255.  255.  260.   3.70   6.08
##  2 Finance     AMBC       td     3.86      -124.  255.  255.  260.   3.70   6.08
##  3 Finance     BAC        ets3   0.00899   -141.  289.  289.  294.   7.68  15.6 
##  4 Finance     BAC        td     0.00899   -141.  289.  289.  294.   7.68  15.6 
##  5 Finance     <aggregat… ets3   4.73      -129.  264.  265.  270.   4.53   8.53
##  6 Finance     <aggregat… td     4.73      -129.  264.  265.  270.   4.53   8.53
##  7 Tech        AAPL       ets3   0.00961   -195.  396.  396.  401.  90.9  188.  
##  8 Tech        AAPL       td     0.00961   -195.  396.  396.  401.  90.9  188.  
##  9 Tech        MSFT       ets3   0.00369   -207.  424.  426.  434. 176.   324.  
## 10 Tech        MSFT       td     0.00369   -207.  424.  426.  434. 176.   324.  
## 11 Tech        <aggregat… ets3   0.00451   -198.  405.  407.  415. 112.   207.  
## 12 Tech        <aggregat… td     0.00451   -198.  405.  407.  415. 112.   207.  
## 13 <aggregate… <aggregat… ets3   0.00443   -172.  354.  355.  363.  33.9   63.0 
## 14 <aggregate… <aggregat… td     0.00443   -172.  354.  355.  363.  33.9   63.0 
## # ℹ 1 more variable: MAE <dbl>
glance(m4)
## # A tibble: 14 × 11
##    Sector      Stock      .model  sigma2 log_lik   AIC  AICc   BIC    MSE   AMSE
##    <chr*>      <chr*>     <chr>    <dbl>   <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>
##  1 Finance     AMBC       ets4   3.86      -124.  255.  255.  260.   3.70   6.08
##  2 Finance     AMBC       mint   3.86      -124.  255.  255.  260.   3.70   6.08
##  3 Finance     BAC        ets4   0.00899   -141.  289.  289.  294.   7.68  15.6 
##  4 Finance     BAC        mint   0.00899   -141.  289.  289.  294.   7.68  15.6 
##  5 Finance     <aggregat… ets4   4.73      -129.  264.  265.  270.   4.53   8.53
##  6 Finance     <aggregat… mint   4.73      -129.  264.  265.  270.   4.53   8.53
##  7 Tech        AAPL       ets4   0.00961   -195.  396.  396.  401.  90.9  188.  
##  8 Tech        AAPL       mint   0.00961   -195.  396.  396.  401.  90.9  188.  
##  9 Tech        MSFT       ets4   0.00369   -207.  424.  426.  434. 176.   324.  
## 10 Tech        MSFT       mint   0.00369   -207.  424.  426.  434. 176.   324.  
## 11 Tech        <aggregat… ets4   0.00451   -198.  405.  407.  415. 112.   207.  
## 12 Tech        <aggregat… mint   0.00451   -198.  405.  407.  415. 112.   207.  
## 13 <aggregate… <aggregat… ets4   0.00443   -172.  354.  355.  363.  33.9   63.0 
## 14 <aggregate… <aggregat… mint   0.00443   -172.  354.  355.  363.  33.9   63.0 
## # ℹ 1 more variable: MAE <dbl>