(Internal) Takes a heuristic object and turns it into a function that takes two arguments, entropy and number of blocks that is used to calculate the score for a given entropy-number of blocks step for agglomerative merging algorithms.

build_score_fn(heuristic)

Arguments

heuristic

How the best partitioning is defined. Takes either a function that takes one/two arguments: an entropy vector and an optional number of blocks vector with each element corresponding to a given location, or a string labeling algorithm. Currently only "lowest", "dev_from_rolling_mean", "delta_ratio", "trend_deviation", and "nls_residual" are supported.

Value

A function that takes and entropy and number of block vector and returns a score for partitioning (higher = better)

See also

Examples

# Setup fake entropy and number of blocks vectors entropy <- -(10:1)*1000 + rnorm(10, 200) num_blocks <- 1:10 # Works with heuristic functions that take two arguments nls_score <- function(e, k){ entropy_model <- nls(e ~ a + b * log(k), start = list(a = max(e), b = -25)) -residuals(entropy_model) } build_score_fn(nls_score)(entropy, num_blocks)
#> [1] -1436.2854 286.8878 879.0978 1013.5037 888.4070 607.3817 #> [7] 211.9168 -263.7188 -801.3782 -1385.8106 #> attr(,"label") #> [1] "Residuals"
# Works with functions that take one argument invert_score <- function(e) -e/2 build_score_fn(invert_score)(entropy, num_blocks)
#> [1] 4900.2768 4399.6855 3898.9675 3400.8155 2899.7438 2400.9315 1900.2610 #> [8] 1400.0263 899.7285 400.4570
# Works with predefined strings build_score_fn("dev_from_rolling_mean")(entropy, num_blocks)
#> [1] 0.0000 -500.5913 -1001.3516 -1498.2417 -1500.1186 -1498.3661 #> [7] -1500.3539 -1500.4287 -1501.0167 -1499.3223
build_score_fn("lowest")(entropy, num_blocks)
#> [1] 9800.5537 8799.3710 7797.9350 6801.6310 5799.4876 4801.8630 3800.5220 #> [8] 2800.0526 1799.4570 800.9141