plot_metrics_overtime: Plot One Performance Metric over Time or One vs. Another over...

Description Usage Arguments Value Examples

View source: R/plot_metrics_overtime.R

Description

Useful for assessing how one or two performance metrics vary over time, for one or several funds. Supports fixed-width rolling windows, fixed-width disjoint windows, and disjoint windows on per-month or per-year basis.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
plot_metrics_overtime(
  metrics = NULL,
  formula = cagr ~ .,
  type = "hop.year",
  minimum.n = 3,
  tickers = NULL,
  ...,
  gains = NULL,
  prices = NULL,
  benchmark = "SPY",
  y.benchmark = benchmark,
  x.benchmark = benchmark,
  plotly = FALSE,
  title = NULL,
  base_size = 16,
  return = "plot"
)

Arguments

metrics

"Long" data frame with Fund column, Date column, and column for each metric you want to plot. Typically the result of a prior call to calc_metrics_overtime.

formula

Formula specifying what to plot, e.g. cagr ~ mdd for CAGR vs. MDD or cagr ~ . for CAGR over time. See ?calc_metrics for list of performance metrics to choose from.

type

Character string or vector specifying type of calculation. Choices are (1) "roll.n" where n is a positive integer; (2) "hop.n" where n is a positive integer; (3) "hop.month"; (4) "hop.year"; and (5) vector of break-point dates, e.g. c("2019-01-01", "2019-06-01") for 3 periods. The "roll" and "hop" options correspond to rolling and disjoint windows, respectively.

minimum.n

Integer value specifying the minimum number of observations per period, e.g. if you want to exclude short partial months at the beginning or end of the analysis period.

tickers

Character vector of ticker symbols that Yahoo! Finance recognizes, if you want to download data on the fly.

...

Arguments to pass along with tickers to load_gains.

gains

Data frame with a date variable named Date and one column of gains for each investment.

prices

Data frame with a date variable named Date and one column of prices for each investment.

benchmark, y.benchmark, x.benchmark

Character string specifying which fund to use as benchmark for metrics (if you request alpha, alpha.annualized, beta, or r.squared).

plotly

Logical value for whether to convert the ggplot to a plotly object internally.

title

Character string. Only really useful if you're going to set plotly = TRUE, otherwise you can change the title, axes, etc. afterwards.

base_size

Numeric value.

return

Character string specifying what to return. Choices are "plot", "data", and "both".

Value

Depending on return, a ggplot, a data frame with the source data, or a list containing both.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## Not run: 
# Plot net growth each year for BRK-B and SPY
plot_metrics_overtime(formula = growth ~ ., type = "hop.year", tickers = c("BRK-B", "SPY"))

# Create previous plot in step-by-step process with pipes
c("BRK-B", "SPY") %>%
  load_gains() %>%
  calc_metrics_overtime("growth", type = "hop.year") %>%
  plot_metrics_overtime(growth ~ .)

# Plot betas from 100-day disjoint intervals for a 2x daily (SSO) and 3x
# daily (UPRO) leveraged ETF
plot_metrics_overtime(formula = beta ~ ., type = "hop.100", tickers = c("SSO", "UPRO"))

# Create previous plot in step-by-step process with pipes
c("SPY", "SSO", "UPRO") %>%
  load_gains() %>%
  calc_metrics_overtime(metrics = "beta", type = "hop.100") %>%
  plot_metrics_overtime(formula = beta ~ .)

# Plot 50-day rolling alpha vs. beta for SSO and UPRO during 2018
plot_metrics_overtime(
  formula = alpha ~ beta,
  type = "roll.50",
  tickers = c("SSO", "UPRO"),
  from = "2018-01-01", to = "2018-12-31"
)

# Create previous plot in step-by-step process with pipes
c("SPY", "SSO", "UPRO") %>%
  load_gains(from = "2018-01-01", to = "2018-12-31") %>%
  calc_metrics_overtime(metrics = c("alpha", "beta"), type = "roll.50") %>%
  plot_metrics_overtime(alpha ~ beta)


## End(Not run)

vandomed/stocks documentation built on July 22, 2020, 3:25 a.m.