# Load packages
library(tidyverse)
library(tidyquant)

1 Import stock prices of your choice

# Choose stocks
symbols <- c("NOK", "INTC", "HMC", "WMT")

prices <- tq_get(x    = symbols, 
                 get  = "stock.prices", 
                 from = "2012-01-01", 
                 to   = "2017-01-01")

2 Convert prices to returns by quarterly

asset_returns_tbl <- prices %>%
    
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "quarterly",
                 type      = "log") %>%
    
    set_names(c("asset", "date", "returns"))

asset_returns_tbl
## # A tibble: 80 × 3
## # Groups:   asset [4]
##    asset date       returns
##    <chr> <date>       <dbl>
##  1 NOK   2012-03-30  0.0659
##  2 NOK   2012-06-29 -0.901 
##  3 NOK   2012-09-28  0.220 
##  4 NOK   2012-12-31  0.426 
##  5 NOK   2013-03-28 -0.186 
##  6 NOK   2013-06-28  0.131 
##  7 NOK   2013-09-30  0.554 
##  8 NOK   2013-12-31  0.220 
##  9 NOK   2014-03-31 -0.0998
## 10 NOK   2014-06-30  0.0928
## # ℹ 70 more rows


## 3 Make plot
asset_returns_tbl %>%

``` r
asset_returns_tbl %>%
    
    ggplot(aes(x = returns)) +
    geom_density(aes(color  = asset), alpha = 1) +
    geom_histogram(aes(fill = asset), show.legend = FALSE, alpha = 0.3, binwidth = 0.01) +
    facet_wrap(~ asset, ncol = 1) +
    
    # labeling
    labs(title = "Distribution of Monthly Returns", "2012-2016 ", 
         y     =  "Frequency",
         x     = "Rate of Returns",
         caption = "A typic monthly return is higher for SPY and IJS than for AGG, EEM, and EFA.")

    

4 Interpret the plot

 Looking at the plots, I chose Honda, Intel, Nokia, and Walmart. Looking at these It is clear to see that the best out of the 4 is Honda, and Intel. This is due to their not as risky high earnings. Walmart on the other hand would be also a great investment, because it is very safe, but does not have as high of earnings as the first too. Theroretically, Nokia could be the highest earner because it could ear potentially over 60%.

5 Change the global chunck options

Hide the code, messages, and warnings