Q1 Import stock prices of Facebook and Apple for the last 5 years.

Hint: Add group_by(symbol) at the end of the code so that calculations below will be done per stock.


library(tidyquant)
library(tidyverse)

# Import data
from = today() - years(5)
Stocks <- tq_get(c("FB", "AAPL"), get = "stock.prices", from = from) %>%
  group_by(symbol)
Stocks
## # A tibble: 2,516 x 8
## # Groups:   symbol [2]
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 FB     2014-02-28  69.5  69.9  67.4  68.5 66783700     68.5
##  2 FB     2014-03-03  67.0  68.1  66.5  67.4 56824100     67.4
##  3 FB     2014-03-04  68.7  68.9  67.6  68.8 42013500     68.8
##  4 FB     2014-03-05  69.7  72.0  69.6  71.6 74567700     71.6
##  5 FB     2014-03-06  71.9  71.9  70.2  70.8 46026500     70.8
##  6 FB     2014-03-07  71.1  71.2  69.5  69.8 38927000     69.8
##  7 FB     2014-03-10  70.8  72.2  70.5  72.0 59871600     72.0
##  8 FB     2014-03-11  72.5  72.6  70.0  70.1 59408300     70.1
##  9 FB     2014-03-12  69.9  71.3  69    70.9 46340500     70.9
## 10 FB     2014-03-13  71.3  71.3  68.2  68.8 57091000     68.8
## # ... with 2,506 more rows

Q2 Create a line chart for both stocks.

Hint: Use ggplot2::facet_wrap. Refer to the ggplot2 cheatsheet. See the section for Faceting.


Stocks %>%
    ggplot(aes(x = date, y = close)) +
    geom_line() +
    facet_wrap(~symbol)

Q3 Calculate quarterly returns, and save the result under returns_quarterly.

Hint: Take the adjusted variable from Stocks, and calculate quarterly returns using tq_transmute(), instead of tq_mutate(), which 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_quarterly <-
  Stocks %>%
    tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "quarterly") 

returns_quarterly
## # A tibble: 42 x 3
## # Groups:   symbol [2]
##    symbol date       quarterly.returns
##    <chr>  <date>                 <dbl>
##  1 FB     2014-03-31          -0.120  
##  2 FB     2014-06-30           0.117  
##  3 FB     2014-09-30           0.175  
##  4 FB     2014-12-31          -0.0129 
##  5 FB     2015-03-31           0.0538 
##  6 FB     2015-06-30           0.0432 
##  7 FB     2015-09-30           0.0482 
##  8 FB     2015-12-31           0.164  
##  9 FB     2016-03-31           0.0902 
## 10 FB     2016-06-30           0.00158
## # ... with 32 more rows

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.

Q4 Create a density plot for returns of both stocks.

Hint: Refer to the ggplot2 cheatsheet. Look for geom_density under One Variable. Use the fill argument to create the plot per each stock.

returns_quarterly %>%
    ggplot(aes(x = quarterly.returns, fill = symbol) ) +
    geom_density(alpha = 0.3) 

Q5 Which of the two stocks would you expect the most stable returns quarter after quarter without worrying too much of fluctutations?

Hint: Examine the density plot. Note that density is mapped to the vertical axis, while return percentage is to the horizontal axis. The thinner and taller the distribution is, the smaller the range of possible return percentages are. The thicker and shorter the distribution is, the wider the range of possible return percentages are. Hence, it would be more difficult to make predictions for a thicker and shorter distribution than for a thinner and taller one.

Although it seems we can see a higher density for that of Facebook, and we are able to have higher density overall in returns, we might pick facebook. But it seems with Apple having higher and more specific peaks we are able to track it better, and in my opinion would be able to see more speicifically where we should invest. All in all allowing for my stable returns, especially considering apple has infrequent negative quarterly returns.

Q6 Which of the two stocks would you expect the most frequent large negative returns?

Hint: As was described in Q3 hint, returns are mapped to the horizontal axis with negative returns on the left while negative returns on the right. Thus, the distribution that is skewed to the far left suggests the possibility of large negative returns (a negative skewness). In addition, the thicker tail on the left is indicative of the larger probability of negative returns.

Apple overall has alot more area covered, an higher peaks when it comes to positive quartely returns. This also comes with Facebook having quite dense negative returns while Apple have very low denseness on that side. Because of this, we would definetely see Facebook having for fequent large negative returns.