filter Select Microsoft 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 = "2020-01-01")
stock_prices
## # A tibble: 552 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2020-01-02 74.1 75.2 73.8 75.1 135480400 74.6
## 2 AAPL 2020-01-03 74.3 75.1 74.1 74.4 146322800 73.8
## 3 AAPL 2020-01-06 73.4 75.0 73.2 74.9 118387200 74.4
## 4 AAPL 2020-01-07 75.0 75.2 74.4 74.6 108872000 74.1
## 5 AAPL 2020-01-08 74.3 76.1 74.3 75.8 132079200 75.3
## 6 AAPL 2020-01-09 76.8 77.6 76.6 77.4 170108400 76.9
## 7 AAPL 2020-01-10 77.7 78.2 77.1 77.6 140644800 77.1
## 8 AAPL 2020-01-13 77.9 79.3 77.8 79.2 121532000 78.7
## 9 AAPL 2020-01-14 79.2 79.4 78.0 78.2 161954400 77.6
## 10 AAPL 2020-01-15 78.0 78.9 77.4 77.8 121923600 77.3
## # ... with 542 more rows
# Calculate daily returns
stock_returns <-
stock_prices %>%
group_by(symbol) %>%
tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily")
stock_returns
## # A tibble: 552 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 2020-01-02 74.1 75.2 73.8 75.1 135480400 74.6 0
## 2 AAPL 2020-01-03 74.3 75.1 74.1 74.4 146322800 73.8 -0.00972
## 3 AAPL 2020-01-06 73.4 75.0 73.2 74.9 118387200 74.4 0.00797
## 4 AAPL 2020-01-07 75.0 75.2 74.4 74.6 108872000 74.1 -0.00470
## 5 AAPL 2020-01-08 74.3 76.1 74.3 75.8 132079200 75.3 0.0161
## 6 AAPL 2020-01-09 76.8 77.6 76.6 77.4 170108400 76.9 0.0212
## 7 AAPL 2020-01-10 77.7 78.2 77.1 77.6 140644800 77.1 0.00226
## 8 AAPL 2020-01-13 77.9 79.3 77.8 79.2 121532000 78.7 0.0214
## 9 AAPL 2020-01-14 79.2 79.4 78.0 78.2 161954400 77.6 -0.0135
## 10 AAPL 2020-01-15 78.0 78.9 77.4 77.8 121923600 77.3 -0.00429
## # ... with 542 more rows
Hint: In your interpretation, make sure to use all variables. Apples stock returns on January 3, 2020 had an open return of $74.26 and a high of $75.14 with a low of $74.12. They also had a close of $74.35
filter Select Microsoft stock prices and save it under plotdata.Hint: See the code in 4.2.2 Line plot.
library(dplyr)
plotdata <- filter(stock_prices,
symbol == "AAPL")
plotdata
## # A tibble: 184 x 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2020-01-02 74.1 75.2 73.8 75.1 135480400 74.6
## 2 AAPL 2020-01-03 74.3 75.1 74.1 74.4 146322800 73.8
## 3 AAPL 2020-01-06 73.4 75.0 73.2 74.9 118387200 74.4
## 4 AAPL 2020-01-07 75.0 75.2 74.4 74.6 108872000 74.1
## 5 AAPL 2020-01-08 74.3 76.1 74.3 75.8 132079200 75.3
## 6 AAPL 2020-01-09 76.8 77.6 76.6 77.4 170108400 76.9
## 7 AAPL 2020-01-10 77.7 78.2 77.1 77.6 140644800 77.1
## 8 AAPL 2020-01-13 77.9 79.3 77.8 79.2 121532000 78.7
## 9 AAPL 2020-01-14 79.2 79.4 78.0 78.2 161954400 77.6
## 10 AAPL 2020-01-15 78.0 78.9 77.4 77.8 121923600 77.3
## # ... with 174 more rows
Hint: See the code in 4.2.2 Line plot. Use plotdata you created in Q3.
ggplot(plotdata,
aes(x = date,
y = close)) +
geom_line()
Hint: Interpret the line plot you created in Q4. Microsoft stock dropped very steep around March when Covid happened. However, it has risin significantly around July to September.
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))
## `summarise()` ungrouping output (override with `.groups` argument)
avgreturns
## # A tibble: 3 x 2
## symbol mean_returns
## <chr> <dbl>
## 1 AAPL 0.00248
## 2 AMZN 0.00281
## 3 MSFT 0.00173
Hint: See the code in 4.3.1 Bar chart (on summary statistics). Use plotdata you created in Q5.
ggplot(avgreturns,
aes(x = symbol,
y = mean_returns)) +
geom_bar(stat = "identity") +
labs(title = "Mean Daily Returns", x = "Symbol", y = "Mean Returns")
Hint: Refer to the RMarkdown Reference Guide.