symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
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"
) %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
symbols_vec <- asset_returns_tbl %>%
distinct(asset) %>%
pull()
weights <- c(0.25, 0.25, 0.20, 0.20, 0.10)
w_tbl <- tibble(symbols_vec, weights) %>%
set_names(c("asset", "weights"))
w_tbl
## # A tibble: 5 × 2
## asset weights
## <chr> <dbl>
## 1 AGG 0.25
## 2 EEM 0.25
## 3 EFA 0.2
## 4 IJS 0.2
## 5 SPY 0.1
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(
assets_col = asset,
returns_col = returns,
weights = w_tbl,
col_rename = "returns",
rebalance_on = "quarters"
)
## Warning in check_weights(weights, assets_col, map, x): Sum of weights does not
## equal 1.
portfolio_returns_tbl %>%
ggplot(aes(x = returns)) +
geom_histogram(binwidth = 0.01, fill = "cornflowerblue") +
geom_density() +
scale_x_continuous(labels = scales::percent_format()) +
labs(
title = "Portfolio Quarterly Returns: Histogram and Density",
x = "Quarterly Returns",
y = "Distribution"
) +
theme_tq()
Based on the plot above, the portfolio’s quarterly returns are centered around (fill in the approximate peak value from your chart, e.g. “2%”), which represents the most typical quarterly return given this asset mix and these weights. The spread of the distribution shows (describe whether returns are tightly clustered or widely spread, and what that implies about predictability quarter to quarter).