# Load packages
library(tidyverse)
library(tidyquant)
# Choose Stocks
symbols <- c("AMZ","TSLA","AAPL","CMG","NKE")
prices <- tq_get(x= symbols,
get ="stock.prices",
from = "2012-01-01",
to = "2017-01-01")
## 2 Convert prices to returns by ***quarterly***
``` r
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select= adjusted,
mutate_fun= periodReturn,
period= "quarterly",
type= "log") %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
asset_returns_tbl %>%
ggplot(aes(x = returns)) +
geom_density(aes(color=asset), show.legend = FALSE, alpha = 1) +
geom_histogram(aes(fill = asset), show.legend = FALSE, alpha = 0.3, bindwith = 0.01) +
facet_wrap(~asset, ncol = 1)
#labeling
labs(title= "Distribution of Quarterly Returns,2012-2016",
y = "Frequency" ,
x = "Rate of Returns" ,
captions = "A typically return is higher for __ than for _." )
## $y
## [1] "Frequency"
##
## $x
## [1] "Rate of Returns"
##
## $captions
## [1] "A typically return is higher for __ than for _."
##
## $title
## [1] "Distribution of Quarterly Returns,2012-2016"
##
## attr(,"class")
## [1] "labels"
-Apple (AAPL) seems to be the safest option because there isnt much risk as there arent expected high returns. -Amazon (AMZ) and Nike (NKE) are a bit riskier but still not heavy returns. -Chipotle (CMG) has almost 0.4% of quarterly returns… higher risk but alot more reward. -Tesla (TSLA) is the most risky, with huge payouts in returns at one quarter they did around 1.1%. Choose Apple for safety, and Tesla for high risk and reward, the rest find themselves in the middle
Hide the code, messages, and warnings