Dataset:

Dataset 01 covers the years 2015 to 2019 (though 2020 is included it is rare) for 464 Biotech companies that are publicly traded in the United States.

URL: https://www.kaggle.com/andersward/10k-statement-data-for-us-biotech-companies

Dataset 02 demonstrates 05 well-known biotech companies through movements of financial market in US, including:

  1. PFIZER: Pfizer Inc. develops, manufactures, and sells healthcare products worldwide. It offers medicines and vaccines in various therapeutic areas.
  2. ASTRAZENECA: Moderna, Inc., a clinical stage biotechnology company, develops therapeutics and vaccines based on messenger RNA for the treatment of infectious diseases, immuno-oncology, rare diseases, and cardiovascular diseases.
  3. BIONTECH: BioNTech SE, a biotechnology company, develops and commercializes immunotherapies for cancer and other infectious diseases.
  4. MODERNA: Moderna, Inc., a clinical stage biotechnology company, develops therapeutics and vaccines based on messenger RNA for the treatment of infectious diseases, immuno-oncology, rare diseases, and cardiovascular diseases.
  5. NOVAVAX: Novavax, Inc., together with its subsidiary, Novavax AB, a late-stage biotechnology company, focuses on the discovery, development, and commercialization of vaccines to prevent serious infectious diseases.

URL: https://www.kaggle.com/patrickgomes/pharmaceutical-companies-on-stock-exchange-in-2020

dt1 <- read.csv("C:/Users/Anhuynh/Desktop/Data Science Project/BioTech/biotech_timeSeries.csv")

dt2 <- read.csv("C:/Users/Anhuynh/Desktop/Data Science Project/BioTech/Pharmaceutical companies on the stock exchange in 2020.csv")

Dataset 01: Explorartory Data Analysis

# Check missing data
mis_dt1 <- function(x) {sum(is.na(x))/length(x)*100}
apply(dt1, 2, mis_dt1)
##                                       year 
##                                    0.00000 
##                                     symbol 
##                                    0.00000 
##                                  marketCap 
##                                   83.33333 
##                              NetIncomeLoss 
##                                   57.83046 
##                        OperatingIncomeLoss 
##                                   61.17098 
##              ResearchAndDevelopmentExpense 
##                                   63.75718 
##            GeneralAndAdministrativeExpense 
##                                   68.49856 
##                                   Revenues 
##                                   86.99713 
##                            CostOfGoodsSold 
##                                   96.08477 
##                       SalesRevenueGoodsNet 
##                                   96.08477 
##                            SalesRevenueNet 
##                                   96.55172 
## PaymentsToAcquirePropertyPlantAndEquipment 
##                                   64.43966 
## NetCashProvidedByUsedInOperatingActivities 
##                                   71.73132 
##                           CommonStockValue 
##                                   82.00431 
##      CashAndCashEquivalentsAtCarryingValue 
##                                   69.68391 
##                            operatingMargin 
##                                   55.35201
library(mice)
## 
## Attaching package: 'mice'
## The following object is masked from 'package:stats':
## 
##     filter
## The following objects are masked from 'package:base':
## 
##     cbind, rbind
# Impute data
imp1 <- mice(dt1, m=3, seed = 123, method = "cart")
## Warning: Number of logged events: 4
new_dt1 <- complete(imp1, 1)

imp2 <- mice(new_dt1, m=3, seed = 1234, method = "cart")
## Warning: Number of logged events: 32
dt1_imp <- complete(imp2, 1)

imp3 <- mice(dt1_imp, m=3, seed = 12345, method = "cart")
## Warning: Number of logged events: 1
dt1_impute <- complete(imp3, 1)
# count on number of distinct symbols (#companies)
uni <- unique(dt1_impute$symbol)
length(uni)
## [1] 464
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(scales)
library(stringr)


# Sorting data for creating graph chart.

subMar = dt1_impute[order(dt1_impute$marketCap, decreasing = TRUE), ]
sort_subMar <- head(subMar, 5)
Mar <- sort_subMar[,c(2,3)]
Mar <- Mar %>%
  mutate(marketCap_rounded = marketCap/10^3) %>%
  group_by(symbol) %>%
  mutate(Percent = (marketCap/sum(Mar$marketCap))*100) 

subInc = dt1_impute[order(dt1_impute$NetIncomeLoss, decreasing = TRUE), ]
sort_subInc <- head(subInc, 5)
Inc <- sort_subInc[,c(2,4)]
Inc <- Inc %>%
  mutate(NetIncomeLoss_rounded = NetIncomeLoss/10^9) %>%
  group_by(symbol) %>%
  mutate(Percent = (NetIncomeLoss/sum(Inc$NetIncomeLoss))*100)

subRev = dt1_impute[order(dt1_impute$Revenues, decreasing = TRUE), ]
sort_subRev <- head(subRev, 5)
Rev <- sort_subRev[,c(2,8)]
Rev <- Rev %>%
  mutate(Revenues_rounded = Revenues/10^9) %>%
  group_by(symbol) %>%
  mutate(Percent = (Revenues/sum(Rev$Revenues))*100)

subStock = dt1_impute[order(dt1_impute$CommonStockValue, decreasing = TRUE), ]
sort_subStock <- head(subStock, 5)
Stock <- sort_subStock[,c(2,14)]
Stock <- Stock %>%
  mutate(StockValue_rounded = CommonStockValue/10^6) %>%
  group_by(symbol) %>%
  mutate(Percent = (CommonStockValue/sum(Stock$CommonStockValue))*100)

subMargin = dt1_impute[order(dt1_impute$operatingMargin, decreasing = TRUE), ]
sort_subMargin <- head(subMargin, 5)
Margin <- sort_subMargin[,c(2,16)]
Margin <- Margin %>%
  group_by(symbol) %>%
  mutate(Percent = (operatingMargin/sum(Margin$operatingMargin))*100)

subRD = dt1_impute[order(dt1_impute$ResearchAndDevelopmentExpense, decreasing = TRUE), ]
sort_subRD <- head(subRD, 5)
RD <- sort_subRD[,c(2,6)]
RD <- RD %>%
  mutate(RD_rounded = ResearchAndDevelopmentExpense/10^9) %>%
  group_by(symbol) %>%
  mutate(Percent = (ResearchAndDevelopmentExpense/sum(RD$ResearchAndDevelopmentExpense))*100)

# Data visualization

ggplot(Mar, aes(x="", y=Percent, fill=symbol))+ 
    geom_bar(width = 0.5,stat ="identity")+
    coord_polar(theta = "y",direction = 1)+
    geom_text(aes(x=1.3,y = Percent, label = paste(round(Mar$Percent,0),'\n', "(", gsub(" ", "", paste("$",round(Mar$marketCap_rounded,1),"K")), ")") ), position = position_stack(vjust = 0.5)) + 
    scale_fill_brewer(palette="GnBu")+ggtitle("Market Cap Value")+theme_minimal() + labs(x = NULL)

ggplot(Inc, aes(x="", y=Percent, fill=symbol))+ 
    geom_bar(width = 0.5,stat ="identity")+
    coord_polar(theta = "y",direction = 1)+
    geom_text(aes(x=1.3,y = Percent, label = paste(round(Inc$Percent,0),'\n', "(", gsub(" ", "", paste("$",round(Inc$NetIncomeLoss_rounded,1),"B")),")") ), position = position_stack(vjust = 0.5)) + 
    scale_fill_brewer(palette="GnBu")+ggtitle("Net Income Loss")+theme_minimal() +
    labs(x = NULL)

ggplot(Rev, aes(x="", y=Percent, fill=symbol))+ 
    geom_bar(width = 0.5,stat ="identity")+
    coord_polar(theta = "y",direction = 1)+
    geom_text(aes(x=1.3,y = Percent, label = paste(round(Rev$Percent,0),'\n', "(", gsub(" ", "", paste("$",round(Rev$Revenues_rounded,1),"B")),")") ), position = position_stack(vjust = 0.5)) + 
    scale_fill_brewer(palette="GnBu")+ggtitle("Revenues")+theme_minimal() +
    labs(x = NULL)

ggplot(RD, aes(x="", y=Percent, fill=symbol))+ 
    geom_bar(width = 0.5,stat ="identity")+
    coord_polar(theta = "y",direction = 1)+
    geom_text(aes(x=1.3,y = Percent, label = paste(round(RD$Percent,0),'\n', "(", gsub(" ", "", paste("$",round(RD$RD_rounded,1),"B")),")") ), position = position_stack(vjust = 0.5)) + 
    scale_fill_brewer(palette="GnBu")+ggtitle("R&D Expenses")+theme_minimal() +
    labs(x = NULL)

ggplot(Stock, aes(x="", y=Percent, fill=symbol))+ 
    geom_bar(width = 0.5,stat ="identity")+
    coord_polar(theta = "y",direction = 1)+
    geom_text(aes(x=1.3,y = Percent, label = paste(round(Stock$Percent,0),'\n', "(", gsub(" ", "", paste("$",round(Stock$StockValue_rounded,1), "M")),")") ), position = position_stack(vjust = 0.5)) + 
    scale_fill_brewer(palette="GnBu")+ggtitle("Common Stock Value")+ theme_minimal() + labs(x = NULL)

ggplot(Margin, aes(x="", y=Percent, fill=symbol))+ 
    geom_bar(width = 0.5,stat ="identity")+
    coord_polar(theta = "y",direction = 1)+
    geom_text(aes(x=1.3,y = Percent, label = paste(round(Margin$Percent,0),'\n', "(", gsub(" ", "", paste("$",round(Margin$operatingMargin,1))),")") ), position = position_stack(vjust = 0.5)) + 
    scale_fill_brewer(palette="GnBu")+ggtitle("Operating Margin")+theme_minimal() + labs(x = NULL)

library(dplyr)
library(ggplot2)
library(scales)
library(stringr)

expenses <- dt1_impute[,c(1,6,7,9,12)] %>%
  group_by(year) %>%
  summarise('R&D' = sum(ResearchAndDevelopmentExpense), GeneralAndAdministrativeExpense = sum(GeneralAndAdministrativeExpense),
 CostOfGoodsSold = sum(CostOfGoodsSold), PaymentsToAcquirePropertyPlantAndEquipment = sum(PaymentsToAcquirePropertyPlantAndEquipment)) 

# create new data for graph
df1 <- expenses[,c(1,2)]
colnames(df1) <- c("year", "value")
df1 <- df1 %>% mutate(category = "R&D")

df2 <- expenses[,c(1,3)]
colnames(df2) <- c("year", "value")
df2 <- df2 %>% mutate(category = "General&AdministrativeExpense")

df3 <- expenses[,c(1,4)]
colnames(df3) <- c("year", "value")
df3 <- df3 %>% mutate(category = "CostOfGoodsSold")

df4 <- expenses[,c(1,5)]
colnames(df4) <- c("year", "value")
df4 <- df4 %>% mutate(category = "PaymentsToAcquirePropertyPlant&Equipment")

df_cb <- rbind(df1, df2, df3, df4)

# data viz
ggplot(df_cb, aes(fill=category, y=value, x=year, label = paste(round(df_cb$value/10^9,1)))) + 
    geom_bar(position="stack", stat="identity") +
    geom_text(position = position_stack(vjust = 0.5), size = 3, color = "black") + scale_fill_brewer(palette="GnBu") +
  labs(title="Spending of Biotech Companies in the U.S (2015-2020)", x="Year",y="Expenses") +
  scale_y_continuous(labels  = 
                       label_number(scale = 1e-9, prefix = "$", suffix = "B", big.mark = ',',  accuracy = 1))
## Warning: Use of `df_cb$value` is discouraged. Use `value` instead.

## Warning: Use of `df_cb$value` is discouraged. Use `value` instead.

# create data for visualization
sum1 = sum(df_cb$value[df_cb$year == '2015'])
sum2 = sum(df_cb$value[df_cb$year == '2016'])
sum3 = sum(df_cb$value[df_cb$year == '2017'])
sum4 = sum(df_cb$value[df_cb$year == '2018'])
sum5 = sum(df_cb$value[df_cb$year == '2019'])
sum6 = sum(df_cb$value[df_cb$year == '2020'])
        
viz2 <- expenses[, c(1,2)] %>%
  mutate(total_exp = c(sum1, sum2, sum3, sum4, sum5, sum6))

df5 <- viz2[,c(1,2)]
colnames(df5) <- c("year", "value")
df5 <- df5 %>% mutate(category = "R&D")

df6 <- viz2[,c(1,3)]
colnames(df6) <- c("year", "value")
df6 <- df6 %>% mutate(category = "total_expense")

df_cb2 <- rbind(df5, df6)
  
# data viz2
ggplot(df_cb2, aes(fill=category, y=value, x=year, label =  paste(round(df_cb2$value/10^9,1))) ) + 
    geom_bar(position="stack", stat="identity") +   
    geom_text(position = position_stack(), vjust = 0.5, size = 3, color = "black")+ scale_fill_brewer(palette="GnBu") +
  labs(title="R&D Vs. Total Expenses of Biotech Companies (2015-2020)", x="Year",y="") +
  scale_y_continuous(labels  = 
                       label_number(scale = 1e-9, prefix = "$", suffix = "B", big.mark = ',',  accuracy = 1))
## Warning: Use of `df_cb2$value` is discouraged. Use `value` instead.
## Warning: Use of `df_cb2$value` is discouraged. Use `value` instead.

# data viz3
RE <- dt1_impute[,c(1,6,8)] %>%
  group_by(year) %>%
  summarise('R&D' = sum(ResearchAndDevelopmentExpense), Revenues = sum(Revenues))

# create new data for graph3
df7 <- RE[,c(1,2)]
colnames(df7) <- c("year", "value")
df7 <- df7 %>% mutate(category = "R&D")

df8 <- RE[,c(1,3)]
colnames(df8) <- c("year", "value")
df8 <- df8 %>% mutate(category = "Revenues")

df_cb3 <- rbind(df7, df8)

ggplot(df_cb3, aes(fill=category, y=value, x=year, label =  paste(round(df_cb3$value/10^9,1))) ) + 
    geom_bar(position="stack", stat="identity") +   
    geom_text(position = position_stack(), vjust = 0.5, size = 3, color = "black")+ scale_fill_brewer(palette="GnBu") +
  labs(title="R&D Vs. Revenues of Biotech Companies (2015-2020)", x="Year",y="") +
  scale_y_continuous(labels  = 
                       label_number(scale = 1e-9, prefix = "$", suffix = "B", big.mark = ',',  accuracy = 1))
## Warning: Use of `df_cb3$value` is discouraged. Use `value` instead.
## Warning: Use of `df_cb3$value` is discouraged. Use `value` instead.

# Compute % of deviation of R&D vs. Total Expenses
viz2_table <- viz2 %>%
  mutate(RD_percent = (viz2$total_exp - viz2$`R&D`)/viz2$total_exp*100)
viz2_table
## # A tibble: 6 x 4
##    year       `R&D`    total_exp RD_percent
##   <int>       <dbl>        <dbl>      <dbl>
## 1  2015 58224239803 124138710065       53.1
## 2  2016 83532459692 141586913840       41.0
## 3  2017 78378233158 162151867375       51.7
## 4  2018 49187724053 100248797818       50.9
## 5  2019 68501523762 115979870386       40.9
## 6  2020 80600876946 156883496498       48.6
# Compute % of deviation of R&D vs. Revenues
viz3_table <- RE %>%
  mutate(RD_percent = (RE$Revenues - RE$`R&D`)/RE$Revenues*100)
viz3_table
## # A tibble: 6 x 4
##    year       `R&D`     Revenues RD_percent
##   <int>       <dbl>        <dbl>      <dbl>
## 1  2015 58224239803 203908403809       71.4
## 2  2016 83532459692 152409600862       45.2
## 3  2017 78378233158 244884325213       68.0
## 4  2018 49187724053 105909017040       53.6
## 5  2019 68501523762 154735402558       55.7
## 6  2020 80600876946 264238945192       69.5

Dataset 02: Explorartory Data Analysis

# Check missing data
mis_dt2 <- function(x) {sum(is.na(x))/length(x)*100}
apply(dt2, 2, mis_dt2)
##     Companies        PFIZER      PFIZER.1      PFIZER.2      PFIZER.3 
##             0             0             0             0             0 
##      PFIZER.4      PFIZER.5      PFIZER.6   ASTRAZENECA ASTRAZENECA.1 
##             0             0             0             0             0 
## ASTRAZENECA.2 ASTRAZENECA.3 ASTRAZENECA.4 ASTRAZENECA.5 ASTRAZENECA.6 
##             0             0             0             0             0 
##      BIONTECH    BIONTECH.1    BIONTECH.2    BIONTECH.3    BIONTECH.4 
##             0             0             0             0             0 
##    BIONTECH.5    BIONTECH.6       MODERNA     MODERNA.1     MODERNA.2 
##             0             0             0             0             0 
##     MODERNA.3     MODERNA.4     MODERNA.5     MODERNA.6       NOVAVAX 
##             0             0             0             0             0 
##     NOVAVAX.1     NOVAVAX.2     NOVAVAX.3     NOVAVAX.4     NOVAVAX.5 
##             0             0             0             0             0 
##     NOVAVAX.6 
##             0

1. PFIZER

library(dplyr)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggplot2)
library(wesanderson)

# PFIZER
pfiz <- dt2[3:255,c(1,6,7,8)]
pfiz$Companies <- as.Date(pfiz$Companies)
pfiz$PFIZER.4 <- as.numeric(as.character(pfiz$PFIZER.4))
pfiz$PFIZER.5 <- as.numeric(as.character(pfiz$PFIZER.5))
pfiz$PFIZER.6 <- as.numeric(as.character(pfiz$PFIZER.6))
colnames(pfiz) <- c("date", "volume_pfiz", "adj.close_pfiz", "variation_pfiz")


