tests/testthat/test_print_mfp2_coefficients.R

data("prostate", package = "mfp2")
# These tests target coefficient extraction and printing. Low-information
# behavior is tested separately, so affected fixture fits opt out explicitly;
# other warning types remain visible to testthat.
x_prostate <- as.matrix(prostate[, 2:8])
y_prostate <- as.numeric(prostate$lpsa)

extract_print_section <- function(output, heading, next_heading) {
  normalized <- trimws(output)
  start <- match(heading, normalized)
  if (is.na(start)) {
    stop(sprintf("Print output is missing the '%s' heading.", heading), call. = FALSE)
  }

  following <- if (start < length(output)) {
    seq.int(start + 1L, length(output))
  } else {
    integer(0L)
  }
  next_relative <- match(next_heading, normalized[following])
  if (is.na(next_relative)) {
    stop(sprintf("Print output is missing the '%s' heading.", next_heading), call. = FALSE)
  }

  end <- following[[next_relative]] - 1L
  output[seq.int(start, end)]
}

make_design_info_object <- function(variables,
                                    components,
                                    powers,
                                    centers = NULL,
                                    zero_handled = NULL,
                                    centered = NULL,
                                    transformations = NULL,
                                    term_to_columns = NULL,
                                    formula_factor_info = NULL) {
  transformed_columns <- paste0("internal_basis_", seq_along(variables))
  model_columns <- paste0("internal_coef_", seq_along(variables))

  if (is.null(zero_handled)) {
    zero_handled <- rep(FALSE, length(variables))
  }
  if (is.null(centered)) {
    centered <- rep(!is.null(centers), length(variables))
  }
  if (is.null(term_to_columns)) {
    term_to_columns <- stats::setNames(
      lapply(unique(variables), identity),
      unique(variables)
    )
  }

  fp_rows <- unique(unname(unlist(term_to_columns, use.names = FALSE)))
  fp_rows <- intersect(fp_rows, names(powers))
  fp_terms <- do.call(rbind, lapply(fp_rows, function(v) {
    p <- powers[[v]]
    length(p) <- max(2L, length(p))
    data.frame(
      selected = TRUE,
      df_final = sum(!is.na(p)),
      power1 = p[[1L]],
      power2 = p[[2L]],
      acd = FALSE,
      zero = FALSE,
      catzero = FALSE,
      spike = FALSE,
      row.names = v,
      check.names = FALSE
    )
  }))

  if (is.null(transformations)) {
    transformations <- data.frame(
      shift = rep(0, nrow(fp_terms)),
      scale = rep(1, nrow(fp_terms)),
      center = rep(!is.null(centers), nrow(fp_terms)),
      row.names = rownames(fp_terms)
    )
  }

  list(
    fp_terms = fp_terms,
    transformations = transformations,
    term_to_columns = term_to_columns,
    transformed_to_model_columns = stats::setNames(
      model_columns,
      transformed_columns
    ),
    transformed_column_to_source = stats::setNames(
      variables,
      transformed_columns
    ),
    transformed_column_component = stats::setNames(
      components,
      transformed_columns
    ),
    transformed_column_zero_handled = stats::setNames(
      zero_handled,
      transformed_columns
    ),
    transformed_column_centered = stats::setNames(
      centered,
      transformed_columns
    ),
    centers = if (is.null(centers)) NULL else stats::setNames(
      centers,
      transformed_columns
    ),
    formula_factor_info = formula_factor_info
  )
}


test_that("ordinary FP basis labels use stored mappings and shifts", {
  object <- make_design_info_object(
    variables = c("age", "age"),
    components = c("fp_basis", "fp_basis"),
    powers = list(age = c(3, 3)),
    centers = c(0.0247, -0.0813),
    transformations = data.frame(
      shift = 3,
      scale = 1,
      center = TRUE,
      row.names = "age"
    )
  )

  info <- mfp2_design_column_info(object)

  expect_identical(info$variable, c("age", "age"))
  expect_identical(
    info$transformed_column,
    c("internal_basis_1", "internal_basis_2")
  )
  expect_identical(
    info$model_column,
    c("internal_coef_1", "internal_coef_2")
  )
  expect_identical(
    info$basis,
    c("(age + 3)^3", "(age + 3)^3 * log(age + 3)")
  )
  expect_equal(info$center, c(0.0247, -0.0813))
})


