Learn R Programming

PortfolioTesteR (version 0.1.4)

analyze_by_period: Period-level summary statistics

Description

Aggregates portfolio results by calendar period and computes standard statistics for each period. Provide at least one of returns or values.

Usage

analyze_by_period(
  dates,
  returns = NULL,
  values = NULL,
  period = c("monthly", "quarterly", "yearly"),
  na_rm = TRUE
)

Value

data.frame with period keys and columns: ret, start_value, end_value, n_obs.

Arguments

dates

Date vector aligned to returns / values.

returns

Numeric simple returns aligned to dates (optional).

values

Numeric equity values aligned to dates (optional).

period

"monthly", "quarterly", or "yearly".

na_rm

Logical; remove NAs inside per-period aggregations.

Examples

Run this code
# \donttest{
  data(sample_prices_weekly)
  mom12 <- PortfolioTesteR::calc_momentum(sample_prices_weekly, lookback = 12)
  sel5  <- PortfolioTesteR::filter_top_n(mom12, n = 5)
  w_eq  <- PortfolioTesteR::weight_equally(sel5)
  pr    <- PortfolioTesteR::portfolio_returns(w_eq, sample_prices_weekly)
  val   <- 1e5 * cumprod(1 + pr$portfolio_return)
  out   <- analyze_by_period(
    dates   = pr$Date,
    returns = pr$portfolio_return,
    values  = val,
    period  = "monthly"
  )
  head(out)
# }

Run the code above in your browser using DataLab