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

# Load packages
library(tidyquant)
library(tidyverse)

# Import stock prices
stock_prices <- tq_get(c("AAPL", "MSFT", "AMZN"), get  = "stock.prices", from = "2021-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: 129 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   2021-01-04  134.  134.  127.  129. 143301900     129.       0      
##  2 AAPL   2021-01-05  129.  132.  128.  131.  97664900     131.       0.0124 
##  3 AAPL   2021-01-06  128.  131.  126.  127. 155088000     126.      -0.0337 
##  4 AAPL   2021-01-07  128.  132.  128.  131. 109578200     131.       0.0341 
##  5 AAPL   2021-01-08  132.  133.  130.  132. 105158200     132.       0.00863
##  6 AAPL   2021-01-11  129.  130.  128.  129. 100620900     129.      -0.0232 
##  7 AAPL   2021-01-12  128.  130.  127.  129.  91951100     129.      -0.00140
##  8 AAPL   2021-01-13  129.  131.  128.  131.  88636800     131.       0.0162 
##  9 AAPL   2021-01-14  131.  131   129.  129.  90221800     129.      -0.0151 
## 10 AAPL   2021-01-15  129.  130.  127   127. 111598500     127.      -0.0137 
## # … with 119 more rows

Q1 Interpret Row 2 of stock_returns.

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

Row 2 of stock_returns shows stock data for Apple. The symbol is AAPL and on January 5, 2021, its opening price was $129 per share. The highest price during the day was $132 per share and the lowest was $128. At market closing, the price was $131. The number of shares traded during the day (volume) was 97 664 900 and the adjusted price per share was $131.

Q2 How much was the highest price per share, at which Apple was traded on January 28, 2021?

Hint: Examine the data in the spreadsheet view.

The highest price per share Apple traded at on January 28 was 141.99.

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

Hint: See the code in 4.2.2 Line plot.

plotdata <- filter(stock_prices, symbol == "AAPL")
plotdata
## # A tibble: 43 x 8
##    symbol date        open  high   low close    volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
##  1 AAPL   2021-01-04  134.  134.  127.  129. 143301900     129.
##  2 AAPL   2021-01-05  129.  132.  128.  131.  97664900     131.
##  3 AAPL   2021-01-06  128.  131.  126.  127. 155088000     126.
##  4 AAPL   2021-01-07  128.  132.  128.  131. 109578200     131.
##  5 AAPL   2021-01-08  132.  133.  130.  132. 105158200     132.
##  6 AAPL   2021-01-11  129.  130.  128.  129. 100620900     129.
##  7 AAPL   2021-01-12  128.  130.  127.  129.  91951100     129.
##  8 AAPL   2021-01-13  129.  131.  128.  131.  88636800     131.
##  9 AAPL   2021-01-14  131.  131   129.  129.  90221800     129.
## 10 AAPL   2021-01-15  129.  130.  127   127. 111598500     127.
## # … with 33 more rows

Q4 Create a simple line plot with date on the x-axis and opening 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 = open)) +
  geom_line() 

Q5 Describe the performance of Apple stock this year.

Hint: Interpret the line plot you created in Q4.

The stock price of Apple was pretty volatile so far this year. Between January 1 and 15, it had a few spikes and then would fall again, staying around $130 per share. At the end of January the stock price spiked really high to about $145, but then fell about halfway back down to where it was, until the end of February when it dropped back to under $125. At the beginning of March there was a small spike, and it is currently hovering around $125 per share.

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).

plotdata <- summarize(stock_returns, mean_return = mean(daily.returns, na.rm=TRUE))
plotdata
## # A tibble: 3 x 2
##   symbol mean_return
## * <chr>        <dbl>
## 1 AAPL      -0.00124
## 2 AMZN      -0.00126
## 3 MSFT       0.00162

Q7 If the stock’s performance this year is any indication, 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(plotdata, 
       aes(x = symbol, 
           y = mean_return)) +
 geom_bar(stat = "identity", fill = 'cornflowerblue')

I would expect the highest daily return from Microsoft (MSFT)

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.