test_that("ACD columns retain separate FP and ACD basis labels", {
  object <- make_design_info_object(
    variables = c("age", "age"),
    components = c("fp_basis", "acd_basis"),
    powers = list(age = c(1, 0.5)),
    centers = c(1.2, 0.4)
  )
  object$fp_terms["age", "acd"] <- TRUE

  info <- mfp2_design_column_info(object)

  expect_identical(info$basis, c("age", "A(age)^0.5"))
})


test_that("numeric binary variables are displayed as the original variable", {
  object <- make_design_info_object(
    variables = "sex",
    components = "identity_binary",
    powers = list(sex = 1),
    centers = 1
  )

  info <- mfp2_design_column_info(object)

  expect_identical(info$variable, "sex")
  expect_identical(info$basis, "sex")
  expect_equal(info$center, 1)
})


test_that("transform metadata marks unchanged numeric binary columns explicitly", {
  x <- matrix(c(1, 2, 1, 2), ncol = 1L, dimnames = list(NULL, "sex"))
  transformed <- transform_matrix(
    x = x,
    power_list = list(sex = 1),
    center = c(sex = TRUE),
    acdx = c(sex = FALSE),
    zero = c(sex = FALSE),
    catzero = c(sex = FALSE),
    spike = c(sex = FALSE)
  )

  expect_identical(
    unname(transformed$transformed_column_component),
    "identity_binary"
  )
  expect_true(unname(transformed$transformed_column_centered))
})


test_that("zero-handled FP components display their positive-part basis", {
  object <- make_design_info_object(
    variables = "age",
    components = "fp_basis",
    powers = list(age = 0.5),
    centers = 1.83,
    zero_handled = TRUE,
    transformations = data.frame(
      shift = 0,
      scale = 1,
      center = TRUE,
      row.names = "age"
    )
  )

  info <- mfp2_design_column_info(object)

  expect_identical(info$basis, "I(age > 0) * age^0.5")
  expect_true(info$zero_handled)
  expect_equal(info$center, 1.83)
})


test_that("catzero and spike component sets describe the fitted columns", {
  x <- matrix(c(0, 0.5, 1, 4), ncol = 1L, dimnames = list(NULL, "age"))
  common <- list(
    x = x,
    power_list = list(age = 0.5),
    center = c(age = TRUE),
    acdx = c(age = FALSE),
    zero = c(age = TRUE),
    reset_zero = FALSE
  )

  zero_fit <- do.call(
    transform_matrix,
    c(common, list(catzero = c(age = FALSE), spike = c(age = FALSE)))
  )
  expect_identical(unname(zero_fit$transformed_column_component), "fp_basis")
  expect_true(unname(zero_fit$transformed_column_zero_handled))

  catzero_fit <- do.call(
    transform_matrix,
    c(common, list(catzero = c(age = TRUE), spike = c(age = FALSE)))
  )
  expect_identical(
    unname(catzero_fit$transformed_column_component),
    c("fp_basis", "zero_indicator")
  )

  spike_1 <- do.call(
    transform_matrix,
    c(common, list(
      catzero = c(age = TRUE),
      spike = c(age = TRUE),
      spike_decision = c(age = saz_decision_codes[["cont_binary"]])
    ))
  )
  expect_identical(
    unname(spike_1$transformed_column_component),
    c("fp_basis", "zero_indicator")
  )

  spike_2 <- do.call(
    transform_matrix,
    c(common, list(
      catzero = c(age = TRUE),
      spike = c(age = TRUE),
      spike_decision = c(age = saz_decision_codes[["continuous_only"]])
    ))
  )
  expect_identical(
    unname(spike_2$transformed_column_component),
    "fp_basis"
  )

  spike_3 <- do.call(
    transform_matrix,
    c(common, list(
      catzero = c(age = TRUE),
      spike = c(age = TRUE),
      spike_decision = c(age = saz_decision_codes[["binary_only"]])
    ))
  )
  expect_identical(
    unname(spike_3$transformed_column_component),
    "zero_indicator"
  )
})


