# Load packages
library(tidyverse)
library(tidyquant)
symbols <- c("NOC", "WMT","UPS","UNH", "SPY")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-01-01",
to = "2017-01-01")
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
## # A tibble: 100 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 NOC 2012-03-30 0.0488
## 2 NOC 2012-06-29 0.0528
## 3 NOC 2012-09-28 0.0486
## 4 NOC 2012-12-31 0.0257
## 5 NOC 2013-03-28 0.0457
## 6 NOC 2013-06-28 0.173
## 7 NOC 2013-09-30 0.147
## 8 NOC 2013-12-31 0.190
## 9 NOC 2014-03-31 0.0789
## 10 NOC 2014-06-30 -0.0250
## # ℹ 90 more rows
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, binwidth = 0.01) +
facet_wrap(~asset, ncol = 1) +
# labeling
labs(title = "distrobution of Monthly Returns, 2012-2016",
y ="frequency",
x = "Rate of Returns",
capition = "A typical monthly return is higher for SPY and IJS than for AGG, EEM, and EFA")
A typical returns for UNH and NOC are higher than SPY(benchmark) with UPS showing similar rate of returns and WMT is lower than the rest. SPY has the tightest spread showing the least risk as for UPS and WMT they have a very wide spread with an even distribution.
Hide the code, messages, and warnings