From 652762090fba0a5cdf6e0f0c6ddbcd0fa744f83c Mon Sep 17 00:00:00 2001
From: Admin_mschuemi <Admin_mschuemi@its.jnj.com>
Date: Thu, 9 Nov 2023 02:58:49 -0500
Subject: [PATCH] Adding unit tests for check for infinite (and NaN) likelihood
 values

---
 tests/testthat/test-profileLikelihood.R | 28 +++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/tests/testthat/test-profileLikelihood.R b/tests/testthat/test-profileLikelihood.R
index 1716d4d1..683cdb69 100644
--- a/tests/testthat/test-profileLikelihood.R
+++ b/tests/testthat/test-profileLikelihood.R
@@ -97,3 +97,31 @@ start, length, event, x1, x2
     expect_equal(coef(fit)["x1"], argMax)
 })
 
+test_that("Check adapative profiling likelihood, other covariate is perfect predictor", {
+
+    test <- read.table(header=T, sep = ",", text = "
+start, length, event, x1, x2
+0, 4,  1,0,1
+0, 4,1,2,1
+0, 4,  0,0,0
+0, 4,1,0,1
+0, 4,  1,1,1
+0, 4,0,1,0
+0, 4,  1,1,1
+")
+    data <- createCyclopsData(event ~ x1 + x2, data = test,
+                              modelType = "cpr")
+
+    # Recreating combination of circumstances that lead to infinite log likelihood
+    # Note: infinite variance would have been selected by cross-validation if we had enough data:
+    fit <- fitCyclopsModel(data,
+                           prior = createPrior("laplace", variance = Inf, exclude = "x1"))
+    expect_warning(getCyclopsProfileLogLikelihood(fit, "x1", bounds = c(0, 2), initialGridSize = 10),
+                   "Failing to compute likelihood at entire initial grid")
+
+    # This just produces NaN likelihood:
+    fit <- fitCyclopsModel(data)
+    expect_warning(getCyclopsProfileLogLikelihood(fit, "x1", bounds = c(0, 2), initialGridSize = 10),
+                   "Failing to compute likelihood at entire initial grid")
+})
+