-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from AngusMcLure/refactor
DEV: Add sample_design class and re-implement design_effect
- Loading branch information
Showing
10 changed files
with
356 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
check_geq2 <- function(val, min) { | ||
name <- deparse(substitute(val)) # get name of variable | ||
if (!is.numeric(val)) { | ||
stop(glue::glue("{name} must be numeric, not {class(val)}.")) | ||
} | ||
if (val < min) { | ||
stop(glue::glue("{name} must be >= {min}.")) | ||
} | ||
} | ||
|
||
check_in_range2 <- function(val) { | ||
name <- deparse(substitute(val)) # get name of variable | ||
if(!is.numeric(val)) { | ||
stop(glue::glue("{name} must be numeric, not {class(val)}.")) | ||
} | ||
if (val < 0 | val > 1) { | ||
message(glue::glue("{name} must be a numeric value between 0 and 1, inclusive.")) | ||
stop(glue::glue("{name} = {val}")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#' S3 sample_design constructors | ||
#' | ||
#' Stores parameters related to the sampling design. Aims to reduce having to | ||
#' input each param separately across functions (e.g. power/optimise). Can | ||
#' either be of class `fixed_design` or `variable_design`. | ||
#' | ||
#' @param pool_size numeric/NULL The number of units per pool. Must be a numeric | ||
#' value greater than 0. `fixed_design` only. | ||
#' @param pool_number numeric/NULL The number of pools per cluster. Numeric | ||
#' inputs must be an integer greater than or equal to 1. `fixed_design` only. | ||
#' @param catch_dist An object of class `distribution` (e.g. produced by | ||
#' `nb_catch()`) defining the distribution of the possible catch. If | ||
#' `correlation = 0` the catch is for the whole survey. For `correlation > 0` | ||
#' the catch is per cluster (i.e. cluster size). `variable_design` only. | ||
#' @param pool_strat function Defines a rule for how a number of units will be | ||
#' divided into pools. Must take a single numeric argument and return a named | ||
#' list of pool sizes and pool numbers. `pool_max_size()` and | ||
#' `pool_target_number` provide convenience functions for defining common | ||
#' pooling strategies. `variable_design` only. | ||
#' @param sensitivity numeric The probability that the test correctly identifies | ||
#' a true positive. Must be a numeric value between 0 and 1, inclusive of | ||
#' both. A value of 1 indicates that the test can perfectly identify all true | ||
#' positives. | ||
#' @param specificity numeric The probability that the test correctly identifies | ||
#' a true negative. Must be a numeric value between 0 and 1, inclusive of | ||
#' both. A value of 1 indicates that the test can perfectly identify all true | ||
#' negatives. | ||
#' | ||
#' @return An object of class \code{sample_design} | ||
#' @export | ||
#' | ||
#' @examples | ||
#' fd_perfect <- fixed_design(pool_size = 10) | ||
#' | ||
#' fd_imperfect <- fixed_design( | ||
#' pool_size = 10, pool_number = NULL, sensitivity = 0.95, specificity = 0.99 | ||
#' ) | ||
#' | ||
#' vd_target <- variable_design( | ||
#' catch_dist = nb_catch(10, 11), | ||
#' pool_strat = pool_target_number(20) | ||
#' ) | ||
#' | ||
#' vd_max <- variable_design( | ||
#' catch_dist = nb_catch(10, 11), | ||
#' pool_strat = pool_max_size(20) | ||
#' ) | ||
#' | ||
#' vd_max_imperfect <- variable_design( | ||
#' catch_dist = nb_catch(10, 11), | ||
#' pool_strat = pool_max_size(20), | ||
#' sensitivity = 0.95, | ||
#' specificity = 0.98 | ||
#' ) | ||
fixed_design <- function(pool_size = NULL, | ||
pool_number = NULL, | ||
sensitivity = 1, | ||
specificity = 1) { | ||
|
||
# allow NULLs for optimise functions to identify which | ||
# variable should be optimised | ||
if (!is.null(pool_size)) { | ||
check_geq2(pool_size, 0) | ||
} | ||
if (!is.null(pool_number)) { | ||
check_geq2(pool_number, 0) | ||
} | ||
# sens and spec cannot be NULL | ||
check_in_range2(sensitivity) | ||
check_in_range2(specificity) | ||
|
||
structure( | ||
list( | ||
pool_size = pool_size, | ||
pool_number = pool_number, | ||
sensitivity = sensitivity, | ||
specificity = specificity | ||
), | ||
class = c("fixed_design", "sample_design") | ||
) | ||
} | ||
|
||
#' @rdname fixed_design | ||
#' @export | ||
variable_design <- function(catch_dist, | ||
pool_strat, | ||
sensitivity = 1, | ||
specificity = 1) { | ||
|
||
# sens and spec cannot be NULL | ||
check_in_range2(sensitivity) | ||
check_in_range2(specificity) | ||
|
||
structure( | ||
list( | ||
catch_dist = catch_dist, | ||
pool_strat = pool_strat, | ||
sensitivity = sensitivity, | ||
specificity = specificity | ||
), | ||
class = c("variable_design", "sample_design") | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.