# Load libraries
library(dplyr)
# Use FANG data set
# Get returns for individual stock components
monthly_returns_stocks <- FANG %>%
group_by(symbol) %>%
tq_transmute(adjusted, periodReturn, period = "monthly")
##### Portfolio Aggregation Methods #####
# Method 1: Use tq_portfolio with numeric vector of weights
weights <- c(0.50, 0.25, 0.25, 0)
tq_portfolio(data = monthly_returns_stocks,
assets_col = symbol,
returns_col = monthly.returns,
weights = weights,
col_rename = NULL,
wealth.index = FALSE)
# Method 2: Use tq_portfolio with two column tibble and map weights
# Note that GOOG's weighting is zero in Method 1. In Method 2,
# GOOG is not added and same result is achieved.
weights_df <- tibble(symbol = c("META", "AMZN", "NFLX"),
weights = c(0.50, 0.25, 0.25))
tq_portfolio(data = monthly_returns_stocks,
assets_col = symbol,
returns_col = monthly.returns,
weights = weights_df,
col_rename = NULL,
wealth.index = FALSE)
# Method 3: Working with multiple portfolios
# 3A: Duplicate monthly_returns_stocks multiple times
mult_monthly_returns_stocks <- tq_repeat_df(monthly_returns_stocks, n = 4)
# 3B: Create weights table grouped by portfolio id
weights <- c(0.50, 0.25, 0.25, 0.00,
0.00, 0.50, 0.25, 0.25,
0.25, 0.00, 0.50, 0.25,
0.25, 0.25, 0.00, 0.50)
stocks <- c("META", "AMZN", "NFLX", "GOOG")
weights_table <- tibble(stocks) %>%
tq_repeat_df(n = 4) %>%
bind_cols(tibble(weights)) %>%
group_by(portfolio)
# 3C: Scale to multiple portfolios
tq_portfolio(data = mult_monthly_returns_stocks,
assets_col = symbol,
returns_col = monthly.returns,
weights = weights_table,
col_rename = NULL,
wealth.index = FALSE)
Run the code above in your browser using DataLab