test_that("zero indicator columns display the structural-zero condition", {
  object <- make_design_info_object(
    variables = c("age", "age"),
    components = c("fp_basis", "zero_indicator"),
    powers = list(age = 0.5),
    centers = c(1.83, 0),
    zero_handled = c(TRUE, FALSE),
    centered = c(TRUE, TRUE)
  )

  info <- mfp2_design_column_info(object)

  expect_identical(
    info$basis,
    c("I(age > 0) * age^0.5", "I(age = 0)")
  )
})


test_that("factor treatment coding is described from design_by_level", {
  term_to_columns <- list(group = c("raw_design_b", "raw_design_c"))
  factor_info <- list(
    group = list(
      variable = "group",
      levels = c("A", "B", "C"),
      ordered = FALSE,
      columns = c("raw_design_b", "raw_design_c"),
      design_by_level = matrix(
        c(
          0, 0,
          1, 0,
          0, 1
        ),
        nrow = 3L,
        byrow = TRUE,
        dimnames = list(
          c("A", "B", "C"),
          c("raw_design_b", "raw_design_c")
        )
      )
    )
  )

  object <- make_design_info_object(
    variables = c("raw_design_b", "raw_design_c"),
    components = c("identity_binary", "identity_binary"),
    powers = list(raw_design_b = 1, raw_design_c = 1),
    centers = c(0, 0),
    term_to_columns = term_to_columns,
    formula_factor_info = factor_info
  )
  object$fp_terms <- data.frame(
    selected = TRUE,
    df_final = 2,
    power1 = 1,
    power2 = NA_real_,
    acd = FALSE,
    zero = FALSE,
    catzero = FALSE,
    spike = FALSE,
    row.names = "group",
    check.names = FALSE
  )
  object$transformations <- data.frame(
    shift = 0,
    scale = 1,
    center = TRUE,
    row.names = "group"
  )

  info <- mfp2_design_column_info(object)

  expect_identical(info$variable, c("group", "group"))
  expect_identical(
    info$basis,
    c('I(group = "B")', 'I(group = "C")')
  )
})


test_that("custom factor contrasts retain their model-matrix column names", {
  factor_info <- list(
    group = list(
      variable = "group",
      levels = c("A", "B", "C"),
      ordered = FALSE,
      columns = c("contrast_1", "contrast_2"),
      design_by_level = matrix(
        c(
          -1, 0,
          0, -1,
          1, 1
        ),
        nrow = 3L,
        byrow = TRUE,
        dimnames = list(
          c("A", "B", "C"),
          c("contrast_1", "contrast_2")
        )
      )
    )
  )

  object <- make_design_info_object(
    variables = c("contrast_1", "contrast_2"),
    components = c("fp_basis", "fp_basis"),
    powers = list(contrast_1 = 1, contrast_2 = 1),
    centers = c(0, 0),
    term_to_columns = list(group = c("contrast_1", "contrast_2")),
    formula_factor_info = factor_info
  )
  object$fp_terms <- data.frame(
    selected = TRUE,
    df_final = 2,
    power1 = 1,
    power2 = NA_real_,
    acd = FALSE,
    zero = FALSE,
    catzero = FALSE,
    spike = FALSE,
    row.names = "group",
    check.names = FALSE
  )
  object$transformations <- data.frame(
    shift = 0,
    scale = 1,
    center = TRUE,
    row.names = "group"
  )

  info <- mfp2_design_column_info(object)

  expect_identical(
    info$basis,
    c("contrast_1", "contrast_2")
  )
})


test_that("ordered factor coefficients use familiar polynomial contrast names", {
  factor_info <- list(
    x4 = list(
      variable = "x4",
      levels = c("1", "2", "3"),
      ordered = TRUE,
      columns = c("x4.L", "x4.Q"),
      design_by_level = stats::contr.poly(3L)
    )
  )
  dimnames(factor_info$x4$design_by_level) <- list(
    c("1", "2", "3"),
    c("x4.L", "x4.Q")
  )

  object <- make_design_info_object(
    variables = c("x4.L", "x4.Q"),
    components = c("fp_basis", "fp_basis"),
    powers = list("x4.L" = 1, "x4.Q" = 1),
    centers = c(0.07354, 0.02),
    term_to_columns = list(x4 = c("x4.L", "x4.Q")),
    formula_factor_info = factor_info
  )
  object$fp_terms <- data.frame(
    selected = TRUE,
    df_final = 2,
    power1 = 1,
    power2 = NA_real_,
    acd = FALSE,
    zero = FALSE,
    catzero = FALSE,
    spike = FALSE,
    row.names = "x4",
    check.names = FALSE
  )
  object$transformations <- data.frame(
    shift = 0,
    scale = 1,
    center = TRUE,
    row.names = "x4"
  )

  info <- mfp2_design_column_info(object)

  expect_identical(info$variable, c("x4", "x4"))
  expect_identical(info$basis, c("x4.L", "x4.Q"))
  expect_equal(info$center, c(0.07354, 0.02))
})


