Skip to content
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 10 additions & 19 deletions R/model-criticism.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-individual-validation.R
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Loading