Skip to content

Instantly share code, notes, and snippets.

@nutterb
Created July 12, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nutterb/03f99569917f049242bf0f792f5d9d60 to your computer and use it in GitHub Desktop.
Save nutterb/03f99569917f049242bf0f792f5d9d60 to your computer and use it in GitHub Desktop.
Comparing code efficiency of multiple assertions in checkmate
# Suppose we have arguments for power and sample size of a two-sample t-test
# (Similar to power.t.test, but vectorized. See https://github.com/nutterb/StudyPlanning/blob/devel/R/test_t2.R
# delta: Difference of means. (-Inf, Inf)
# delta0: Difference under the null hypothesis. (-Inf, Inf)
# se: Standard error (0, Inf)
# alpha: significance level (0, 1)
# power: power of the test (0, 1)
### STANDARD ARGUMENT CHECKS ###
# 19 lines of code (259 characters)
# Adding `add = coll` (assert collection) adds another 50 characters
assert_numeric(x = delta,
null.ok = TRUE)
assert_numeric(x = mu0,
null.ok = TRUE)
assert_numeric(x = se,
lower = 0,
null.ok = TRUE)
assert_numeric(x = alpha,
lower = 0,
upper = 1,
null.ok = TRUE)
assert_numeric(x = power,
lower = 0,
upper = 1,
null.ok = TRUE)
### aapply ###
#mllg's aaply (https://github.com/mllg/checkmate/issues/115#issuecomment-314690205)
# 13 lines of code (180 characters)
# Adding `add = coll` (assert collection) adds another 30 characters
aapply(assert_numeric,
~ delta + mu,
null.ok = TRUE)
aapply(assert_numeric,
~ alpha + power,
lower = 0,
upper = 1,
null.ok = TRUE)
assert_numeric(x = se,
lower = 0,
null.ok = TRUE)
### checkapply ###
# https://github.com/mllg/checkmate/issues/115#issuecomment-314742154
# 5 lines of code (172 characters)
# Adding `add = coll` (assert collection) adds another 10 characters
checkapply(assert_numeric,
~ delta + mu0 + alpha + power + se,
lower = list(alpha = 0, power = 0, se = 0),
upper = list(alpha = 1, power = 1),
fixed = list(null.ok = TRUE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment