tests/testthat/helper-comparisons.R

# Helper function to compare result and expected tables with proper handling of p-value column.
expect_equal_result_tables <- function(result,
                                       expected,
                                       tol = 0.001,
                                       pval_name = "Pr(>|t|)",
                                       pval_threshold = 0.0001) {
  pval_col <- match(pval_name, colnames(result))

  # Compare first non-pvalue columns.
  expect_equal(
    result[, -pval_col],
    expected[, -pval_col],
    tolerance = tol
  )

  # Then compare p-values which are not below the threshold in the expected table.
  exp_pval_is_below_thresh <- expected[, pval_col] < pval_threshold
  expect_equal(
    result[, pval_col][!exp_pval_is_below_thresh],
    expected[, pval_col][!exp_pval_is_below_thresh],
    tolerance = tol
  )

  # Now expect that the same p-values are below the thresholds in both tables.
  res_pval_is_below_thresh <- result[, pval_col] < pval_threshold
  expect_identical(
    exp_pval_is_below_thresh,
    res_pval_is_below_thresh
  )
}

Try the tern.mmrm package in your browser

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

tern.mmrm documentation built on June 22, 2024, 9:38 a.m.