Skip to content

Commit c27e702

Browse files
authored
Merge pull request #135 from signaturescience/126-clarify-weighting-scheme-documentation
Edited plane_score so that weights must be >= 1. Also edited documention
2 parents b112fef + ba8995f commit c27e702

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

R/planes.R

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ plane_repeat <- function(location, input, seed, tolerance = NULL, prepend = NULL
393393
#' @param seed Prepared [seed][plane_seed()]
394394
#' @param components Character vector specifying component; must be either `"all"` or any combination of `"cover"`, `"diff"`, `"taper"`, `"trend"`, `"repeat"`, `"shape"`, and `"zero"`; default is `"all"` and will use all available components for the given signal
395395
#' @param args Named list of arguments for component functions. List elements must be named to match the given component and arguments passed as a nested list (e.g., `args = list("trend" = list("sig_lvl" = 0.05))`). Default is `NULL` and defaults for all components will be used
396-
#' @param weights Named vector with weights to be applied; default is `NULL` and all components will be equally weighted; if not `NULL` then the length of the vector must equal the number of components, with each component given a numeric weight (see Examples)
396+
#' @param weights Named vector with weights to be applied; default is `NULL` and all components will be equally weighted; if not `NULL` then the length of the vector must equal the number of components, with each component given a numeric weight (see Examples). Specified weights must be real numbers greater than or equal to 1.
397397
#'
398398
#'
399399
#'
@@ -431,7 +431,7 @@ plane_repeat <- function(location, input, seed, tolerance = NULL, prepend = NULL
431431
#'
432432
#' ## run plane scoring with specific components and weights
433433
#' comps <- c("cover", "taper", "diff")
434-
#' wts <- c("cover" = 2, "taper" = 1, "diff" = 4)
434+
#' wts <- c("cover" = 1.5, "taper" = 1, "diff" = 4)
435435
#' plane_score(input = prepped_forecast, seed = prepped_seed, components = comps, weights = wts)
436436
#'
437437
#' }
@@ -511,14 +511,17 @@ plane_score <- function(input, seed, components = "all", args = NULL, weights =
511511
## construct a tibble with weights for components
512512
## if the weights argument is NULL then apply equal weights to all components
513513
if(is.null(weights)) {
514-
weights_tbl <-
515-
dplyr::tibble(component = components, weight = 1)
514+
weights_tbl <- dplyr::tibble(component = components, weight = 1)
516515
} else {
516+
if(any(weights < 1)) {
517+
stop("Weights must be a real number >= 1")
518+
}
519+
517520
if(!all(sort(names(weights)) == sort(components))) {
518521
stop("Weights must be provided as a vector with all components used included by name (e.g., c('diff' = 4, 'cover' = 1))")
519522
}
520-
weights_tbl <-
521-
dplyr::tibble(component = names(weights), weight = weights)
523+
524+
weights_tbl <- dplyr::tibble(component = names(weights), weight = weights)
522525
}
523526

524527
## convert the tibble into a list

man/plane_score.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-plane-components.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ test_that("plane_score handles weights", {
257257
expect_error(plane_score(prepped_forecast, prepped_seed, components = c("diff","repeat"), weights = c("diff" = 4, "foo"= 1)))
258258
expect_error(plane_score(prepped_forecast, prepped_seed, components = c("diff","repeat"), weights = c("diff" = 4, "cover"= 1)))
259259

260+
## check that weights are enforced to be >= 1
261+
expect_error( plane_score(prepped_forecast, prepped_seed, components = c("diff","repeat"), weights = c("diff" = 0.5, "repeat"= 1)))
262+
260263
})
261264

262265
test_that("plane_trend flags known changepoints and is sensitive to changes in sig.lvl", {

0 commit comments

Comments
 (0)