filter Select stock returns of January 31, 2020.filter Create the same line plot as in Q7, but without Amazon.# Load packages
library(tidyquant)
library(tidyverse)
# Import stock prices
stock_prices <- tq_get(c("WMT", "TGT", "AMZN"), get = "stock.prices", from = "2020-01-31")
# Calculate daily returns
stock_returns <-
stock_prices %>%
group_by(symbol) %>%
tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily")
stock_returns
## # A tibble: 66 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
## 2 WMT 2020-02-03 115. 115. 114. 114. 4351900 114. -0.00192
## 3 WMT 2020-02-04 115. 116. 115. 115. 5232200 115. 0.00875
## 4 WMT 2020-02-05 116. 117. 115. 117. 5682900 117. 0.0134
## 5 WMT 2020-02-06 117. 117. 116. 116. 5352900 116. -0.00428
## 6 WMT 2020-02-07 116. 117. 116. 116. 3689600 116. 0.00120
## 7 WMT 2020-02-10 116. 117. 115. 115. 6144900 115. -0.0103
## 8 WMT 2020-02-11 115. 116. 114. 115. 5796800 115. 0.00130
## 9 WMT 2020-02-12 116. 116. 115. 116. 4735600 116. 0.00390
## 10 WMT 2020-02-13 116. 118. 116. 117. 5333200 117. 0.0137
## # … with 56 more rows
filter Select stock returns of January 31, 2020.Hint: See the code in 1.2.2 Selecting observations.
Stock_Jan <- filter(stock_returns, date == "2020-01-31")
Stock_Jan
## # 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
## 2 TGT 2020-01-31 113. 114. 110. 111. 6961900 110. 0
## 3 AMZN 2020-01-31 2051. 2056. 2002. 2009. 15567300 2009. 0
Hint: Answer the question by comparing daily returns of each stock on January 31, 2020. Amazon performed the best on January 31, 2020
Hint: See the code in 4.3.3 Box plots. Add an appropriate title and labels for both axes.
ggplot(stock_returns,
aes(x = symbol,
y = daily.returns)) +
geom_boxplot() +
labs(title = "Distribution of Daily Returns", x = "Symbol", y = "Daily Returns")
Hint: Answer the question by comparing median and outliers of each stock. Amazon performed the best of the three
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))
avgreturns
## # A tibble: 3 x 2
## symbol mean_returns
## <chr> <dbl>
## 1 AMZN -0.00211
## 2 TGT -0.00158
## 3 WMT -0.000415
Hint: See the code in 4.3.1 Bar chart (on summary statistics). Add an appropriate title and labels for both axes.
ggplot(avgreturns,
aes(x = symbol,
y = mean_returns)) +
geom_bar(stat = "identity") +
labs(title = "Mean Daily Returns", x = "Symbol", y = "Mean Returns")
Hint: Google search something like “ggplot2 multiple lines”.
ggplot(stock_prices, aes(x = date, y = adjusted, group = symbol)) +
geom_line(aes(color = symbol)) +
scale_color_manual(values = c("goldenrod", "darkred", "steelblue")) +
labs(y = "Adjusted Stock Price", x = "Date", title = "Daily Stock Prices")
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.
stock_prices2 <- tq_get(c("WMT", "TGT"), get = "stock.prices", from = "2020-01-01")
ggplot(stock_prices2, aes(x = date, y = adjusted, group = symbol)) +
geom_line(aes(color = symbol)) +
scale_color_manual(values = c("darkred", "steelblue")) +
labs(y = "Adjusted Stock Price", x = "Date", title = "Daily Stock Prices for Target vs. Walmart")
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.