test_that("print.mfp2 shows basis and center when centering is used", {
  fit <- mfp2(x_prostate, y_prostate, center = TRUE, verbose = FALSE)
  output <- capture.output(print(fit, detailed_settings = FALSE))

  coefficient_lines <- extract_print_section(
    output,
    heading = "Final Model Coefficients",
    next_heading = "Model Fit"
  )
  coefficient_output <- paste(coefficient_lines, collapse = "\n")

  expect_match(coefficient_output, "Estimates are for: Basis - Center", fixed = TRUE)
  expect_false(grepl("within the positive component", coefficient_output, fixed = TRUE))
  expect_match(coefficient_output, "Variable", fixed = TRUE)
  expect_match(coefficient_output, "Basis", fixed = TRUE)
  expect_match(coefficient_output, "Center", fixed = TRUE)
  expect_match(coefficient_output, "Estimate", fixed = TRUE)
  expect_match(coefficient_output, "SE", fixed = TRUE)
  expect_false(grepl("Std. Error", coefficient_output, fixed = TRUE))
  expect_match(coefficient_output, "(Intercept)", fixed = TRUE)
  expect_false(grepl("\\b[A-Za-z][A-Za-z0-9_]*\\.[0-9]+\\b", coefficient_output))
})


test_that("print.mfp2 reports ordinary centering for zero-handled terms", {
  set.seed(73103)
  dat <- data.frame(
    y = stats::rnorm(180),
    x = sample(c(0, 0, 0, 1:8), 180, replace = TRUE)
  )

  fit <- mfp2(
    y ~ fp(x, df = 2, select = 1, zero = TRUE),
    data = dat,
    center = TRUE,
    verbose = FALSE
  )
  output <- paste(
    capture.output(print(fit, detailed_settings = FALSE)),
    collapse = "\n"
  )

  expect_match(output, "Estimates are for: Basis - Center", fixed = TRUE)
  expect_false(grepl("positive part for zero-handled terms", output, fixed = TRUE))
  expect_match(output, "I(x > 0)", fixed = TRUE)
})


test_that("print.mfp2 omits centering output when centering is not used", {
  fit <- mfp2(x_prostate, y_prostate, center = FALSE, verbose = FALSE)
  output <- capture.output(print(fit, detailed_settings = FALSE))

  coefficient_lines <- extract_print_section(
    output,
    heading = "Final Model Coefficients",
    next_heading = "Model Fit"
  )
  coefficient_output <- paste(coefficient_lines, collapse = "\n")

  expect_match(coefficient_output, "Variable", fixed = TRUE)
  expect_match(coefficient_output, "Basis", fixed = TRUE)
  expect_match(coefficient_output, "Estimate", fixed = TRUE)
  expect_match(coefficient_output, "SE", fixed = TRUE)
  expect_false(grepl("Std. Error", coefficient_output, fixed = TRUE))
  expect_false(grepl("Estimates are for: Basis - Center", coefficient_output, fixed = TRUE))
  expect_false(grepl("Center", coefficient_output, fixed = TRUE))
})



test_that("print.mfp2 standard errors come from the final covariance matrix", {
  fit <- mfp2(x_prostate, y_prostate, center = TRUE, verbose = FALSE)
  covariance <- stats::vcov(fit)
  expected_se <- sqrt(diag(covariance))

  output <- capture.output(
    print(fit, detailed_settings = FALSE, digits = 6)
  )
  coefficient_lines <- extract_print_section(
    output,
    heading = "Final Model Coefficients",
    next_heading = "Model Fit"
  )

  intercept_line <- coefficient_lines[
    grepl("(Intercept)", coefficient_lines, fixed = TRUE)
  ]
  expect_length(intercept_line, 1L)
  expect_match(
    intercept_line,
    formatC(expected_se[["(Intercept)"]], format = "f", digits = 6L),
    fixed = TRUE
  )
})

