# Load packages
library(tidyquant)
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
## 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 ──
## ✓ ggplot2 3.2.1     ✓ purrr   0.3.3
## ✓ tibble  2.1.3     ✓ dplyr   0.8.4
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.4.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("WMT", "TGT", "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: 126 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 WMT    2020-01-02  119.  120.  119.  119. 6764900     119.       0      
##  2 WMT    2020-01-03  118.  119.  118.  118. 5399200     118.      -0.00883
##  3 WMT    2020-01-06  117.  118.  117.  118. 6445500     118.      -0.00204
##  4 WMT    2020-01-07  117.  118.  116.  117. 6846900     117.      -0.00926
##  5 WMT    2020-01-08  116.  117.  116.  116. 5875800     116.      -0.00343
##  6 WMT    2020-01-09  116.  117.  116.  117. 5563700     117.       0.0103 
##  7 WMT    2020-01-10  117.  117.  116.  116. 6054800     116.      -0.00835
##  8 WMT    2020-01-13  116.  117.  115.  116. 6112600     116.      -0.00430
##  9 WMT    2020-01-14  115.  116.  115.  116. 6585800     116.       0.00259
## 10 WMT    2020-01-15  115.  116.  115.  115. 7454200     115.      -0.00775
## # … with 116 more rows

Q1 filter Select stock returns of January 31, 2020.

Hint: See the code in 1.2.2 Selecting observations.

# Load packages
library(tidyquant)
library(tidyverse)

# Import stock prices
stock_prices <- tq_get(c("WMT", "TGT", "AMZN"), get  = "stock.prices")

# Calculate daily returns
stock_returns <-
  stock_prices  %>%
    group_by(symbol) %>%
    tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily") 

stock_returns.2020.01.31 <- filter(stock_returns, 
                  date == "2020-01-31")
stock_returns.2020.01.31
## # A tibble: 3 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 WMT    2020-01-31  116.  116.  114.  114.  7775800     114.       -0.0179
## 2 TGT    2020-01-31  113.  114.  110.  111.  6961900     110.       -0.0343
## 3 AMZN   2020-01-31 2051. 2056. 2002. 2009. 15567300    2009.        0.0738

Q2 Which of the three stocks performed best on January 31, 2020?

Hint: Answer the question by comparing daily returns of each stock on January 31, 2020.

Amazon did the best with a .0738 increase.

Q3 Plot the distribution of daily returns by stock using boxplots.

Hint: See the code in 4.3.3 Box plots. Add an appropriate title and labels for both axes.

ggplot(data = stock_returns, mapping = aes(x = symbol, y = daily.returns)) +
  geom_boxplot()

Q4 Based on the boxplot above, which of the three stocks performed best this year?

Hint: Answer the question by comparing median and outliers of each stock.

Amazon performed the best.

Q5 Calculate mean daily returns for each stock.

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

# Load packages
library(tidyquant)
library(tidyverse)

# Import stock prices
stock_prices <- tq_get(c("WMT", "TGT", "AMZN"), get  = "stock.prices")

# Calculate mean daily returns
mean_stock_returns <-
  stock_prices  %>%
    group_by(symbol) %>%
    tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily") 

Q6 Plot mean daily returns using bar charts.

Hint: See the code in 4.3.1 Bar chart (on summary statistics). Add an appropriate title and labels for both axes.

ggplot(data = mean_stock_returns) + 
  geom_bar(mapping = aes(x = daily.returns, fill = symbol), position = "dodge")

Q7 Create the line plot of stock prices for all three stocks in one graph.

Hint: Google search something like “ggplot2 multiple lines”.

# Load packages
library(tidyquant)
library(tidyverse)

# Import stock prices
stock_prices <- tq_get(c("WMT", "TGT", "AMZN"), get  = "stock.prices")
stock_prices
## # A tibble: 7,674 x 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 WMT    2010-01-04  53.7  54.7  53.7  54.2 20753100     42.3
##  2 WMT    2010-01-05  54.1  54.2  53.6  53.7 15648400     41.9
##  3 WMT    2010-01-06  53.5  53.8  53.4  53.6 12517200     41.8
##  4 WMT    2010-01-07  53.7  53.8  53.3  53.6 10662700     41.8
##  5 WMT    2010-01-08  53.4  53.5  53.0  53.3 11363200     41.6
##  6 WMT    2010-01-11  53.3  54.4  53.1  54.2 13987700     42.3
##  7 WMT    2010-01-12  54    54.8  53.9  54.7 15117000     42.7
##  8 WMT    2010-01-13  54.8  55.2  54.4  55.0 13290700     42.9
##  9 WMT    2010-01-14  54.7  54.8  54.2  54.2 13772000     42.3
## 10 WMT    2010-01-15  54.3  54.5  53.6  53.7 19087500     41.9
## # … with 7,664 more rows

Q7.a filter Create the same line plot as in Q7, but without Amazon.

Note: Insert a new code chunk below, copy and paste the code in Q7, and revise it using the dplyr::filter function. This is an extra credit question worth 10 points. However, the total number of points you could earn for this quiz is capped at 100 points. In other words, the extra credit can only offset any one question you missed in the first seven questions.

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

Hint: Use message, echo and results in the chunk options. 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.