Step 1: Import Stock Prices

symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")

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

Step 2: Convert Prices to Quarterly Returns

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"))

Step 3: Assign a Weight to Each Asset

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

Step 4: Build a Portfolio

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.

Step 5: Plot Density (Histogram + Density Plot)

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()

What return should you expect from your portfolio in a typical quarter?

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).