Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions tests/testthat/test-boundary-detection-enhanced.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ test_that("boundary detection handles large coefficients", {
test_that("boundary detection handles large standard errors", {
# Create data designed to have large SEs WITHOUT boundary issues
set.seed(789)
n <- 50 # Smaller sample for larger SEs
n <- 30 # Smaller sample for larger SEs
test_data_large_se <- data.frame(
outcome = rbinom(n, 1, 0.5), # Moderate probability, not near boundaries
exposure = factor(sample(c("No", "Yes"), n, replace = TRUE, prob = c(0.5, 0.5))),
confounder = rnorm(n)
confounder = rnorm(n),
x1 = rnorm(n),
x2 = rnorm(n),
x3 = rnorm(n),
x4 = rnorm(n) # Many confounders relative to sample size
)

# Ensure we're not creating boundary conditions
Expand All @@ -97,7 +101,7 @@ test_that("boundary detection handles large standard errors", {
data = test_data_large_se,
outcome = "outcome",
exposure = "exposure",
adjust_vars = "confounder" # Adjustment with small n creates large SEs
adjust_vars = c("confounder", "x1", "x2", "x3", "x4") # Many confounders relative to sample size
)

# The test should check for EITHER large SEs OR boundary near
Expand Down
35 changes: 7 additions & 28 deletions tests/testthat/test-calc-risk-diff.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,17 @@ create_convergence_challenge_data <- function(n = 500) {
# Create data that challenges model convergence
set.seed(456)

exposure <- factor(c(rep("No", n*0.8), rep("Yes", n*0.2)))
outcome_prob <- ifelse(exposure == "Yes", 0.85, 0.05)

data.frame(
id = 1:n,
# Create near-perfect separation scenario
exposure = factor(c(rep("No", n*0.8), rep("Yes", n*0.2))),
exposure = exposure,
confounder = c(rep(0, n*0.78), rep(1, n*0.02), rep(0, n*0.02), rep(1, n*0.18)),
# Outcome highly associated with exposure
outcome_prob = ifelse(exposure == "Yes", 0.85, 0.05),
outcome = rbinom(n, 1, outcome_prob)
) %>%
dplyr::select(-outcome_prob)
)
}

# Core functionality tests
Expand Down Expand Up @@ -603,8 +604,8 @@ test_that("calc_risk_diff shows expected sex differences in tobacco use patterns
male_result <- result_sex_strat[result_sex_strat$sex == "male", ]
female_result <- result_sex_strat[result_sex_strat$sex == "female", ]

if (!is.na(male_result$rd)) expect_true(male_result$rd > 0)
if (!is.na(female_result$rd)) expect_true(female_result$rd >= 0) # May be 0 if no exposure
if (nrow(male_result) > 0 && !is.na(male_result$rd)) expect_true(male_result$rd > 0)
if (nrow(female_result) > 0 && !is.na(female_result$rd)) expect_true(female_result$rd >= 0) # May be 0 if no exposure
})

# Verbose output testing
Expand All @@ -624,28 +625,6 @@ test_that("calc_risk_diff verbose mode works", {
)
})

# Integration with birthweight data
# =================================

test_that("calc_risk_diff works with package birthweight data", {
skip_if_not_installed("riskdiff")

# Use the included birthweight dataset
data(birthweight, package = "riskdiff", envir = environment())

result <- calc_risk_diff(
data = birthweight,
outcome = "low_birthweight",
exposure = "smoking"
)

expect_s3_class(result, "riskdiff_result")
expect_equal(nrow(result), 1)
expect_true(!is.na(result$rd))
expect_true(result$rd > 0) # Smoking should increase risk of low birth weight
expect_equal(result$exposure_var, "smoking")
})

# Format and print method tests
# =============================

Expand Down
Loading