Hint: Insert the code below (only importing code).
library(tidyquant)
# Import data
from = today() - years(5)
Stocks <- tq_get("FB", get = "stock.prices", from = from)
Stocks
## # A tibble: 1,258 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2014-02-26 70.2 71.2 68.8 69.3 55322700 69.3
## 2 2014-02-27 69.3 70.0 68.9 68.9 41653700 68.9
## 3 2014-02-28 69.5 69.9 67.4 68.5 66783700 68.5
## 4 2014-03-03 67.0 68.1 66.5 67.4 56824100 67.4
## 5 2014-03-04 68.7 68.9 67.6 68.8 42013500 68.8
## 6 2014-03-05 69.7 72.0 69.6 71.6 74567700 71.6
## 7 2014-03-06 71.9 71.9 70.2 70.8 46026500 70.8
## 8 2014-03-07 71.1 71.2 69.5 69.8 38927000 69.8
## 9 2014-03-10 70.8 72.2 70.5 72.0 59871600 72.0
## 10 2014-03-11 72.5 72.6 70.0 70.1 59408300 70.1
## # ... with 1,248 more rows
Hint: Take the adjusted variable from Stocks, and calculate daily returns using tidyquant::tq_mutate. Note that there are a variety of functions available with the mutate_fun argument: See above for the result of tq_transmute_fun_options(). Note that tq_mutate allows to calculate returns using quantmod::periodReturn. Google the quantmod package manual for more information on the periodReturn function.
Stocks %>%
tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily")
## # A tibble: 1,258 x 8
## date open high low close volume adjusted daily.returns
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2014-02-26 70.2 71.2 68.8 69.3 55322700 69.3 0
## 2 2014-02-27 69.3 70.0 68.9 68.9 41653700 68.9 -0.00462
## 3 2014-02-28 69.5 69.9 67.4 68.5 66783700 68.5 -0.00696
## 4 2014-03-03 67.0 68.1 66.5 67.4 56824100 67.4 -0.0153
## 5 2014-03-04 68.7 68.9 67.6 68.8 42013500 68.8 0.0206
## 6 2014-03-05 69.7 72.0 69.6 71.6 74567700 71.6 0.0403
## 7 2014-03-06 71.9 71.9 70.2 70.8 46026500 70.8 -0.0102
## 8 2014-03-07 71.1 71.2 69.5 69.8 38927000 69.8 -0.0147
## 9 2014-03-10 70.8 72.2 70.5 72.0 59871600 72.0 0.0319
## 10 2014-03-11 72.5 72.6 70.0 70.1 59408300 70.1 -0.0268
## # ... with 1,248 more rows
Stocks
## # A tibble: 1,258 x 7
## date open high low close volume adjusted
## <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2014-02-26 70.2 71.2 68.8 69.3 55322700 69.3
## 2 2014-02-27 69.3 70.0 68.9 68.9 41653700 68.9
## 3 2014-02-28 69.5 69.9 67.4 68.5 66783700 68.5
## 4 2014-03-03 67.0 68.1 66.5 67.4 56824100 67.4
## 5 2014-03-04 68.7 68.9 67.6 68.8 42013500 68.8
## 6 2014-03-05 69.7 72.0 69.6 71.6 74567700 71.6
## 7 2014-03-06 71.9 71.9 70.2 70.8 46026500 70.8
## 8 2014-03-07 71.1 71.2 69.5 69.8 38927000 69.8
## 9 2014-03-10 70.8 72.2 70.5 72.0 59871600 72.0
## 10 2014-03-11 72.5 72.6 70.0 70.1 59408300 70.1
## # ... with 1,248 more rows
Hint: Note that tq_transmute(), instead of tq_mutate(), is used when periodicity changes. Another difference between the two is that tq_transmute() returns only newly-created columns while tq_mutate() adds new columns to existing variables.
returns_monthly <-
Stocks %>%
tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "monthly")
returns_monthly
## # A tibble: 61 x 2
## date monthly.returns
## <date> <dbl>
## 1 2014-02-28 -0.0116
## 2 2014-03-31 -0.120
## 3 2014-04-30 -0.00764
## 4 2014-05-30 0.0589
## 5 2014-06-30 0.0630
## 6 2014-07-31 0.0797
## 7 2014-08-29 0.0299
## 8 2014-09-30 0.0564
## 9 2014-10-31 -0.0512
## 10 2014-11-28 0.0361
## # ... with 51 more rows
Hint: Refer to the ggplot2 cheatsheet. Google it. See the section for One Variable. Note that there are two different cases: 1) Continuous and 2) Discrete. The type of chart you can use depends on what type of data your variable is.
returns_monthly %>%
ggplot(aes(monthly.returns)) +
geom_histogram()
Hint: Refer to the ggplot2 cheatsheet. See the section for Two Variables.
returns_monthly %>%
ggplot(aes(x = date, y = monthly.returns)) +
geom_line()
Hint: Note that tq_transmute(), instead of tq_mutate(), is used when periodicity changes. Another difference between the two is that tq_transmute() returns only newly-created columns while tq_mutate() adds new columns to existing variables.
returns_yearly <-
Stocks %>%
tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "yearly")
returns_yearly
## # A tibble: 6 x 2
## date yearly.returns
## <date> <dbl>
## 1 2014-12-31 0.126
## 2 2015-12-31 0.341
## 3 2016-12-30 0.0993
## 4 2017-12-29 0.534
## 5 2018-12-31 -0.257
## 6 2019-02-25 0.256
Hint: Refer to the ggplot2 cheatsheet. Google it. See the section for One Variable. Note that there are two different cases: 1) Continuous and 2) Discrete. The type of chart you can use depends on what type of data your variable is.
returns_yearly %>%
ggplot(aes(yearly.returns)) +
geom_histogram()
Hint: Refer to the ggplot2 cheatsheet. See the section for Two Variables.
returns_yearly %>%
ggplot(aes(x = date, y = yearly.returns)) +
geom_line()