plot_ly(data = pfiz,
        x = ~ date,
        y = ~ volume_pfiz,
        type = "scatter", 
        mode = "line",
        name = "Trading Volume of Pfizer") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(pfiz$volume_pfiz),
               yend = max(pfiz$volume_pfiz) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(pfiz$volume_pfiz) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(pfiz$volume_pfiz) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Trading Volume of Pfizer Inc.",
         yaxis = list(title = "Volume"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
plot_ly(data = pfiz,
        x = ~ date,
        y = ~ adj.close_pfiz,
        type = "scatter", 
        mode = "line",
        name = "Ajusted Close Price of Pfizer") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(pfiz$adj.close_pfiz),
               yend = max(pfiz$adj.close_pfiz) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(pfiz$adj.close_pfiz) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(pfiz$adj.close_pfiz) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Adjusted Close Price of Pfizer Inc.",
         yaxis = list(title = "Adjusted Close Price (US Dollars)"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
pfiz %>% 
  ggplot(aes(x = date, y = variation_pfiz)) + 
  geom_line(color = wes_palette('Moonrise2', n=1, 'discrete')) + 
  geom_smooth(se = F, lty = 2, color = wes_palette('BottleRocket1', 1)) + 
  labs(x = "Date", y = "Variation", 
       title = "Pfizer Stock Price Variation",
       subtitle = "Smoothed Line in Red") + 
  cowplot::theme_cowplot()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

library(dplyr)
library(tsbox)
library(TSstudio)
## 
## Attaching package: 'TSstudio'
## The following object is masked from 'package:tsbox':
## 
##     ts_plot
## Quantify impact of Covid-19 on share prices

pre_covid_pfiz <- pfiz %>% 
  dplyr::filter(date < as.Date("2020-04-01")) %>%
  dplyr::arrange(date)

post_covid_pfiz <- pfiz %>% 
  dplyr::filter(date >= as.Date("2020-04-01")) %>%
  dplyr::arrange(date)


## The series before the outbreak of the pandemic

# Step 01: convert the series into a 'ts' object first.
ts.obj_pfiz <- ts(pre_covid_pfiz$adj.close_pfiz, start = c(2020, 1), frequency = 12)

ts_plot(ts.obj_pfiz,
        title = "Stock Value of Pfizer",
        Ytitle = "Price per share (US Dollars)",
        slider = TRUE)
ts_seasonal(ts.obj = ts.obj_pfiz, type = "all")
# Step 02: review the series correlation with its past lags using the ACF and PACF to select time-series models for seasonal data functions.
ts_cor(ts.obj = ts.obj_pfiz, lag.max = 36)

Observation: we can see strong correlation between the series and the first and seasonal lags.

## Step 03: select time-series models for seasonal data

methods <- list(
                auto_arima = list(method = "auto.arima",
                              notes = "Auto ARIMA"),
                hw1 = list(method = "HoltWinters",
                          method_arg = NULL,
                          notes = "HoltWinters Model"),
                 hw2 = list(method = "HoltWinters",
                          method_arg = list(seasonal = "multiplicative"),
                          notes = "HW with multip. seasonality"),
                tslm = list(method = "tslm",
                            method_arg = list(formula = input ~ trend + season),
                            notes = "tslm with trend and seasonal"))

# Backtesting is the time series equivalent of the machine learning cross-validation training approach. The idea here is simple - test each model with backtesting, and select the one that performed best, on average, on the different testing partition.
# For the backtesting, we will split the series into 6 testing partitions, each 12 months spaced by 3 months from each other.
train_method = list(partitions = 6,
                    sample.out = 12,
                    space = 3)

# train the models:
# Note that the forecast horizon is set the the length of the post_covid series. In addition we will set the MAPA as the error metric to evaluate the performance of the different models on the testing partitions:
md_pfiz <- train_model(input = ts.obj_pfiz,
                  methods = methods,
                  train_method = train_method,
                  horizon = nrow(post_covid_pfiz),
                  error = "MAPE")
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## # A tibble: 4 x 7
##   model_id  model   notes    avg_mape avg_rmse `avg_coverage_8~ `avg_coverage_9~
##   <chr>     <chr>   <chr>       <dbl>    <dbl>            <dbl>            <dbl>
## 1 tslm      tslm    tslm wi~   0.0656     2.19            0.514            0.708
## 2 hw2       HoltWi~ HW with~   0.0657     2.17            0.458            0.736
## 3 hw1       HoltWi~ HoltWin~   0.0663     2.20            0.486            0.736
## 4 auto_ari~ auto.a~ Auto AR~   0.0748     2.50            0.472            0.681
# Quantify the Covid-19 impact

# select the tslm with trend and seasonal model (tslm) to calculate the Covid19 effect when it demonstrated the lowest error through backtesting 
post_covid_pfiz$yhat <- as.numeric(md_pfiz$forecast$tslm$forecast$mean)
post_covid_pfiz$upper95 <- as.numeric(md_pfiz$forecast$tslm$forecast$upper[,2])
post_covid_pfiz$lower95 <- as.numeric(md_pfiz$forecast$tslm$forecast$lower[,2])

post_covid_pfiz$price_change <- 
  post_covid_pfiz$adj.close_pfiz - post_covid_pfiz$yhat

post_covid_pfiz
##           date volume_pfiz adj.close_pfiz variation_pfiz        yhat   upper95
## 1   2020-04-01    26709625       29.23673   -0.161289215 27.73603115 30.822415
## 2   2020-04-02    27186876       30.26808    0.920303345 27.84106770 30.927451
## 3   2020-04-03    32837370       30.97713    0.958255768 28.20082607 31.287210
## 4   2020-04-06    40503217       31.83351    0.616699219 27.18829441 30.274678
## 5   2020-04-07    24226295       30.94950   -1.318786621 27.24531155 30.331695
## 6   2020-04-08    25523980       31.86113    0.578746796 26.90290623 29.989290
## 7   2020-04-09    30714930       32.58860    0.341556549 27.17977314 30.266157
## 8   2020-04-13    18270141       32.35839   -0.777988434 27.04605427 30.132438
## 9   2020-04-14    21845309       33.55548    0.483871460 27.56552067 30.651904
## 10  2020-04-15    21312196       33.12268   -0.085388184 26.84008389 29.926467
## 11  2020-04-16    20657451       33.03981   -0.227706909 26.27056220 29.345644
## 12  2020-04-17    24404843       33.98827    0.388992310 26.71312526 29.788207
## 13  2020-04-20    22530620       33.22398   -0.284629822 25.87754326 29.026465
## 14  2020-04-21    23975233       32.80039   -0.151802063 25.98257980 29.131501
## 15  2020-04-22    20186103       33.38052   -0.673625946 26.34233818 29.491260
## 16  2020-04-23    20185365       33.78569    0.388992310 25.32980652 28.478728
## 17  2020-04-24    17359380       34.42107    0.512336731 25.38682365 28.535745
## 18  2020-04-27    23394057       35.29587    0.702083588 25.04441833 28.193340
## 19  2020-04-28    35666728       34.90912   -1.081592560 25.32128525 28.470207
## 20  2020-04-29    26518535       35.10250   -0.322578430 25.18756638 28.336488
## 21  2020-04-30    29450973       35.32349    0.341556549 25.70703278 28.855954
## 22  2020-05-01    22679867       34.66049   -0.199241638 24.98159599 28.130517
## 23  2020-05-04    19893512       34.64208    0.018978119 24.41207431 27.558784
## 24  2020-05-05    24123952       35.46163   -0.284629822 24.85463737 28.001347
## 25  2020-05-06    24047432       35.46163   -0.436431885 24.01905537 27.246603
## 26  2020-05-07    33546080       34.37350   -1.375709534 24.12409191 27.351639
## 27  2020-05-08    19901312       34.61531   -0.028465271 24.48385029 27.711398
## 28  2020-05-11    25620421       35.43372    0.806449890 23.47131863 26.698866
## 29  2020-05-12    19335314       34.74551   -0.721065521 23.52833576 26.755883
## 30  2020-05-13    24596882       34.45720   -0.246681213 23.18593044 26.413478
## 31  2020-05-14    22362507       35.26632    1.062618256 23.46279736 26.690345
## 32  2020-05-15    25460002       35.11752   -0.189750671 23.32907848 26.556626
## 33  2020-05-18    22639077       35.40582   -0.256168365 23.84854488 27.076092
## 34  2020-05-19    19476234       35.04312   -0.161289215 23.12310810 26.350656
## 35  2020-05-20    17504200       34.99662   -0.303604126 22.55358642 25.787600
## 36  2020-05-21    17652181       34.65250   -0.294116974 22.99614948 26.230163
## 37  2020-05-22    15781226       34.87571    0.370018005 22.16056747 25.481686
## 38  2020-05-26    23260094       34.86641   -0.151802063 22.26560402 25.586723
## 39  2020-05-27    25904158       34.79201    0.000000000 22.62536240 25.946481
## 40  2020-05-28    22521767       35.50812    0.322578430 21.61283073 24.933950
## 41  2020-05-29    48638200       35.51743   -0.075901031 21.66984787 24.990967
## 42  2020-06-01    76884979       32.97847   -0.037948608 21.32744255 24.648561
## 43  2020-06-02    42690900       33.62949    0.597721100 21.60430946 24.925428
## 44  2020-06-03    38751996       33.62949   -0.123340607 21.47059059 24.791709
## 45  2020-06-04    27013388       33.48998    0.284629822 21.99005699 25.311176
## 46  2020-06-05    36705655       33.47139   -0.237190247 21.26462021 24.585739
## 47  2020-06-08    30194149       34.02940    0.749526978 20.69509853 24.030862
## 48  2020-06-09    21675088       33.67599   -0.275142670 21.13766158 24.473425
## 49  2020-06-10    29557217       33.40628   -0.417457581 20.30207958 23.730492
## 50  2020-06-11    63166642       30.96963   -2.182165146 20.40711613 23.835529
## 51  2020-06-12    37902472       31.38814   -0.313091278 20.76687450 24.195287
## 52  2020-06-15    36247482       31.02543    0.056926727 19.75434284 23.182755
## 53  2020-06-16    55616945       31.06263   -0.180265427 19.81135998 23.239772
## 54  2020-06-17    27509822       31.21144   -0.028463364 19.46895466 22.897367
## 55  2020-06-18    32278855       30.91383   -0.028463364 19.74582157 23.174234
## 56  2020-06-19    43986266       31.08124   -0.256166458 19.61210270 23.040515
## 57  2020-06-22    23332503       30.79293   -0.208728790 20.13156910 23.559981
## 58  2020-06-23    37690408       30.47672   -0.407968521 19.40613232 22.834545
## 59  2020-06-24    30817695       29.93731   -0.493360519 18.83661064 22.287292
## 60  2020-06-25    27418651       30.05821    0.199241638 19.27917369 22.729855
## 61  2020-06-26    42153992       29.79781   -0.199241638 18.44359169 21.991775
## 62  2020-06-29    23956261       30.35582    0.170778275 18.54862823 22.096812
## 63  2020-06-30    25451676       30.41162    0.161289215 18.90838661 22.456570
## 64  2020-07-01    73503009       31.37884   -0.759010315 17.89585495 21.444038
## 65  2020-07-02    45270881       32.09496   -0.104362488 17.95287209 21.501055
## 66  2020-07-06    27150935       32.09496   -0.417457581 17.61046677 21.158650
## 67  2020-07-07    25148018       31.64855   -0.018974304 17.88733368 21.435517
## 68  2020-07-08    24256545       31.38814   -0.246677399 17.75361481 21.301798
## 69  2020-07-09    20363702       31.11843   -0.256168365 18.27308121 21.821264
## 70  2020-07-10    24459019       31.46254    0.161291122 17.54764442 21.095828
## 71  2020-07-13    56569129       32.74596    0.455406189 16.97812274 20.555622
## 72  2020-07-14    32854129       32.76457   -0.132827759 17.42068580 20.998185
## 73  2020-07-15    26294454       33.22028   -0.256168365 16.58510380 20.264317
## 74  2020-07-16    18421812       33.10868    0.000000000 16.69014034 20.369354
## 75  2020-07-17    23347365       33.71318    0.370018005 17.04989872 20.729112
## 76  2020-07-20    36815693       33.94569   -0.939277649 16.03736706 19.716580
## 77  2020-07-21    23646912       34.12239   -0.237194061 16.09438419 19.773597
## 78  2020-07-22    90705554       35.86153    0.313091278 15.75197887 19.431192
## 79  2020-07-23    59639536       35.72203   -0.777992249 16.02884579 19.708059
## 80  2020-07-24    33870501       35.02451   -0.351043701 15.89512691 19.574340
## 81  2020-07-27    27640307       34.91291    0.170780182 16.41459332 20.093807
## 82  2020-07-28    56393954       36.28934    0.455410004 15.68915653 19.368370
## 83  2020-07-29    37033660       36.51255   -0.037952423 15.11963485 18.834633
## 84  2020-07-30    32273796       36.38107    0.436431885 15.56219791 19.277197
## 85  2020-07-31    28503006       36.13690    0.189750671 14.72661591 18.546960
## 86  2020-08-03    31036400       36.01482   -0.246677399 14.83165245 18.651996
## 87  2020-08-04    29448549       36.05238    0.075901031 15.19141083 19.011755
## 88  2020-08-05    24604787       36.10873   -0.018974304 14.17887917 17.999223
## 89  2020-08-06    20922111       35.93969   -0.474380493 14.23589630 18.056240
## 90  2020-08-07    23097251       36.10873    0.142314911 13.89349098 17.713835
## 91  2020-08-10    19964552       36.05238   -0.151802063 14.17035789 17.990702
## 92  2020-08-11    21326847       35.48891   -0.730548859 14.03663902 17.856983
## 93  2020-08-12    19965817       35.99603    0.474380493 14.55610542 18.376449
## 94  2020-08-13    15340021       35.84578    0.094875336 13.83066864 17.651013
## 95  2020-08-14    12901276       35.74248    0.047439575 13.26114696 17.123185
## 96  2020-08-17    17494187       36.01482    0.294116974 13.70371002 17.565748
## 97  2020-08-18    10846398       36.02421   -0.085388184 12.86812801 16.838627
## 98  2020-08-19    15690055       35.93030   -0.208728790 12.97316456 16.943663
## 99  2020-08-20    21957455       36.36229    0.512336731 13.33292294 17.303422
## 100 2020-08-21    25467170       36.51255   -0.028461456 12.32039127 16.290890
## 101 2020-08-24    25011947       36.47498   -0.170780182 12.37740841 16.347907
## 102 2020-08-25    26221201       36.07116   -0.094879150 12.03500309 16.005502
## 103 2020-08-26    24083689       35.73309   -0.132827759 12.31187000 16.282369
## 104 2020-08-27    22156556       35.55465   -0.094875336 12.17815113 16.148650
## 105 2020-08-28    32830519       35.60161    0.037952423 12.69761753 16.668116
## 106 2020-08-31    30032465       35.48891   -0.047439575 11.97218075 15.942679
## 107 2020-09-01    36161475       34.63433   -0.863376617 11.40265907 15.420230
## 108 2020-09-02    29068688       34.93484    0.407970428 11.84522212 15.862793
## 109 2020-09-03    35956788       34.18356   -0.920303345 11.00964012 15.138333
## 110 2020-09-04    26872257       34.14599   -0.123340607 11.11467667 15.243369
## 111 2020-09-08    26736818       33.74218   -0.569259644 11.47443504 15.603128
## 112 2020-09-09    27490533       33.97696   -0.104362488 10.46190338 14.590596
## 113 2020-09-10    21650741       33.47922   -0.521823883 10.51892052 14.647613
## 114 2020-09-11    24109301       33.87365    0.313095093 10.17651520 14.305208
## 115 2020-09-14    29565543       34.75641    0.616699219 10.45338211 14.582075
## 116 2020-09-15    22003726       34.70946   -0.218215942 10.31966324 14.448356
## 117 2020-09-16    22208412       34.54042   -0.474380493 10.83912964 14.967822
## 118 2020-09-17    17894390       34.57798    0.037948608 10.11369286 14.242385
## 119 2020-09-18    31694623       34.39955   -0.142314911  9.54417117 13.724820
## 120 2020-09-21    26608968       33.82669   -0.332069397  9.98673423 14.167383
## 121 2020-09-22    22727297       34.04269    0.417457581  9.15115223 13.445190
## 122 2020-09-23    23102521       33.80791   -0.322578430  9.25618877 13.550226
## 123 2020-09-24    20859925       33.58253   -0.180263519  9.61594715 13.909985
## 124 2020-09-25    14365598       33.85487    0.483871460  8.60341549 12.897453
## 125 2020-09-28    15044269       34.17416    0.218215942  8.66043262 12.954470
## 126 2020-09-29    13512491       33.96756   -0.246681213  8.31802731 12.612065
## 127 2020-09-30    22629275       34.46529    0.407966614  8.59489422 12.888932
## 128 2020-10-01    22520396       34.15538   -0.512332916  8.46117535 12.755213
## 129 2020-10-02    21461759       34.16477    0.303604126  8.98064175 13.274679
## 130 2020-10-05    19400029       34.51224    0.208728790  8.25520496 12.549242
## 131 2020-10-06    25233392       33.96756   -0.787475586  7.68568328 12.036107
## 132 2020-10-07    22160034       34.24929    0.199237823  8.12824634 12.478670
## 133 2020-10-08    17857395       34.64372    0.275142670  7.29266434 11.758403
## 134 2020-10-09    23667465       34.54981   -0.037948608  7.39770088 11.863440
## 135 2020-10-12    19413837       34.57798    0.113853455  7.75745926 12.223198
## 136 2020-10-13    21543233       34.65311    0.218215942  6.74492760 11.210667
## 137 2020-10-14    22124619       34.61555   -0.142314911  6.80194473 11.267684
## 138 2020-10-15    16089205       34.32442   -0.075904846  6.45953941 10.925278
## 139 2020-10-16    42993503       35.63917    0.834915161  6.73640633 11.202145
## 140 2020-10-19    31931352       35.49831   -0.588237762  6.60268745 11.068426
## 141 2020-10-20    22384852       35.20718   -0.360530853  7.12215385 11.587893
## 142 2020-10-21    18984859       34.82215   -0.256164551  6.39671707 10.862456
## 143 2020-10-22    19243616       35.15084    0.332065582  5.82719539 10.353337
## 144 2020-10-23    30557884       35.85517    0.237190247  6.26975845 10.795900
## 145 2020-10-26    33783124       35.61100    0.047439575  5.43417645 10.077269
## 146 2020-10-27    36851529       35.15084    0.370018005  5.53921299 10.182305
## 147 2020-10-28    33857537       33.29140   -1.280834198  5.89897137 10.542064
## 148 2020-10-29    27545341       33.13175   -0.189754486  4.88643970  9.529532
## 149 2020-10-30    25631067       33.31958    0.227703094  4.94345684  9.586549
## 150 2020-11-02    21430666       34.02391    0.284629822  4.60105152  9.244144
## 151 2020-11-03    21636617       33.98634   -0.227706909  4.87791843  9.521011
## 152 2020-11-04    40870536       35.05693    0.256168365  4.74419956  9.387292
## 153 2020-11-05    33926995       34.52562   -0.815940857  5.26366596  9.906758
## 154 2020-11-06    23870676       34.53510    0.009487152  4.53822918  9.181321
## 155 2020-11-09   230153864       37.19165   -2.523719788  3.96870750  8.675845
## 156 2020-11-10    80091668       36.69829   -1.679317474  4.41127055  9.118408
## 157 2020-11-11    58980997       36.52752   -0.360530853  3.57568855  8.401162
## 158 2020-11-12    46767877       35.62619   -0.692600250  3.68072510  8.506199
## 159 2020-11-13    40175634       36.64137    0.711574554  4.04048347  8.865957
## 160 2020-11-16    75530167       35.41746   -0.502845764  3.02795181  7.853426
## 161 2020-11-17    46984500       36.04000   -0.450000763  3.08496895  7.910443
## 162 2020-11-18    63442700       36.32000   -0.610000610  2.74256363  7.568037
## 163 2020-11-19    43261800       36.19000   -0.409999847  3.01943054  7.844904
## 164 2020-11-20    60633800       36.70000    0.000000000  2.88571167  7.711186
## 165 2020-11-23    43673100       36.52000   -0.509998322  3.40517807  8.230652
## 166 2020-11-24    42485300       36.60000    0.469997406  2.67974129  7.505215
## 167 2020-11-25    26955800       36.53000    0.000000000  2.11021961  7.003046
## 168 2020-11-27    25277000       37.23000    0.450000763  2.55278266  7.445609
## 169 2020-11-30    65425900       38.31000    0.360000610  1.71720066  6.729536
## 170 2020-12-01    72660800       39.41000    0.009998322  1.82223721  6.834572
## 171 2020-12-02    84347500       40.80000    0.329998016  2.18199558  7.194331
## 172 2020-12-03    68570100       40.09000   -0.889999390  1.16946392  6.181799
## 173 2020-12-04    35368100       40.34000    0.450000763  1.22648106  6.238816
## 174 2020-12-07    48253700       41.25000    0.590000153  0.88407574  5.896411
## 175 2020-12-08    87039300       42.56000    1.159999847  1.16094265  6.173278
## 176 2020-12-09    86118500       41.85000   -1.220001221  1.02722378  6.039559
## 177 2020-12-10    57749200       41.73000   -0.090000153  1.54669018  6.559025
## 178 2020-12-11    60737000       41.12000   -0.850002289  0.82125340  5.833588
## 179 2020-12-14    94809700       39.21000   -2.409999847  0.25173171  5.334424
## 180 2020-12-15    66003300       38.71000   -0.350002289  0.69429477  5.776987
## 181 2020-12-16    56515300       37.84000   -0.340000153 -0.14128723  5.061906
## 182 2020-12-17    52036400       38.03000    0.199996948 -0.03625069  5.166943
## 183 2020-12-18    60259200       37.68000   -0.310001373  0.32350769  5.526701
## 184 2020-12-21    40891800       37.38000    0.100002289 -0.68902397  4.514169
## 185 2020-12-22    33634400       36.74000   -0.469997406 -0.63200684  4.571186
## 186 2020-12-23    36182000       37.44000    0.430000305 -0.97441216  4.228781
## 187 2020-12-24    14790100       37.27000   -0.130001068 -0.69754524  4.505648
## 188 2020-12-28    26993700       36.82000   -0.540000916 -0.83126411  4.371929
## 189 2020-12-29    23152100       37.05000    0.149997711 -0.31179771  4.891396
## 190 2020-12-30    24889800       36.74000   -0.289997101 -1.03723450  4.165959
## 191 2020-12-31    30755600       36.81000    0.150001526 -1.60675618  3.669529
##         lower95 price_change
## 1   24.64964770     1.500701
## 2   24.75468425     2.427008
## 3   25.11444262     2.776299
## 4   24.10191096     4.645213
## 5   24.15892810     3.704186
## 6   23.81652278     4.958227
## 7   24.09338969     5.408823
## 8   23.95967082     5.312333
## 9   24.47913722     5.989956
## 10  23.75370043     6.282601
## 11  23.19548009     6.769248
## 12  23.63804314     7.275148
## 13  22.72862181     7.346433
## 14  22.83365835     6.817809
## 15  23.19341673     7.038178
## 16  22.18088507     8.455884
## 17  22.23790220     9.034246
## 18  21.89549688    10.251453
## 19  22.17236380     9.587837
## 20  22.03864492     9.914931
## 21  22.55811132     9.616461
## 22  21.83267454     9.678896
## 23  21.26536496    10.230004
## 24  21.70792801    10.606991
## 25  20.79150796    11.442573
## 26  20.89654451    10.249409
## 27  21.25630288    10.131457
## 28  20.24377122    11.962405
## 29  20.30078836    11.217171
## 30  19.95838304    11.271272
## 31  20.23524995    11.803522
## 32  20.10153108    11.788437
## 33  20.62099748    11.557278
## 34  19.89556070    11.920009
## 35  19.31957314    12.443030
## 36  19.76213619    11.656354
## 37  18.83944859    12.715146
## 38  18.94448514    12.600805
## 39  19.30424352    12.166649
## 40  18.29171185    13.895291
## 41  18.34872899    13.847578
## 42  18.00632367    11.651027
## 43  18.28319058    12.025177
## 44  18.14947171    12.158895
## 45  18.66893811    11.499926
## 46  17.94350133    12.206766
## 47  17.35933527    13.334298
## 48  17.80189833    12.538326
## 49  16.87366721    13.104201
## 50  16.97870375    10.562515
## 51  17.33846213    10.621267
## 52  16.32593047    11.271090
## 53  16.38294760    11.251274
## 54  16.04054228    11.742483
## 55  16.31740920    11.168010
## 56  16.18369032    11.469133
## 57  16.70315672    10.661358
## 58  15.97771994    11.070588
## 59  15.38592904    11.100701
## 60  15.82849209    10.779039
## 61  14.89540840    11.354214
## 62  15.00044495    11.807191
## 63  15.36020332    11.503233
## 64  14.34767166    13.482986
## 65  14.40468880    14.142083
## 66  14.06228348    14.484489
## 67  14.33915039    13.761214
## 68  14.20543152    13.634527
## 69  14.72489792    12.845354
## 70  13.99946114    13.914899
## 71  13.40062320    15.767841
## 72  13.84318626    15.343886
## 73  12.90589053    16.635172
## 74  13.01092707    16.418537
## 75  13.37068545    16.663286
## 76  12.35815379    17.908323
## 77  12.41517092    18.028010
## 78  12.07276560    20.109551
## 79  12.34963252    19.693181
## 80  12.21591364    19.129386
## 81  12.73538004    18.498317
## 82  12.00994326    20.600184
## 83  11.40463622    21.392912
## 84  11.84719928    20.818871
## 85  10.90627189    21.410286
## 86  11.01130844    21.183168
## 87  11.37106681    20.860969
## 88  10.35853515    21.929847
## 89  10.41555229    21.703793
## 90  10.07314697    22.215236
## 91  10.35001388    21.882022
## 92  10.21629501    21.452275
## 93  10.73576141    21.439927
## 94  10.01032463    22.015107
## 95   9.39910875    22.481330
## 96   9.84167181    22.311110
## 97   8.89762944    23.156080
## 98   9.00266599    22.957133
## 99   9.36242436    23.029366
## 100  8.34989270    24.192155
## 101  8.40690984    24.097571
## 102  8.06450452    24.036160
## 103  8.34137143    23.421216
## 104  8.20765256    23.376502
## 105  8.72711896    22.903995
## 106  8.00168218    23.516734
## 107  7.38508818    23.231668
## 108  7.82765123    23.089619
## 109  6.88094761    23.173915
## 110  6.98598415    23.031316
## 111  7.34574253    22.267741
## 112  6.33321087    23.515052
## 113  6.39022801    22.960301
## 114  6.04782269    23.697134
## 115  6.32468960    24.303030
## 116  6.19097073    24.389794
## 117  6.71043713    23.701291
## 118  5.98500034    24.464287
## 119  5.36352230    24.855380
## 120  5.80608536    23.839960
## 121  4.85711482    24.891534
## 122  4.96215137    24.551722
## 123  5.32190975    23.966580
## 124  4.30937808    25.251454
## 125  4.36639522    25.513731
## 126  4.02398990    25.649533
## 127  4.30085681    25.870392
## 128  4.16713794    25.694209
## 129  4.68660434    25.184130
## 130  3.96116756    26.257036
## 131  3.33525955    26.281877
## 132  3.77782261    26.121044
## 133  2.82692531    27.351054
## 134  2.93196186    27.152108
## 135  3.29172023    26.820521
## 136  2.27918857    27.908183
## 137  2.33620571    27.813602
## 138  1.99380039    27.864882
## 139  2.27066730    28.902765
## 140  2.13694843    28.895619
## 141  2.65641483    28.085030
## 142  1.93097805    28.425434
## 143  1.30105347    29.323642
## 144  1.74361653    29.585409
## 145  0.79108423    30.176824
## 146  0.89612078    29.611624
## 147  1.25587915    27.392433
## 148  0.24334749    28.245312
## 149  0.30036463    28.376119
## 150 -0.04204069    29.422855
## 151  0.23482622    29.108421
## 152  0.10110735    30.312727
## 153  0.62057375    29.261950
## 154 -0.10486303    29.996874
## 155 -0.73843039    33.222943
## 156 -0.29586733    32.287021
## 157 -1.24978531    32.951827
## 158 -1.14474876    31.945461
## 159 -0.78499038    32.600882
## 160 -1.79752205    32.389506
## 161 -1.74050491    32.955032
## 162 -2.08291023    33.577436
## 163 -1.80604332    33.170568
## 164 -1.93976219    33.814289
## 165 -1.42029579    33.114822
## 166 -2.14573257    33.920257
## 167 -2.78260637    34.419779
## 168 -2.34004331    34.677217
## 169 -3.29513443    36.592801
## 170 -3.19009789    37.587763
## 171 -2.83033951    38.618004
## 172 -3.84287117    38.920536
## 173 -3.78585403    39.113519
## 174 -4.12825935    40.365924
## 175 -3.85139244    41.399059
## 176 -3.98511131    40.822775
## 177 -3.46564491    40.183309
## 178 -4.19108170    40.298746
## 179 -4.83096022    38.958267
## 180 -4.38839716    38.015704
## 181 -5.34448054    37.981287
## 182 -5.23944399    38.066249
## 183 -4.87968562    37.356493
## 184 -5.89221728    38.069025
## 185 -5.83520014    37.372009
## 186 -6.17760546    38.414411
## 187 -5.90073855    37.967546
## 188 -6.03445742    37.651264
## 189 -5.51499102    37.361797
## 190 -6.24042780    37.777236
## 191 -6.88304096    38.416758
# the estimated total stock value change between April and August 2020 as result of the pandemic
sum(post_covid_pfiz$price_change)
## [1] 4090.168
# Data viz
plot_ly() %>%
   add_ribbons(x = post_covid_pfiz$date,
              ymin = post_covid_pfiz$adj.close_pfiz,
              ymax = post_covid_pfiz$yhat,
              line = list(color = 'rgba(255, 0, 0, 0.05)'),
              fillcolor = 'rgba(255, 0, 0, 0.6)',
              name = "Estimated Change") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(pfiz$adj.close_pfiz),
               yend = max(pfiz$adj.close_pfiz) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2020-01-01"),
                  y = max(pfiz$adj.close_pfiz) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-11-01"),
                  y = max(pfiz$adj.close_pfiz) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = paste("Estimated increase in", "<br>",
                               "stock price: ~$4,090",
                               sep = ""),
                  x = as.Date("2020-07-01"),
                  y = 2 * 10^1,
                  arrowhead = 1,
                  ax = -150,
                  ay = -10,
                  showarrow = TRUE) %>%
  add_lines(x = pfiz$date,
            y = pfiz$adj.close_pfiz,
            line = list(color = "#1f77b4"),
            name = "Actual") %>%
  layout(title = "Covid19 Impact on Stock Value of Pfizer Inc.",
         yaxis = list(title = "Stock Price (US Dollars)"),
         xaxis = list(title = "Time Series Model - tslm with trend and seasonal",
                      range = c(as.Date("2019-10-01"), as.Date("2021-01-01"))),
         legend = list(x = 0, y = 0.95))

2. ASTRAZENECA

library(dplyr)
library(plotly)
library(ggplot2)
library(wesanderson)

# ASTRAZENECA
astra <- dt2[3:253,c(1,13,14,15)]
astra$Companies <- as.Date(astra$Companies)
astra$ASTRAZENECA.4 <- as.numeric(as.character(astra$ASTRAZENECA.4))
astra$ASTRAZENECA.5 <- as.numeric(as.character(astra$ASTRAZENECA.5))
astra$ASTRAZENECA.6 <- as.numeric(as.character(astra$ASTRAZENECA.6))
colnames(astra) <- c("date", "volume_astra", "adj.close_astra", "variation_astra")


plot_ly(data = astra,
        x = ~ date,
        y = ~ volume_astra,
        type = "scatter", 
        mode = "line",
        name = "Trading Volume of Astrazeneca") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(astra$volume_astra),
               yend = max(astra$volume_astra) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(astra$volume_astra) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(astra$volume_astra) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Trading Volume of Astrazeneca Inc.",
         yaxis = list(title = "Volume"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
plot_ly(data = astra,
        x = ~ date,
        y = ~ adj.close_astra,
        type = "scatter", 
        mode = "line",
        name = "Ajusted Close Price of Astrazeneca") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(astra$adj.close_astra),
               yend = max(astra$adj.close_astra) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(astra$adj.close_astra) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(astra$adj.close_astra) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Adjusted Close Price of Astrazeneca Inc.",
         yaxis = list(title = "Adjusted Close Price"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
astra %>% 
  ggplot(aes(x = date, y = variation_astra)) + 
  geom_line(color = wes_palette('Moonrise2', n=1, 'discrete')) + 
  geom_smooth(se = F, lty = 2, color = wes_palette('BottleRocket1', 1)) + 
  labs(x = "Date", y = "Variation", 
       title = "Astrazeneca Stock Price Variation",
       subtitle = "Smoothed Line in Red") + 
  cowplot::theme_cowplot()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

library(dplyr)
library(tsbox)
library(TSstudio)

## Quantify impact of Covid-19 on share prices

pre_covid_astra <- astra %>% 
  dplyr::filter(date < as.Date("2020-04-01")) %>%
  dplyr::arrange(date)

post_covid_astra <- astra %>% 
  dplyr::filter(date >= as.Date("2020-04-01")) %>%
  dplyr::arrange(date)


## The series before the outbreak of the pandemic

# Step 01: convert the series into a 'ts' object first.
ts.obj_astra <- ts(pre_covid_astra$adj.close_astra, start = c(2020, 1), frequency = 12)

ts_plot(ts.obj_astra,
        title = "Stock Value of ASTRAZENECA",
        Ytitle = "Price per share (US Dollars)",
        slider = TRUE)
ts_seasonal(ts.obj = ts.obj_astra, type = "all")
# Step 02: review the series correlation with its past lags using the ACF and PACF to select time-series models for seasonal data functions.
ts_cor(ts.obj = ts.obj_astra, lag.max = 36)
## Step 03: select time-series models for seasonal data

methods <- list(
                auto_arima = list(method = "auto.arima",
                              notes = "Auto ARIMA"),
                hw1 = list(method = "HoltWinters",
                          method_arg = NULL,
                          notes = "HoltWinters Model"),
                 hw2 = list(method = "HoltWinters",
                          method_arg = list(seasonal = "multiplicative"),
                          notes = "HW with multip. seasonality"),
                tslm = list(method = "tslm",
                            method_arg = list(formula = input ~ trend + season),
                            notes = "tslm with trend and seasonal"))

# Backtesting is the time series equivalent of the machine learning cross-validation training approach. The idea here is simple - test each model with backtesting, and select the one that performed best, on average, on the different testing partition.
# For the backtesting, we will split the series into 6 testing partitions, each 12 months spaced by 3 months from each other.
train_method = list(partitions = 6,
                    sample.out = 12,
                    space = 3)

# train the models:
# Note that the forecast horizon is set the the length of the post_covid series. In addition we will set the MAPA as the error metric to evaluate the performance of the different models on the testing partitions:
md_astra <- train_model(input = ts.obj_astra,
                  methods = methods,
                  train_method = train_method,
                  horizon = nrow(post_covid_astra),
                  error = "MAPE")
## # A tibble: 4 x 7
##   model_id  model   notes    avg_mape avg_rmse `avg_coverage_8~ `avg_coverage_9~
##   <chr>     <chr>   <chr>       <dbl>    <dbl>            <dbl>            <dbl>
## 1 hw1       HoltWi~ HoltWin~   0.0821     3.80            0.389            0.528
## 2 hw2       HoltWi~ HW with~   0.0822     3.81            0.389            0.486
## 3 tslm      tslm    tslm wi~   0.0864     3.99            0.194            0.375
## 4 auto_ari~ auto.a~ Auto AR~   0.0955     4.43            0.361            0.431
# Quantify the Covid-19 impact

# select the HoltWinters Model (hw1) to calculate the Covid-19 effect when it demonstrated the lowest error through backtesting 
post_covid_astra$yhat <- as.numeric(md_astra$forecast$hw1$forecast$mean)
post_covid_astra$upper95 <- as.numeric(md_astra$forecast$hw1$forecast$upper[,2])
post_covid_astra$lower95 <- as.numeric(md_astra$forecast$hw1$forecast$lower[,2])

post_covid_astra$price_change <- 
  post_covid_astra$adj.close_astra - post_covid_astra$yhat

post_covid_astra
##           date volume_astra adj.close_astra variation_astra     yhat  upper95
## 1   2020-04-01      6097700        43.43278      0.02999878 43.12983 46.07910
## 2   2020-04-02      5918800        43.41294      0.32999802 43.40092 47.05644
## 3   2020-04-03      3372700        43.61135      0.18999863 42.93923 47.18511
## 4   2020-04-06      5735200        43.95858      0.29000092 43.31835 48.08197
## 5   2020-04-07      5600600        42.75817     -0.38000107 43.25865 48.48903
## 6   2020-04-08      3860200        43.50223      0.43999863 42.89990 48.55865
## 7   2020-04-09      5628200        44.30581      0.75000000 43.19489 49.25179
## 8   2020-04-13      6555700        46.98439      1.95000076 42.95786 49.38832
## 9   2020-04-14      5109000        48.29392      1.04000092 43.87225 50.65572
## 10  2020-04-15      2901800        47.64907      0.36000061 43.53043 50.64943
## 11  2020-04-16      3218600        48.74035      0.58000183 43.38274 50.82214
## 12  2020-04-17      3349400        49.63321     -0.17000198 43.51511 51.26168
## 13  2020-04-20      3416400        50.03996      0.61999893 42.38282 50.55395
## 14  2020-04-21      3545400        49.67290     -0.02000046 42.65392 51.10566
## 15  2020-04-22      2803600        49.96060     -0.27999878 42.19222 50.91556
## 16  2020-04-23      2549400        49.99036      0.09999847 42.57134 51.55806
## 17  2020-04-24      2087600        50.77409      0.47999954 42.51165 51.75426
## 18  2020-04-27      2105000        51.07172     -0.13000107 42.15289 51.64449
## 19  2020-04-28      5767500        50.56576     -1.34999847 42.44788 52.18211
## 20  2020-04-29      3786000        50.31775     -1.29999924 42.21086 52.18180
## 21  2020-04-30      5863600        51.86537     -1.69000244 43.12525 53.32743
## 22  2020-05-01      3206400        51.44870     -0.16999817 42.78343 53.21171
## 23  2020-05-04      2852900        51.93481      0.12999725 42.63573 53.28532
## 24  2020-05-05      4579300        53.04593      0.84000015 42.76811 53.63449
## 25  2020-05-06      6604600        53.61141     -0.02000046 41.63582 52.80884
## 26  2020-05-07      2626600        52.32172     -0.75000000 41.90691 53.28676
## 27  2020-05-08      1660100        52.86736      0.19000244 41.44522 53.02821
## 28  2020-05-11      3095300        53.52213      0.92000198 41.82433 53.60696
## 29  2020-05-12      2221500        53.68086     -0.31999969 41.76464 53.74358
## 30  2020-05-13      3431200        54.87134      0.49000168 41.40589 53.57797
## 31  2020-05-14      4998300        53.07570     -0.49000168 41.70088 54.06309
## 32  2020-05-15      3716800        52.98641      0.82999802 41.46385 54.01331
## 33  2020-05-18      4926300        53.02609     -0.18999863 42.37824 55.11219
## 34  2020-05-19      3271200        53.14514      0.18000031 42.03642 54.95223
## 35  2020-05-20      4038000        53.35347     -0.55000305 41.88873 54.98387
## 36  2020-05-21     20428200        54.84158     -1.67000198 42.02110 55.29316
## 37  2020-05-22      6031200        54.78205      0.51000214 40.88881 54.41306
## 38  2020-05-26      5626600        52.68879     -1.40999985 41.15991 54.85553
## 39  2020-05-27      7010200        52.05386      0.52000046 40.69821 54.56308
## 40  2020-05-28      4445800        53.07570     -0.11999893 41.07733 55.10941
## 41  2020-05-29      7615800        54.16697     -0.68000031 41.01764 55.21496
## 42  2020-06-01      5759800        54.72253      0.84000015 40.65888 55.01954
## 43  2020-06-02      4279400        54.28602      0.22999954 40.95387 55.47604
## 44  2020-06-03      5464200        54.56380      0.45000076 40.71685 55.39874
## 45  2020-06-04      4483700        53.44276     -0.72000122 41.63124 56.47113
## 46  2020-06-05      6092400        53.42292     -0.49000168 41.28942 56.28566
## 47  2020-06-08      9762600        52.16299     -0.22999954 41.14172 56.29269
## 48  2020-06-09      4782300        52.31180     -0.06999969 41.27409 56.57823
## 49  2020-06-10      4582600        53.21458     -0.22000122 40.14181 55.66516
## 50  2020-06-11      4787000        51.16100     -2.20000076 40.41290 56.08578
## 51  2020-06-12      5630300        51.06179     -0.70999908 39.95121 55.77220
## 52  2020-06-15      5711800        52.22252      0.20000076 40.33032 56.29805
## 53  2020-06-16      5535500        53.15506     -0.59999847 40.27063 56.38376
## 54  2020-06-17      4919100        53.47252     -0.21999741 39.91188 56.16911
## 55  2020-06-18      3796100        52.24236      0.02000046 40.20687 56.60694
## 56  2020-06-19      6807200        52.59950      0.52000046 39.96984 56.51151
## 57  2020-06-22      3375400        52.99633      0.04999924 40.88423 57.56630
## 58  2020-06-23     10772000        53.81975      1.08000183 40.54241 57.36372
## 59  2020-06-24      6418600        51.71656     -0.78999710 40.39472 57.35411
## 60  2020-06-25      4948300        52.58958      0.51999664 40.52709 57.62345
## 61  2020-06-26      6234700        52.28204     -0.38000107 39.39480 56.68768
## 62  2020-06-29      4526200        52.49038     -0.45999908 39.66590 57.09312
## 63  2020-06-30      4621900        52.47053      0.61000061 39.20420 56.76475
## 64  2020-07-01      5462000        52.89712      0.48999786 39.58332 57.27618
## 65  2020-07-02      4591300        53.37331      0.16999817 39.52363 57.34783
## 66  2020-07-06      5180900        53.67094      0.25000000 39.16487 57.11945
## 67  2020-07-07      3747500        53.04593     -0.02999878 39.45986 57.54387
## 68  2020-07-08      6247800        53.76022      0.47999954 39.22283 57.43536
## 69  2020-07-09      4203000        53.54197     -0.52999878 40.13722 58.47736
## 70  2020-07-10      5414500        53.33363     -0.55000305 39.79541 58.26228
## 71  2020-07-13      5639800        52.81776     -0.68999863 39.64771 58.24045
## 72  2020-07-14      7199600        53.55189      0.47999954 39.78008 58.49785
## 73  2020-07-15     19902500        57.54000      1.68000031 38.64780 57.54522
## 74  2020-07-16     12358700        56.97453      0.47000122 38.91889 57.93933
## 75  2020-07-17     24319500        60.61542      3.44999695 38.45720 57.59987
## 76  2020-07-20     56346600        58.21461     -4.00999832 38.83631 58.10044
## 77  2020-07-21     15419100        57.60945     -1.58000183 38.77662 58.16144
## 78  2020-07-22      9562100        56.52810     -0.40000153 38.41787 57.92263
## 79  2020-07-23      8658900        54.73245     -1.58000183 38.71286 58.33683
## 80  2020-07-24      7748000        55.35745      0.48999786 38.47583 58.21830
## 81  2020-07-27      5660200        56.13127      0.42000198 39.39022 59.25047
## 82  2020-07-28      4979100        55.87333     -0.56000137 39.04840 59.02575
## 83  2020-07-29      5038900        56.19079     -0.40999985 38.90070 58.99446
## 84  2020-07-30      8215100        57.43088      0.77000046 39.03308 59.24257
## 85  2020-07-31      6844500        55.33761     -2.04000092 37.90079 58.27680
## 86  2020-08-03      4799700        56.36937      0.13999939 38.17188 58.66203
## 87  2020-08-04      4121300        55.63523      0.00000000 37.71019 58.31385
## 88  2020-08-05      3267200        55.50626     -0.47999954 38.08931 58.80586
## 89  2020-08-06      3532100        55.76420      0.11999893 38.02961 58.85845
## 90  2020-08-07      3849200        54.97055     -0.36999893 37.67086 58.61137
## 91  2020-08-10      2937300        54.94078     -0.31999969 37.96585 59.01744
## 92  2020-08-11      3948900        54.74237     -0.54000092 37.72882 58.89092
## 93  2020-08-12      5466200        56.29000      0.44000244 38.64321 59.91523
## 94  2020-08-13      3490600        55.80000     -0.54999924 38.30139 59.68278
## 95  2020-08-14      2893300        55.19000     -0.28000259 38.15370 59.64389
## 96  2020-08-17      2997600        56.46000      0.52000046 38.28607 59.88452
## 97  2020-08-18      4174800        56.33000     -0.50000000 37.15378 58.90811
## 98  2020-08-19      3322100        56.23000     -0.90999985 37.42488 59.28615
## 99  2020-08-20      2527500        56.49000      0.21000290 36.96318 58.93089
## 100 2020-08-21      3419200        55.71000      0.21999741 37.34230 59.41592
## 101 2020-08-24      7379000        56.76000     -0.74000168 37.28261 59.46164
## 102 2020-08-25      3399700        57.01000     -0.15999985 36.92385 59.20779
## 103 2020-08-26      2802400        57.00000      0.06999969 37.21885 59.60720
## 104 2020-08-27      4195100        56.11000     -0.88999939 36.98182 59.47411
## 105 2020-08-28      3251900        55.69000     -0.50000000 37.89621 60.49196
## 106 2020-08-31      3262600        56.00000      0.20000076 37.55439 60.25312
## 107 2020-09-01      3965900        55.38000     -0.46999741 37.40669 60.20795
## 108 2020-09-02      3614200        55.90000      0.43000031 37.53907 60.44238
## 109 2020-09-03      5122000        54.16000     -1.18999863 36.40678 59.45715
## 110 2020-09-04      4610000        53.58000     -0.20999908 36.67787 59.82921
## 111 2020-09-08      6735600        54.71000      0.56999969 36.21618 59.46804
## 112 2020-09-09     11416300        53.64000     -0.19000244 36.59529 59.94725
## 113 2020-09-10      4407100        53.07000     -1.09000015 36.53560 59.98722
## 114 2020-09-11      4627700        53.73000     -0.26000214 36.17685 59.72771
## 115 2020-09-14      4960000        54.02000     -0.20999908 36.47184 60.12153
## 116 2020-09-15      4300900        55.28000     -0.25000000 36.23481 59.98291
## 117 2020-09-16      3948500        55.13000     -0.59000015 37.14920 60.99531
## 118 2020-09-17      3991900        55.94000      0.19999695 36.80738 60.75110
## 119 2020-09-18      2931700        56.45000     -0.30999756 36.65969 60.70062
## 120 2020-09-21      6792700        55.91000      0.81999969 36.79206 60.92981
## 121 2020-09-22      3878600        55.44000      0.14999771 35.65977 59.93710
## 122 2020-09-23      2922900        55.66000     -0.56000137 35.93087 60.30408
## 123 2020-09-24      4123700        54.17000     -0.77000046 35.46917 59.93789
## 124 2020-09-25      4954900        55.51000      1.30999756 35.84829 60.41214
## 125 2020-09-28      2741400        54.75000     -0.31999969 35.78860 60.44721
## 126 2020-09-29      4177500        55.11000      0.40000153 35.42984 60.18286
## 127 2020-09-30      3001400        54.80000     -0.22000122 35.72483 60.57190
## 128 2020-10-01      4728200        54.30000     -0.90000153 35.48781 60.42856
## 129 2020-10-02      5699100        54.22000     -0.19999695 36.40220 61.43629
## 130 2020-10-05      6704300        54.49000      0.03000259 36.06038 61.18747
## 131 2020-10-06      3695000        53.71000     -0.44000244 35.91268 61.13242
## 132 2020-10-07      3518700        53.53000     -0.28000259 36.04505 61.35711
## 133 2020-10-08      2351300        54.09000      0.40999985 34.91277 60.35796
## 134 2020-10-09      2092000        54.72000      0.33000183 35.18386 60.72055
## 135 2020-10-12      2783500        55.27000      0.08000183 34.72217 60.35003
## 136 2020-10-13      2949800        54.48000     -0.29000092 35.10128 60.81999
## 137 2020-10-14      4657400        53.19000     -0.81000137 35.04159 60.85082
## 138 2020-10-15      3038700        52.55000      0.09999847 34.68284 60.58228
## 139 2020-10-16      2665500        53.03000      0.02999878 34.97783 60.96717
## 140 2020-10-19      3143700        52.44000     -0.79000092 34.74080 60.81973
## 141 2020-10-20      4919100        52.19000     -0.49000168 35.65519 61.82340
## 142 2020-10-21      8219800        51.57000     -0.56999969 35.31337 61.57056
## 143 2020-10-22      3751000        51.96000     -0.04999924 35.16568 61.51154
## 144 2020-10-23      5576900        52.00000     -0.16999817 35.29805 61.73229
## 145 2020-10-26      6022000        53.07000      0.20000076 34.16576 60.72752
## 146 2020-10-27      3702300        52.48000     -0.83000183 34.43686 61.08628
## 147 2020-10-28      5263200        50.98000     -0.90999985 33.97516 60.71196
## 148 2020-10-29      3534000        51.07000      0.02999878 34.35428 61.17816
## 149 2020-10-30      4761600        50.16000     -0.75999832 34.29459 61.20528
## 150 2020-11-02      5368800        50.62000      0.14999771 33.93583 60.93306
## 151 2020-11-03      4156800        51.70000      0.24000168 34.23082 61.31430
## 152 2020-11-04      7385800        55.04000      1.09000015 33.99379 61.16325
## 153 2020-11-05      6660900        55.16000     -0.90000153 34.90818 62.16335
## 154 2020-11-06      3686300        55.52000     -0.16999817 34.56637 61.90697
## 155 2020-11-09      4381300        54.05000     -0.72000122 34.41867 61.84445
## 156 2020-11-10      5835800        56.57000     -0.06999969 34.55104 62.06173
## 157 2020-11-11      4069700        58.02000      0.47999954 33.41876 61.05200
## 158 2020-11-12      2902900        56.84000      0.02999878 33.68985 61.40737
## 159 2020-11-13      2900500        57.38000      0.51000214 33.22816 61.02969
## 160 2020-11-16      5190500        56.29000     -0.20000076 33.60727 61.49257
## 161 2020-11-17      6451500        54.96000     -0.15000153 33.54758 61.51640
## 162 2020-11-18      4732300        53.99000     -1.34000015 33.18883 61.24091
## 163 2020-11-19      6768200        54.03000     -0.22000122 33.48382 61.61892
## 164 2020-11-20      4306000        55.30000      0.41999817 33.24679 61.46467
## 165 2020-11-23     13670600        54.70000      0.18000031 34.16118 62.46159
## 166 2020-11-24      8341400        53.57000     -0.43000031 33.81936 62.20206
## 167 2020-11-25     11660900        52.60000      0.25999832 33.67167 62.13642
## 168 2020-11-27     14783200        52.61000      1.02999878 33.80404 62.35062
## 169 2020-11-30     14725200        52.94000     -0.36000061 32.67175 61.33645
## 170 2020-12-01     13308200        52.98000     -0.08000183 32.94284 61.68880
## 171 2020-12-02      4953700        53.39000      0.50000000 32.48115 61.30812
## 172 2020-12-03      7917100        52.78000     -0.09000015 32.86027 61.76803
## 173 2020-12-04      4685400        53.74000      0.19000244 32.80058 61.78891
## 174 2020-12-07      6582900        54.26000     -0.12000275 32.44182 61.51050
## 175 2020-12-08      6707600        54.72000      1.05000305 32.73681 61.88562
## 176 2020-12-09      7952400        54.04000     -0.91999817 32.49978 61.72850
## 177 2020-12-10      7566800        53.89000      0.02999878 33.41417 62.72257
## 178 2020-12-11      3870000        54.27000      0.63999939 33.07236 62.46023
## 179 2020-12-14     54898600        50.03000     -0.63999939 32.92466 62.39179
## 180 2020-12-15     22345300        50.83000      0.22000122 33.05703 62.60320
## 181 2020-12-16     19333500        50.93000     -0.86999893 31.92475 61.58506
## 182 2020-12-17     13957200        50.79000     -0.46999741 32.19584 61.93468
## 183 2020-12-18     25875200        50.21000     -0.70999908 31.73414 61.55131
## 184 2020-12-21     13007200        49.68000      0.20000076 32.11326 62.00855
## 185 2020-12-22     14221900        48.73000     -0.54000092 32.05357 62.02677
## 186 2020-12-23     11311000        48.77000     -0.34000015 31.69481 61.74573
## 187 2020-12-24      4931700        48.52000     -0.38000107 31.98981 62.11823
## 188 2020-12-28     15481900        49.38000     -0.84999847 31.75278 61.95852
## 189 2020-12-29     17308500        49.90000     -0.75999832 32.66717 62.95002
##       lower95 price_change
## 1   40.180561   0.30295138
## 2   39.745402   0.01201741
## 3   38.693351   0.67212541
## 4   38.554719   0.64023496
## 5   38.028282  -0.50047933
## 6   37.241150   0.60232875
## 7   37.137986   1.11091477
## 8   36.527408   4.02652785
## 9   37.088787   4.42167045
## 10  36.411442   4.11864039
## 11  35.943337   5.35761418
## 12  35.768541   6.11810179
## 13  34.211702   7.65713856
## 14  34.202179   7.01897940
## 15  33.468890   7.76837420
## 16  33.584616   7.41902084
## 17  33.269038   8.26244503
## 18  32.661292   8.91882290
## 19  32.713659   8.11787676
## 20  32.239909   8.10688843
## 21  32.923068   8.74012155
## 22  32.355145   8.66527523
## 23  31.986143   9.29908163
## 24  31.901717  10.27782714
## 25  30.462802  11.97559347
## 26  30.527064  10.41481163
## 27  29.862230  11.42214482
## 28  30.041709  11.69779145
## 29  29.785707  11.91621565
## 30  29.233807  13.46545393
## 31  29.338670  11.37481586
## 32  28.914395  11.52255708
## 33  29.644290  10.64785182
## 34  29.120612  11.10871488
## 35  28.793583  11.46474647
## 36  28.749044  12.82047562
## 37  27.364562  13.89324195
## 38  27.464286  11.52888269
## 39  26.833341  11.35565099
## 40  27.045246  11.99836687
## 41  26.820315  13.14933196
## 42  26.298220  14.06364822
## 43  26.431709  13.33214864
## 44  26.034955  13.84695147
## 45  26.791337  11.81152538
## 46  26.293177  12.13349914
## 47  25.990752  11.02127282
## 48  25.969960  11.03770754
## 49  24.618456  13.07277703
## 50  24.740024  10.74810206
## 51  24.130217  11.11058738
## 52  24.362593  11.89219257
## 53  24.157500  12.88443192
## 54  23.654642  13.56064622
## 55  23.806797  12.03548743
## 56  23.428166  12.62966267
## 57  24.202154  12.11209699
## 58  23.721107  13.27733628
## 59  23.435325  11.32184443
## 60  23.430727  12.06249180
## 61  22.101928  12.88723795
## 62  22.238671  12.82448010
## 63  21.643655  13.26633019
## 64  21.890453  13.31380452
## 65  21.699427  13.84968783
## 66  21.210297  14.50606570
## 67  21.375853  13.58607048
## 68  21.010310  14.53738531
## 69  21.797085  13.40474547
## 70  21.328534  13.53822359
## 71  21.054970  13.17005016
## 72  21.062320  13.77180440
## 73  19.750374  18.89220867
## 74  19.898448  18.05563566
## 75  19.314525  22.15822184
## 76  19.572187  19.37830359
## 77  19.391803  18.83283085
## 78  18.913102  18.11022999
## 79  19.088880  16.01959192
## 80  18.733359  16.88162376
## 81  19.529963  16.74105317
## 82  19.071053  16.82492878
## 83  18.806948  17.29008741
## 84  18.823582  18.39780020
## 85  17.524785  17.43682172
## 86  17.681734  18.19748153
## 87  17.106528  17.92504482
## 88  17.372753  17.41695808
## 89  17.200784  17.73458730
## 90  16.730352  17.29968709
## 91  16.914259  16.97493342
## 92  16.566730  17.01354760
## 93  17.371193  17.64678835
## 94  16.920014  17.49860448
## 95  16.663513  17.03629958
## 96  16.687628  18.17392723
## 97  15.399460  19.17621703
## 98  15.563604  18.80512101
## 99  14.995482  19.52681729
## 100 15.268682  18.36769907
## 101 15.103581  19.47738913
## 102 14.639915  20.08614432
## 103 14.830487  19.78115460
## 104 14.489525  19.12818323
## 105 15.300458  17.79379169
## 106 14.855655  18.44561087
## 107 14.605440  17.97330765
## 108 14.635751  18.36093530
## 109 13.356406  17.75322068
## 110 13.526537  16.90212893
## 111 12.964318  18.49382033
## 112 13.243342  17.04470500
## 113 13.083987  16.53439613
## 114 12.625989  17.55315117
## 115 12.822154  17.54816068
## 116 12.486711  19.04518703
## 117 13.303092  17.98079976
## 118 12.863666  19.13261513
## 119 12.618759  19.79031297
## 120 12.654312  19.11793925
## 121 11.382443  19.78022509
## 122 11.557656  19.72913257
## 123 11.000457  18.70082504
## 124 11.284439  19.66170956
## 125 11.129981  18.96140207
## 126 10.676822  19.68015787
## 127 10.877768  19.07516509
## 128 10.547049  18.81219312
## 129 11.368099  17.81780554
## 130 10.933288  18.42962381
## 131 10.692942  17.79731693
## 132 10.733004  17.48494381
## 133  9.467575  19.17723224
## 134  9.647172  19.53613958
## 135  9.094309  20.54783296
## 136  9.382578  19.37871641
## 137  9.232360  18.14840632
## 138  8.783395  17.86716212
## 139  8.988489  18.05217026
## 140  8.661874  17.69919814
## 141  9.486983  16.53480858
## 142  9.056188  16.25662746
## 143  8.819816  16.79432255
## 144  8.863810  16.70195066
## 145  7.604006  18.90423741
## 146  7.787437  18.04314353
## 147  7.238367  17.00483768
## 148  7.530392  16.71572219
## 149  7.383892  15.86541317
## 150  6.938606  16.68416745
## 151  7.147344  17.46917788
## 152  6.824337  21.04620606
## 153  7.653018  20.25181543
## 154  7.225760  20.95363385
## 155  6.992891  19.63132834
## 156  7.040354  22.01895599
## 157  5.785516  24.60124381
## 158  5.972336  23.15014977
## 159  5.426623  24.15184483
## 160  5.721973  22.68272904
## 161  5.578767  21.41241804
## 162  5.136745  20.80117582
## 163  5.348717  20.54618152
## 164  5.028913  22.05321001
## 165  5.860769  20.53882197
## 166  5.436658  19.75063872
## 167  5.206907  18.92833320
## 168  5.257460  18.80596253
## 169  4.007051  20.26824760
## 170  4.196894  20.03715479
## 171  3.654178  20.90884878
## 172  3.952499  19.91973254
## 173  3.812239  20.93942626
## 174  3.373138  21.81817810
## 175  3.588005  21.98318959
## 176  3.271072  21.54021731
## 177  4.105774  20.47582623
## 178  3.684486  21.19764511
## 179  3.457533  17.10533914
## 180  3.510862  17.77296938
## 181  2.264434  19.00525491
## 182  2.456997  18.59416179
## 183  1.916979  18.47585411
## 184  2.217976  17.56673969
## 185  2.080370  16.67642976
## 186  1.643902  17.07518586
## 187  1.861381  16.53019446
## 188  1.547039  17.62722310
## 189  2.384313  17.23283399
# the estimated total stock value change between April and August 2020 as result of the pandemic
sum(post_covid_astra$price_change)
## [1] 2924.645
# Data viz
plot_ly() %>%
   add_ribbons(x = post_covid_astra$date,
              ymin = post_covid_astra$adj.close_astra,
              ymax = post_covid_astra$yhat,
              line = list(color = 'rgba(255, 0, 0, 0.05)'),
              fillcolor = 'rgba(255, 0, 0, 0.6)',
              name = "Estimated Change") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(astra$adj.close_astra),
               yend = max(astra$adj.close_astra) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2020-01-01"),
                  y = max(astra$adj.close_astra) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2020-11-01"),
                  y = max(astra$adj.close_astra) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = paste("Estimated increase in", "<br>",
                               "stock price: ~$3,000",
                               sep = ""),
                  x = as.Date("2020-09-01"),
                  y = 4 * 10^1,
                  arrowhead = 1,
                  ax = -120,
                  ay = 40,
                  showarrow = TRUE) %>%
  add_lines(x = astra$date,
            y = astra$adj.close_astra,
            line = list(color = "#1f77b4"),
            name = "Actual") %>%
  layout(title = "Covid19 Impact on Stock Value of ASTRAZENECA",
         yaxis = list(title = "Stock Price (US Dollars)"),
         xaxis = list(title = "Time Series Model - HoltWinters Model",
                      range = c(as.Date("2019-10-01"), as.Date("2021-01-01"))),
         legend = list(x = 0, y = 0.95))

3. BIONTECH

library(dplyr)
library(plotly)
library(ggplot2)
library(wesanderson)

# BIONTECH
bio <- dt2[3:251,c(1,20,21,22)]
bio$Companies <- as.Date(bio$Companies)
bio$BIONTECH.4 <- as.numeric(as.character(bio$BIONTECH.4))
bio$BIONTECH.5 <- as.numeric(as.character(bio$BIONTECH.5))
bio$BIONTECH.6 <- as.numeric(as.character(bio$BIONTECH.6))
colnames(bio) <- c("date", "volume_bio", "adj.close_bio", "variation_bio")


plot_ly(data = bio,
        x = ~ date,
        y = ~ volume_bio,
        type = "scatter", 
        mode = "line",
        name = "Trading Volume of BIONTECH") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(bio$volume_bio),
               yend = max(bio$volume_bio) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(bio$volume_bio) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(bio$volume_bio) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Trading Volume of BIONTECH Inc.",
         yaxis = list(title = "Volume"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
plot_ly(data = bio,
        x = ~ date,
        y = ~ adj.close_bio,
        type = "scatter", 
        mode = "line",
        name = "Ajusted Close Price of BIONTECH") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(bio$adj.close_bio),
               yend = max(bio$adj.close_bio) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(bio$adj.close_bio) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(bio$adj.close_bio) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Adjusted Close Price of BIONTECH Inc.",
         yaxis = list(title = "Adjusted Close Price"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
bio %>% 
  ggplot(aes(x = date, y = variation_bio)) + 
  geom_line(color = wes_palette('Moonrise2', n=1, 'discrete')) + 
  geom_smooth(se = F, lty = 2, color = wes_palette('BottleRocket1', 1)) + 
  labs(x = "Date", y = "Variation", 
       title = "BIONTECH Stock Price Variation",
       subtitle = "Smoothed Line in Red") + 
  cowplot::theme_cowplot()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

library(dplyr)
library(tsbox)
library(TSstudio)

## Quantify impact of Covid-19 on share prices

pre_covid_bio <- bio %>% 
  dplyr::filter(date < as.Date("2020-04-01")) %>%
  dplyr::arrange(date)

post_covid_bio <- bio %>% 
  dplyr::filter(date >= as.Date("2020-04-01")) %>%
  dplyr::arrange(date)


## The series before the outbreak of the pandemic

# Step 01: convert the series into a 'ts' object first.
ts.obj_bio <- ts(pre_covid_bio$adj.close_bio, start = c(2020, 3), frequency = 12)

ts_plot(ts.obj_bio,
        title = "Stock Value of BIONTECH",
        Ytitle = "Price per share (US Dollars)",
        slider = TRUE)
ts_seasonal(ts.obj = ts.obj_bio, type = "all")
# Step 02: review the series correlation with its past lags using the ACF and PACF to select time-series models for seasonal data functions.
ts_cor(ts.obj = ts.obj_bio, lag.max = 36)
## Step 03: select time-series models for seasonal data

methods <- list(
                auto_arima = list(method = "auto.arima",
                              notes = "Auto ARIMA"),
                hw1 = list(method = "HoltWinters",
                          method_arg = NULL,
                          notes = "HoltWinters Model"),
                 hw2 = list(method = "HoltWinters",
                          method_arg = list(seasonal = "multiplicative"),
                          notes = "HW with multip. seasonality"),
                tslm = list(method = "tslm",
                            method_arg = list(formula = input ~ trend + season),
                            notes = "tslm with trend and seasonal"))


# Backtesting is the time series equivalent of the machine learning cross-validation training approach. The idea here is simple - test each model with backtesting, and select the one that performed best, on average, on the different testing partition.
# For the backtesting, we will split the series into 6 testing partitions, each 12 months spaced by 3 months from each other.
train_method = list(partitions = 6,
                    sample.out = 12,
                    space = 3)

# train the models:
# Note that the forecast horizon is set the the length of the post_covid series. In addition we will set the MAPA as the error metric to evaluate the performance of the different models on the testing partitions:
md_bio <- train_model(input = ts.obj_bio,
                  methods = methods,
                  train_method = train_method,
                  horizon = nrow(post_covid_bio),
                  error = "MAPE")
## # A tibble: 4 x 7
##   model_id  model   notes    avg_mape avg_rmse `avg_coverage_8~ `avg_coverage_9~
##   <chr>     <chr>   <chr>       <dbl>    <dbl>            <dbl>            <dbl>
## 1 auto_ari~ auto.a~ Auto AR~    0.220     16.4            0.556            0.653
## 2 hw2       HoltWi~ HW with~    0.237     16.9            0.444            0.556
## 3 hw1       HoltWi~ HoltWin~    0.239     16.9            0.458            0.583
## 4 tslm      tslm    tslm wi~    0.294     19.5            0.194            0.403
# Quantify the Covid-19 impact

# select the Auto ARIMA (auto_arima) to calculate the Covid-19 effect when it demonstrated the lowest error through back-testing 
post_covid_bio$yhat <- as.numeric(md_bio$forecast$auto_arima$forecast$mean)
post_covid_bio$upper95 <- as.numeric(md_bio$forecast$auto_arima$forecast$upper[,2])
post_covid_bio$lower95 <- as.numeric(md_bio$forecast$auto_arima$forecast$lower[,2])

post_covid_bio$price_change <- 
  post_covid_bio$adj.close_bio - post_covid_bio$yhat

post_covid_bio
##           date volume_bio adj.close_bio variation_bio yhat   upper95
## 1   2020-04-01     259100         52.92  -3.430000305 58.4  72.32269
## 2   2020-04-02     245700         56.91   1.409999847 58.4  78.08965
## 3   2020-04-03     227300         52.75  -5.229999542 58.4  82.51480
## 4   2020-04-06     331000         52.73  -2.770000458 58.4  86.24537
## 5   2020-04-07    1461700         45.60  -4.400001526 58.4  89.53208
## 6   2020-04-08     819800         44.95  -3.149997711 58.4  92.50348
## 7   2020-04-09    1483000         45.41  -6.599998474 58.4  95.23597
## 8   2020-04-13     880700         41.71  -4.310001373 58.4  97.77931
## 9   2020-04-14     863900         41.63  -2.189998627 58.4 100.16806
## 10  2020-04-15     980600         38.58  -2.319999695 58.4 102.42740
## 11  2020-04-16     849400         38.79   0.030002594 58.4 104.57633
## 12  2020-04-17    1008300         40.81   0.510002136 58.4 106.62960
## 13  2020-04-20    1278800         43.37   1.879997253 58.4 108.59896
## 14  2020-04-21    1507200         42.26  -3.010002136 58.4 110.49392
## 15  2020-04-22   10852500         53.50  -0.939998627 58.4 112.32233
## 16  2020-04-23    4271200         50.00  -8.000000000 58.4 114.09075
## 17  2020-04-24    1822700         47.75  -3.490001678 58.4 115.80471
## 18  2020-04-27    1183300         49.40   0.510002136 58.4 117.46896
## 19  2020-04-28    1511200         45.83  -2.769996643 58.4 119.08759
## 20  2020-04-29    1766400         49.25   1.250000000 58.4 120.66415
## 21  2020-04-30    1034100         47.48  -1.729999542 58.4 122.20177
## 22  2020-05-01     874600         45.11  -0.889999390 58.4 123.70319
## 23  2020-05-04     784900         45.78   0.579998016 58.4 125.17086
## 24  2020-05-05    2604800         50.00   1.590000153 58.4 126.60696
## 25  2020-05-06     889400         47.78  -2.360000610 58.4 128.01343
## 26  2020-05-07     815300         49.32   0.060001373 58.4 129.39205
## 27  2020-05-08     696100         49.24   0.080001831 58.4 130.74440
## 28  2020-05-11    1887400         49.98   0.369998932 58.4 132.07193
## 29  2020-05-12    1910200         49.33  -1.069999695 58.4 133.37596
## 30  2020-05-13    1468800         48.49  -2.009998322 58.4 134.65770
## 31  2020-05-14    1099100         48.66   0.099998474 58.4 135.91824
## 32  2020-05-15     793800         49.49   0.980003357 58.4 137.15861
## 33  2020-05-18    5013900         60.17   5.429996490 58.4 138.37975
## 34  2020-05-19    3612100         50.59  -6.409999847 58.4 139.58252
## 35  2020-05-20    1979300         53.04  -0.169998169 58.4 140.76773
## 36  2020-05-21    1094300         51.90  -2.199996948 58.4 141.93612
## 37  2020-05-22     941600         51.03  -1.120002747 58.4 143.08840
## 38  2020-05-26    1511100         50.88  -1.520000458 58.4 144.22521
## 39  2020-05-27    1810200         47.59  -1.939998627 58.4 145.34715
## 40  2020-05-28    1107900         46.87  -1.470001221 58.4 146.45480
## 41  2020-05-29     760600         49.53   1.340000153 58.4 147.54869
## 42  2020-06-01    1275800         52.30   2.299999237 58.4 148.62932
## 43  2020-06-02     688500         52.38   0.400001526 58.4 149.69716
## 44  2020-06-03    1379900         51.16  -0.689998627 58.4 150.75266
## 45  2020-06-04     623300         50.00  -0.450000763 58.4 151.79622
## 46  2020-06-05     768700         49.18  -0.619998932 58.4 152.82826
## 47  2020-06-08    1142400         48.78  -0.440002441 58.4 153.84913
## 48  2020-06-09     970100         47.79  -0.009998322 58.4 154.85920
## 49  2020-06-10     913100         47.41  -0.389999390 58.4 155.85881
## 50  2020-06-11    1324800         48.60  -0.400001526 58.4 156.84826
## 51  2020-06-12     858800         48.58  -0.789997101 58.4 157.82787
## 52  2020-06-15    1087500         50.85   2.129997253 58.4 158.79792
## 53  2020-06-16     901100         49.90  -1.099998474 58.4 159.75869
## 54  2020-06-17     723000         50.85   1.099998474 58.4 160.71044
## 55  2020-06-18     691500         50.94   1.079998016 58.4 161.65341
## 56  2020-06-19     735800         51.36   0.459999084 58.4 162.58785
## 57  2020-06-22    2678200         57.45   5.500000000 58.4 163.51398
## 58  2020-06-23    2142200         52.75  -2.869998932 58.4 164.43202
## 59  2020-06-24    1344500         54.73   1.729999542 58.4 165.34219
## 60  2020-06-25    2406400         59.71   5.200000763 58.4 166.24467
## 61  2020-06-26    2830700         62.13   5.500000000 58.4 167.13966
## 62  2020-06-29    4011400         65.80   0.120002747 58.4 168.02735
## 63  2020-06-30    1979100         66.74   0.579994202 58.4 168.90790
## 64  2020-07-01    9879300         64.14 -13.559997559 58.4 169.78149
## 65  2020-07-02    3926500         63.27  -4.975002289 58.4 170.64829
## 66  2020-07-06    1649400         65.51  -0.079994202 58.4 171.50844
## 67  2020-07-07    2264400         69.34   5.339996338 58.4 172.36210
## 68  2020-07-08    1449600         67.56   0.349998474 58.4 173.20942
## 69  2020-07-09    1186700         65.61  -1.000000000 58.4 174.05052
## 70  2020-07-10    2874300         70.36   3.360000610 58.4 174.88555
## 71  2020-07-13    8529600         77.78  -0.690002441 58.4 175.71464
## 72  2020-07-14    4748700         83.88   1.309997559 58.4 176.53791
## 73  2020-07-15    3908700         78.00  -8.080001831 58.4 177.35549
## 74  2020-07-16    2847900         75.87  -4.629997253 58.4 178.16748
## 75  2020-07-17    5053500         85.25   8.709999084 58.4 178.97400
## 76  2020-07-20    7790800         88.20  -3.547004700 58.4 179.77517
## 77  2020-07-21    6025500         91.60  -0.400001526 58.4 180.57108
## 78  2020-07-22   13817800        104.17   4.979995728 58.4 181.36184
## 79  2020-07-23   15899700         88.51  -9.003997803 58.4 182.14755
## 80  2020-07-24    7109700         84.17  -2.130004883 58.4 182.92830
## 81  2020-07-27    5129200         86.53  -0.910003662 58.4 183.70418
## 82  2020-07-28    5108600         85.38  -3.620002747 58.4 184.47529
## 83  2020-07-29    3846300         82.50  -3.040000916 58.4 185.24171
## 84  2020-07-30    3302800         84.16   5.110000610 58.4 186.00353
## 85  2020-07-31    2640500         82.11  -3.889999390 58.4 186.76083
## 86  2020-08-03    2431700         85.47   2.480003357 58.4 187.51369
## 87  2020-08-04    2410300         83.17  -1.270004272 58.4 188.26218
## 88  2020-08-05    4084400         81.30  -1.519996643 58.4 189.00638
## 89  2020-08-06    3175200         77.08  -2.209999084 58.4 189.74636
## 90  2020-08-07    3038200         77.00  -0.089996338 58.4 190.48220
## 91  2020-08-10    2830600         73.95  -3.050003052 58.4 191.21397
## 92  2020-08-11    4032200         68.45  -0.550003052 58.4 191.94172
## 93  2020-08-12    3252000         69.11  -1.779998779 58.4 192.66553
## 94  2020-08-13    2390900         71.00   2.750000000 58.4 193.38546
## 95  2020-08-14    1862400         68.64  -2.290000916 58.4 194.10156
## 96  2020-08-17    1948000         70.26   1.170005798 58.4 194.81391
## 97  2020-08-18    1905100         67.88  -1.810005188 58.4 195.52256
## 98  2020-08-19    2326600         65.60  -1.819999695 58.4 196.22757
## 99  2020-08-20    2932600         66.27   0.192993164 58.4 196.92898
## 100 2020-08-21    6290400         73.02   1.250000000 58.4 197.62687
## 101 2020-08-24    3393900         70.85  -6.139999390 58.4 198.32127
## 102 2020-08-25    1576200         70.49   0.419998169 58.4 199.01224
## 103 2020-08-26    1711400         67.90  -2.099998474 58.4 199.69984
## 104 2020-08-27    1980500         65.59  -2.610000610 58.4 200.38410
## 105 2020-08-28    1760000         64.00  -1.040000916 58.4 201.06509
## 106 2020-08-31    2217200         61.25  -1.569999695 58.4 201.74283
## 107 2020-09-01    3569600         57.81  -0.899997711 58.4 202.41739
## 108 2020-09-02    3892800         61.99   5.980003357 58.4 203.08880
## 109 2020-09-03    2586000         59.28  -4.170001984 58.4 203.75712
## 110 2020-09-04    1985500         58.57  -0.709999084 58.4 204.42237
## 111 2020-09-08    2887800         59.83  -0.329998016 58.4 205.08461
## 112 2020-09-09    3406900         62.32  -0.680000305 58.4 205.74387
## 113 2020-09-10    1751800         61.20  -0.799999237 58.4 206.40019
## 114 2020-09-11    2515200         65.13   3.677997589 58.4 207.05362
## 115 2020-09-14    3373600         67.48  -0.019996643 58.4 207.70418
## 116 2020-09-15    3168300         66.92  -3.080001831 58.4 208.35192
## 117 2020-09-16    2231500         66.04   0.529998779 58.4 208.99688
## 118 2020-09-17    2860000         66.78  -0.040000916 58.4 209.63909
## 119 2020-09-18    2608900         67.00  -1.010002136 58.4 210.27858
## 120 2020-09-21    2118700         66.12   0.560005188 58.4 210.91539
## 121 2020-09-22    1572300         67.05   1.670005798 58.4 211.54955
## 122 2020-09-23    1678000         63.83  -3.419998169 58.4 212.18110
## 123 2020-09-24    2863400         62.83   0.210002899 58.4 212.81007
## 124 2020-09-25    1670500         66.69   5.420001984 58.4 213.43648
## 125 2020-09-28    1750000         66.50  -0.489997864 58.4 214.06037
## 126 2020-09-29     847600         66.86   0.260002136 58.4 214.68177
## 127 2020-09-30    1746800         69.23   1.830001831 58.4 215.30071
## 128 2020-10-01    2371700         72.71   2.709999084 58.4 215.91722
## 129 2020-10-02    2327800         73.70   1.849998474 58.4 216.53132
## 130 2020-10-05    3750600         80.70   5.000000000 58.4 217.14305
## 131 2020-10-06    5702100         85.59  -1.250000000 58.4 217.75243
## 132 2020-10-07    2354700         86.23   2.910003662 58.4 218.35949
## 133 2020-10-08    1826000         87.95   2.119995117 58.4 218.96426
## 134 2020-10-09    1462500         89.04   0.059997559 58.4 219.56675
## 135 2020-10-12    2573100         88.74  -2.260002136 58.4 220.16700
## 136 2020-10-13    2527500         93.00   2.919998169 58.4 220.76503
## 137 2020-10-14    4180000         86.86  -8.558998108 58.4 221.36087
## 138 2020-10-15    2029700         90.38   4.729995728 58.4 221.95454
## 139 2020-10-16    2792500         94.09   0.489997864 58.4 222.54606
## 140 2020-10-19    2800200         93.92  -2.310005188 58.4 223.13545
## 141 2020-10-20    1872100         94.35   0.430000305 58.4 223.72274
## 142 2020-10-21    3384600         87.72  -5.029998779 58.4 224.30796
## 143 2020-10-22    2205900         86.36  -2.629997253 58.4 224.89112
## 144 2020-10-23    2079700         88.69  -0.915000916 58.4 225.47224
## 145 2020-10-26    2879900         82.93  -5.290000916 58.4 226.05135
## 146 2020-10-27    2222100         83.95   0.949996948 58.4 226.62846
## 147 2020-10-28    2566100         77.54  -2.349998474 58.4 227.20360
## 148 2020-10-29    2512000         84.81   3.319999695 58.4 227.77679
## 149 2020-10-30    2253000         85.36  -1.650001526 58.4 228.34805
## 150 2020-11-02    2139900         87.10  -0.804000854 58.4 228.91739
## 151 2020-11-03    1082600         87.22   2.309997559 58.4 229.48484
## 152 2020-11-04    1701500         90.69   4.180000305 58.4 230.05041
## 153 2020-11-05    1459800         92.00   0.669998169 58.4 230.61412
## 154 2020-11-06     861300         92.00   0.000000000 58.4 231.17600
## 155 2020-11-09   14614900        104.80  -9.219993591 58.4 231.73605
## 156 2020-11-10    8029100        112.76   2.760002136 58.4 232.29430
## 157 2020-11-11    3274100        109.45  -2.550003052 58.4 232.85076
## 158 2020-11-12    3596500        101.63  -5.180000305 58.4 233.40546
## 159 2020-11-13    2997500        106.00   1.879997253 58.4 233.95840
## 160 2020-11-16   11171600         91.52  -5.120002747 58.4 234.50960
## 161 2020-11-17    4057400         86.93  -2.069999695 58.4 235.05909
## 162 2020-11-18    5772600         90.44  -0.360000610 58.4 235.60687
## 163 2020-11-19    3400100         94.93   3.330001831 58.4 236.15296
## 164 2020-11-20    6776800        104.07   4.069999695 58.4 236.69739
## 165 2020-11-23    5194200        106.50  -3.230003357 58.4 237.24015
## 166 2020-11-24    3864000        101.87   0.950004578 58.4 237.78127
## 167 2020-11-25    3007400        104.96   4.959999084 58.4 238.32076
## 168 2020-11-27    3874600        109.99   3.549995422 58.4 238.85864
## 169 2020-11-30    9934800        124.24  10.599998474 58.4 239.39493
## 170 2020-12-01    9634500        114.01 -10.489997864 58.4 239.92962
## 171 2020-12-02    7164700        121.09   0.239997864 58.4 240.46275
## 172 2020-12-03    4264600        118.68  -1.169998169 58.4 240.99432
## 173 2020-12-04    3142600        120.00   2.157997131 58.4 241.52435
## 174 2020-12-07    4925300        125.70   4.699996948 58.4 242.05285
## 175 2020-12-08    4597500        128.11   1.110000610 58.4 242.57983
## 176 2020-12-09    5265200        122.81  -6.180007935 58.4 243.10531
## 177 2020-12-10    5381300        129.54   5.719993591 58.4 243.62930
## 178 2020-12-11    4303700        127.30  -2.800003052 58.4 244.15181
## 179 2020-12-14   10907900        108.27 -18.359001160 58.4 244.67285
## 180 2020-12-15    5711800        111.20  -3.010002136 58.4 245.19244
## 181 2020-12-16    4802600        105.78  -6.440002441 58.4 245.71059
## 182 2020-12-17    3487800        106.43   3.794998169 58.4 246.22731
## 183 2020-12-18    3108200        104.24  -2.220001221 58.4 246.74261
## 184 2020-12-21    2611000        106.46  -1.050003052 58.4 247.25651
## 185 2020-12-22    5021000        100.56  -6.510002136 58.4 247.76901
## 186 2020-12-23    3414000        100.06   2.939994812 58.4 248.28013
## 187 2020-12-24    1039100         96.96  -1.540000916 58.4 248.78988
##          lower95 price_change
## 1     44.4773150   -5.4800034
## 2     38.7103494   -1.4900017
## 3     34.2852010   -5.6500015
## 4     30.5546284   -5.6700020
## 5     27.2679280  -12.8000031
## 6     24.2965236  -13.4500008
## 7     21.5640353  -12.9900017
## 8     19.0206972  -16.6900024
## 9     16.6319419  -16.7700005
## 10    14.3726009  -19.8199997
## 11    12.2236741  -19.6100006
## 12    10.1704005  -17.5900002
## 13     8.2010413  -15.0300026
## 14     6.3060785  -16.1400032
## 15     4.4776684   -4.9000015
## 16     2.7092553   -8.4000015
## 17     0.9952943  -10.6500015
## 18    -0.6689549   -9.0000000
## 19    -2.2875822  -12.5699997
## 20    -3.8641456   -9.1500015
## 21    -5.4017635  -10.9200020
## 22    -6.9031869  -13.2900009
## 23    -8.3708576  -12.6200027
## 24    -9.8069543   -8.4000015
## 25   -11.2134313  -10.6200027
## 26   -12.5920489   -9.0800018
## 27   -13.9444000   -9.1599998
## 28   -15.2719309   -8.4200020
## 29   -16.5759601   -9.0699997
## 30   -17.8576934   -9.9099998
## 31   -19.1182365   -9.7400017
## 32   -20.3586071   -8.9099998
## 33   -21.5797436    1.7699966
## 34   -22.7825140   -7.8100014
## 35   -23.9677229   -5.3600006
## 36   -25.1361178   -6.5000000
## 37   -26.2883946   -7.3700027
## 38   -27.4252024   -7.5200005
## 39   -28.5471482  -10.8100014
## 40   -29.6547998  -11.5300026
## 41   -30.7486902   -8.8700027
## 42   -31.8293199   -6.1000023
## 43   -32.8971597   -6.0200005
## 44   -33.9526532   -7.2400017
## 45   -34.9962192   -8.4000015
## 46   -36.0282530   -9.2200012
## 47   -37.0491286   -9.6200027
## 48   -38.0592005  -10.6100006
## 49   -39.0588044  -10.9900017
## 50   -40.0482592   -9.8000031
## 51   -41.0278681   -9.8199997
## 52   -41.9979190   -7.5500031
## 53   -42.9586866   -8.5000000
## 54   -43.9104322   -7.5500031
## 55   -44.8534055   -7.4600029
## 56   -45.7878445   -7.0400009
## 57   -46.7139769   -0.9500008
## 58   -47.6320203   -5.6500015
## 59   -48.5421831   -3.6700020
## 60   -49.4446648    1.3099976
## 61   -50.3396566    3.7299995
## 62   -51.2273420    7.4000015
## 63   -52.1078971    8.3399963
## 64   -52.9814909    5.7399979
## 65   -53.8482860    4.8699989
## 66   -54.7084388    7.1100006
## 67   -55.5620995   10.9399948
## 68   -56.4094130    9.1599960
## 69   -57.2505189    7.2099991
## 70   -58.0855515   11.9599991
## 71   -58.9146406   19.3799973
## 72   -59.7379114   25.4799957
## 73   -60.5554846   19.5999985
## 74   -61.3674768   17.4700012
## 75   -62.1740009   26.8499985
## 76   -62.9751659   29.7999954
## 77   -63.7710772   33.1999969
## 78   -64.5618368   45.7699966
## 79   -65.3475434   30.1100006
## 80   -66.1282928   25.7699966
## 81   -66.9041775   28.1299973
## 82   -67.6752874   26.9799957
## 83   -68.4417096   24.0999985
## 84   -69.2035285   25.7600021
## 85   -69.9608261   23.7099991
## 86   -70.7136820   27.0699997
## 87   -71.4621734   24.7699966
## 88   -72.2063754   22.9000015
## 89   -72.9463608   18.6800003
## 90   -73.6822005   18.5999985
## 91   -74.4139634   15.5499954
## 92   -75.1417166   10.0499954
## 93   -75.8655253   10.7099991
## 94   -76.5854528   12.5999985
## 95   -77.3015611   10.2399979
## 96   -78.0139101   11.8600006
## 97   -78.7225586    9.4799957
## 98   -79.4275635    7.1999969
## 99   -80.1289806    7.8699951
## 100  -80.8268640   14.6199951
## 101  -81.5212667   12.4499969
## 102  -82.2122401   12.0899963
## 103  -82.8998346    9.5000000
## 104  -83.5840993    7.1899948
## 105  -84.2650822    5.5999985
## 106  -84.9428298    2.8499985
## 107  -85.6173881   -0.5900002
## 108  -86.2888014    3.5900002
## 109  -86.9571136    0.8799973
## 110  -87.6223670    0.1699982
## 111  -88.2846033    1.4300003
## 112  -88.9438633    3.9199982
## 113  -89.6001867    2.7999992
## 114  -90.2536123    6.7299957
## 115  -90.9041783    9.0800018
## 116  -91.5519218    8.5199966
## 117  -92.1968793    7.6399994
## 118  -92.8390864    8.3799973
## 119  -93.4785780    8.5999985
## 120  -94.1153882    7.7200012
## 121  -94.7495506    8.6500015
## 122  -95.3810978    5.4300003
## 123  -96.0100620    4.4300003
## 124  -96.6364746    8.2900009
## 125  -97.2603663    8.0999985
## 126  -97.8817675    8.4599991
## 127  -98.5007076   10.8300018
## 128  -99.1172157   14.3099976
## 129  -99.7313202   15.2999954
## 130 -100.3430491   22.2999954
## 131 -100.9524297   27.1899948
## 132 -101.5594887   27.8300018
## 133 -102.1642527   29.5499954
## 134 -102.7667473   30.6399994
## 135 -103.3669980   30.3399963
## 136 -103.9650296   34.5999985
## 137 -104.5608666   28.4599991
## 138 -105.1545329   31.9799957
## 139 -105.7460521   35.6899948
## 140 -106.3354474   35.5199966
## 141 -106.9227414   35.9499969
## 142 -107.5079565   29.3199997
## 143 -108.0911146   27.9599991
## 144 -108.6722372   30.2900009
## 145 -109.2513454   24.5299988
## 146 -109.8284602   25.5499954
## 147 -110.4036019   19.1399994
## 148 -110.9767907   26.4099960
## 149 -111.5480463   26.9599991
## 150 -112.1173880   28.6999969
## 151 -112.6848352   28.8199997
## 152 -113.2504064   32.2900009
## 153 -113.8141203   33.5999985
## 154 -114.3759949   33.5999985
## 155 -114.9360482   46.4000015
## 156 -115.4942978   54.3600006
## 157 -116.0507610   51.0499954
## 158 -116.6054548   43.2299957
## 159 -117.1583960   47.5999985
## 160 -117.7096011   33.1199951
## 161 -118.2590864   28.5299988
## 162 -118.8068679   32.0400009
## 163 -119.3529612   36.5299988
## 164 -119.8973820   45.6699982
## 165 -120.4401454   48.0999985
## 166 -120.9812666   43.4700012
## 167 -121.5207604   46.5599976
## 168 -122.0586413   51.5899963
## 169 -122.5949237   65.8399963
## 170 -123.1296218   55.6100006
## 171 -123.6627496   62.6899948
## 172 -124.1943209   60.2799988
## 173 -124.7243490   61.5999985
## 174 -125.2528475   67.2999954
## 175 -125.7798295   69.7099991
## 176 -126.3053080   64.4099960
## 177 -126.8292958   71.1399918
## 178 -127.3518054   68.9000015
## 179 -127.8728493   49.8699951
## 180 -128.3924399   52.7999954
## 181 -128.9105891   47.3799973
## 182 -129.4273090   48.0299988
## 183 -129.9426112   45.8399963
## 184 -130.4565074   48.0599976
## 185 -130.9690091   42.1599960
## 186 -131.4801274   41.6599960
## 187 -131.9898737   38.5599976
# the estimated total stock value change between April and August 2020 as result of the pandemic
sum(post_covid_bio$price_change)
## [1] 2757.56
# Data viz
plot_ly() %>%
   add_ribbons(x = post_covid_bio$date,
              ymin = post_covid_bio$adj.close_bio,
              ymax = post_covid_bio$yhat,
              line = list(color = 'rgba(255, 0, 0, 0.05)'),
              fillcolor = 'rgba(255, 0, 0, 0.6)',
              name = "Estimated Change") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(bio$adj.close_bio),
               yend = max(bio$adj.close_bio) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2020-01-01"),
                  y = max(bio$adj.close_bio) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2020-11-01"),
                  y = max(bio$adj.close_bio) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = paste("Estimated increase in", "<br>",
                               "stock price: ~$2,800",
                               sep = ""),
                  x = as.Date("2020-11-01"),
                  y = 6 * 10^1,
                  arrowhead = 1,
                  ax = -110,
                  ay = 40,
                  showarrow = TRUE) %>%
  add_lines(x = bio$date,
            y = bio$adj.close_bio,
            line = list(color = "#1f77b4"),
            name = "Actual") %>%
  layout(title = "Covid19 Impact on Stock Value of BIONTECH",
         yaxis = list(title = "Stock Price (US Dollars)"),
         xaxis = list(title = "Time Series Model - Auto ARIMA Model",
                      range = c(as.Date("2019-10-01"), as.Date("2021-01-01"))),
         legend = list(x = 0, y = 0.95))

4. MODERNA

library(dplyr)
library(plotly)
library(ggplot2)
library(wesanderson)

# MODERNA
mod <- dt2[3:249,c(1,27,28,29)]
mod$Companies <- as.Date(mod$Companies)
mod$MODERNA.4 <- as.numeric(as.character(mod$MODERNA.4))
mod$MODERNA.5 <- as.numeric(as.character(mod$MODERNA.5))
mod$MODERNA.6 <- as.numeric(as.character(mod$MODERNA.6))
colnames(mod) <- c("date", "volume_mod", "adj.close_mod", "variation_mod")


plot_ly(data = mod,
        x = ~ date,
        y = ~ volume_mod,
        type = "scatter", 
        mode = "line",
        name = "Trading Volume of MODERNA") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(mod$volume_mod),
               yend = max(mod$volume_mod) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(mod$volume_mod) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(mod$volume_mod) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Trading Volume of MODERNA Inc.",
         yaxis = list(title = "Volume"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
plot_ly(data = mod,
        x = ~ date,
        y = ~ adj.close_mod,
        type = "scatter", 
        mode = "line",
        name = "Ajusted Close Price of MODERNA") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(mod$adj.close_mod),
               yend = max(mod$adj.close_mod) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(mod$adj.close_mod) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(mod$adj.close_mod) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Adjusted Close Price of MODERNA Inc.",
         yaxis = list(title = "Adjusted Close Price"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
mod %>% 
  ggplot(aes(x = date, y = variation_mod)) + 
  geom_line(color = wes_palette('Moonrise2', n=1, 'discrete')) + 
  geom_smooth(se = F, lty = 2, color = wes_palette('BottleRocket1', 1)) + 
  labs(x = "Date", y = "Variation", 
       title = "MODERNA Stock Price Variation",
       subtitle = "Smoothed Line in Red") + 
  cowplot::theme_cowplot()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

library(dplyr)
library(tsbox)
library(TSstudio)

## Quantify impact of Covid-19 on share prices

pre_covid_mod <- mod %>% 
  dplyr::filter(date < as.Date("2020-04-01")) %>%
  dplyr::arrange(date)

post_covid_mod <- mod %>% 
  dplyr::filter(date >= as.Date("2020-04-01")) %>%
  dplyr::arrange(date)


## The series before the outbreak of the pandemic

# Step 01: convert the series into a 'ts' object first.
ts.obj_mod <- ts(pre_covid_mod$adj.close_mod, start = c(2020, 3), frequency = 12)

ts_plot(ts.obj_mod,
        title = "Stock Value of MODERNA",
        Ytitle = "Price per share (US Dollars)",
        slider = TRUE)
ts_seasonal(ts.obj = ts.obj_mod, type = "all")
# Step 02: review the series correlation with its past lags using the ACF and PACF to select time-series models for seasonal data functions.
ts_cor(ts.obj = ts.obj_mod, lag.max = 36)
## Step 03: select time-series models for seasonal data

methods <- list(
                auto_arima = list(method = "auto.arima",
                              notes = "Auto ARIMA"),
                hw1 = list(method = "HoltWinters",
                          method_arg = NULL,
                          notes = "HoltWinters Model"),
                 hw2 = list(method = "HoltWinters",
                          method_arg = list(seasonal = "multiplicative"),
                          notes = "HW with multip. seasonality"),
                tslm = list(method = "tslm",
                            method_arg = list(formula = input ~ trend + season),
                            notes = "tslm with trend and seasonal"))


# Backtesting is the time series equivalent of the machine learning cross-validation training approach. The idea here is simple - test each model with backtesting, and select the one that performed best, on average, on the different testing partition.
# For the backtesting, we will split the series into 6 testing partitions, each 12 months spaced by 3 months from each other.
train_method = list(partitions = 6,
                    sample.out = 12,
                    space = 3)

# train the models:
# Note that the forecast horizon is set the the length of the post_covid series. In addition we will set the MAPA as the error metric to evaluate the performance of the different models on the testing partitions:
md_mod <- train_model(input = ts.obj_mod,
                  methods = methods,
                  train_method = train_method,
                  horizon = nrow(post_covid_mod),
                  error = "MAPE")
## # A tibble: 4 x 7
##   model_id  model   notes    avg_mape avg_rmse `avg_coverage_8~ `avg_coverage_9~
##   <chr>     <chr>   <chr>       <dbl>    <dbl>            <dbl>            <dbl>
## 1 tslm      tslm    tslm wi~    0.128     4.06            0.569            0.792
## 2 hw2       HoltWi~ HW with~    0.176     5.25            0.625            0.819
## 3 auto_ari~ auto.a~ Auto AR~    0.179     5.44            0.472            0.736
## 4 hw1       HoltWi~ HoltWin~    0.180     5.33            0.639            0.819
# Quantify the Covid-19 impact

# select the tslm with trend and seasonal (tslm) to calculate the Covid-19 effect when it demonstrated the lowest error through back-testing 
post_covid_mod$yhat <- as.numeric(md_mod$forecast$tslm$forecast$mean)
post_covid_mod$upper95 <- as.numeric(md_mod$forecast$tslm$forecast$upper[,2])
post_covid_mod$lower95 <- as.numeric(md_mod$forecast$tslm$forecast$lower[,2])

post_covid_mod$price_change <- 
  post_covid_mod$adj.close_mod - post_covid_mod$yhat

post_covid_mod
##           date volume_mod adj.close_mod variation_mod     yhat  upper95
## 1   2020-04-01    7004600        29.670   -0.37999916 29.60756 35.59695
## 2   2020-04-02   15934400        33.200    2.60000038 29.29956 35.28895
## 3   2020-04-03   16896200        34.840    2.02999878 30.66556 36.65495
## 4   2020-04-06   10841600        34.640   -0.27000046 29.62456 35.61395
## 5   2020-04-07   10104300        32.020   -1.79000092 29.30356 35.29295
## 6   2020-04-08    4119800        32.420    0.05999756 29.37556 35.36495
## 7   2020-04-09    4861800        31.860   -0.68999863 29.39956 35.38895
## 8   2020-04-13    5603900        32.900    0.90000153 28.54356 34.53295
## 9   2020-04-14    8126900        34.660    0.65999985 28.50556 34.49495
## 10  2020-04-15   15683000        37.250    2.61000061 29.01156 35.00095
## 11  2020-04-16   20670700        40.600    2.52599716 30.26381 36.23128
## 12  2020-04-17   32286000        46.850   -2.14000320 31.58381 37.55128
## 13  2020-04-20   34102500        51.690    6.68999863 31.75007 37.86083
## 14  2020-04-21   39642000        49.260   -3.74000168 31.44207 37.55283
## 15  2020-04-22   11670100        51.200    0.59000015 32.80807 38.91883
## 16  2020-04-23   15152400        47.440   -1.65999985 31.76707 37.87783
## 17  2020-04-24   11702100        50.500    3.34999847 31.44607 37.55683
## 18  2020-04-27   13744400        48.050   -3.47999954 31.51807 37.62883
## 19  2020-04-28   13067200        47.880   -2.52000046 31.54207 37.65283
## 20  2020-04-29   11762000        46.370   -0.22999954 30.68607 36.79683
## 21  2020-04-30    7741900        45.990   -0.78999710 30.64807 36.75883
## 22  2020-05-01   21701300        47.930   -0.77999878 31.15407 37.26483
## 23  2020-05-04   10460500        50.500    2.45000076 32.40633 38.51279
## 24  2020-05-05   11160200        49.340   -1.13999939 33.72633 39.83279
## 25  2020-05-06    6692300        48.950    0.50000000 33.89259 40.15593
## 26  2020-05-07   30942700        53.190   -2.59000015 33.58459 39.84793
## 27  2020-05-08   24083200        59.250    6.18999863 34.95059 41.21393
## 28  2020-05-11   34852000        66.900    7.30000305 33.90959 40.17293
## 29  2020-05-12   25934500        62.350   -3.63999939 33.58859 39.85193
## 30  2020-05-13   22126000        65.180    1.38000107 33.66059 39.92393
## 31  2020-05-14   12198200        64.560   -0.23000336 33.68459 39.94793
## 32  2020-05-15   16341900        66.690    2.90000153 32.82859 39.09193
## 33  2020-05-18   80813300        80.000   -6.13999939 32.79059 39.05393
## 34  2020-05-19   64932300        71.670   -6.40000153 33.29659 39.55993
## 35  2020-05-20   53655300        73.470    3.45999908 34.54885 40.82473
## 36  2020-05-21   35318000        67.050   -6.00000000 35.86885 42.14473
## 37  2020-05-22   28912700        69.000   -0.93000031 36.03511 42.48003
## 38  2020-05-26   40332600        57.710   -8.69000244 35.72711 42.17203
## 39  2020-05-27   59514700        52.180    0.97999954 37.09311 43.53803
## 40  2020-05-28   32258900        55.540    2.04000092 36.05211 42.49703
## 41  2020-05-29   38070400        61.500    4.04999924 35.73111 42.17603
## 42  2020-06-01   21257500        62.180   -0.57999802 35.80311 42.24803
## 43  2020-06-02   21701100        59.870    1.16999817 35.82711 42.27203
## 44  2020-06-03   20811500        59.890    2.38000107 34.97111 41.41603
## 45  2020-06-04   13504000        60.590   -1.02000046 34.93311 41.37803
## 46  2020-06-05   14677900        58.190   -0.30000305 35.43911 41.88403
## 47  2020-06-08   11249300        59.100    0.79999924 36.69137 43.16471
## 48  2020-06-09    7600200        58.230   -0.31999969 38.01137 44.48471
## 49  2020-06-10   11074400        60.070    1.27000046 38.17763 44.83076
## 50  2020-06-11   32085200        60.200   -2.75000000 37.86963 44.52276
## 51  2020-06-12   14742900        62.000   -0.35499954 39.23563 45.88876
## 52  2020-06-15   19339300        66.570    0.66999817 38.19463 44.84776
## 53  2020-06-16   16537800        64.340   -1.97000122 37.87363 44.52676
## 54  2020-06-17   13616000        63.310   -0.28999710 37.94563 44.59876
## 55  2020-06-18   10914400        64.950    1.91999817 37.96963 44.62276
## 56  2020-06-19   16076700        66.350    1.36000061 37.11363 43.76676
## 57  2020-06-22   13586400        64.750   -1.16999817 37.07563 43.72876
## 58  2020-06-23   12361300        62.940   -1.15999985 37.58163 44.23476
## 59  2020-06-24   14377100        64.840    1.60999680 38.83389 45.53023
## 60  2020-06-25   26905700        62.000   -5.00000000 40.15389 46.85023
## 61  2020-06-26   13866300        61.280    0.56999969 40.32015 47.20570
## 62  2020-06-29    8074200        62.080    0.26000214 40.01215 46.89770
## 63  2020-06-30   11955200        64.210    2.02999878 41.37815 48.26370
## 64  2020-07-01   17916500        61.590   -1.40999985 40.33715 47.22270
## 65  2020-07-02   26544400        58.570   -1.43000031 40.01615 46.90170
## 66  2020-07-06    9977300        59.150    0.60000229 40.08815 46.97370
## 67  2020-07-07    8904300        61.090    2.29000092 40.11215 46.99770
## 68  2020-07-08    7590900        61.580    0.79000092 39.25615 46.14170
## 69  2020-07-09   20646100        64.960    2.86999893 39.21815 46.10370
## 70  2020-07-10    7407500        62.610   -2.04000092 39.72415 46.60970
## 71  2020-07-13   56617300        71.780    7.88999939 40.97641 47.91885
## 72  2020-07-14   38708800        75.040    1.04000092 42.29641 49.23885
## 73  2020-07-15   90756100        80.220   -7.29999542 42.46267 49.60250
## 74  2020-07-16   27277400        81.810    1.64999390 42.15467 49.29450
## 75  2020-07-17  103509200        94.850   11.50000000 43.52067 50.66050
## 76  2020-07-20   78391300        82.680   -5.18000031 42.47967 49.61950
## 77  2020-07-21   26157900        80.860   -0.05000305 42.15867 49.29850
## 78  2020-07-22   27274000        83.225    3.61499786 42.23067 49.37050
## 79  2020-07-23   29288100        75.330   -6.72000122 42.25467 49.39450
## 80  2020-07-24   27797500        73.210    4.15999603 41.39867 48.53850
## 81  2020-07-27   44710700        79.910   -0.71999359 41.36067 48.50050
## 82  2020-07-28   32946900        81.490    0.48999786 41.86667 49.00650
## 83  2020-07-29   24453900        79.510   -2.47000122 43.11893 50.32820
## 84  2020-07-30   13102000        77.630   -0.37000275 44.43893 51.64820
## 85  2020-07-31   14210400        74.100   -4.09999847 44.60519 52.01889
## 86  2020-08-03   13382300        77.980    3.00000000 44.29719 51.71089
## 87  2020-08-04   10126500        78.460    0.26799774 45.66319 53.07689
## 88  2020-08-05   19831900        75.800   -1.68999481 44.62219 52.03589
## 89  2020-08-06   11691500        73.760   -2.02999878 44.30119 51.71489
## 90  2020-08-07    8771400        74.100   -0.27999878 44.37319 51.78689
## 91  2020-08-10    8678300        72.010   -2.25999451 44.39719 51.81089
## 92  2020-08-11   18118700        68.970   -0.54999542 43.54119 50.95489
## 93  2020-08-12   50362100        69.520   -6.15000153 43.50319 50.91689
## 94  2020-08-13   15055900        67.830   -1.25999451 44.00919 51.42289
## 95  2020-08-14   12799600        69.150    1.09000397 45.26144 52.75606
## 96  2020-08-17    8749100        69.760    0.62000275 46.58144 54.07606
## 97  2020-08-18   11329200        67.030   -1.25000000 46.74770 54.45280
## 98  2020-08-19    8558800        68.000    1.26000214 46.43970 54.14480
## 99  2020-08-20    6346700        67.810    0.01999664 47.80570 55.51080
## 100 2020-08-21    6935600        66.450   -1.05000305 46.76470 54.46980
## 101 2020-08-24   13143200        64.880   -2.27000427 46.44370 54.14880
## 102 2020-08-25    7798500        66.250    1.93000031 46.51570 54.22080
## 103 2020-08-26   28768100        70.500    2.87999725 46.53970 54.24480
## 104 2020-08-27    9262600        68.030   -1.95000458 45.68370 53.38880
## 105 2020-08-28    6368200        67.490   -0.55000305 45.64570 53.35080
## 106 2020-08-31   15271800        64.890   -2.25000000 46.15170 53.85680
## 107 2020-09-01    8183600        63.320   -0.22000122 47.40396 55.20041
## 108 2020-09-02    9874600        64.720    1.88000107 48.72396 56.52041
## 109 2020-09-03   14307300        64.840   -0.81000519 48.89022 56.90231
## 110 2020-09-04   11653300        62.600   -2.38999939 48.58222 56.59431
## 111 2020-09-08   17706200        54.340   -3.40999985 49.94822 57.96031
## 112 2020-09-09    9355400        56.900    0.87000275 48.90722 56.91931
## 113 2020-09-10    7985300        57.560    0.51900101 48.58622 56.59831
## 114 2020-09-11    4821900        59.340    0.84999847 48.65822 56.67031
## 115 2020-09-14    8930800        63.670    3.51999664 48.68222 56.69431
## 116 2020-09-15    9101200        66.880    1.85299683 47.82622 55.83831
## 117 2020-09-16   11345200        68.840    1.19999695 47.78822 55.80031
## 118 2020-09-17   13275200        67.890   -0.61000061 48.29422 56.30631
## 119 2020-09-18   13206200        69.870    1.59000397 49.54648 57.65939
## 120 2020-09-21    8223000        69.260    0.66000366 50.86648 58.97939
## 121 2020-09-22    6303600        68.720    1.22000122 51.03274 59.36569
## 122 2020-09-23    6919400        67.160   -0.70499420 50.72474 59.05769
## 123 2020-09-24    5565200        65.170   -0.33000183 52.09074 60.42369
## 124 2020-09-25    9190600        69.470    3.58300018 51.04974 59.38269
## 125 2020-09-28    7337400        70.550    0.96000671 50.72874 59.06169
## 126 2020-09-29    6027600        70.520    0.05999756 50.80074 59.13369
## 127 2020-09-30   15690700        70.750   -1.76000214 50.82474 59.15769
## 128 2020-10-01    6839000        70.030    0.45999908 49.96874 58.30169
## 129 2020-10-02    8004900        68.810    0.90999603 49.93074 58.26369
## 130 2020-10-05    6518100        71.950    2.30999756 50.43674 58.76969
## 131 2020-10-06    7285500        71.040   -0.95999908 51.68900 60.13137
## 132 2020-10-07    4190600        72.370    1.13999939 53.00900 61.45137
## 133 2020-10-08    4023800        72.930   -0.56999969 53.17526 61.84141
## 134 2020-10-09    4774000        73.000   -0.41999817 52.86726 61.53341
## 135 2020-10-12    7590800        75.310    1.69999695 54.23326 62.89941
## 136 2020-10-13   13669200        78.290    2.36000061 53.19226 61.85841
## 137 2020-10-14    6634600        76.560   -3.45000458 52.87126 61.53741
## 138 2020-10-15    5009100        75.580    0.09999847 52.94326 61.60941
## 139 2020-10-16    4693600        73.940   -2.11999512 52.96726 61.63341
## 140 2020-10-19    6931300        70.960   -3.01000214 52.11126 60.77741
## 141 2020-10-20    5441300        71.310   -0.76000214 52.07326 60.73941
## 142 2020-10-21    4614800        68.370   -3.19999695 52.57926 61.24541
## 143 2020-10-22    5212100        70.840    2.33599854 53.83152 62.61489
## 144 2020-10-23    3735100        70.530   -1.77000427 55.15152 63.93489
## 145 2020-10-26    3872800        70.240   -0.12000275 55.31778 64.32810
## 146 2020-10-27    3812900        70.670    0.22000122 55.00978 64.02010
## 147 2020-10-28    6044000        65.740   -3.20999908 56.37578 65.38610
## 148 2020-10-29   13776400        71.280    2.93000031 55.33478 64.34510
## 149 2020-10-30    7070500        67.470   -3.33000183 55.01378 64.02410
## 150 2020-11-02    4650300        67.110   -0.90999603 55.08578 64.09610
## 151 2020-11-03    4366900        69.080    2.33000183 55.10978 64.12010
## 152 2020-11-04    5442100        69.810    0.83999634 54.25378 63.26410
## 153 2020-11-05    4449900        71.480    0.39000702 54.21578 63.22610
## 154 2020-11-06    5841000        72.450    1.02999878 54.72178 63.73210
## 155 2020-11-09   20266800        77.740   -0.01000214 55.97404 65.10864
## 156 2020-11-10   10499400        76.050   -3.94999695 57.29404 66.42864
## 157 2020-11-11   18576500        82.440    5.38999939 57.46030 66.82454
## 158 2020-11-12   21700100        87.810    2.62999725 57.15230 66.51655
## 159 2020-11-13   14121100        89.390    2.76999664 58.51830 67.88254
## 160 2020-11-16   70853100        97.950   -2.36000061 57.47730 66.84154
## 161 2020-11-17   26172000        93.150   -4.08000183 57.15630 66.52054
## 162 2020-11-18   21823500        88.890   -4.98000336 57.22830 66.59254
## 163 2020-11-19   16002000        92.770    2.76999664 57.25230 66.61654
## 164 2020-11-20   15306300        97.610    2.61000061 56.39630 65.76054
## 165 2020-11-23   16359600       101.030    0.26000214 56.35830 65.72254
## 166 2020-11-24   12515300        98.560    0.15999603 56.86430 66.22854
## 167 2020-11-25   25850700       109.180   10.15000153 58.11656 67.61151
## 168 2020-11-27   43438500       127.030   11.90999603 59.43656 68.93151
## 169 2020-11-30   73203100       152.740    8.74000549 59.60281 69.32968
## 170 2020-12-01  125552300       141.010  -36.65000916 59.29482 69.02168
## 171 2020-12-02   51217300       143.000    4.36999512 60.66082 70.38768
## 172 2020-12-03   38656500       157.260   13.75999451 59.61982 69.34668
## 173 2020-12-04   27704800       152.520   -5.06999207 59.29882 69.02568
## 174 2020-12-07   31409800       159.520    4.49000549 59.37082 69.09768
## 175 2020-12-08   27738700       169.860    4.11000061 59.39482 69.12168
## 176 2020-12-09   40951700       156.590  -13.61000061 58.53882 68.26568
## 177 2020-12-10   22060000       155.690    4.55999756 58.50082 68.22768
## 178 2020-12-11   22069900       156.930   -2.77000427 59.00682 68.73368
## 179 2020-12-14   22467700       155.070   -8.32998657 60.25907 70.12248
## 180 2020-12-15   36451300       147.220   -9.77999878 61.57907 71.44248
## 181 2020-12-16   28476300       137.030   -0.72000122 61.74533 71.84258
## 182 2020-12-17   32404500       144.000    6.08000183 61.43733 71.53458
## 183 2020-12-18   33157800       140.230   -1.27999878 62.80333 72.90058
## 184 2020-12-21   23921000       138.300   -2.53999329 61.76233 71.85958
## 185 2020-12-22   36035800       125.880  -13.12000275 61.44133 71.53858
##      lower95 price_change
## 1   23.61816   0.06244488
## 2   23.31016   3.90044460
## 3   24.67616   4.17444477
## 4   23.63516   5.01544361
## 5   23.31416   2.71644497
## 6   23.38616   3.04444256
## 7   23.41016   2.46044483
## 8   22.55416   4.35644588
## 9   22.51616   6.15444393
## 10  23.02216   8.23844433
## 11  24.29635  10.33618383
## 12  25.61635  15.26618382
## 13  25.63932  19.93992488
## 14  25.33132  17.81792361
## 15  26.69732  18.39192683
## 16  25.65632  15.67292430
## 17  25.33532  19.05392596
## 18  25.40732  16.53192507
## 19  25.43132  16.33792674
## 20  24.57532  15.68392474
## 21  24.53732  15.34192721
## 22  25.04332  16.77592608
## 23  26.29987  18.09366681
## 24  27.61987  15.61366695
## 25  27.62926  15.05740846
## 26  27.32126  19.60540536
## 27  28.68726  24.29940751
## 28  27.64626  32.99040864
## 29  27.32526  28.76140588
## 30  27.39726  31.51940759
## 31  27.42126  30.87540468
## 32  26.56526  33.86140970
## 33  26.52726  47.20940698
## 34  27.03326  38.37340539
## 35  28.27297  38.92114948
## 36  29.59297  31.18115129
## 37  29.59019  32.96488914
## 38  29.28219  21.98288727
## 39  30.64819  15.08688927
## 40  29.60719  19.48788948
## 41  29.28619  25.76888885
## 42  29.35819  26.37688904
## 43  29.38219  24.04288750
## 44  28.52619  24.91888809
## 45  28.48819  25.65688858
## 46  28.99419  22.75088730
## 47  30.21803  22.40862818
## 48  31.53803  20.21862923
## 49  31.52450  21.89237029
## 50  31.21650  22.33037039
## 51  32.58250  22.76437041
## 52  31.54150  28.37536971
## 53  31.22050  26.46636664
## 54  31.29250  25.36437155
## 55  31.31650  26.98036696
## 56  30.46050  29.23636862
## 57  30.42250  27.67436988
## 58  30.92850  25.35836875
## 59  32.13754  26.00610749
## 60  33.45754  21.84611114
## 61  33.43459  20.95985082
## 62  33.12659  22.06785291
## 63  34.49259  22.83185094
## 64  33.45159  21.25285161
## 65  33.13059  18.55385145
## 66  33.20259  19.06185315
## 67  33.22659  20.97785161
## 68  32.37059  22.32385343
## 69  32.33259  25.74185041
## 70  32.83859  22.88585218
## 71  34.03396  30.80359138
## 72  35.35396  32.74359350
## 73  35.32284  37.75733471
## 74  35.01484  39.65533009
## 75  36.38084  51.32933178
## 76  35.33984  40.20033321
## 77  35.01884  38.70133381
## 78  35.09084  40.99433155
## 79  35.11484  33.07533474
## 80  34.25884  31.81133213
## 81  34.22084  38.54933643
## 82  34.72684  39.62333088
## 83  35.90965  36.39107618
## 84  37.22965  33.19107129
## 85  37.19148  29.49481341
## 86  36.88348  33.68281733
## 87  38.24948  32.79681384
## 88  37.20848  31.17781741
## 89  36.88748  29.45881678
## 90  36.95948  29.72681300
## 91  36.98348  27.61281649
## 92  36.12748  25.42881571
## 93  36.08948  26.01681086
## 94  36.59548  23.82081629
## 95  37.76683  23.88855702
## 96  39.08683  23.17855762
## 97  39.04261  20.28229516
## 98  38.73461  21.56029542
## 99  40.10061  20.00429376
## 100 39.05961  19.68529275
## 101 38.73861  18.43629335
## 102 38.81061  19.73429597
## 103 38.83461  23.96029580
## 104 37.97861  22.34629472
## 105 37.94061  21.84429353
## 106 38.44661  18.73829530
## 107 39.60752  15.91603664
## 108 40.92752  15.99603815
## 109 40.87814  15.94977417
## 110 40.57014  14.01777534
## 111 41.93614   4.39177780
## 112 40.89514   7.99277878
## 113 40.57414   8.97377892
## 114 40.64614  10.68177757
## 115 40.67014  14.98777542
## 116 39.81414  19.05377464
## 117 39.77614  21.05177345
## 118 40.28214  19.59577675
## 119 41.43357  20.32352114
## 120 42.75357  18.39352051
## 121 42.69979  17.68726050
## 122 42.39179  16.43526198
## 123 43.75779  13.07925727
## 124 42.71679  18.42025992
## 125 42.39579  19.82126204
## 126 42.46779  19.71925551
## 127 42.49179  19.92525870
## 128 41.63579  20.06125762
## 129 41.59779  18.87925612
## 130 42.10379  21.51325576
## 131 43.24663  19.35100075
## 132 44.56663  19.36100257
## 133 44.50911  19.75474103
## 134 44.20111  20.13273977
## 135 45.56711  21.07673810
## 136 44.52611  25.09774106
## 137 44.20511  23.68873800
## 138 44.27711  22.63674215
## 139 44.30111  20.97274259
## 140 43.44511  18.84873937
## 141 43.40711  19.23673757
## 142 43.91311  15.79074300
## 143 45.04815  17.00847762
## 144 46.36815  15.37848005
## 145 46.30746  14.92222004
## 146 45.99946  15.66021938
## 147 47.36546   9.36421986
## 148 46.32446  15.94522038
## 149 46.00346  12.45622311
## 150 46.07546  12.02422237
## 151 46.09946  13.97022343
## 152 45.24346  15.55621929
## 153 45.20546  17.26422482
## 154 45.71146  17.72821865
## 155 46.83943  21.76596060
## 156 48.15943  18.75596577
## 157 48.09605  24.97970606
## 158 47.78805  30.65770022
## 159 49.15405  30.87170283
## 160 48.11305  40.47269999
## 161 47.79205  35.99370486
## 162 47.86405  31.66170260
## 163 47.88805  35.51769969
## 164 47.03205  41.21370379
## 165 46.99405  44.67170169
## 166 47.50005  41.69570071
## 167 48.62161  51.06344449
## 168 49.94161  67.59344295
## 169 49.87595  93.13719056
## 170 49.56795  81.71517862
## 171 50.93395  82.33918489
## 172 49.89295  97.64017900
## 173 49.57195  93.22118905
## 174 49.64395 100.14918893
## 175 49.66795 110.46518510
## 176 48.81195  98.05118097
## 177 48.77395  97.18918680
## 178 49.27995  97.92317727
## 179 50.39567  94.81093295
## 180 51.71567  85.64092684
## 181 51.64809  75.28466530
## 182 51.34009  82.56266556
## 183 52.70609  77.42666206
## 184 51.66509  76.53766899
## 185 51.34409  64.43866348
# the estimated total stock value change between April and August 2020 as result of the pandemic
sum(post_covid_mod$price_change)
## [1] 5355.499
# Data viz
plot_ly() %>%
   add_ribbons(x = post_covid_mod$date,
              ymin = post_covid_mod$adj.close_mod,
              ymax = post_covid_mod$yhat,
              line = list(color = 'rgba(255, 0, 0, 0.05)'),
              fillcolor = 'rgba(255, 0, 0, 0.6)',
              name = "Estimated Change") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(mod$adj.close_mod),
               yend = max(mod$adj.close_mod) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2020-01-01"),
                  y = max(mod$adj.close_mod) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2020-11-01"),
                  y = max(mod$adj.close_mod) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = paste("Estimated increase in", "<br>",
                               "stock price: ~$5,400",
                               sep = ""),
                  x = as.Date("2020-09-01"),
                  y = 5 * 10^1,
                  arrowhead = 1,
                  ax = 120,
                  ay = 40,
                  showarrow = TRUE) %>%
  add_lines(x = mod$date,
            y = mod$adj.close_mod,
            line = list(color = "#1f77b4"),
            name = "Actual") %>%
  layout(title = "Covid19 Impact on Stock Value of MODERNA",
         yaxis = list(title = "Stock Price (US Dollars)"),
         xaxis = list(title = "Time Series Model - Auto ARIMA Model",
                      range = c(as.Date("2019-10-01"), as.Date("2021-01-01"))),
         legend = list(x = 0, y = 0.95))

5. NOVAVAX

library(dplyr)
library(plotly)
library(ggplot2)
library(wesanderson)

# NOVAVAX
nova <- dt2[3:247,c(1,34,35,36)]
nova$Companies <- as.Date(nova$Companies)
nova$NOVAVAX.4 <- as.numeric(as.character(nova$NOVAVAX.4))
nova$NOVAVAX.5 <- as.numeric(as.character(nova$NOVAVAX.5))
nova$NOVAVAX.6 <- as.numeric(as.character(nova$NOVAVAX.6))
colnames(nova) <- c("date", "volume_nova", "adj.close_nova", "variation_nova")


plot_ly(data = nova,
        x = ~ date,
        y = ~ volume_nova,
        type = "scatter", 
        mode = "line",
        name = "Trading Volume of NOVAVAX") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(nova$volume_nova),
               yend = max(nova$volume_nova) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(nova$volume_nova) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(nova$volume_nova) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Trading Volume of NOVAVAX Inc.",
         yaxis = list(title = "Volume"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
plot_ly(data = nova,
        x = ~ date,
        y = ~ adj.close_nova,
        type = "scatter", 
        mode = "line",
        name = "Ajusted Close Price of NOVAVAX") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(nova$adj.close_nova),
               yend = max(nova$adj.close_nova) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2019-09-01"),
                  y = max(nova$adj.close_nova) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2021-08-01"),
                  y = max(nova$adj.close_nova) * 1.05, 
                  showarrow = FALSE) %>%
  layout(title = "Adjusted Close Price of NOVAVAX Inc.",
         yaxis = list(title = "Adjusted Close Price"),
         xaxis = list(title = "Source: COVID-19 biotech companies on stock exchange(2020)"))
nova %>% 
  ggplot(aes(x = date, y = variation_nova)) + 
  geom_line(color = wes_palette('Moonrise2', n=1, 'discrete')) + 
  geom_smooth(se = F, lty = 2, color = wes_palette('BottleRocket1', 1)) + 
  labs(x = "Date", y = "Variation", 
       title = "NOVAVAX Stock Price Variation",
       subtitle = "Smoothed Line in Red") + 
  cowplot::theme_cowplot()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

library(dplyr)
library(tsbox)
library(TSstudio)

## Quantify impact of Covid-19 on share prices

pre_covid_nova <- nova %>% 
  dplyr::filter(date < as.Date("2020-04-01")) %>%
  dplyr::arrange(date)

post_covid_nova <- nova %>% 
  dplyr::filter(date >= as.Date("2020-04-01")) %>%
  dplyr::arrange(date)


## The series before the outbreak of the pandemic

# Step 01: convert the series into a 'ts' object first.
ts.obj_nova <- ts(pre_covid_nova$adj.close_nova, start = c(2020, 3), frequency = 12)

ts_plot(ts.obj_nova,
        title = "Stock Value of NOVAVAX",
        Ytitle = "Price per share (US Dollars)",
        slider = TRUE)
ts_seasonal(ts.obj = ts.obj_nova, type = "all")
# Step 02: review the series correlation with its past lags using the ACF and PACF to select time-series models for seasonal data functions.
ts_cor(ts.obj = ts.obj_nova, lag.max = 36)
## Step 03: select time-series models for seasonal data

methods <- list(
                auto_arima = list(method = "auto.arima",
                              notes = "Auto ARIMA"),
                hw1 = list(method = "HoltWinters",
                          method_arg = NULL,
                          notes = "HoltWinters Model"),
                 hw2 = list(method = "HoltWinters",
                          method_arg = list(seasonal = "multiplicative"),
                          notes = "HW with multip. seasonality"),
                tslm = list(method = "tslm",
                            method_arg = list(formula = input ~ trend + season),
                            notes = "tslm with trend and seasonal"))


# Backtesting is the time series equivalent of the machine learning cross-validation training approach. The idea here is simple - test each model with backtesting, and select the one that performed best, on average, on the different testing partition.
# For the backtesting, we will split the series into 6 testing partitions, each 12 months spaced by 3 months from each other.
train_method = list(partitions = 6,
                    sample.out = 12,
                    space = 3)

# train the models:
# Note that the forecast horizon is set the the length of the post_covid series. In addition we will set the MAPA as the error metric to evaluate the performance of the different models on the testing partitions:
md_nova <- train_model(input = ts.obj_nova,
                  methods = methods,
                  train_method = train_method,
                  horizon = nrow(post_covid_nova),
                  error = "MAPE")
## # A tibble: 4 x 7
##   model_id  model   notes    avg_mape avg_rmse `avg_coverage_8~ `avg_coverage_9~
##   <chr>     <chr>   <chr>       <dbl>    <dbl>            <dbl>            <dbl>
## 1 tslm      tslm    tslm wi~    0.213     2.68            0.556            0.819
## 2 auto_ari~ auto.a~ Auto AR~    0.236     2.97            0.764            0.917
## 3 hw1       HoltWi~ HoltWin~    0.315     3.78            0.625            0.806
## 4 hw2       HoltWi~ HW with~    0.380     4.60            0.736            0.861
# Quantify the Covid-19 impact

# select the tslm with trend and seasonal (tslm) to calculate the Covid-19 effect when it demonstrated the lowest error through back-testing 
post_covid_nova$yhat <- as.numeric(md_nova$forecast$tslm$forecast$mean)
post_covid_nova$upper95 <- as.numeric(md_nova$forecast$tslm$forecast$upper[,2])
post_covid_nova$lower95 <- as.numeric(md_nova$forecast$tslm$forecast$lower[,2])

post_covid_nova$price_change <- 
  post_covid_nova$adj.close_nova - post_covid_nova$yhat

post_covid_nova
##           date volume_nova adj.close_nova variation_nova     yhat  upper95
## 1   2020-04-01     5953000          14.03    -0.15000057 12.64600 16.43004
## 2   2020-04-02     3110300          13.86     0.21000004 14.06800 17.85204
## 3   2020-04-03     9144800          15.61     1.86999989 13.60400 17.38804
## 4   2020-04-06     5415800          16.10     0.50000000 13.31800 17.10204
## 5   2020-04-07     4704500          14.90    -0.89000034 13.07600 16.86004
## 6   2020-04-08    13825700          17.18     0.03000069 13.56600 17.35004
## 7   2020-04-09     4766100          17.05    -0.40000153 14.01400 17.79804
## 8   2020-04-13     4114700          17.64    -0.21000099 13.23200 17.01604
## 9   2020-04-14     3936500          18.00     0.14999962 13.63200 17.41604
## 10  2020-04-15     3455100          17.53    -0.01999855 13.93800 17.72204
## 11  2020-04-16     4188800          18.10     0.50000000 14.67567 18.44585
## 12  2020-04-17     7478600          19.08     0.97999954 14.06900 17.83918
## 13  2020-04-20    27006300          23.80     4.69999886 14.33000 18.19072
## 14  2020-04-21    15517500          21.53    -2.51000023 15.75200 19.61272
## 15  2020-04-22     6488700          21.37    -1.60999870 15.28800 19.14872
## 16  2020-04-23     4911400          20.38    -1.04000092 15.00200 18.86272
## 17  2020-04-24     4969800          19.83    -0.48999977 14.76000 18.62072
## 18  2020-04-27     5280400          21.01     0.80999947 15.25000 19.11072
## 19  2020-04-28     3983000          20.55    -0.85000038 15.69800 19.55872
## 20  2020-04-29     6512300          18.41    -1.61000061 14.91600 18.77672
## 21  2020-04-30     4548900          18.13    -0.72000122 15.31600 19.17672
## 22  2020-05-01     4761400          17.33    -0.46999931 15.62200 19.48272
## 23  2020-05-04     3736700          18.06     1.05999947 16.35967 20.21767
## 24  2020-05-05     2948300          17.78    -0.46999931 15.75300 19.61100
## 25  2020-05-06     2481000          17.14    -0.57999992 16.01400 19.97111
## 26  2020-05-07     3392200          18.15     0.84000015 17.43600 21.39311
## 27  2020-05-08     3075400          18.73     0.52999878 16.97200 20.92911
## 28  2020-05-11    24642100          24.50     4.69000053 16.68600 20.64311
## 29  2020-05-12    74649600          39.82     0.79999924 16.44400 20.40111
## 30  2020-05-13    24237100          40.67     1.34999847 16.93400 20.89111
## 31  2020-05-14     7832800          38.30    -0.93000031 17.38200 21.33911
## 32  2020-05-15    21892500          43.63     5.45000076 16.60000 20.55711
## 33  2020-05-18    24660100          56.96     7.68000031 17.00000 20.95711
## 34  2020-05-19    12579400          53.90    -4.34999847 17.30600 21.26311
## 35  2020-05-20     8180300          52.81    -1.19999695 18.04367 22.00871
## 36  2020-05-21     5479500          50.07    -2.68000031 17.43700 21.40204
## 37  2020-05-22     6083300          46.11    -2.38999939 17.69800 21.76984
## 38  2020-05-26    22045300          48.17    -6.18000031 19.12000 23.19184
## 39  2020-05-27    10439500          45.47    -2.72999954 18.65600 22.72784
## 40  2020-05-28     4955100          44.84    -1.25999832 18.37000 22.44184
## 41  2020-05-29     4119800          46.04     1.31999969 18.12800 22.19984
## 42  2020-06-01     9686800          52.82     6.65000153 18.61800 22.68984
## 43  2020-06-02     6538800          49.64    -3.10000229 19.06600 23.13784
## 44  2020-06-03    16950300          44.25    -4.65000153 18.28400 22.35584
## 45  2020-06-04     5713100          44.66     0.65999985 18.68400 22.75584
## 46  2020-06-05     8077200          46.30    -3.15999985 18.99000 23.06184
## 47  2020-06-08     4812700          44.79     0.20999908 19.72767 23.81746
## 48  2020-06-09     3875400          45.93     1.88000107 19.12100 23.21079
## 49  2020-06-10     2308700          44.96    -1.20999908 19.38200 23.58538
## 50  2020-06-11     4089100          44.43     1.15000153 20.80400 25.00738
## 51  2020-06-12     3297800          45.57     0.02000046 20.34000 24.54338
## 52  2020-06-15     8694400          51.07     5.13000107 20.05400 24.25738
## 53  2020-06-16     5026200          52.15     0.97000122 19.81200 24.01538
## 54  2020-06-17     9350000          55.41     3.38000107 20.30200 24.50538
## 55  2020-06-18     5654100          59.27     4.02000046 20.75000 24.95338
## 56  2020-06-19    11569300          64.75     1.47000122 19.96800 24.17138
## 57  2020-06-22     8431300          65.95    -1.65000153 20.36800 24.57138
## 58  2020-06-23     6138100          69.66     6.72000504 20.67400 24.87738
## 59  2020-06-24    10960400          77.50     7.44999695 21.41167 25.64235
## 60  2020-06-25    10743500          83.54     2.23000336 20.80500 25.03569
## 61  2020-06-26    13522400          77.39    -2.34000397 21.06600 25.41623
## 62  2020-06-29     8693300          83.61     2.12999725 22.48800 26.83823
## 63  2020-06-30     7782200          83.35    -1.65000153 22.02400 26.37423
## 64  2020-07-01     6612100          79.70     0.22999573 21.73800 26.08823
## 65  2020-07-02     5210900          81.64     4.16999817 21.49600 25.84623
## 66  2020-07-06     3832000          79.44    -0.70999908 21.98600 26.33623
## 67  2020-07-07    26477900         104.56     0.55999756 22.43400 26.78423
## 68  2020-07-08     7898700          98.30    -5.54999542 21.65200 26.00223
## 69  2020-07-09     4348600          96.30    -2.01999664 22.05200 26.40223
## 70  2020-07-10     3072200          94.36     0.36000061 22.35800 26.70823
## 71  2020-07-13     8636400         104.32     8.98999786 23.09567 27.48184
## 72  2020-07-14     5390800         109.97     2.76000214 22.48900 26.87517
## 73  2020-07-15     5250900         111.15     0.84000397 22.75000 27.26088
## 74  2020-07-16     6940400         120.29    10.88999939 24.17200 28.68288
## 75  2020-07-17    15200800         140.49    11.60000610 23.70800 28.21888
## 76  2020-07-20    12934500         138.23   -11.26000977 23.42200 27.93288
## 77  2020-07-21     8868700         140.69     0.18000793 23.18000 27.69088
## 78  2020-07-22     6143100         146.45    10.44999695 23.67000 28.18088
## 79  2020-07-23     5923200         139.59    -5.10000610 24.11800 28.62888
## 80  2020-07-24     4819300         133.93     0.72999573 23.33600 27.84688
## 81  2020-07-27     5035600         139.60     5.58000183 23.73600 28.24688
## 82  2020-07-28     5907800         148.60     9.17001343 24.04200 28.55288
## 83  2020-07-29     5638600         145.36    -7.13999939 24.77967 29.33442
## 84  2020-07-30     5410600         146.62     6.11999512 24.17300 28.72775
## 85  2020-07-31     4232100         143.10    -4.50999451 24.43400 29.11791
## 86  2020-08-03     6762900         155.87    10.58999634 25.85600 30.53991
## 87  2020-08-04    11517400         157.17     0.66999817 25.39200 30.07591
## 88  2020-08-05    17875600         173.49    -8.50999451 25.10600 29.78991
## 89  2020-08-06     7882700         167.50   -10.50000000 24.86400 29.54791
## 90  2020-08-07     6540200         170.29     1.65998840 25.35400 30.03791
## 91  2020-08-10     7607000         178.51     3.04998779 25.80200 30.48591
## 92  2020-08-11    15226000         149.48   -20.29000854 25.02000 29.70391
## 93  2020-08-12    17941200         124.42   -23.63999939 25.42000 30.10391
## 94  2020-08-13    10776300         133.28     7.11999512 25.72600 30.40991
## 95  2020-08-14    13721100         146.51     2.50999451 26.46367 31.19869
## 96  2020-08-17     5690500         155.53     6.52999878 25.85700 30.59203
## 97  2020-08-18     7441000         146.23    -6.44999695 26.11800 30.98600
## 98  2020-08-19     5328700         147.06     5.47999573 27.54000 32.40800
## 99  2020-08-20     3727400         143.13     0.20001221 27.07600 31.94400
## 100 2020-08-21     5660900         137.62    -4.76000977 26.79000 31.65800
## 101 2020-08-24     8483700         119.26   -12.29999542 26.54800 31.41600
## 102 2020-08-25    13008400         113.11     5.09999847 27.03800 31.90600
## 103 2020-08-26     6988800         114.62     7.62000275 27.48600 32.35400
## 104 2020-08-27     4827000         107.35    -7.95000458 26.70400 31.57200
## 105 2020-08-28     3217200         107.74    -0.36000061 27.10400 31.97200
## 106 2020-08-31     6236200         110.34    -5.16000366 27.41000 32.27800
## 107 2020-09-01     4830700         105.08    -3.13999939 28.14767 33.07338
## 108 2020-09-02     6355200         102.90     3.34000397 27.54100 32.46672
## 109 2020-09-03     7538300         102.98    -9.82999420 27.80200 32.86396
## 110 2020-09-04     7674000          92.93    -8.01000214 29.22400 34.28596
## 111 2020-09-08     5625700          85.31    -1.09000397 28.76000 33.82196
## 112 2020-09-09     3780500          90.84     2.62999725 28.47400 33.53596
## 113 2020-09-10     4656600          92.86     0.02999878 28.23200 33.29396
## 114 2020-09-11     5303400          95.45     1.44999695 28.72200 33.78396
## 115 2020-09-14     7979000         105.80     9.42000580 29.17000 34.23196
## 116 2020-09-15     6779500         110.34     0.33999634 28.38800 33.44996
## 117 2020-09-16    10805500         120.13    12.04000092 28.78800 33.84996
## 118 2020-09-17     5531300         115.51    -3.48999786 29.09400 34.15596
## 119 2020-09-18     5820800         108.36    -6.58000183 29.83167 34.95732
## 120 2020-09-21     3241200         110.15     7.65000153 29.22500 34.35066
## 121 2020-09-22     2407200         111.63     1.87999725 29.48600 34.75068
## 122 2020-09-23     3216800         101.57    -9.37000275 30.90800 36.17268
## 123 2020-09-24     3337200         102.44     4.01000214 30.44400 35.70868
## 124 2020-09-25     8336900         113.56     6.07999420 30.15800 35.42268
## 125 2020-09-28     6169800         111.18    -5.72000122 29.91600 35.18068
## 126 2020-09-29     3212000         107.95     0.15999603 30.40600 35.67068
## 127 2020-09-30     4848700         108.35     1.34999847 30.85400 36.11868
## 128 2020-10-01     3704500         104.90    -4.31999969 30.07200 35.33668
## 129 2020-10-02     2731800         103.60     1.90000153 30.47200 35.73668
## 130 2020-10-05     3210200         110.17     5.04000092 30.77800 36.04268
## 131 2020-10-06     2492000         107.41    -1.78999329 31.51567 36.84948
## 132 2020-10-07     1862600         110.00     1.69999695 30.90900 36.24281
## 133 2020-10-08     2352600         111.02     0.06999969 31.17000 36.64519
## 134 2020-10-09     2421500         111.22    -1.08000183 32.59200 38.06719
## 135 2020-10-12     1788200         110.22    -1.02999878 32.12800 37.60319
## 136 2020-10-13     4425900         117.28     5.86999512 31.84200 37.31719
## 137 2020-10-14     2447700         111.76    -7.47000122 31.60000 37.07519
## 138 2020-10-15     2297400         111.42     1.15000153 32.09000 37.56519
## 139 2020-10-16     2118300         107.04    -4.63999939 32.53800 38.01319
## 140 2020-10-19     2708300         102.05    -5.75000000 31.75600 37.23119
## 141 2020-10-20     4039200          98.36    -4.33999634 32.15600 37.63119
## 142 2020-10-21     3398700          91.16    -5.86999512 32.46200 37.93719
## 143 2020-10-22     2571800          93.59     2.98999786 33.19967 38.74891
## 144 2020-10-23     3107000          93.13     0.29000092 32.59300 38.14225
## 145 2020-10-26     2767700          87.38    -3.65000153 32.85400 38.54663
## 146 2020-10-27     2900800          89.59     2.14999390 34.27600 39.96863
## 147 2020-10-28     3414800          81.33    -6.19999695 33.81200 39.50463
## 148 2020-10-29     2815900          87.63     4.68999481 33.52600 39.21863
## 149 2020-10-30     3007300          80.71    -5.52000427 33.28400 38.97663
## 150 2020-11-02     1855200          82.40     0.13000488 33.77400 39.46663
## 151 2020-11-03     1818000          83.64     1.38999939 34.22200 39.91463
## 152 2020-11-04     2272300          87.27     3.16999817 33.44000 39.13263
## 153 2020-11-05     2233100          93.11     3.91000366 33.84000 39.53263
## 154 2020-11-06     1893100          89.86    -2.54000092 34.14600 39.83863
## 155 2020-11-09     6586500          90.31    -4.22000122 34.88367 40.65482
## 156 2020-11-10     6854400          78.74    -6.56000519 34.27700 40.04816
## 157 2020-11-11     3986000          85.01     4.91000366 34.53800 40.45424
## 158 2020-11-12     4953400          90.87     5.10000610 35.96000 41.87624
## 159 2020-11-13     3731700          96.60     3.40999603 35.49600 41.41224
## 160 2020-11-16     6852900          90.65    -5.04999542 35.21000 41.12624
## 161 2020-11-17     2920100          92.89     2.94000244 34.96800 40.88424
## 162 2020-11-18     2525200          89.91    -2.38999939 35.45800 41.37424
## 163 2020-11-19     3249900          86.40    -2.59999847 35.90600 41.82224
## 164 2020-11-20     2557300          86.55    -0.05999756 35.12400 41.04024
## 165 2020-11-23     6548300          93.98     4.99000549 35.52400 41.44024
## 166 2020-11-24     3099200          95.33     0.06999969 35.83000 41.74624
## 167 2020-11-25     3794700         102.60     7.27999878 36.56767 42.56648
## 168 2020-11-27    11777400         125.69    17.59000397 35.96100 41.95982
## 169 2020-11-30    16386900         139.50    20.23999786 36.22200 42.36734
## 170 2020-12-01    12364600         123.47   -26.52999878 37.64400 43.78934
## 171 2020-12-02     3969300         125.48     4.47000122 37.18000 43.32534
## 172 2020-12-03     3234300         129.16     4.18000031 36.89400 43.03934
## 173 2020-12-04     2181800         126.25    -3.11999512 36.65200 42.79734
## 174 2020-12-07     3270700         123.12    -3.77999878 37.14200 43.28734
## 175 2020-12-08     3744200         120.12    -4.47999573 37.59000 43.73534
## 176 2020-12-09     5561800         115.22    -8.40000153 36.80800 42.95334
## 177 2020-12-10     2473300         115.09     4.75999451 37.20800 43.35334
## 178 2020-12-11     6060800         124.88     6.12999725 37.51400 43.65934
## 179 2020-12-14     4690300         129.70    -0.41000366 38.25167 44.48327
## 180 2020-12-15     2727200         126.22    -4.61000061 37.64500 43.87660
## 181 2020-12-16     4096600         120.88    -0.84000397 37.90600 44.28534
## 182 2020-12-17     5070800         131.75     7.26999664 39.32800 45.70734
## 183 2020-12-18     4237400         124.85    -6.12000275 38.86400 45.24334
##       lower95 price_change
## 1    8.861959    1.3839998
## 2   10.283959   -0.2080004
## 3    9.819959    2.0059996
## 4    9.533959    2.7820004
## 5    9.291959    1.8239996
## 6    9.781959    3.6140003
## 7   10.229959    3.0359993
## 8    9.447959    4.4079993
## 9    9.847959    4.3680001
## 10  10.153959    3.5920007
## 11  10.905482    3.4243338
## 12  10.298815    5.0109999
## 13  10.469285    9.4699993
## 14  11.891285    5.7780006
## 15  11.427285    6.0820008
## 16  11.141285    5.3779992
## 17  10.899285    5.0699999
## 18  11.389285    5.7600003
## 19  11.837285    4.8519993
## 20  11.055285    3.4939997
## 21  11.455285    2.8139992
## 22  11.761285    1.7080000
## 23  12.501664    1.7003329
## 24  11.894997    2.0270007
## 25  12.056886    1.1259994
## 26  13.478886    0.7139996
## 27  13.014886    1.7579995
## 28  12.728886    7.8140000
## 29  12.486886   23.3759997
## 30  12.976886   23.7359982
## 31  13.424886   20.9179993
## 32  12.642886   27.0300010
## 33  13.042886   39.9599992
## 34  13.348886   36.5940016
## 35  14.078625   34.7663348
## 36  13.471959   32.6329997
## 37  13.626163   28.4120007
## 38  15.048163   29.0499981
## 39  14.584163   26.8140012
## 40  14.298163   26.4700002
## 41  14.056163   27.9120009
## 42  14.546163   34.2019997
## 43  14.994163   30.5739995
## 44  14.212163   25.9659999
## 45  14.612163   25.9759999
## 46  14.918163   27.3099993
## 47  15.637875   25.0623343
## 48  15.031209   26.8090003
## 49  15.178617   25.5779991
## 50  16.600617   23.6260002
## 51  16.136617   25.2299997
## 52  15.850617   31.0159997
## 53  15.608617   32.3380015
## 54  16.098617   35.1079999
## 55  16.546617   38.5200005
## 56  15.764617   44.7819999
## 57  16.164617   45.5819970
## 58  16.470617   48.9860037
## 59  17.180980   56.0883334
## 60  16.574314   62.7350009
## 61  16.715772   56.3239995
## 62  18.137772   61.1220005
## 63  17.673772   61.3259984
## 64  17.387772   57.9619970
## 65  17.145772   60.1439994
## 66  17.635772   57.4540025
## 67  18.083772   82.1259976
## 68  17.301773   76.6480030
## 69  17.701772   74.2480031
## 70  18.007772   72.0020007
## 71  18.709496   81.2243331
## 72  18.102829   87.4810012
## 73  18.239124   88.4000016
## 74  19.661124   96.1180009
## 75  19.197124  116.7820055
## 76  18.911124  114.8079958
## 77  18.669124  117.5100024
## 78  19.159124  122.7799970
## 79  19.607124  115.4719964
## 80  18.825124  110.5939926
## 81  19.225124  115.8640062
## 82  19.531124  124.5580062
## 83  20.224916  120.5803340
## 84  19.618250  122.4469951
## 85  19.750091  118.6660062
## 86  21.172092  130.0139951
## 87  20.708091  131.7779981
## 88  20.422091  148.3840055
## 89  20.180091  142.6360000
## 90  20.670091  144.9359933
## 91  21.118091  152.7079946
## 92  20.336092  124.4599956
## 93  20.736091   98.9999983
## 94  21.042091  107.5539988
## 95  21.728639  120.0463279
## 96  21.121973  129.6729988
## 97  21.249995  120.1119958
## 98  22.671995  119.5199975
## 99  22.207995  116.0540049
## 100 21.921995  110.8299952
## 101 21.679995   92.7120021
## 102 22.169995   86.0720007
## 103 22.617995   87.1340028
## 104 21.835996   80.6459984
## 105 22.235995   80.6359980
## 106 22.541995   82.9299964
## 107 23.221949   76.9323353
## 108 22.615283   75.3590015
## 109 22.740043   75.1780034
## 110 24.162043   63.7060003
## 111 23.698043   56.5499975
## 112 23.412043   62.3659964
## 113 23.170043   64.6280006
## 114 23.660043   66.7279970
## 115 24.108043   76.6300031
## 116 23.326043   81.9519963
## 117 23.726043   91.3419974
## 118 24.032043   86.4160022
## 119 24.706009   78.5283340
## 120 24.099342   80.9250015
## 121 24.221323   82.1439973
## 122 25.643323   70.6619996
## 123 25.179323   71.9960024
## 124 24.893323   83.4019976
## 125 24.651323   81.2640003
## 126 25.141323   77.5439970
## 127 25.589323   77.4959986
## 128 24.807323   74.8280014
## 129 25.207323   73.1279986
## 130 25.513323   79.3919982
## 131 26.181857   75.8943371
## 132 25.575191   79.0910000
## 133 25.694809   79.8499967
## 134 27.116809   78.6280012
## 135 26.652809   78.0920012
## 136 26.366809   85.4379988
## 137 26.124809   80.1600021
## 138 26.614809   79.3299982
## 139 27.062809   74.5020010
## 140 26.280809   70.2940030
## 141 26.680809   66.2040007
## 142 26.986809   58.6980037
## 143 27.650419   60.3903298
## 144 27.043752   60.5369973
## 145 27.161366   54.5259973
## 146 28.583366   55.3139963
## 147 28.119366   47.5180018
## 148 27.833366   54.1039973
## 149 27.591366   47.4259991
## 150 28.081366   48.6260016
## 151 28.529366   49.4179995
## 152 27.747366   53.8299966
## 153 28.147366   59.2700007
## 154 28.453366   55.7140007
## 155 29.112510   55.4263310
## 156 28.505843   44.4629979
## 157 28.621758   50.4720022
## 158 30.043758   54.9100027
## 159 29.579758   61.1039985
## 160 29.293758   55.4400016
## 161 29.051758   57.9219994
## 162 29.541758   54.4520037
## 163 29.989758   50.4940016
## 164 29.207758   51.4260030
## 165 29.607758   58.4560035
## 166 29.913758   59.5000019
## 167 30.568848   66.0323319
## 168 29.962182   89.7290024
## 169 30.076658  103.2780001
## 170 31.498658   85.8260012
## 171 31.034658   88.3000034
## 172 30.748658   92.2660037
## 173 30.506658   89.5980000
## 174 30.996658   85.9780028
## 175 31.444658   82.5300028
## 176 30.662658   78.4120011
## 177 31.062658   77.8819965
## 178 31.368658   87.3659973
## 179 32.020064   91.4483304
## 180 31.413398   88.5750012
## 181 31.526658   82.9739973
## 182 32.948658   92.4220000
## 183 32.484658   85.9859985
# the estimated total stock value change between April and August 2020 as result of the pandemic
sum(post_covid_nova$price_change)
## [1] 11349.04
# Data viz
plot_ly() %>%
   add_ribbons(x = post_covid_nova$date,
              ymin = post_covid_nova$adj.close_nova,
              ymax = post_covid_nova$yhat,
              line = list(color = 'rgba(255, 0, 0, 0.05)'),
              fillcolor = 'rgba(255, 0, 0, 0.6)',
              name = "Estimated Change") %>%
  add_segments(x = as.Date("2020-04-01"), 
               xend = as.Date("2020-04-01"),
               y = min(nova$adj.close_nova),
               yend = max(nova$adj.close_nova) * 1.05,
               line = list(color = "black", dash = "dash"),
               showlegend = FALSE) %>%
  add_annotations(text = "Pre-Covid19",
                  x = as.Date("2020-01-01"),
                  y = max(nova$adj.close_nova) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = "Post-Covid19",
                  x = as.Date("2020-11-01"),
                  y = max(nova$adj.close_nova) * 1.05, 
                  showarrow = FALSE) %>%
  add_annotations(text = paste("Estimated increase in", "<br>",
                               "stock price: ~$11,400",
                               sep = ""),
                  x = as.Date("2020-08-01"),
                  y = 3 * 10^1,
                  arrowhead = 1,
                  ax = 120,
                  ay = 40,
                  showarrow = TRUE) %>%
  add_lines(x = nova$date,
            y = nova$adj.close_nova,
            line = list(color = "#1f77b4"),
            name = "Actual") %>%
  layout(title = "Covid19 Impact on Stock Value of NOVAVAX",
         yaxis = list(title = "Stock Price (US Dollars)"),
         xaxis = list(title = "Time Series Model - Auto ARIMA Model",
                      range = c(as.Date("2019-10-01"), as.Date("2021-01-01"))),
         legend = list(x = 0, y = 0.95))