Skip to content

Commit

Permalink
export loglik_array generic
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakethompson committed Aug 7, 2024
1 parent 5f3d3fd commit 33b9a87
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 8 deletions.
3 changes: 2 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ S3method(as_draws,measrfit)
S3method(as_measrfit,default)
S3method(c,measrprior)
S3method(fit_m2,measrdcm)
S3method(loglik_array,measrdcm)
S3method(loo,measrfit)
S3method(loo_compare,measrfit)
S3method(measr_extract,measrdcm)
S3method(predict,measrdcm)
S3method(prep_loglik_array,measrdcm)
S3method(reliability,measrdcm)
S3method(waic,measrfit)
export("%>%")
Expand All @@ -32,6 +32,7 @@ export(fit_m2)
export(fit_ppmc)
export(get_parameters)
export(is.measrprior)
export(loglik_array)
export(loo)
export(loo_compare)
export(measr_dcm)
Expand Down
4 changes: 2 additions & 2 deletions R/loo-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ loo.measrfit <- function(x, ..., r_eff = NA, force = FALSE) { #nolint
"estimated with `method = \"mcmc\"`."))
}

log_lik_array <- prep_loglik_array(model)
log_lik_array <- loglik_array(model)

loo::loo(log_lik_array, r_eff = r_eff, ...)
}
Expand Down Expand Up @@ -70,7 +70,7 @@ waic.measrfit <- function(x, ..., force = FALSE) { #nolint
"estimated with `method = \"mcmc\"`."))
}

log_lik_array <- prep_loglik_array(model)
log_lik_array <- loglik_array(model)

loo::waic(log_lik_array, ...)
}
Expand Down
2 changes: 1 addition & 1 deletion R/model-evaluation.R
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ add_criterion <- function(x, criterion = c("loo", "waic"), overwrite = FALSE,
all_criteria <- c(new_criteria, redo_criteria)

if (length(all_criteria) > 0) {
log_lik_array <- prep_loglik_array(model)
log_lik_array <- loglik_array(model)
}

if ("loo" %in% all_criteria) {
Expand Down
18 changes: 15 additions & 3 deletions R/utils-loo.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
prep_loglik_array <- function(model) {
UseMethod("prep_loglik_array")
#' Extract the log-likelihood of an estimated model
#'
#' The `loglik_array()` methods for [measrfit] objects calculates the
#' log-likelihood for an estimated model via the generated quantities
#' functionality in *Stan* and returns the draws of the `log_lik` parameter.
#'
#' @param model A [measrfit] object.
#'
#' @return A "[`draws_array`][posterior::draws_array()]" object containing the
#' log-likelihood estimates for the model.
#' @export
loglik_array <- function(model) {
UseMethod("loglik_array")
}

#' @rdname loglik_array
#' @export
prep_loglik_array.measrdcm <- function(model) {
loglik_array.measrdcm <- function(model) {
score_data <- model$data$data
clean_qmatrix <- model$data$qmatrix %>%
dplyr::select(-"item_id") %>%
Expand Down
23 changes: 23 additions & 0 deletions man/loglik_array.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ reference:
- loo_compare.measrfit
- loo.measrfit
- waic.measrfit
- loglik_array

- subtitle: Add evaluations to model objects
desc: >
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ test_that("get_mcmc_draws works as expected", {
test_that("log_lik is calculated correctly", {
skip_on_cran()

log_lik <- prep_loglik_array(cmds_mdm_lcdm)
log_lik <- loglik_array(cmds_mdm_lcdm)

# expected value from 2-class LCA fit in Mplus
expect_equal(sum(apply(log_lik, c(3), mean)), -331.764, tolerance = 1.000)
Expand Down

0 comments on commit 33b9a87

Please sign in to comment.