filter Select Apple stock prices and save it under plotdata.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
Hint: In your interpretation, make sure to use all variables.
Interpreting Row 2 shows Apples stock data for 1/5/2021. It shows apple opened at $128.89, closed at $131.01, high of the day was $131.74, and low of day was $128.43. Also, it shows the volume traded on the 5th was 97664900 and the adjusted price was $130.8145. Lastly, it states the daily return was 0.0123636673 or 1.23%.
filter Select Apple stock prices and save it under plotdata.Hint: See the code in 4.2.2 Line plot.
plotdata <- filter(stock_returns, symbol == "AAPL")
plotdata
## # A tibble: 43 x 9
## # Groups: symbol [1]
## 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 33 more rows
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()+
labs(y = "Opening Price",
x = "Date")
Hint: Interpret the line plot you created in Q4.
Apples stock performance this year has been asymmetric. It technically was trading horizontally with a bullish run in mid January and then continued to hold at $135 area. On around Feb 16th Apple began to show a bearish pattern lowering too the $120 area.
Hint: See the code in 4.3.1 Bar chart (on summary statistics).
plotdata <- stock_returns %>%
group_by(symbol) %>%
summarise(mean_DR = mean(daily.returns))
plotdata
## # A tibble: 3 x 2
## symbol mean_DR
## * <chr> <dbl>
## 1 AAPL -0.00124
## 2 AMZN -0.00126
## 3 MSFT 0.00162
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_DR)) +
geom_bar(stat = "identity")
I would personally believe Microsoft to show the highest daily return. As seen on the graph it has the highest mean DR, as well as the only positive daily return. Although the sector APPL, MSFT, and AMZN are inhave began showinga bearish pattern as a whole.
Hint: Refer to the RMarkdown Reference Guide.