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-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
filter Select stock returns of January 31, 2020.filtered_stock <- filter(stock_returns,
date == "2020-01-31")
filtered_stock
## # 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
Amazon stock perfomed the best because it was the only positive return.
ggplot(stock_returns,
aes(x = symbol,
y = daily.returns)) +
geom_boxplot() +
labs(title = "Daily Returns By Stock",
y="Daily Returns",
x="Stock Tickers")
Amazon’s median is the closest out of the three stocks to zero, the other two fall further below. Along with this, it’s outliers are closer to it’s main set of data than the other two.
mean_returndata <- stock_returns %>%
group_by(symbol) %>%
summarize(mean_return = mean(daily.returns))
mean_returndata
## # A tibble: 3 x 2
## symbol mean_return
## <chr> <dbl>
## 1 AMZN 0.000326
## 2 TGT -0.00382
## 3 WMT -0.00111
ggplot(mean_returndata,
aes(x = symbol,
y = mean_return)) +
geom_bar(stat = "identity") +
labs(title = "Mean Daily Returns of Stocks",
x = "Stock Tickers",
y = "Mean Daily Return")
library(ggplot2)
ggplot(stock_returns,
aes(x = date, y = daily.returns,
group = symbol, color = symbol)) +
geom_line()
filter Create the same line plot as in Q7, but without Amazon.newstocks <- filter(stock_returns,
symbol != "AMZN")
newstocks
## # A tibble: 84 x 9
## # Groups: symbol [2]
## 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 74 more rows
library(ggplot2)
ggplot(newstocks,
aes(x = date, y = daily.returns,
group = symbol, color = symbol)) +
geom_line()
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.