tests/testthat/test_mfpi_create_variables_validation.R

library(testthat)
library(mfp2)


test_that("create_z_variables rejects a constant continuous covariate", {
  cont_var <- matrix(
    rep(5, 6),
    ncol = 1L,
    dimnames = list(NULL, "age")
  )
  group_var <- matrix(
    rep(c(0, 1), each = 3L),
    ncol = 1L,
    dimnames = list(NULL, "treatment")
  )

  # Centering would turn the constant FP basis into exact zero columns.
  expect_error(
    mfp2:::create_z_variables(
      cont_var = cont_var,
      group_var = group_var,
      power = 1,
      center = TRUE
    ),
    "must contain at least two distinct values",
    fixed = TRUE
  )

  # Without centering, each constant group-specific column is a scaled group
  # indicator and remains unidentifiable. The guard is therefore unconditional.
  expect_error(
    mfp2:::create_z_variables(
      cont_var = cont_var,
      group_var = group_var,
      power = 1,
      center = FALSE
    ),
    "must contain at least two distinct values",
    fixed = TRUE
  )
})


test_that("create_z_variables retains nonconstant continuous input", {
  cont_var <- matrix(
    c(1, 2, 3, 2, 3, 4),
    ncol = 1L,
    dimnames = list(NULL, "age")
  )
  group_var <- matrix(
    rep(c(0, 1), each = 3L),
    ncol = 1L,
    dimnames = list(NULL, "treatment")
  )

  result <- expect_silent(
    mfp2:::create_z_variables(
      cont_var = cont_var,
      group_var = group_var,
      power = 1,
      center = TRUE
    )
  )

  expect_equal(nrow(result$z), nrow(cont_var))
  expect_false(anyNA(result$z))
  expect_true(all(is.finite(result$z)))
})

Try the mfp2 package in your browser

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

mfp2 documentation built on Nov. 14, 2026, 1:06 a.m.