Compute Breakpoints Based on Sorting Variable
Source:R/compute_breakpoints.R
compute_breakpoints.RdComputes breakpoints based on a specified sorting. It can optionally filter the data by exchanges or lagged size quantiles before computing the breakpoints. The function requires either the number of portfolios to be created or specific percentiles for the breakpoints, but not both. The function also optionally handles cases where the sorting variable clusters on the edges, by assigning all extreme values to the edges and attempting to compute equally populated breakpoints with the remaining values.
Arguments
- data
A data frame containing the dataset for breakpoint computation.
- sorting_variable
A character string specifying the column name in
datato be used for determining breakpoints.- breakpoint_options
A named list of
breakpoint_options()for the breakpoints. The arguments includen_portfoliosAn optional integer specifying the number of equally sized portfolios to create. This parameter is mutually exclusive withpercentiles.percentilesAn optional numeric vector specifying the percentiles for determining the breakpoints of the portfolios. This parameter is mutually exclusive withn_portfolios.breakpoints_exchangesAn optional character vector specifying exchange names to filter the data before computing breakpoints. Exchanges must be stored in a column given bydata_options(defaults toexchange). IfNULL, no filtering is applied.smooth_bunchingAn optional logical parameter specifying if to attempt smoothing non-extreme portfolios if the sorting variable bunches on the extremes (TRUE), or not (FALSE, the default). In some cases, smoothing will not result in equal-sized portfolios off the edges due to multiple clusters. If sufficiently large bunching is detected,percentilesis ignored and equally-spaced portfolios are returned for these cases with a warning.breakpoints_min_size_thresholdAn optional numeric value between 0 and 1 (exclusive). When set, stocks with market capitalization below this quantile are excluded from breakpoint computation. The quantile is computed amongbreakpoints_exchangesstocks if specified, otherwise among all stocks. Requires a market capitalization column in the data (column name determined bydata_options).
- data_options
A list of class
tidyfinance_data_options(created viadata_options()) specifying column name mappings. Theexchangeelement is used to specify the exchange column, andmktcap_lagis used to specify the market capitalization. Usesdata_options()default ifNULL:"exchange" = "exchange"and"mktcap_lag" = "mktcap_lag".
Note
This function will stop and throw an error if both n_portfolios and
percentiles are provided or missing simultaneously.
Examples
set.seed(42)
data <- data.frame(
id = 1:100,
exchange = sample(c("NYSE", "NASDAQ"), 100, replace = TRUE),
market_cap = 1:100
)
compute_breakpoints(data, "market_cap", breakpoint_options(n_portfolios = 5))
#> [1] 1.0 20.8 40.6 60.4 80.2 100.0
compute_breakpoints(
data,
"market_cap",
breakpoint_options(
percentiles = c(0.2, 0.4, 0.6, 0.8),
breakpoints_exchanges = c("NYSE")
)
)
#> [1] 1.0 19.0 31.0 59.8 80.8 97.0