From ffeaad8859f4550d5d695463400f83bfb4b953bd Mon Sep 17 00:00:00 2001 From: ***** <721466+soodoku@users.noreply.github.com> Date: Fri, 20 Nov 2026 22:01:14 -0800 Subject: [PATCH] Score complete response pairs by item and retain all observed counts --- NEWS.md | 4 +++ R/model-criticism.R | 29 +++++++-------------- tests/testthat/test-individual-validation.R | 17 ++++++++++++ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/NEWS.md b/NEWS.md index 77737c6..a047429 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # guess (development version) +* `score_individual_lca()` counts every observed item even when a response has + zero probability under the fitted model. It evaluates each item's response + probabilities once across respondents. + * The "Using guess" vignette now runs its examples. They had all been `eval = FALSE`, and eight had broken as the API changed, including both `lca_se()` examples. Its simulation sections no longer report coverage that diff --git a/R/model-criticism.R b/R/model-criticism.R index 3638f5f..5d456ba 100644 --- a/R/model-criticism.R +++ b/R/model-criticism.R @@ -227,25 +227,16 @@ individual_likelihood_details <- function( n_obs <- integer(n_ind) log_likelihoods <- numeric(n_ind) - for (i in seq_len(n_ind)) { - ll <- 0 - for (j in seq_len(n_items)) { - params <- param_matrix[, j] - probs <- cell_probs(params) - cell <- response_to_cell( - pre_test[i, j], post_test[i, j], has_dk, - na_as = "missing", missing_action = "omit" - ) - - if (is.na(cell)) next - n_obs[i] <- n_obs[i] + 1L - if (probs[cell] <= 0) { - ll <- -Inf - break - } - ll <- ll + log(probs[cell]) - } - log_likelihoods[i] <- ll + for (j in seq_len(n_items)) { + probs <- cell_probs(param_matrix[, j]) + cells <- response_to_cell( + pre_test[[j]], post_test[[j]], has_dk, + na_as = "missing", missing_action = "omit" + ) + observed <- !is.na(cells) + n_obs <- n_obs + observed + log_likelihoods[observed] <- log_likelihoods[observed] + + log(probs[cells[observed]]) } list(log_likelihood = log_likelihoods, n_obs = n_obs) diff --git a/tests/testthat/test-individual-validation.R b/tests/testthat/test-individual-validation.R index 2c72f15..2896844 100644 --- a/tests/testthat/test-individual-validation.R +++ b/tests/testthat/test-individual-validation.R @@ -1,3 +1,20 @@ +test_that("impossible responses retain all observed pairs in either item order", { + knows <- c(gg = 0, gk = 0, kk = 1, gamma = 0.25) + fit <- structure( + list(params = cbind(a = knows, b = knows), model_type = "nodk"), + class = "guess_fit" + ) + responses <- data.frame(a = c(0, 1, NA), b = c(1, 1, NA)) + for (items in list(c("a", "b"), c("b", "a"))) { + score <- score_individual_lca( + fit, responses[items], responses[items], na_as = "missing" + ) + expect_equal(score$individual_scores$log_likelihood, c(-Inf, 0, 0)) + expect_equal(score$individual_scores$n_observations, c(2L, 2L, 0L)) + expect_equal(score$n_observations, 4L) + } +}) + test_that("score_individual_lca matches the aggregated held-out score", { sim <- simulate_lca(n = 120, n_items = 2, seed = 123) fit <- fit_item_lca(sim$pre, sim$post)