tests/testthat/test-stat_ci.R

# Copyright 2019 Province of British Columbia
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.

library(elucidate)
set.seed(1234)

y1 <- c(1:500)

test_that("stat_ci works for sd", {
 expect_equal(stat_ci(y1, stat = sd, replicates = 500, ci_type = "perc"),
              c("sd" = 144.5, "lower" = 138.5, "upper" = 149.8),
              tolerance = 1)
})


test_that("stat_ci works for sd in parallel", {
  expect_equal(stat_ci(y1, stat = sd, replicates = 500,  ci_type = "perc", parallel = TRUE, cores = 2),
               c("sd" = 144.5, "lower" = 138.5, "upper" = 149.8),
               tolerance = 1)
})

test_that("stat_ci works for se", {
  expect_equal(stat_ci(y1, stat = se, replicates = 500, ci_type = "perc"),
               c("se" = 6.4, "lower" = 6.2, "upper" = 6.7),
               tolerance = 1)
})

test_that("stat_ci works with BCa interval", {
  expect_equal(stat_ci(y1, stat = sd, replicates = 500),
               c("sd" = 144.5, "lower" = 138.5, "upper" = 149.8),
               tolerance = 1)
})

y1[y1 < 100] <- NA

test_that("stat_ci BCa interval fails with NA present", {
  expect_error(stat_ci(y1, stat = sd, ci_type = "bca", na.rm = FALSE),
               regexp = "*missing value where TRUE/FALSE needed*")
})
bcgov/elucidate documentation built on Sept. 3, 2022, 7:16 p.m.