In this exercise, use Chapter 4.2 Quantitative vs. Quantitative Data Visualization with R.

# Load packages
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: quantmod
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
## == Need to Learn tidyquant? =============================================
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(tidyverse)
## -- Attaching packages -------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ----------------------------------- tidyverse_conflicts() --
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date()        masks base::date()
## x dplyr::filter()          masks stats::filter()
## x dplyr::first()           masks xts::first()
## x lubridate::intersect()   masks base::intersect()
## x dplyr::lag()             masks stats::lag()
## x dplyr::last()            masks xts::last()
## x lubridate::setdiff()     masks base::setdiff()
## x lubridate::union()       masks base::union()
# Import stock prices
stock_prices <- tq_get(c("AAPL", "MSFT", "AMZN"), get  = "stock.prices", from = "2020-01-01")

# Calculate daily returns
stock_returns <-
  stock_prices  %>%
    group_by(symbol) %>%
    tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily") 
stock_returns
## # A tibble: 564 x 9
## # Groups:   symbol [3]
##    symbol date        open  high   low close    volume adjusted daily.returns
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>         <dbl>
##  1 AAPL   2020-01-02  74.1  75.2  73.8  75.1 135480400     74.6       0      
##  2 AAPL   2020-01-03  74.3  75.1  74.1  74.4 146322800     73.8      -0.00972
##  3 AAPL   2020-01-06  73.4  75.0  73.2  74.9 118387200     74.4       0.00797
##  4 AAPL   2020-01-07  75.0  75.2  74.4  74.6 108872000     74.1      -0.00470
##  5 AAPL   2020-01-08  74.3  76.1  74.3  75.8 132079200     75.3       0.0161 
##  6 AAPL   2020-01-09  76.8  77.6  76.6  77.4 170108400     76.9       0.0212 
##  7 AAPL   2020-01-10  77.7  78.2  77.1  77.6 140644800     77.1       0.00226
##  8 AAPL   2020-01-13  77.9  79.3  77.8  79.2 121532000     78.7       0.0214 
##  9 AAPL   2020-01-14  79.2  79.4  78.0  78.2 161954400     77.6      -0.0135 
## 10 AAPL   2020-01-15  78.0  78.9  77.4  77.8 121923600     77.3      -0.00429
## # ... with 554 more rows

Q1 Interpret Row 2 of stock_returns.

Hint: In your interpretation, make sure to use all variables.

Row 2 of stock_returns describes Apple stock returns on march 1st of 2020.

Q2 How much was Microsoft per share at closing on July 30, 2020?

Hint: Examine the data in the spreadsheet view.

A share of Microsoft on July 30th of 2020 was $203.41.

Q3 filter Select Microsoft stock prices and save it under plotdata.

Hint: See the code in 4.2.2 Line plot.

library(dplyr)
plotdata <- filter(stock_prices,
                  symbol == "MSFT")
plotdata
## # A tibble: 188 x 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 MSFT   2020-01-02  159.  161.  158.  161. 22622100     159.
##  2 MSFT   2020-01-03  158.  160.  158.  159. 21116200     157.
##  3 MSFT   2020-01-06  157.  159.  157.  159. 20813700     158.
##  4 MSFT   2020-01-07  159.  160.  157.  158. 21634100     156.
##  5 MSFT   2020-01-08  159.  161.  158.  160. 27746500     159.
##  6 MSFT   2020-01-09  162.  162.  161.  162. 21385000     161.
##  7 MSFT   2020-01-10  163.  163.  161.  161. 20725900     160.
##  8 MSFT   2020-01-13  162.  163.  161.  163. 21626500     162.
##  9 MSFT   2020-01-14  163.  164.  162.  162. 23477400     161.
## 10 MSFT   2020-01-15  163.  164.  163.  163. 21417900     162.
## # ... with 178 more rows

Q4 Create a simple line plot with date on the x-axis and closing price on the y-axis.

Hint: See the code in 4.2.2 Line plot. Use plotdata you created in Q3.

ggplot(plotdata, aes(x = date, y = close)) + geom_line()

Q5 Describe the performance of Microsoft stock this year.

Hint: Interpret the line plot you created in Q4.

Microsoft stock did relatively well, Fluctuated a little bit but still had an overall increase in shareholder equity.

Q6 Calculate mean daily returns for each stock and save it under plotdata.

Hint: See the code in 4.3.1 Bar chart (on summary statistics).

avgreturns <- stock_returns %>% 
  group_by(symbol) %>%
  summarize(mean_returns = mean(daily.returns))
## `summarise()` ungrouping output (override with `.groups` argument)
avgreturns
## # A tibble: 3 x 2
##   symbol mean_returns
##   <chr>         <dbl>
## 1 AAPL        0.00276
## 2 AMZN        0.00301
## 3 MSFT        0.00187

Q7 Which of the stocks would you expect the highest daily return? Plot mean daily returns using bar chart.

Hint: See the code in 4.3.1 Bar chart (on summary statistics). Use plotdata you created in Q5.

ggplot(avgreturns,
       aes(x = symbol,
           y = mean_returns)) +
geom_bar(stat = "identity") +
 labs(title = "Mean Daily Returns", x = "symbol", y = "Mean Returns")

Q8 Hide the messages and warnings, but display the code and its results on the webpage.

Hint: Refer to the RMarkdown Reference Guide.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slug.