test_that("coef.mfp2 keeps fitted coefficient names", {
  fit <- mfp2(x_prostate, y_prostate, center = TRUE, verbose = FALSE)
  coefficient_names <- names(stats::coef(fit))

  expect_identical(coefficient_names, names(fit$coefficients))
  expect_true(any(grepl("\\.[0-9]+$", coefficient_names)))
})


test_that("print.mfp2 leaves intercept centers blank and formats exact zero", {
  set.seed(73104)
  n <- 240
  exposure <- c(rep(0, 60), stats::rgamma(n - 60, shape = 2, rate = 1))
  dat <- data.frame(
    y = 1.2 * exposure^0.5 +
      0.8 * (exposure == 0) + stats::rnorm(n, sd = 0.5),
    exposure = exposure
  )

  fit <- mfp2(
    y ~ fp(exposure, df = 2, select = 1, catzero = TRUE),
    data = dat,
    center = TRUE,
    verbose = FALSE
  )

  output <- capture.output(print(fit, detailed_settings = FALSE))
  coefficient_lines <- extract_print_section(
    output,
    heading = "Final Model Coefficients",
    next_heading = "Model Fit"
  )
  coefficient_output <- paste(coefficient_lines, collapse = "\n")

  intercept_line <- coefficient_lines[grepl("(Intercept)", coefficient_lines, fixed = TRUE)]
  expect_length(intercept_line, 1L)
  expect_false(grepl("\\bNA\\b", intercept_line))

  indicator_line <- coefficient_lines[
    grepl("I(exposure = 0)", coefficient_lines, fixed = TRUE)
  ]
  expect_length(indicator_line, 1L)
  expect_match(
    indicator_line,
    "I\\(exposure = 0\\)\\s+0\\.000\\s+[-+0-9]",
    perl = TRUE
  )

  expect_false(grepl("\\bNA\\b", coefficient_output))
})


# An intercept-only selected model has no transformed predictor metadata.
# Printing must still render the intercept rather than treating the empty
# final design as incomplete metadata.
test_that("print.mfp2 handles an intercept-only final model", {
  set.seed(2406)
  n <- 160L
  dat <- data.frame(
    y = rnorm(n),
    x = runif(n, 1, 10)
  )

  fit <- mfp2(
    y ~ x,
    data = dat,
    select = 0,
    verbose = FALSE
  )

  expect_length(get_selected_variable_names(fit), 0L)

  design_info <- mfp2_design_column_info(fit)
  expect_s3_class(design_info, "data.frame")
  expect_equal(nrow(design_info), 0L)
  expect_identical(
    names(design_info),
    c(
      "variable", "transformed_column", "model_column", "basis",
      "center", "centered", "zero_handled", "component"
    )
  )

  output <- capture.output(print(fit))
  coefficient_section <- extract_print_section(
    output,
    "Final Model Coefficients",
    "Model Fit"
  )

  expect_true(any(grepl("\\(Intercept\\)", coefficient_section)))
  expect_false(any(grepl("Final design-column metadata is incomplete", output)))
})

# An empty design map is valid only for a model with no fitted predictor
# coefficients. Do not silently omit predictor coefficients when metadata are
# genuinely incomplete.
test_that("mfp2_design_column_info rejects an empty map with predictor coefficients", {
  object <- list(
    coefficients = c("(Intercept)" = 1, x = 0.5),
    transformed_to_model_columns = stats::setNames(character(0L), character(0L)),
    transformed_column_to_source = NULL,
    transformed_column_component = NULL,
    transformed_column_zero_handled = NULL,
    transformed_column_centered = NULL,
    term_to_columns = list(x = "x")
  )

  expect_error(
    mfp2_design_column_info(object),
    "Final design-column metadata is incomplete"
  )
})


# Migrated coverage from the former test_mfp2.R

