(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) n_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, n_blocks)
#> [1] -1437.0772 285.7101 882.9747 1011.4861 890.8656 606.0891 #> [7] 211.4663 -264.3210 -799.9503 -1387.2422 #> attr(,"label") #> [1] "Residuals"
# Works with functions that take one argument invert_score <- function(e) -e/2 build_score_fn(invert_score)(entropy, n_blocks)
#> [1] 4899.6855 4398.9675 3900.8155 3399.7438 2900.9315 2400.2610 1900.0263 #> [8] 1399.7285 900.4570 399.7659
# Works with predefined strings build_score_fn("dev_from_rolling_mean")(entropy, n_blocks)
#> [1] 0.0000 -500.7180 -998.0147 -1500.1186 -1498.3661 -1500.3539 #> [7] -1500.4287 -1501.0167 -1499.3223 -1500.4570
build_score_fn("lowest")(entropy, n_blocks)
#> [1] 9799.3710 8797.9350 7801.6310 6799.4876 5801.8630 4800.5220 3800.0526 #> [8] 2799.4570 1800.9141 799.5318