Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for edge case when likelihood is infinite when profiling #72

Merged
merged 2 commits into from
Nov 9, 2023
Merged
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
13 changes: 7 additions & 6 deletions R/ModelFit.R
Original file line number Diff line number Diff line change
Expand Up @@ -937,21 +937,22 @@ getCyclopsProfileLogLikelihood <- function(object,
while (length(grid) != 0) {
ll <- fixedGridProfileLogLikelihood(object, parm, grid, includePenalty)
profile <- bind_rows(profile, ll) %>% arrange(.data$point)

if (any(is.nan(profile$value))) {
if (all(is.nan(profile$value))) {
invalid <- is.nan(profile$value) | is.infinite(profile$value)
if (any(invalid)) {
if (all(invalid)) {
warning("Failing to compute likelihood at entire initial grid.")
return(NULL)
}

start <- min(which(!is.nan(profile$value)))
end <- max(which(!is.nan(profile$value)))
start <- min(which(!invalid))
end <- max(which(!invalid))
if (start == end) {
warning("Failing to compute likelihood at entire grid except one. Giving up")
return(NULL)
}
profile <- profile[start:end, ]
if (any(is.nan(profile$value))) {
invalid <- invalid[start:end]
if (any(invalid)) {
warning("Failing to compute likelihood in non-extreme regions. Giving up.")
return(NULL)
}
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-profileLikelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})