# Test purpose: Checks that coef() returns named numeric coefficients.
test_that("coef.mfp2() returns named numeric vector", {
  fit <- mfp2(x_prostate, y_prostate, verbose = FALSE)
  cf <- coef(fit)
  expect_true(is.numeric(cf))
  expect_true(!is.null(names(cf)))
})


# Test purpose: Checks that the print method produces console output without error.
test_that("print.mfp2() runs without error", {
  fit <- mfp2(x_prostate, y_prostate, verbose = FALSE)
  expect_output(print(fit))
})


# Test purpose: Ensures grouped terms print their actual initial and final
# model degrees of freedom without exposing the internal FP search setting.
test_that("print.mfp2() reports grouped initial and final df only", {
  set.seed(91501)
  n <- 120L
  group <- factor(rep(c("A", "B", "C"), length.out = n))
  group_mm <- stats::model.matrix(~ group)[, -1L, drop = FALSE]
  x <- cbind(x1 = stats::runif(n, 1, 8), group_mm)
  y <- 0.4 * x[, "x1"] + 0.8 * group_mm[, 1L] -
    0.5 * group_mm[, 2L] + stats::rnorm(n, sd = 0.3)

  fit <- mfp2(
    x, y,
    term_groups = list(group = colnames(group_mm)),
    keep = "group",
    verbose = FALSE
  )

  expect_equal(as.numeric(fit$fp_terms["group", "df_setting"]), 1)
  expect_equal(as.numeric(fit$fp_terms["group", "df_initial"]), 2)
  expect_equal(as.numeric(fit$fp_terms["group", "df_final"]), 2)

  output <- capture.output(print(fit, detailed_settings = TRUE, notes = TRUE))
  printed <- paste(output, collapse = "\n")
  expect_match(printed, "df (init->final)", fixed = TRUE)
  expect_match(printed, "2 -> 2", fixed = TRUE)
  expect_false(grepl("df setting", printed, fixed = TRUE))
  expect_false(grepl("df_setting", printed, fixed = TRUE))
})


# Test purpose: Ensures an SAZ-only fit does not print an empty Standard MFP
# data frame in the function-selection summary.
test_that("print.mfp2() omits an empty Standard MFP subsection", {
  fit <- mfp2(
    lpsa ~ fp(
      pgg45,
      df = 4,
      select = 0.05,
      alpha = 0.05,
      spike = TRUE
    ),
    data = prostate,
    family = "gaussian",
    criterion = "pvalue",
    verbose = FALSE
  )

  output <- capture.output(print(fit, detailed_settings = FALSE))

  expect_false(any(output == "Standard MFP"))
  expect_false(any(grepl("<0 rows>", output, fixed = TRUE)))
  expect_true(any(grepl(
    paste0(
      "Spike-at-Zero (SAZ; x is transformed only for x > 0, ",
      "binary is I(x = 0))"
    ),
    output,
    fixed = TRUE
  )))
})


# Test purpose: Ensures retained SAZ terms store and print the structural-zero
# proportion from the actual fitting sample.
test_that("SAZ metadata and print output include prop_zero", {
  set.seed(91502)
  n <- 100L
  exposure <- c(rep(0, 25L), stats::runif(75L, 0.5, 6))
  exposure <- sample(exposure)
  x <- cbind(exposure = exposure, age = stats::runif(n, 20, 70))
  y <- 1.2 * (exposure == 0) + 0.25 * exposure +
    0.01 * x[, "age"] + stats::rnorm(n, sd = 0.25)

  fit <- mfp2(
    x, y,
    df = c(exposure = 1),
    spike_vars = "exposure",
    keep = "exposure",
    verbose = FALSE
  )

  expect_equal(fit$fp_terms["exposure", "prop_zero"], 0.25)
  expect_true(is.na(fit$fp_terms["age", "prop_zero"]))

  output <- capture.output(
    print(fit, detailed_settings = FALSE, digits = 3L)
  )
  printed <- paste(output, collapse = "\n")

  expect_match(printed, "prop_zero", fixed = TRUE)
  expect_match(printed, "0.250", fixed = TRUE)

  saz_header <- output[grepl("prop_zero", output, fixed = TRUE)][1L]
  expect_false(grepl("Selected", saz_header, fixed = TRUE))
})

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.