Skip to content
Draft
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
8 changes: 6 additions & 2 deletions R/llcont.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,16 @@ llcont.glm <- function(x, ...){
if(is.matrix(y)) {
## Bolt: replaced apply(..., 1, sum) with optimized rowSums() for performance
n <- rowSums(y)
y <- ifelse(n == 0, 0, y[, 1]/n)
## Bolt: replaced ifelse with preallocation and vectorized subsetting for performance
y <- y[, 1]/n
y[n == 0] <- 0
} else {
n <- rep.int(1, length(y))
}
m <- if (any(n > 1)) n else wt
wt <- ifelse(m > 0, (wt/m), 0)
## Bolt: replaced ifelse with preallocation and vectorized subsetting for performance
wt <- wt / m
wt[m <= 0] <- 0
Comment on lines +57 to +65

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Zero denominators remain contained

Zero-trial and zero-weight rows create transient NaN values. The indexed assignments replace them before likelihood evaluation, preserving prior results.

Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

dbinom(round(m * y), round(m), mpreds, log = TRUE) * wt
},
quasibinomial = {
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test_llcont_glm_binomial_zero_contract.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
test_that("glm binomial zero totals and zero prior weights preserve likelihood contributions", {
observed <- data.frame(
success = c(0, 1, 2, 3, 1, 4, 2),
failure = c(0, 4, 3, 2, 4, 1, 3),
x = 0:6,
prior_weight = c(1, 1, 1, 0, 1, 1, 1)
)

fit <- glm(
cbind(success, failure) ~ x,
data = observed,
family = binomial(),
weights = prior_weight
)
contributions <- llcont(fit)

expect_true(all(is.finite(contributions)))
expect_equal(contributions[c(1, 4)], c(0, 0))
expect_equal(sum(contributions), as.numeric(logLik(fit)))
})
Loading