tests/testthat/test-all.R

test_that("Data generation with seed produces identical samples across runs", {
  d1 <- gen_type1_hybrid(
    pdf = function(x) dexp(x, rate = 1), cdf = function(x) pexp(x, rate = 1),
    lower = 0, upper = 10, n = 20, r = 10, T_star = 1.5, seed = 42
  )
  d2 <- gen_type1_hybrid(
    pdf = function(x) dexp(x, rate = 1), cdf = function(x) pexp(x, rate = 1),
    lower = 0, upper = 10, n = 20, r = 10, T_star = 1.5, seed = 42
  )
  expect_equal(d1$observed_times, d2$observed_times)
  expect_equal(d1$censor_status, d2$censor_status)
})

test_that("All 7 MLE optimization algorithms execute cleanly without NAs or NaNs", {
  dat <- gen_type1_hybrid(
    pdf = function(x) dexp(x, rate = 1.2), cdf = function(x) pexp(x, rate = 1.2),
    lower = 0, upper = 10, n = 25, r = 15, T_star = 1.8, seed = 99
  )
  pdf_fn <- function(x, th) dexp(x, rate = th[1])
  cdf_fn <- function(x, th) pexp(x, rate = th[1])
  
  methods <- c("NR", "BFGS", "BFGSR", "BHHH", "SANN", "CG", "NM")
  for (m in methods) {
    fit <- mle_type1_hybrid(dat, pdf = pdf_fn, cdf = cdf_fn, init_par = c(0.8), method = m)
    expect_true(is.finite(fit$loglik))
    expect_false(any(is.na(fit$coefficients)))
    expect_false(any(is.nan(fit$coefficients)))
  }
})

test_that("Bayesian, Importance Sampling, and Lindley methods execute cleanly", {
  log_post <- function(th) {
    if (th[1] <= 0) return(-Inf)
    dexp(th[1], rate = 1, log = TRUE) + sum(dexp(c(0.5, 1.2, 0.8), rate = th[1], log = TRUE))
  }
  
  fit_g <- bayes_gibbs_censored(log_post, init_par = c(1.0), n_sim = 500, burn_in = 100)
  expect_false(any(is.na(fit_g$post_means)))
  
  fit_mh <- bayes_mh_censored(log_post, init_par = c(1.0), n_sim = 500, burn_in = 100)
  expect_false(any(is.na(fit_mh$post_means)))
  
  fit_is <- importance_sampling_censored(
    log_target = function(th) dexp(th, rate = 2, log = TRUE),
    proposal_pdf = function(th) dexp(th, rate = 1),
    rproposal = function(n) rexp(n, rate = 1),
    n_sim = 500
  )
  expect_false(any(is.na(fit_is$post_means)))
  
  fit_lind <- lindley_approx_censored(
    log_lik = function(th) -sum((c(1.2, 0.8, 1.5) - th[1])^2),
    log_prior = function(th) dexp(th[1], rate = 1, log = TRUE),
    init_par = c(1.1)
  )
  expect_false(any(is.na(fit_lind$post_means)))
})

test_that("Stress-Strength reliability model executes correctly", {
  ss_dat <- gen_stress_strength(
    pdf_X = function(x) dexp(x, rate = 1.2), cdf_X = function(x) pexp(x, rate = 1.2),
    pdf_Y = function(y) dexp(y, rate = 0.8), cdf_Y = function(y) pexp(y, rate = 0.8),
    lower_X = 0, upper_X = 10, lower_Y = 0, upper_Y = 10,
    n_X = 20, n_Y = 20, censoring_type = "type1_hybrid",
    r_X = 12, T_X = 1.2, r_Y = 12, T_Y = 1.5, seed = 123
  )
  fit_ss <- stress_strength_rel(
    data_X = ss_dat$data_X, data_Y = ss_dat$data_Y,
    pdf_X = function(x, th) dexp(x, rate = th[1]), cdf_X = function(x, th) pexp(x, rate = th[1]),
    pdf_Y = function(y, th) dexp(y, rate = th[1]), cdf_Y = function(y, th) pexp(y, rate = th[1]),
    init_par_X = c(1.0), init_par_Y = c(0.7), method = "BFGS"
  )
  expect_true(fit_ss$R_hat >= 0 && fit_ss$R_hat <= 1)
  expect_false(is.na(fit_ss$R_hat))
})

test_that("Optimal designs and Reliability Acceptance Sampling Plans work", {
  opt <- optimal_design_hybrid(
    n = 20, r_candidates = c(5, 10, 15), T_candidates = c(0.5, 1.0, 1.5),
    pdf = function(x, th) dexp(x, rate = th[1]), cdf = function(x, th) pexp(x, rate = th[1]),
    par = c(1.0), criterion = "D-optimal"
  )
  expect_true(opt$optimal_r %in% c(5, 10, 15))
  
  plan_exp <- sampling_plan_exponential(n = 20, T_star = 1.0, r = 10, alpha = 0.05, beta = 0.10, theta0 = 2.0, theta1 = 0.8)
  expect_true(is.numeric(plan_exp$critical_value))
  
  plan_weib <- sampling_plan_weibull(n = 25, T_star = 1.5, r = 12, beta_shape = 1.5, alpha = 0.05, beta = 0.10, theta0 = 3.0, theta1 = 1.0)
  expect_true(is.numeric(plan_weib$critical_value))
  
  plan_bayes <- bayesian_sampling_plan_exp(n = 20, T_star = 1.0, r = 10, prior_shape = 2, prior_rate = 1, c_accept = 1.2)
  expect_true(plan_bayes$posterior_acceptance_prob >= 0 && plan_bayes$posterior_acceptance_prob <= 1)
})

Try the CompRiskRel package in your browser

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

CompRiskRel documentation built on Aug. 5, 2026, 9:08 a.m.