tests/testthat/test-compute_scale.R

## Test 1: compute_scale() works as expected ----
test_that("compute_scale Test 1: compute_scale works as expected", {
  input <- c(5, 1, 2, NA)

  expected_output <- mean((input - 1) * 25, na.rm = TRUE)

  expect_equal(
    compute_scale(input,
      c(1, 5),
      c(0, 100),
      min_n = 3
    ),
    expected_output
  )
})

## Test 2: scale is flipped if flip_direction == TRUE ----
test_that("compute_scale Test 2: scale is flipped if flip_direction == TRUE", {
  input <- c(1, 3, 3)

  expected_output <- 100 - (mean((input - 1) * 50, na.rm = TRUE))

  expect_equal(
    compute_scale(input,
      c(1, 3),
      c(0, 100),
      flip_direction = TRUE,
      min_n = 3
    ),
    expected_output
  )
})

## Test 3: result is missing if min_n is not met ----
test_that("compute_scale Test 3: result is missing if min_n is not met", {
  input <- c(4)

  expected_output <- NA

  expect_equal(
    compute_scale(input,
      c(1, 5),
      c(0, 100),
      min_n = 2
    ),
    expected_output
  )
})

## Test 4: average is calculated without transformation if source and  ----
##  target range aren't specified ----
test_that("compute_scale Test 4: average is calculated without transformation
            if source and target range aren't specified", {
  input <- c(1, 3, 5, 5, 1, 3, 3)

  expected_output <- mean(input, na.rm = TRUE)

  expect_equal(
    compute_scale(input,
      min_n = 4
    ),
    expected_output
  )
})

## Test 5: compute_scale() works as expected within derive_summary_records() ----
test_that("compute_scale Test 5: compute_scale() works as expected within
          derive_summary_records()", {
  input <- tibble::tribble(
    ~STUDYID, ~USUBJID, ~PARAMCD, ~AVISIT, ~AVISITN, ~AVAL,
    "ADMIRAL01", "01-701-1015", "ITEM1", "WEEK 10", 100, 5,
    "ADMIRAL01", "01-701-1015", "ITEM2", "WEEK 10", 100, 1,
    "ADMIRAL01", "01-701-1015", "ITEM3", "WEEK 10", 100, 2,
    "ADMIRAL01", "01-701-1015", "ITEM4", "WEEK 10", 100, NA,
    "ADMIRAL01", "01-701-1015", "ITEM1", "WEEK 20", 200, 1,
    "ADMIRAL01", "01-701-1015", "ITEM2", "WEEK 20", 200, 3,
    "ADMIRAL01", "01-701-1015", "ITEM3", "WEEK 20", 200, 5,
    "ADMIRAL01", "01-701-1015", "ITEM4", "WEEK 20", 200, 5,
    "ADMIRAL01", "01-701-1281", "ITEM1", "WEEK 10", 100, 4,
  )

  expected_output <- bind_rows(
    input,
    input %>%
      filter(PARAMCD %in% c("ITEM1", "ITEM2", "ITEM3")) %>%
      group_by(STUDYID, USUBJID, AVISIT, AVISITN) %>%
      summarise(n = n(), AVAL = mean(AVAL, na.rm = TRUE)) %>%
      mutate(
        PARAMCD = "ITEMAVG",
        AVAL = ifelse(n >= 3, 100 - ((AVAL - 1) * 25), NA)
      ) %>%
      select(-n)
  )

  expect_equal(
    derive_summary_records(
      input,
      by_vars = exprs(STUDYID, USUBJID, AVISIT, AVISITN),
      filter = (PARAMCD %in% c("ITEM1", "ITEM2", "ITEM3")),
      analysis_var = AVAL,
      summary_fun = function(x) {
        compute_scale(x, c(1, 5), c(0, 100), flip_direction = TRUE, min_n = 3)
      },
      set_values_to = exprs(PARAMCD = "ITEMAVG")
    ),
    expected_output
  )
})

## Test 6: error is thrown if source_range is supplied, but not ----
##  target_range, or vice-versa ----
test_that("compute_scale Test 6: error is thrown if source_range is supplied,
          but not target_range, or vice-versa", {
  input <- c(1, 3, 5, 5, 1, 3, 3)

  expect_error(
    compute_scale(input,
      source_range = c(1, 5),
      min_n = 2
    ),
    paste0(
      "argument `target_range` is missing, with no default, but ",
      "`source_range` is not missing\nEither both or neither ",
      "argument should exist"
    )
  )

  expect_error(
    compute_scale(input,
      target_range = c(0, 100),
      min_n = 2
    ),
    paste0(
      "argument `source_range` is missing, with no default, but ",
      "`target_range` is not missing\nEither both or neither ",
      "argument should exist"
    )
  )
})

Try the admiral package in your browser

Any scripts or data that you put into this service are public.

admiral documentation built on Oct. 19, 2023, 1:08 a.m.