knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7
)

InvestmentSuite is designed for quantitative analysis on time-series. The functions work on time-series in data.frame structures organized by column with dates as Date class in the first column. The ETF data provides an example of a properly structured data.frame for this package.

library(InvestmentSuite)
data(ETF)
head(ret)

For users more acustom to using xts structures there's an xts_to_dataframe function to convert xts structures to the data.frame time-series structure this package functions work on.

x <- matrix(runif(25), ncol = 5)
sample_xts <- xts::xts(x, seq.Date(from = as.Date('2019-01-01'), 
                                   length.out = 5,
                                   by = 'day'))
sample_xts
xts_to_dataframe(sample_xts)

InvestmentSuite has time-series utilities to change the return or price frequency and convert between prices and returns.

ret_monthly <- change_freq(ret, 'm')
head(ret_monthly)
price_index <- ret_to_price(ret_monthly, init_val = 1)
head(price_index)

The data.frame time-series are organized by columns for ease of time-series analysis calculations, however they can easily be converted into a 'tidy' structure with tidy_ret to work with tidyverse packages such as ggplot2.

library(ggplot2)
plot_data <- tidy_ret(price_index[, 1:4])
head(plot_data)
ggplot(plot_data, aes(x = date, y = values, color = series)) +
  geom_line()

The rebal function combines the return data.frame into a portfolio time-series based on the wgt parameter. The wgt parameter can be a numeric vector or a data.frame of a time-series of weights organized in the same manner as the ret time-series. If the wgt parameter is specified as a vector then the reb_freq parameter can be used to create periodic rebalance dates to bring the weights back to the target wgt. The code below will show how this works with a quarterly rebalance assumption. Note, if passing a numeric vector of wgt, leaving the reb_freq as NA will run the rebal as a buy-and-hold strategy without periodic rebalances.

n_assets <- ncol(ret) - 1
equal_wgt <- rep(1 / n_assets, n_assets)
rebal_wgt <- auto_reb_wgt(equal_wgt, 'q')
head(rebal_wgt)
port <- rebal(ret = ret, ret_freq = 'd', wgt = equal_wgt, reb_freq = 'q')
head(port$port_index)
ggplot(port$port_index, aes(x = date, y = port_index)) + 
  geom_line()

The result of the rebal function is a list containing the asset returns and historical weights as well as the portfolio returns. The portfolio list contains all the data needed for calculating a contribution to return.

# 2019 year-to-date contribution to return
contr_to_ret(port, date_start = as.Date('2019-01-01'))

The run_reg function runs a multi-variate regression on multiple y variables. The FF data contains factor returns from Ken French's data library. The result contains a list of fit from the lm() results and tables to show the regression summary.

data(FF)
y <- ret[, 1:7]
x <- ff$ff_5
res <- run_reg(y = y, x = x, rf = ff$ff_rf, freq = 'w')
res$fmt


alejandro-sotolongo/InvestmentSuite documentation built on Jan. 19, 2020, 5:20 p.m.